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: CvString.c /main/4 1996/08/30 15:28:00 cde-hp $ */
24 /************************************<+>*************************************
25 ****************************************************************************
29 ** Project: Cde DtHelp
31 ** Description: Semi private string functions - can be platform dependent.
32 ** These routines are straight C code. They do not require
33 ** includes to know about the Canvas Engine, the Help
34 ** dialogs or anything else.
37 ** (c) Copyright 1987, 1988, 1989, 1990, 1991, 1992 Hewlett-Packard Company
39 ** (c) Copyright 1993, 1994 Hewlett-Packard Company
40 ** (c) Copyright 1993, 1994 International Business Machines Corp.
41 ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
42 ** (c) Copyright 1993, 1994 Novell, Inc.
46 ****************************************************************************
47 ************************************<+>*************************************/
57 #include "CvStringI.h"
59 /******************************************************************************
61 * Private variables and defines.
63 *****************************************************************************/
64 #define REALLOC_INCR 10
66 /******************************************************************************
68 * Semi Public Functions
70 *****************************************************************************/
71 /******************************************************************************
72 * Function: _DtCvStrLen (const void *p1, int type)
75 * p1 Specifies the string or wide char array to process.
76 * type Specifies the type p1 is. 0 indicaates a string.
77 * non-zero indicates a wide char array.
79 * >= 0 If p1 is non-null.
83 * Returns in number of characters in p1.
85 *****************************************************************************/
95 return (strlen((const char *) p1));
107 /******************************************************************************
108 * Function: _DtCvStrPtr (const void *p1, int type, int count)
111 * p1 Specifies the string or wide char array to process.
112 * type Specifies the type p1 is. 0 indicaates a string.
113 * non-zero indicates a wide char array.
114 * count Specifies an index into p1.
116 * ptr If everything works okay.
120 * Get to a point in the wide character or char string.
122 *****************************************************************************/
131 return ((void *) (((char *) p1) + count));
133 return ((void *) (((wchar_t *) p1) + count));
136 /******************************************************************************
137 * Function: _DtCvChar (const void *p1, int type, int count)
140 * p1 Specifies the string or wide char array to process.
141 * type Specifies the type p1 is. 0 indicaates a string.
142 * non-zero indicates a wide char array.
143 * count Specifies an index into p1.
145 * value If everything works okay.
146 * (wchar_t) -1 If problems.
149 * Get a character value.
151 *****************************************************************************/
158 wchar_t value = (wchar_t) -1;
161 value = *(((char *) p1) + count);
163 value = *(((wchar_t *) p1) + count);
168 /******************************************************************************
169 * Function: _DtCvStrcspn (const void *s1, const char *s2, type, &ret_len)
171 * Returns in 'ret_len' the length of the initial segment of string
172 * s1 which consists entirely of characters not found in string s2.
174 * if type is 0, then s1 is a char* string.
175 * if type is not 0, then s1 is a wchar_t* string.
177 * s2 cannot be more than 15 characters if looking a wide character
181 * -1 If found an invalid character.
182 * 0 If found a character in string s2
183 * 1 If found the null byte character.
184 * 2 s2 contained more than 15 characters, but found one of the
185 * first fifteen characters in string s2.
186 * 3 s2 contained more than 15 characters, and didn't find any
187 * of the first fifteen characters in string s2.
188 *****************************************************************************/
207 if ((0 == type && '\0' == *((char *) s1)) ||
208 (0 != type && 0 == *((wchar_t *) s1)))
214 if (NULL == s2 || '\0' == *s2)
216 *ret_len = _DtCvStrLen (s1, type);
223 * no need to go through any hassle, just use the 3C function
225 *ret_len = strcspn ((char *) s1, s2);
226 if ('\0' == ((char *) s1)[*ret_len])
232 * convert the test string into a wide char array
234 num = mbstowcs(widec, s2, 16);
235 if ((size_t) -1 == num)
239 * force a null termination of the array
244 * if greater than 15 characters were converted, set the flag to 2
246 num = ((num > 15) ? 2 : 0);
256 *ret_len = wcp - (wchar_t *) s1;
264 *ret_len = wcp - (wchar_t *) s1;
268 /****************************************************************************
269 * Function: void **_DtCvAddPtrToArray (void **array, void *ptr)
271 * Parameters: array A pointer to a NULL-terminated array
273 * ptr The pointer which is to be added to
274 * the end of the array.
276 * Returns: A pointer to the NULL-terminated array created
277 * by adding 'ptr' to the end of 'array'.
279 * Purpose: Add a new element to a NULL-terminated array of pointers.
280 * These are typed as "void *" so that they can be used with
281 * pointers to any type of data.
283 ****************************************************************************/
293 /* If this is the first item for the array, malloc the array and set
294 nextP to point to the first element. */
295 if (array == NULL || *array == NULL) {
296 array = (void **) malloc (REALLOC_INCR * sizeof (void *));
303 /* Find the NULL pointer at the end of the array. */
305 for (nextP = array; *nextP != NULL; nextP++)
308 /* The array always grows by chunks of size REALLOC_INCR. So see if
309 it currently is an exact multiple of REALLOC_INCR size (remember to
310 count the NULL pointer). If it is then it must be full, so realloc
311 another chunk. Also remember to move 'nextP' because the array
312 will probably move in memory. */
313 if ((numElements + 1) % REALLOC_INCR == 0) {
314 array = (void **) realloc (array,
315 (numElements + 1 + REALLOC_INCR) * sizeof (void *));
317 nextP = array + numElements;
332 /******************************************************************************
333 * Function: int _DtCvFreeArray (void **array)
335 * Parameters: array A pointer to the NULL-terminated
336 * string array which is to be freed.
338 * Return Value: 0 if successful, -1 if a failure occurs
340 * Purpose: Free the memory used for a NULL-terminated string array.
342 ******************************************************************************/
344 _DtCvFreeArray (void **array)
351 for (next = array; *next != NULL; next++)
358 #if defined(_AIX) || defined (USL) || defined(__uxp__)
359 /*****************************************************************************
360 * Function: _DtCvStrCaseCmp
366 * Purpose: IBM and USL do not support the 'strcasecmp' routine. This takes it's
368 *****************************************************************************/
378 if (s1 == s2) return 0;
379 if (NULL == s1) return -1;
380 if (NULL == s2) return 1;
383 while (result == 0 && *s1 != '\0' && *s2 != '\0')
385 c1 = (unsigned char) *s1;
386 c2 = (unsigned char) *s2;
398 if (result == 0 && (*s1 != '\0' || *s2 != '\0'))
400 c1 = (unsigned char) *s1;
401 c2 = (unsigned char) *s2;
413 #endif /* _AIX or USL */
415 /*****************************************************************************
416 * Function: _DtCvStrNCaseCmpLatin1()
418 * Purpose: Does not use the tolower() functions to determine the lower
419 * case of a character. On some platforms, using tolower() on
420 * the upper case of a typical Latin1 character does not match
421 * it's Latin1 one lower case.
423 * This routine is to be used only when we want to map strict
424 * Latin1 characters to it's lower case. I.e. when we -know-
425 * the data is in English.
426 *****************************************************************************/
428 _DtCvStrNCaseCmpLatin1 (
437 if (s1 == s2 || n < 1) return 0;
438 if (NULL == s1) return -1;
439 if (NULL == s2) return 1;
442 while (result == 0 && *s1 != '\0' && *s2 != '\0' && n > 0)
444 c1 = (unsigned char) *s1;
445 c2 = (unsigned char) *s2;
447 c1 = _DtCvToLower(c1);
448 c2 = _DtCvToLower(c2);
456 if (result == 0 && n > 0 && (*s1 != '\0' || *s2 != '\0'))
458 c1 = (unsigned char) *s1;
459 c2 = (unsigned char) *s2;
461 c1 = _DtCvToLower(c1);
462 c2 = _DtCvToLower(c2);
470 /*****************************************************************************
471 * Function: _DtCvStrCaseCmpLatin1
473 * Purpose: Does not use the tolower() functions to determine the lower
474 * case of a character. On some platforms, using tolower() on
475 * the upper case of a typical Latin1 character does not match
476 * it's Latin1 one lower case.
478 * This routine is to be used only when we want to map strict
479 * Latin1 characters to it's lower case. I.e. when we -know-
480 * the data is in English.
481 *****************************************************************************/
483 _DtCvStrCaseCmpLatin1 (
491 if (s1 == s2) return 0;
492 if (NULL == s1) return -1;
493 if (NULL == s2) return 1;
496 while (result == 0 && *s1 != '\0' && *s2 != '\0')
498 c1 = (unsigned char) *s1;
499 c2 = (unsigned char) *s2;
501 c1 = _DtCvToLower(c1);
502 c2 = _DtCvToLower(c2);
509 if (result == 0 && (*s1 != '\0' || *s2 != '\0'))
511 c1 = (unsigned char) *s1;
512 c2 = (unsigned char) *s2;
514 c1 = _DtCvToLower(c1);
515 c2 = _DtCvToLower(c2);