74 lines
2.0 KiB
C
74 lines
2.0 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* fontcharacter/string.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2016/11/21 13:29:30 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libg1m/fontcharacter.h>
|
|
#include <string.h>
|
|
|
|
/**
|
|
* g1m_mbstofcs:
|
|
* Multi-byte string to FONTCHARACTER string.
|
|
*
|
|
* This function imitates `mbstowcs` (unicode), but for CASIO's encoding.
|
|
* You should be able to use it the same way.
|
|
*
|
|
* @arg dest the destination.
|
|
* @arg src the source.
|
|
* @arg n the size of the source.
|
|
* @return the size of the destination.
|
|
*/
|
|
|
|
size_t g1m_mbstofcs(FONTCHARACTER *dest, const char *src, size_t n)
|
|
{
|
|
size_t len = 0;
|
|
while (n) {
|
|
FONTCHARACTER fc;
|
|
int count = g1m_mbtofc(&fc, src, n);
|
|
if (count < 0)
|
|
return (-1);
|
|
if (dest)
|
|
*dest++ = fc;
|
|
if (!count)
|
|
break;
|
|
len++;
|
|
n -= count;
|
|
}
|
|
return (len);
|
|
}
|
|
|
|
/**
|
|
* g1m_fcstombs:
|
|
* FONTCHARACTER string to multi-byte string conversion.
|
|
*
|
|
* This function imitates `wcstombs` (unicode), but for CASIO's encoding.
|
|
* You should be able to use it the same way.
|
|
*
|
|
* @arg dst the destination.
|
|
* @arg src the source.
|
|
* @arg n number of FONTCHARACTER elements in the source.
|
|
* @return the size of the destination.
|
|
*/
|
|
|
|
size_t g1m_fcstombs(char *dst, const FONTCHARACTER *src, size_t n)
|
|
{
|
|
size_t len = 0;
|
|
while (n) {
|
|
char buf[2];
|
|
int count = g1m_fctomb(buf, *src);
|
|
if ((size_t)count > n)
|
|
break;
|
|
memcpy(dst, buf, count);
|
|
dst += count;
|
|
n -= count;
|
|
if (!*src++)
|
|
break;
|
|
len += count;
|
|
}
|
|
return (len);
|
|
}
|