85 lines
2.2 KiB
C
85 lines
2.2 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* fontcharacter/string.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2016/11/21 13:29:30 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libg1m/internals.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;
|
|
n--; /* terminating character */
|
|
|
|
while (n--) {
|
|
/* read the FONTCHARACTER char */
|
|
FONTCHARACTER fc;
|
|
int count = g1m_mbtofc(&fc, src, 0);
|
|
if (count <= 0)
|
|
break;
|
|
|
|
/* write the entry and iterate */
|
|
if (dest) *dest++ = fc;
|
|
src += count;
|
|
len++;
|
|
}
|
|
|
|
/* write the terminating entry and return length */
|
|
if (dest) *dest = 0;
|
|
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 the size of the source.
|
|
* @return the size of the destination.
|
|
*/
|
|
|
|
size_t g1m_fcstombs(char *dst, const FONTCHARACTER *src, size_t n)
|
|
{
|
|
char buf[2];
|
|
size_t len = 0;
|
|
n--; /* terminating character */
|
|
|
|
/* main loop */
|
|
while (n--) {
|
|
/* to multi-byte */
|
|
int count = g1m_fctomb(buf, *src++);
|
|
if (count <= 0) break;
|
|
|
|
/* copy */
|
|
if (dst) {
|
|
memcpy(dst, buf, count);
|
|
dst += count;
|
|
}
|
|
len += count;
|
|
n -= count;
|
|
}
|
|
if (dst) *dst = 0;
|
|
return (len);
|
|
}
|