Added mbtouni/mbstous
This commit is contained in:
parent
a69b330bc1
commit
319909ff21
|
@ -58,13 +58,13 @@ typedef uint16_t FONTCHARACTER;
|
|||
/* ************************************************************************** */
|
||||
/* Fixed-width to multi-byte: */
|
||||
|
||||
int fc_wctomb(char *s, FONTCHARACTER wc);
|
||||
size_t fc_wcstombs(char *dst, const FONTCHARACTER *src, size_t n);
|
||||
extern int fc_wctomb(char *s, FONTCHARACTER wc);
|
||||
extern size_t fc_wcstombs(char *dst, const FONTCHARACTER *src, size_t n);
|
||||
|
||||
/* Multi-byte to fixed-width: */
|
||||
|
||||
int fc_mbtowc(FONTCHARACTER *pwc, const char *s, size_t n);
|
||||
size_t fc_mbstowcs(FONTCHARACTER *dst, const char *src, size_t n);
|
||||
extern int fc_mbtowc(FONTCHARACTER *pwc, const char *s, size_t n);
|
||||
extern size_t fc_mbstowcs(FONTCHARACTER *dst, const char *src, size_t n);
|
||||
|
||||
# ifndef FC_DISABLED_REFERENCE
|
||||
/* ************************************************************************** */
|
||||
|
@ -72,22 +72,25 @@ size_t fc_mbstowcs(FONTCHARACTER *dst, const char *src, size_t n);
|
|||
/* ************************************************************************** */
|
||||
/* Multi-characters to fixed-width string: */
|
||||
|
||||
int fc_multowcs(FONTCHARACTER *dest, FONTCHARACTER opcode);
|
||||
extern int fc_multowcs(FONTCHARACTER *dest, FONTCHARACTER opcode);
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* FONTCHARACTER/Unicode conversions */
|
||||
/* ************************************************************************** */
|
||||
/* FONTCHARACTER to Unicode: */
|
||||
/* FONTCHARACTER (multi-byte, fixed-width) to Unicode: */
|
||||
|
||||
int fc_wctouni(wchar_t *dest, FONTCHARACTER opcode);
|
||||
size_t fc_wcstous(wchar_t *dest, const FONTCHARACTER *src, size_t n);
|
||||
extern int fc_wctouni(wchar_t *dest, FONTCHARACTER opcode);
|
||||
extern size_t fc_wcstous(wchar_t *dest, const FONTCHARACTER *src, size_t n);
|
||||
extern int fc_mbtouni(wchar_t *dest, const char *src);
|
||||
extern size_t fc_mbstous(wchar_t *dest, const char *src, size_t n);
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* Locale management */
|
||||
/* ************************************************************************** */
|
||||
/* List locales, set current locale */
|
||||
int fc_listlocales(void (*callback)(void*, const char*), void *cookie);
|
||||
int fc_setlocale(const char *locale);
|
||||
extern int fc_listlocales(void (*callback)(void*, const char*),
|
||||
void *cookie);
|
||||
extern int fc_setlocale(const char *locale);
|
||||
|
||||
# endif /* FC_DISABLED_REFERENCE */
|
||||
# ifdef __cplusplus
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
/* *****************************************************************************
|
||||
* translations/mbtouni.c -- multi-byte FONTCHARACTER -> Unicode translations.
|
||||
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
||||
*
|
||||
* This file is part of libfontcharacter.
|
||||
* libfontcharacter is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 3.0 of the License,
|
||||
* or (at your option) any later version.
|
||||
*
|
||||
* libfontcharacter is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with libfontcharacter; if not, see <http://www.gnu.org/licenses/>.
|
||||
* ************************************************************************** */
|
||||
#include <libfontcharacter/internals.h>
|
||||
#include <string.h>
|
||||
#ifndef FC_DISABLED_REFERENCE
|
||||
|
||||
/**
|
||||
* fc_mbtouni:
|
||||
* Multi-byte FONTCHARACTER to Unicode character conversion.
|
||||
*
|
||||
* @arg dest the destination buffer (at least FC_UNI_MAX bytes).
|
||||
* @arg src the multi-byte FONTCHARACTER character.
|
||||
* @return the number of unicode characters in the opcode.
|
||||
*/
|
||||
|
||||
int fc_mbtouni(wchar_t *dest, const char *src)
|
||||
{
|
||||
/* make a FONTCHARACTER opcode out of the multi-byte character */
|
||||
FONTCHARACTER opcode;
|
||||
fc_mbtowc(&opcode, src, 1);
|
||||
|
||||
/* get the Unicode representation of it */
|
||||
return (fc_wctouni(dest, opcode));
|
||||
}
|
||||
|
||||
/**
|
||||
* fc_mbstous:
|
||||
* Multi-byte FONTCHARACTER string to Unicode string conversion.
|
||||
*
|
||||
* Heavily inspired from `fc_wcstous`.
|
||||
*
|
||||
* @arg dest the destination buffer.
|
||||
* @arg src the source buffer (expected to be null-terminated).
|
||||
* @arg n the size of the destination buffer.
|
||||
* @return the number of written characters.
|
||||
*/
|
||||
|
||||
size_t fc_mbstous(wchar_t *dest, const char *src, size_t n)
|
||||
{
|
||||
FONTCHARACTER multi[FC_MAX_MULTI];
|
||||
wchar_t uni[FC_MAX_UNI];
|
||||
size_t len = 0;
|
||||
n--; /* terminating character */
|
||||
|
||||
/* main loop */
|
||||
FONTCHARACTER opcode; int oplen;
|
||||
for (; (oplen = fc_mbtowc(&opcode, src, 1)); src += oplen) {
|
||||
int mcount = fc_multowcs(multi, opcode);
|
||||
if (mcount < 0) continue;
|
||||
|
||||
for (int mini = 0; mini < mcount; mini++) {
|
||||
int ucount = fc_wctouni(uni, multi[mini]);
|
||||
if (ucount < 0) continue;
|
||||
|
||||
if ((unsigned)ucount > n) goto end;
|
||||
if (dest) {
|
||||
memcpy(dest, uni, ucount * sizeof(wchar_t));
|
||||
dest += ucount;
|
||||
}
|
||||
len += ucount;
|
||||
n -= ucount;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
if (dest) *dest = 0;
|
||||
return (len);
|
||||
}
|
||||
|
||||
#endif /* FC_DISABLED_REFERENCE */
|
|
@ -38,16 +38,16 @@ int fc_mbtowc(FONTCHARACTER *pfc, const char *s, size_t n)
|
|||
{
|
||||
/* null string? */
|
||||
if (!s) return (-1);
|
||||
const unsigned char *us = (const unsigned char*)s;
|
||||
|
||||
/* so that the 'no size = unlimited' (n == 0) case is easy to manage */
|
||||
n--;
|
||||
|
||||
/* nul character? */
|
||||
if (!*s)
|
||||
if (!*us || *us == 0xFF)
|
||||
return (0);
|
||||
|
||||
/* extended char? */
|
||||
const unsigned char *us = (const unsigned char*)s;
|
||||
if (*us && strchr("\x7F\xF7\xF9\xE5\xE6\xE7", *us)) {
|
||||
if (!n) return (-1);
|
||||
*pfc = *us;
|
||||
|
|
|
@ -47,7 +47,7 @@ int fc_wctomb(char *s, FONTCHARACTER fc)
|
|||
|
||||
/* normal char. */
|
||||
*s = fc;
|
||||
return (fc != 0);
|
||||
return (fc != 0x00 && fc != 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Reference in New Issue