Tizen 2.0 Release
[external/libgnutls26.git] / src / cfg / platon / str / strplus.c
1 #ifdef HAVE_CONFIG_H
2 #  include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <stdlib.h>
9
10 #include <platon/str/strplus.h>
11 #include <platon/str/strctype.h>
12
13         char *
14 PLATON_FUNC(strestr)(s1, s2)
15         const char *s1;
16         const char *s2;
17 {
18         return strstr(s1,s2) == NULL ? NULL : strendstr(s1,s2);
19 }
20
21 #if ! defined(SELF) && ! defined(SELFTEST) && ! defined(SELF_STRPLUS)
22
23 /*
24  * Functions strtolower() and strtoupper(), used by function below,
25  * are defined in strctype.c so when we are building self-testing
26  * binary, skip this section.
27  */
28
29         char *
30 PLATON_FUNC(stristr)(s1, s2)
31         const char *s1;
32         const char *s2;
33 {
34         char *a_s1, *a_s2;
35         register char *ret = NULL;
36
37         a_s1 = strdup(s1);
38         a_s2 = strdup(s2);
39
40         if (a_s1 != NULL && a_s2 != NULL) {
41                 ret = strstr(PLATON_FUNC(strtolower)(a_s1),
42                                 PLATON_FUNC(strtolower)(a_s2));
43                 if (ret != NULL)
44                         ret = (char *) s1 + (ret - a_s1);
45         }
46
47         if (a_s2 != NULL)
48                 free(a_s2);
49
50         if (a_s1 != NULL)
51                 free(a_s1);
52
53         return ret;
54 }
55
56 #endif
57
58         char *
59 PLATON_FUNC(str_white_str)(str, substr, size)
60         char *str;
61         char *substr;
62         int  *size;
63 {
64 #if 0
65         /*
66          * This is fastfix code substitution for str_white_str() function,
67          * because new wersion from 'Crasher' was not fully tested yet.
68          */
69
70         *size = strlen(substr);
71         return strstr(str, substr);
72
73 #else
74         register int slen, plen, ssize;
75         register char *pptr, *sptr, *start;
76
77         slen = strlen(str);
78         plen = strlen(substr);
79
80         for (start = str, pptr = substr; slen >= plen; start++, slen--) {
81
82                 /* Find start of pattern in string. */
83                 while (*start != *substr) {
84
85                         if ((isspace(*start)) && (isspace(*substr)))
86                                 break;
87
88                         start++;
89                         slen--;
90                         /* If pattern longer than string. */
91                         if (slen < plen)
92                                 return NULL;
93                 }
94
95                 ssize = 0;
96                 sptr = start;
97                 pptr = substr;
98
99                 while (1) {
100
101 #ifdef DEBUG_STRPLUS /* if str_white_str() works properly, delete this */
102                         printf("comparing %d [%s] with %d [%s]\n",
103                                         *sptr, sptr, *pptr, pptr);
104 #endif
105
106                         if ((isspace(*sptr)) && (isspace(*pptr))) {
107                                 while (isspace(*sptr)) {
108                                         ++sptr;
109
110                                         if (isspace(*pptr)) {
111                                                 ++ssize;
112                                                 ++pptr;
113                                         }
114                                         else
115                                                 ++ssize;
116                                 }
117                         }
118                         else if (*sptr == *pptr) {
119                                 while (*sptr == *pptr && *sptr != '\0' && ! isspace(*sptr)) {
120                                         sptr++;
121                                         pptr++;
122                                         ssize++;
123                                 }
124                         }
125                         else {
126                                 break;
127                         }
128
129                         /* If end of pattern then pattern was found. */
130                         if (*pptr == '\0') {
131                                 if (size != NULL)
132                                         *size = ssize;
133                                 return start;
134                         }
135                 }
136         }
137
138         return NULL;
139 #endif
140 }
141
142
143         int
144 PLATON_FUNC(strcnt)(str, c)
145         const char *str;
146         const int c;
147 {
148         register int i = 0;
149
150         if (str != NULL)
151                 while (*str != '\0')
152                         if (*str++ == (char) c)
153                                 i++;
154
155         return i;
156 }
157
158         int
159 PLATON_FUNC(strcnt_str)(str, substr)
160         const char *str;
161         const char *substr;
162 {
163         register char *s;
164         register int count;
165
166         for (count = 0; ; count++) {
167                 if ((s = strstr(str, substr)) == NULL)
168                         break;
169                 else
170                         str += (s - str) + 1;
171         }
172
173         return count;
174 }
175
176         int
177 PLATON_FUNC(strcnt_sepstr)(str, substr)
178         const char *str;
179         const char *substr;
180 {
181         register char *s;
182         register int count, substr_size;
183
184         substr_size = strlen(substr);
185
186         for (count = 0; ; count++) {
187                 if ((s = strstr(str, substr)) == NULL)
188                         break;
189                 else
190                         str += (s - str) + substr_size;
191         }
192
193         return count;
194 }
195
196         char *
197 PLATON_FUNC(strdel)(s)
198         char *s;
199 {
200 #if 1
201         return (char *) memmove(s, s + 1, strlen(s));
202 #else
203         register int i;
204
205         for (i = 0; s[i] != '\0'; i++)
206                 s[i] = s[i + 1];
207
208         return s;
209 #endif
210 }
211
212         char *
213 PLATON_FUNC(strrmlf)(s)
214         char *s;
215 {
216         register char *p_lf;
217
218         while ((p_lf = strchr(s, '\n')) != NULL)
219                 PLATON_FUNC(strdel)(p_lf);
220
221         return s;
222 }
223
224         char *
225 PLATON_FUNC(strrmcr)(s)
226         char *s;
227 {
228         register char *p_cr;
229
230         while ((p_cr = strchr(s, '\r')) != NULL)
231                 PLATON_FUNC(strdel)(p_cr);
232
233         return s;
234 }
235
236         char *
237 PLATON_FUNC(str_left_trim)(s)
238         char *s;
239 {
240         register char *pos;
241
242         for (pos = s; *pos != '\0' && isspace(*pos); pos++) ;
243
244         if (pos > s)
245                 memmove((void *) s, (void *) pos, strlen(pos) + 1);
246
247         return s;
248 }
249
250         char *
251 PLATON_FUNC(str_right_trim)(s)
252         char *s;
253 {
254         register char *pos;
255
256         for (pos = s + (strlen(s) - 1); pos >= s && isspace(*pos); pos--) ;
257
258         *(pos + 1) = '\0';
259
260         return s;
261 }
262
263         char *
264 PLATON_FUNC(str_trim_whitechars)(s)
265         char *s;
266 {
267         register char *pos, *start;
268
269         for (pos = s, start = NULL; ; pos++) {
270                 if (isspace(*pos)) {
271                         if (start == NULL)
272                                 start = pos;
273                 }
274                 else {
275                         if (start != NULL) {
276                                 memmove(start + 1, pos, strlen(pos) + 1);
277                                 *start = ' ';
278
279                                 pos = start + 1;
280                                 start = NULL;
281                         }
282                 }
283
284                 if (*pos == '\0')
285                         break;
286         }
287
288         return s;
289 }
290
291         char *
292 PLATON_FUNC(strins)(str, ins)
293         char *str;
294         char *ins;
295 {
296         register int ins_len = strlen(ins);
297
298         memmove(str + ins_len, str, strlen(str) + 1);
299         strncpy(str, ins, ins_len);
300
301         return str;
302 }
303
304         char *
305 PLATON_FUNC(strrev)(str)
306         char *str;
307 {
308         register int i, c, len = strlen(str);
309
310         /* Code borrowed from PHP: Hypertext Preprocessor, http://www.php.net/ */
311         for (i = 0; i < len - 1 - i; i++) {
312                 c = str[i];
313                 str[i] = str[len - 1 - i];
314                 str[len - 1 - i] = c;
315         }
316
317         return str;
318 }
319
320         int
321 PLATON_FUNC(strrcmp)(s1, s2)
322         const char *s1;
323         const char *s2;
324 {
325         register char *x1, *x2;
326
327         x1 = strchr(s1,'\0');
328         x2 = strchr(s2,'\0');
329
330         while (x1 > s1 && x2 > s2) {
331                 x1--;
332                 x2--;
333                 if (strcmp(x1,x2))
334                         return strcmp(x1,x2);
335         }
336
337         return strlen(s1) - strlen(s2);
338 }
339
340 #if defined(SELF) || defined(SELFTEST) || defined(SELF_STRPLUS) || defined(SELF_STRPLUS2)
341
342 #include <stdio.h>
343
344 #define TESTSTR1 "___AAA_BBBB__C_DaDaD____"
345 #define TESTSEP1 '_'
346 #define TESTSEP2 "BB"
347
348 #define TESTSTR2 " \t  AAA\nBBBB__C   D\taDa D \t   \t"
349
350         static void
351 strins_selftest(void)
352 {
353         char long_str[80];
354
355 #define SEARCH_STR      "56"
356 #define INSERT_STR      "<-now-goes-56->"
357
358         strcpy(long_str, "1234567890");
359         printf("  long_str before: %s\n", long_str);
360
361         printf("  Now we're searching '%s' and want to insert '%s' before it.\n",
362                         SEARCH_STR, INSERT_STR);
363
364         strins(strstr(long_str, SEARCH_STR), INSERT_STR);
365         printf("  long_str  after: %s\n", long_str);
366
367         return;
368 }
369
370         int
371 main(argc, argv)
372         int argc;
373         char **argv;
374 {
375         char *str2;
376
377         puts("Entering str_white_str() selftest:");
378
379         if (argc > 2) {
380                 int size;
381
382                 str2 = PLATON_FUNC(str_white_str)(argv[1], argv[2], &size);
383                 /* str2 = str_white_str("telnet \t atlantis.sk  5678", "t  a", &size);
384                 */
385                 printf("  ptr = [%s], size = %d\n", str2, size);
386
387                 return 0;
388         }
389
390         printf("  Usage: %s <string> <substring>\n\n", argv[0]);
391
392         str2 = strdup(TESTSTR2);
393
394         printf("strcnt(\"%s\", '%c') = %d\n",
395                         TESTSTR1, TESTSEP1,
396                         PLATON_FUNC(strcnt)(TESTSTR1, TESTSEP1));
397
398         printf("strcnt_str(\"%s\", \"%s\") = %d\n",
399                         TESTSTR1, TESTSEP2,
400                         PLATON_FUNC(strcnt_str)(TESTSTR1, TESTSEP2));
401
402         printf("strcnt_sepstr(\"%s\", \"%s\") = %d\n",
403                         TESTSTR1, TESTSEP2,
404                         PLATON_FUNC(strcnt_sepstr)(TESTSTR1, TESTSEP2));
405
406         printf("str_trim_whitechars(\"%s\") = \"%s\"\n",
407                         TESTSTR2,
408                         PLATON_FUNC(str_trim_whitechars)(str2));
409
410         printf("strdel(\"%s\") = ", str2);
411         printf("\"%s\"\n", PLATON_FUNC(strdel)(str2));
412
413         printf("strrev(\"%s\") = ", str2);
414         printf("\"%s\"\n", PLATON_FUNC(strrev)(str2));
415         strrev(str2); /* Reversing back, just for sure */
416         free(str2);
417
418 #if ! defined(SELF) && ! defined(SELFTEST) && ! defined(SELF_STRPLUS)
419         { /* stristr() selftest */
420                 char *ptr    = "Reply-To";
421                 char *search = "reply-to";
422                 char *output = stristr(ptr, search);
423                 printf("stristr(\"%s\", \"%s\") = \"%s\"\n", ptr, search, output);
424                 printf("  (\"%s\" == \"%s\") == %d\n", ptr, output, ptr == output);
425         }
426 #endif
427
428
429         puts("\nEntering strins_selftest():");
430         strins_selftest();
431
432         return 0;
433 }
434
435 #endif /* #if defined(SELF) || defined(SELFTEST) || defined(SELF_STRPLUS) || defined(SELF_STRPLUS2) */
436