Debian Codegraph @

gui/GraphLib/libsrc/uimxR5/src/str_utils.c

raw
1 /*---------------------------------------------------------------------
2 * $Date: 2009-08-18 15:23:20 $ $Revision: 1.3 $
3 *---------------------------------------------------------------------
4 *
5 *
6 * Copyright (c) 1990, Visual Edge Software Ltd.
7 *
8 * ALL RIGHTS RESERVED. Permission to use, copy, modify, and
9 * distribute this software and its documentation for any purpose
10 * and without fee is hereby granted, provided that the above
11 * copyright notice appear in all copies and that both that
12 * copyright notice and this permission notice appear in supporting
13 * documentation, and that the name of Visual Edge Software not be
14 * used in advertising or publicity pertaining to distribution of
15 * the software without specific, written prior permission. The year
16 * included in the notice is the year of the creation of the work.
17
18 .VERSION
19 cw 090818051111 last modif
20
21 *-------------------------------------------------------------------*/
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <valloc.h>
28 #include "uimx_cat.h"
29 #include "misc1_ds.h"
30 #define GetMSG_MISC1(ms) UXCATGETS(MC_MISC1,MS_MISC1_,ms)
31
32 /* ---------------------------------------------------------------------------
33 NAME: char *UxCopyString(string)
34 INPUT: string -- String to be duplicated
35 RETURNS: A pointer to a copy of the input string
36 DESCRIPTION: Copies a string and returns a copy of it.
37 CREATION: 7 Sept 1990
38 ---------------------------------------------------------------------------*/
39 char * UxCopyString(string)
40 char * string;
41 {
42 char * new_str = NULL;
43 if ( string != NULL )
44 {
45 new_str = UxMalloc(strlen(string) + 1);
46 strcpy(new_str, string);
47 }
48 return(new_str);
49 }
50
51 /*-----------------------------------------------------------------------------
52 * NAME: UxToLowerString()
53 * INPUT: pointer to a character string
54 * RETURNS: nothing
55 *
56 * DESCRIPTION:
57 * This procedure takes the incoming string and puts it in lower case.
58 * Notice: The string being passed is directly modified by this procedure.
59 *
60 * CREATION: 20 Novembre 1990
61 *---------------------------------------------------------------------------*/
62
63 char *
64 UxToLowerString(string_ptr)
65 char *string_ptr;
66 {
67 int i=0;
68 while ( string_ptr[i] != '\0') {
69 if (isupper(string_ptr[i])) {
70 string_ptr[i] = tolower(string_ptr[i]);
71 }
72 i++;
73 }
74 return(string_ptr);
75 }
76
77 /*-----------------------------------------------------------------------------
78 * NAME: Ux_wcslen()
79 * INPUT: pointer to a wide character string
80 * RETURNS: number of wide characters is string
81 * (not including terminating null wide character, WNULL)
82 *
83 * DESCRIPTION:
84 * Wide character string equivalent of strlen()
85 *
86 * CREATION: February 15, 1993
87 *---------------------------------------------------------------------------*/
88
89 int Ux_wcslen(pwchar)
90 wchar_t *pwchar;
91 {
92 wchar_t null_wchar;
93 int count = 0;
94
95 if (!pwchar)
96 return 0;
97
98 /* The following is done to obtain the null wide character. Using
99 * WNULL is not done because it is not defined in standard header
100 * files (provided in X11R5 distribution: contrib/Xwchar/wchar.h).
101 */
102 mbtowc(&null_wchar, "", MB_CUR_MAX);
103 for (; *pwchar != null_wchar; pwchar++, count++);
104 return count;
105 }
106
107 /******************************************************************************
108 NAME: Ux_strpbrk (s1, s2)
109 INPUT: char *s1 - multibyte character string to be searched
110 char *s2 - multibyte search characters
111 OUTPUT: ---
112 RETURN: char * - pointer to matching multibyte character
113 DESCRIPTION:
114 This function is similar to the standard C library function, strpbrk().
115 The difference is that it supports multibyte character strings.
116 The Ux_strpbrk is functionally equivalent to the IBM specific function,
117 mbspbrk().
118 It returns a pointer to the first occurrence in string s1
119 of any multibyte character from string s2, or a null pointer if
120 no character from s2 exists in s1.
121 A null pointer is also returned if either string is a null pointer.
122 The terminating null character of s2 is not considered.
123 The mechanism used to compare multibyte characters involves a conversion
124 to wide characters. The argument multibyte character strings are converted
125 to wide character strings one character at a time. If a multibyte character
126 is encountered which cannot be converted to a wide character string, then
127 the function resturns NULL. This applies to both string arguments.
128 The function makes use of the ANSI C wide character string function mbtowc().
129 The source for a possible implementation of mbtowc() is available from the
130 X11R5 distribution in contrib/lib/Xwchar.
131
132 EXT REFERENCES: ---
133 EXT EFFECTS: ---
134
135 CREATED: March 10, 1993
136 LAST REV:
137
138 ******************************************************************************/
139
140 char *Ux_strpbrk(s1, s2)
141 char *s1, *s2;
142 {
143 typedef struct {
144 wchar_t wchar;
145 char *str;
146 } WcStr;
147
148 wchar_t *wset;
149 WcStr *wcstr;
150 int len, i;
151 char *ps1 = s1;
152 int s1_count = 0, s2_count = 0;
153 int ws1_count = 0;
154 register wchar_t *wp;
155 int s1_error = 0, s2_error = 0;
156 char *ret_val;
157
158 if (s1)
159 s1_count = strlen(s1);
160 if (s2)
161 s2_count = strlen(s2);
162
163 /* If either s1 or s2 is a null pointer or a pointer to a null character
164 * then return NULL.
165 */
166 if (!s1_count || !s2_count)
167 return NULL;
168
169 /* The s1 characters are converted to wide characters and stored in
170 * the array of WcStr structure. The pointer to the s1 character which
171 * yielded the wide character is also stored.
172 * The terminating null character is not converted.
173 * The ws1_count is the number of valid converted wide characters.
174 */
175
176 wcstr = (WcStr *)UxMalloc(s1_count * sizeof(WcStr));
177 while (ps1 < (s1+s1_count)) {
178 len = mbtowc(&(wcstr[ws1_count].wchar), ps1, MB_CUR_MAX);
179 if (len > 0) {
180 wcstr[ws1_count].str = ps1;
181 ps1 += len;
182 ws1_count++;
183 }
184 else {
185 /* If we got here, then len must be -1 because ps1 is
186 * not the null character.
187 */
188 s1_error = 1;
189 break;
190 }
191 }
192
193 /* The s2 characters are converted to wide characters and stored in
194 * the allocated wchar_t memory. The call to mbstowcs is sure to append
195 * a terminating null wide character to wset (if there are no errors).
196 */
197
198 wset = (wchar_t *)UxMalloc((s2_count+1) * sizeof(wchar_t));
199 s2_error = ((size_t)-1 == mbstowcs(wset, s2, (s2_count+1)));
200
201 if (s1_error || s2_error) {
202 UxFree(wset);
203 UxFree(wcstr);
204 return NULL;
205 }
206
207 /* Now that both arguments have been converted, do the work
208 */
209 i = 0;
210 ret_val = NULL;
211 do {
212 for (wp = wset; *wp; wp++) {
213 if (wcstr[i].wchar == *wp) {
214 ret_val = wcstr[i].str;
215 break;
216 }
217 }
218 i++;
219 }
220 while ((i < ws1_count) && !ret_val);
221
222 UxFree(wset);
223 UxFree(wcstr);
224 return ret_val;
225 }
226
227 /******************************************************************************
228 NAME: char *UxStripLeadingAndTrailingSpaces(str)
229
230 INPUT: char *str - NULL terminateINPUT: char *str - NULL terminated string
231
232 RETURN: stripped string;
233
234 DESCRIPTION: strips leading and trailing spaces.
235
236 CREATION: July 25/90 (see bug798)
237 REVISIONS: --
238 -----------------------------------------------------------------------------*/
239 char *UxStripLeadingAndTrailingSpaces(str)
240 char *str;
241 {
242 char *st;
243
244 while( isspace( *str)) str++;
245 for (st = str + strlen(str) - 1; (st > str) &&
246 isspace((unsigned char) *st); st--) {
247 *st = '\0';
248 }
249 return str;
250 }
251