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:09:48 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 /* compare two wide character strings to length "n" */
61 int w_strncmp(const M_WCHAR *string1, const M_WCHAR *string2, int max)
63 int w_strcmp(string1, string2, max)
71 if (string1 == string2) return 0;
72 if (max <= 0) return 0;
75 if (!string1) string1 = &null;
76 if (!string2) string2 = &null;
82 if (c1 < c2) return -1;
83 if (c1 > c2) return 1;
90 /* copy a wide character string */
92 M_WCHAR *w_strcpy(M_WCHAR *string1, const M_WCHAR *string2)
94 M_WCHAR *w_strcpy(string1, string2)
99 M_WCHAR *string1start;
101 string1start = string1;
103 while (*string1++ = *string2++);
109 /* copy a wide character string, stopping after "max" moves */
111 M_WCHAR *w_strncpy(M_WCHAR *string1, const M_WCHAR *string2, int max)
113 M_WCHAR *w_strncpy(string1, string2, max)
119 M_WCHAR *string1start;
121 string1start = string1;
123 while ((--max >= 0) && (*string1++ = *string2++));
129 /* get the length of a wide character string */
131 int w_strlen(const M_WCHAR *string)
141 while (*string++) length++;
147 /* find wide character "chr" in wide string "string" */
149 M_WCHAR *w_strchr(M_WCHAR *string, const M_WCHAR chr)
151 M_WCHAR *w_strchr(string, chr)
152 M_WCHAR *string, chr;
161 if (*where == chr) return where;
172 /* find wide character "chr" in wide string "string" */
174 M_WCHAR *w_strstr(M_WCHAR *string1, M_WCHAR *string2)
176 M_WCHAR *w_strstr(string1, string2)
177 M_WCHAR *string1, *string2;
180 M_WCHAR *where1, *where2;
182 if (!*string2) return string1;
189 if (*where1 == *where2)
193 while (*where1 && *where2 && (*where1 == *where2))
199 if (!*where2) return string1;
200 if (!*where1) return NULL;
212 /* make a multi-byte string from a wide character string */
214 char *MakeMByteString(const M_WCHAR *wc_string)
216 char *MakeMByteString(wc_string)
223 static M_WCHAR empty[] = {0};
228 /* Do a little work to compensate for m_malloc allocating sizeof(M_WCHAR)
229 * bytes for each increment in the size requested.
231 length = (w_strlen(wc_string) * MB_CUR_MAX) + 1;
232 length = (length + sizeof(M_WCHAR) - 1) / sizeof(M_WCHAR);
233 mb_string = (char *) m_malloc(length, "multi-byte string");
236 while (wc = *wc_string++)
238 if ((retVal = wctomb(&mb_string[length], wc)) > 0)
241 mb_string[length] = 0;
247 /* make a wide character string from a multi-byte string */
249 M_WCHAR *MakeWideCharString(const char *mb_string)
251 M_WCHAR *MakeWideCharString(mb_string)
255 M_WCHAR *wc_string, *wc_stringStart;
262 length = strlen(mb_string);
263 wc_stringStart = wc_string =
264 (M_WCHAR *) m_malloc(length + 1, "wide character string");
267 while (mb_string[length])
269 if ((incr = mbtowc(wc_string, &mb_string[length], MB_CUR_MAX)) < 0)
271 char badOne[2], buffer[32];
273 badOne[0] = mb_string[length];
275 sprintf(buffer, "0x%x", mb_string[length]);
276 m_err2("invalid multibyte character found: '%c' (%s)", badOne, buffer);
282 *wc_string = (M_WCHAR) 0;
284 return wc_stringStart;
288 /* Get-wide-char procedure */
295 char mbyte[32]; /* make this bigger than any possible multi-byte char */
299 if ((c = getc((FILE *) m_ptr)) == EOF) return(EOF);
305 if (mblen(mbyte,length) != -1) break; /* hurray! */
306 if (length == MB_CUR_MAX)
307 { /* reached max without a hit */
308 m_error("An invalid multi-byte character was found in the input");
313 if ((c = getc((FILE *) m_ptr)) == EOF)
315 m_error("End-of-file found in within a multi-byte character");
319 mbtowc(&wc,mbyte,length);