Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
document last changes
[simgrid.git] / src / nws_portability / strutil.c
1 /* $Id$ */
2
3 #include <ctype.h>
4 #include <string.h>
5 #include <stdarg.h>
6
7 #include "strutil.h"
8
9
10 #define EOS '\0'
11
12
13 void
14 strcase(char *string,
15         CaseTypes toWhatCase) {
16
17   char *c;
18   int raiseIt;
19
20   raiseIt = (toWhatCase == ALL_UPPER) || (toWhatCase == INITIAL_UPPER);
21
22   for(c = string; *c != EOS; c++) {
23     *c = raiseIt ? toupper((int)*c) : tolower((int)*c);
24     raiseIt = (toWhatCase == ALL_UPPER) ||
25               ((toWhatCase == INITIAL_UPPER) && !isalpha((int)*c));
26   }
27
28 }
29
30
31 int
32 strnmatch(const char *string,
33           const char *pattern,
34           size_t len) {
35
36   const char *lastChance;
37   const char *nextWild;
38   int tameLen;
39
40   if(*pattern == '*') {
41     do pattern++; while (*pattern == '*');
42     if(*pattern == '\0')
43       return 1; /* Trailing star matches everything. */
44     nextWild = strchr(pattern, '*');
45     tameLen = (nextWild == NULL) ? strlen(pattern) : nextWild - pattern;
46     /*
47     ** The wildcard we're processing can match any string of characters up to
48     ** an occurrence of the following non-wild subpattern.  For each subpattern
49     ** occurrence, see if the remaining string matches the remaining pattern.
50     */
51     lastChance = string + len - tameLen;
52     for(; string <= lastChance; string++) {
53       if((strncmp(string, pattern, tameLen) == 0) &&
54          strnmatch(string + tameLen, pattern + tameLen, lastChance - string)) {
55         return 1;
56       }
57     }
58     return 0;
59   }
60   else {
61     nextWild = strchr(pattern, '*');
62     if(nextWild == NULL)
63       /* No wildcards in pattern; check for exact match. */
64       return (strlen(pattern) == len) && (strncmp(string, pattern, len) == 0);
65     else {
66       /* Check for leading exact match followed by a wildcard match. */
67       tameLen = nextWild - pattern;
68       return (tameLen <= len) &&
69              (strncmp(string, pattern, tameLen) == 0) &&
70              strnmatch(string + tameLen, nextWild, len - tameLen);
71     }
72   }
73
74 }
75
76
77 #ifndef HAVE_STRNLEN
78 size_t
79 strnlen(        const char *s,
80                 size_t maxlen) {
81         size_t i;
82
83         if (maxlen <= 0 || s == NULL) {
84                 return 0;
85         }
86
87         for (i=0; i<maxlen; i++) {
88                 if (s[i] == '\0') {
89                         /* done */
90                         break;
91                 }
92         }
93         return i;
94 }
95 #endif
96
97 int
98 strntok(char *dest,
99         const char *source,
100         int len,
101         const char *delim,
102         const char **end) {
103
104         char *last = dest + len - 1;
105
106         /* Skip leading delimiters. */
107         while( (*source != EOS) && (strchr(delim, *source) != NULL) ) {
108                 source++;
109         }
110
111         if(*source == EOS) {
112                 return 0;
113         }
114
115         while( (dest < last) && (*source != EOS) &&
116                         (strchr(delim, *source) == NULL) ) {
117                 *dest++ = *source++;
118         }
119
120         if(end != NULL) {
121                 *end = (const char *)source;
122         }
123         *dest = EOS;
124
125         return 1;
126 }
127
128
129 int
130 vstrncpy(char *dest,
131          size_t len,
132          int count,
133          ...) {
134
135   va_list paramList;
136   int i;
137   const char *source;
138   char *end = dest + len - 1;
139   char *start = dest;
140
141   va_start(paramList, count);
142   for (i = 0; i < count; i++) {
143     for (source = va_arg(paramList, const char*);
144          (dest < end) && (*source != EOS); dest++, source++)
145       *dest = *source;
146   }
147   *dest = EOS;
148   va_end(paramList);
149   return dest - start;
150
151 }