From: <ny...@us...> - 2006-06-25 06:06:51
|
Revision: 68 Author: nyaochi Date: 2006-06-24 23:06:43 -0700 (Sat, 24 Jun 2006) ViewCVS: http://svn.sourceforge.net/pmplib/?rev=68&view=rev Log Message: ----------- Added wstring_iconv.c Modified Paths: -------------- branches/branch_0.12/lib/wstring/wstring.vcproj branches/branch_0.12/lib/wstring/wstring_win32.c Added Paths: ----------- branches/branch_0.12/lib/wstring/wstring.c branches/branch_0.12/lib/wstring/wstring_iconv.c Added: branches/branch_0.12/lib/wstring/wstring.c =================================================================== --- branches/branch_0.12/lib/wstring/wstring.c (rev 0) +++ branches/branch_0.12/lib/wstring/wstring.c 2006-06-25 06:06:43 UTC (rev 68) @@ -0,0 +1,192 @@ +/* + * wchar_t utility routine. + * + * Copyright (c) 2005-2006 Nyaochi + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/* $Id$ */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif/*HAVE_CONFIG_H*/ + +#include <os.h> +#include <ctype.h> +#include <stdlib.h> +#include <wchar.h> +#include <string.h> + +#define COMP(a, b) ((a)>(b))-((a)<(b)) + +char *wcsdupmbs(const wchar_t* src) +{ + size_t dst_size = wcstombs(NULL, src, 0); + char* dst = (char*)malloc((dst_size+1) * sizeof(char)); + if (dst) { + wcstombs(dst, src, dst_size); + dst[dst_size] = 0; + } + return dst; +} + +wchar_t *mbsdupwcs(const char* src) +{ + size_t dst_size = mbstowcs(NULL, src, strlen(src)); + wchar_t* dst = (wchar_t*)malloc(dst_size + sizeof(wchar_t)); + if (dst) { + mbstowcs(dst, src, strlen(src)); + dst[dst_size/sizeof(wchar_t)] = 0; + } + return dst; +} + +char *wcsduputf8(const wchar_t* src) +{ + size_t dst_size = wcstoutf8(NULL, src, 0); + char* dst = (char*)malloc((dst_size+1) * sizeof(char)); + if (dst) { + wcstoutf8(dst, src, dst_size); + dst[dst_size] = 0; + } + return dst; +} + +wchar_t *utf8dupwcs(const char *src) +{ + size_t dst_length = utf8towcs(NULL, src, 0); + wchar_t* dst = (wchar_t*)malloc((dst_length+1) * sizeof(wchar_t)); + if (dst) { + utf8towcs(dst, src, dst_length); + dst[dst_length] = 0; + } + return dst; +} + +wchar_t *wcsstrip(wchar_t* str) +{ + wchar_t *p = str + wcslen(str); + while (str < p) { + if (!*p || *p == L'\n' || *p == L'\r') { + *p = 0; + continue; + } + } + return str; +} + +int has_utf8bom(FILE *fp) +{ + int c[3]; + int ret = 0; + + /* Read three bytes. */ + if ((c[0] = fgetc(fp)) == EOF) { + return ret; + } + if ((c[1] = fgetc(fp)) == EOF) { + ungetc(c[0], fp); + return ret; + } + if ((c[2] = fgetc(fp)) == EOF) { + ungetc(c[1], fp); + ungetc(c[0], fp); + return ret; + } + + if ((char)c[0] == 0xEF && (char)c[1] == 0xBB && (char)c[2] == 0xBF) { + ret = 1; + } + + /* Put back the read characters. */ + ungetc(c[2], fp); + ungetc(c[1], fp); + ungetc(c[0], fp); + + return ret; +} + +wchar_t *wcsfgets(wchar_t* str, size_t n, FILE *fp, int is_utf8) +{ + /* Limit maximum bytes for a line to length. */ + char *line = alloca(n); + if (!fgets(line, (int)n, fp)) { + return NULL; + } + + /* Convert multi-byte characters to wide characters. */ + if (is_utf8) { + /* Make sure we have enough buffer to store the line. */ + size_t actual_length = utf8towcs(NULL, line, 0) / sizeof(wchar_t); + if (actual_length < n) { + /* Convert UTF-8 characters to wide characters. */ + utf8towcs(str, line, actual_length); + str[actual_length] = 0; + return str; + } else { + /* Otherwise return NULL. */ + return NULL; + } + } else { + /* Make sure we have enough buffer to store the line. */ + size_t actual_length = mbstowcs(NULL, line, strlen(line)) / sizeof(wchar_t); + if (actual_length < n) { + /* Convert locale characters to wide characters. */ + mbstowcs(str, line, strlen(line)); + str[actual_length] = 0; + return str; + } else { + /* Otherwise return NULL. */ + return NULL; + } + } +} + +wchar_t* wcsndup(const wchar_t* src, size_t length) +{ + wchar_t* dst = NULL; + size_t src_length = wcslen(src); + if (length < src_length) { + src_length = length; + } + dst = (wchar_t*)malloc((src_length+1) * sizeof(wchar_t)); + if (dst) { + size_t i; + for (i = 0;i < src_length;i++) { + dst[i] = src[i]; + } + dst[i] = 0; + } + return dst; +} + +int wcsincmp(const wchar_t* x, const wchar_t* y, size_t n) +{ + size_t i; + wchar_t a = 0, b = 0; + + for (i = 0;i < n-1;i++) { + a = towupper(*x); + b = towupper(*y); + if (!*x || !*y || a != b) { + break; + } + x++; + y++; + } + return COMP(a, b); +} Property changes on: branches/branch_0.12/lib/wstring/wstring.c ___________________________________________________________________ Name: svn:keywords + Id Name: svn:eol-style + native Modified: branches/branch_0.12/lib/wstring/wstring.vcproj =================================================================== --- branches/branch_0.12/lib/wstring/wstring.vcproj 2006-06-25 05:40:52 UTC (rev 67) +++ branches/branch_0.12/lib/wstring/wstring.vcproj 2006-06-25 06:06:43 UTC (rev 68) @@ -100,6 +100,12 @@ Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> <File + RelativePath=".\wstring.c"> + </File> + <File + RelativePath=".\wstring_iconv.c"> + </File> + <File RelativePath=".\wstring_win32.c"> </File> </Filter> Added: branches/branch_0.12/lib/wstring/wstring_iconv.c =================================================================== --- branches/branch_0.12/lib/wstring/wstring_iconv.c (rev 0) +++ branches/branch_0.12/lib/wstring/wstring_iconv.c 2006-06-25 06:06:43 UTC (rev 68) @@ -0,0 +1,56 @@ +/* + * wchar_t utility routine. + * + * Copyright (c) 2005-2006 Nyaochi + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/* $Id$ */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif/*HAVE_CONFIG_H*/ + +#include <os.h> +#include <ctype.h> +#include <stdlib.h> +#include <wchar.h> +#include <string.h> + +#include <iconv.h> + +#ifdef USE_LIBICONV_GNU +#define iconv_open libiconv_open +#define iconv_convert libiconv_convert +#define iconv_close libiconv_close +#endif/*USE_LIBICONV_GNU*/ + +size_t utf8towcs(wchar_t* dst, const char *src, size_t dst_length) +{ + iconv_t cd = iconv_open("WCHAR_T", "UTF-8"); + size_t ret = iconv_convert(cd, (char **)&dst, dst_length * sizeof(wchar_t), (char **)&src, strlen(src)); + iconv_close(cd); + return ret; +} + +size_t wcstoutf8(char *dst, const wchar_t *src, size_t dst_size) +{ + iconv_t cd = iconv_open("UTF-8", "WCHAR_T"); + size_t ret = iconv_convert(cd, (char **)&dst, dst_length * sizeof(char), (char **)&src, wcslen(src)); + iconv_close(cd); + return ret; +} Property changes on: branches/branch_0.12/lib/wstring/wstring_iconv.c ___________________________________________________________________ Name: svn:keywords + Id Name: svn:eol-style + native Modified: branches/branch_0.12/lib/wstring/wstring_win32.c =================================================================== --- branches/branch_0.12/lib/wstring/wstring_win32.c 2006-06-25 05:40:52 UTC (rev 67) +++ branches/branch_0.12/lib/wstring/wstring_win32.c 2006-06-25 06:06:43 UTC (rev 68) @@ -31,30 +31,6 @@ #include <wchar.h> #include <string.h> -#define COMP(a, b) ((a)>(b))-((a)<(b)) - -char *wcsdupmbs(const wchar_t* src) -{ - size_t dst_size = wcstombs(NULL, src, 0); - char* dst = (char*)malloc((dst_size+1) * sizeof(char)); - if (dst) { - wcstombs(dst, src, dst_size); - dst[dst_size] = 0; - } - return dst; -} - -wchar_t *mbsdupwcs(const char* src) -{ - size_t dst_size = mbstowcs(NULL, src, strlen(src)); - wchar_t* dst = (wchar_t*)malloc(dst_size + sizeof(wchar_t)); - if (dst) { - mbstowcs(dst, src, strlen(src)); - dst[dst_size/sizeof(wchar_t)] = 0; - } - return dst; -} - size_t utf8towcs(wchar_t* dst, const char *src, size_t dst_length) { size_t length = 0; @@ -134,141 +110,6 @@ } } -char *wcsduputf8(const wchar_t* src) -{ - size_t dst_size = wcstoutf8(NULL, src, 0); - char* dst = (char*)malloc((dst_size+1) * sizeof(char)); - if (dst) { - wcstoutf8(dst, src, dst_size); - dst[dst_size] = 0; - } - return dst; -} -wchar_t *utf8dupwcs(const char *src) -{ - size_t dst_length = utf8towcs(NULL, src, 0); - wchar_t* dst = (wchar_t*)malloc((dst_length+1) * sizeof(wchar_t)); - if (dst) { - utf8towcs(dst, src, dst_length); - dst[dst_length] = 0; - } - return dst; -} -wchar_t *wcsstrip(wchar_t* str) -{ - wchar_t *p = str + wcslen(str); - while (str < p) { - if (!*p || *p == L'\n' || *p == L'\r') { - *p = 0; - continue; - } - } - return str; -} -int has_utf8bom(FILE *fp) -{ - int c[3]; - int ret = 0; - - /* Read three bytes. */ - if ((c[0] = fgetc(fp)) == EOF) { - return ret; - } - if ((c[1] = fgetc(fp)) == EOF) { - ungetc(c[0], fp); - return ret; - } - if ((c[2] = fgetc(fp)) == EOF) { - ungetc(c[1], fp); - ungetc(c[0], fp); - return ret; - } - - if ((char)c[0] == 0xEF && (char)c[1] == 0xBB && (char)c[2] == 0xBF) { - ret = 1; - } - - /* Put back the read characters. */ - ungetc(c[2], fp); - ungetc(c[1], fp); - ungetc(c[0], fp); - - return ret; -} - -wchar_t *wcsfgets(wchar_t* str, size_t n, FILE *fp, int is_utf8) -{ - /* Limit maximum bytes for a line to length. */ - char *line = alloca(n); - if (!fgets(line, (int)n, fp)) { - return NULL; - } - - /* Convert multi-byte characters to wide characters. */ - if (is_utf8) { - /* Make sure we have enough buffer to store the line. */ - size_t actual_length = utf8towcs(NULL, line, 0) / sizeof(wchar_t); - if (actual_length < n) { - /* Convert UTF-8 characters to wide characters. */ - utf8towcs(str, line, actual_length); - str[actual_length] = 0; - return str; - } else { - /* Otherwise return NULL. */ - return NULL; - } - } else { - /* Make sure we have enough buffer to store the line. */ - size_t actual_length = mbstowcs(NULL, line, strlen(line)) / sizeof(wchar_t); - if (actual_length < n) { - /* Convert locale characters to wide characters. */ - mbstowcs(str, line, strlen(line)); - str[actual_length] = 0; - return str; - } else { - /* Otherwise return NULL. */ - return NULL; - } - } -} - -wchar_t* wcsndup(const wchar_t* src, size_t length) -{ - wchar_t* dst = NULL; - size_t src_length = wcslen(src); - if (length < src_length) { - src_length = length; - } - dst = (wchar_t*)malloc((src_length+1) * sizeof(wchar_t)); - if (dst) { - size_t i; - for (i = 0;i < src_length;i++) { - dst[i] = src[i]; - } - dst[i] = 0; - } - return dst; -} - -int wcsincmp(const wchar_t* x, const wchar_t* y, size_t n) -{ - size_t i; - wchar_t a = 0, b = 0; - - for (i = 0;i < n-1;i++) { - a = towupper(*x); - b = towupper(*y); - if (!*x || !*y || a != b) { - break; - } - x++; - y++; - } - return COMP(a, b); -} - - - This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |