2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: wchar.c /main/3 1995/11/08 11:46:43 rswiston $ */
25 Copyright 1992 Hewlett-Packard Co.
31 /* compare two wide character strings */
33 int w_strcmp(const M_WCHAR *string1, const M_WCHAR *string2)
35 int w_strcmp(string1, string2)
42 if (string1 == string2) return 0;
45 if (!string1) string1 = &null;
46 if (!string2) string2 = &null;
52 if (c1 < c2) return -1;
53 if (c1 > c2) return 1;
59 /* copy a wide character string */
61 M_WCHAR *w_strcpy(M_WCHAR *string1, const M_WCHAR *string2)
63 M_WCHAR *w_strcpy(string1, string2)
68 M_WCHAR *string1start;
70 string1start = string1;
72 while (*string1++ = *string2++);
78 /* copy a wide character string, stopping after "max" moves */
80 M_WCHAR *w_strncpy(M_WCHAR *string1, const M_WCHAR *string2, int max)
82 M_WCHAR *w_strncpy(string1, string2, max)
88 M_WCHAR *string1start;
90 string1start = string1;
92 while ((--max >= 0) && (*string1++ = *string2++));
98 /* get the length of a wide character string */
100 int w_strlen(const M_WCHAR *string)
110 while (*string++) length++;
116 /* find wide character "chr" in wide string "string" */
118 M_WCHAR *w_strchr(M_WCHAR *string, const M_WCHAR chr)
120 M_WCHAR *w_strchr(string, chr)
121 M_WCHAR *string, chr;
130 if (*where == chr) return where;
141 /* find wide character "chr" in wide string "string" */
143 M_WCHAR *w_strstr(M_WCHAR *string1, M_WCHAR *string2)
145 M_WCHAR *w_strstr(string1, string2)
146 M_WCHAR *string1, *string2;
149 M_WCHAR *where1, *where2;
151 if (!*string2) return string1;
158 if (*where1 == *where2)
162 while (*where1 && *where2 && (*where1 == *where2))
168 if (!*where2) return string1;
169 if (!*where1) return NULL;
181 /* make a multi-byte string from a wide character string */
183 char *MakeMByteString(const M_WCHAR *wc_string)
185 char *MakeMByteString(wc_string)
193 /* Do a little work to compensate for m_malloc allocating sizeof(M_WCHAR)
194 * bytes for each increment in the size requested.
196 length = (w_strlen(wc_string) * MB_CUR_MAX) + 1;
197 length = (length + sizeof(M_WCHAR) - 1) / sizeof(M_WCHAR);
198 mb_string = (char *) m_malloc(length, "multi-byte string");
201 while (wc = *wc_string++)
203 length += wctomb(&mb_string[length], wc);
205 mb_string[length] = 0;
211 /* make a wide character string from a multi-byte string */
213 M_WCHAR *MakeWideCharString(const char *mb_string)
215 M_WCHAR *MakeWideCharString(mb_string)
219 M_WCHAR *wc_string, *wc_stringStart;
223 length = strlen(mb_string);
224 wc_stringStart = wc_string =
225 (M_WCHAR *) m_malloc(length + 1, "wide character string");
228 while (mb_string[length])
230 if ((incr = mbtowc(wc_string, &mb_string[length], MB_CUR_MAX)) < 0)
232 char badOne[2], buffer[32];
234 badOne[0] = mb_string[length];
236 sprintf(buffer, "0x%x", mb_string[length]);
237 m_err2("invalid multibyte character found: '%c' (%s)", badOne, buffer);
243 *wc_string = (M_WCHAR) 0;
245 return wc_stringStart;
249 /* Get-wide-char procedure */
256 char mbyte[32]; /* make this bigger than any possible multi-byte char */
260 if ((c = getc((FILE *) m_ptr)) == EOF) return(EOF);
266 if (mblen(mbyte,length) != -1) break; /* hurray! */
267 if (length == MB_CUR_MAX)
268 { /* reached max without a hit */
269 m_error("An invalid multi-byte character was found in the input");
274 if ((c = getc((FILE *) m_ptr)) == EOF)
276 m_error("End-of-file found in within a multi-byte character");
280 mbtowc(&wc,mbyte,length);