00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef lint
00021 static const char rcsid[] = "$Id: string.c,v 1.5 2007-04-14 15:21:12 sven Exp $";
00022 #endif
00023
00024 #include <eggdrop/eggdrop.h>
00025
00026 int egg_get_word(const char *text, const char **next, char **word)
00027 {
00028 int len;
00029 const char *ptr;
00030
00031 if (!word || !text) {
00032 if (next) *next = NULL;
00033 if (word) *word = NULL;
00034 return(-1);
00035 }
00036 while (isspace(*text)) text++;
00037 ptr = text;
00038 while (*ptr && !isspace(*ptr)) ptr++;
00039 if (next) *next = ptr;
00040 len = ptr - text;
00041 if (!len) {
00042 *word = NULL;
00043 return(-1);
00044 }
00045 *word = malloc(len + 1);
00046 memcpy(*word, text, len);
00047 (*word)[len] = 0;
00048 return(0);
00049 }
00050
00051 int egg_get_arg(const char *text, const char **next, char **arg)
00052 {
00053 int len = 0, max = 64, inquote = 0, insingle = 0, n, done;
00054
00055 if (!arg || !text) {
00056 if (next) *next = NULL;
00057 if (arg) *arg = NULL;
00058 return(-1);
00059 }
00060 while (isspace(*text)) text++;
00061 if (!*text) {
00062 if (next) *next = NULL;
00063 *arg = NULL;
00064 return(-1);
00065 }
00066 *arg = malloc(max+10);
00067
00068 done = 0;
00069 while (!done) {
00070 n = strcspn(text, "\"\\ \t'");
00071 if (n) {
00072
00073 if (n + len + 10 > max) {
00074 max = 2*max + n + 10;
00075 *arg = realloc(*arg, max+1);
00076 }
00077 memcpy(*arg + len, text, n);
00078 len += n;
00079 }
00080 text += n;
00081 switch (*text) {
00082 case '"':
00083 if (insingle) (*arg)[len++] = '"';
00084 else if (inquote) inquote = 0;
00085 else inquote = 1;
00086 text++;
00087 break;
00088 case '\'':
00089 if (inquote) (*arg)[len++] = '\'';
00090 else if (insingle) insingle = 0;
00091 else insingle = 1;
00092 text++;
00093 break;
00094 case ' ':
00095 case '\t':
00096
00097
00098 if (!insingle && !inquote) done = 1;
00099 else (*arg)[len++] = *text;
00100 text++;
00101 break;
00102 case '\\':
00103 text++;
00104 if (insingle) (*arg)[len++] = '\\';
00105 else if (!inquote) (*arg)[len++] = *text++;
00106 else if (*text == '\\' || *text == '"') (*arg)[len++] = *text++;
00107 else (*arg)[len++] = '\\';
00108 break;
00109 default:
00110 done = 1;
00111 break;
00112 }
00113 }
00114 while (*text && isspace(*text)) text++;
00115 if (next) *next = text;
00116 (*arg)[len] = 0;
00117 return(0);
00118 }
00119
00120
00121
00122
00123
00124 int egg_get_words(const char *text, const char **next, char **word, ...)
00125 {
00126 va_list args;
00127 int nwords = 0;
00128
00129 va_start(args, word);
00130 while (word) {
00131 if (egg_get_word(text, &text, word)) break;
00132 nwords++;
00133 word = va_arg(args, char **);
00134 }
00135 while (word) {
00136 *word = NULL;
00137 word = va_arg(args, char **);
00138 }
00139 va_end(args);
00140 if (next) *next = text;
00141 return(nwords);
00142 }
00143
00144 int egg_get_args(const char *text, const char **next, char **arg, ...)
00145 {
00146 va_list args;
00147 int nargs = 0;
00148
00149 va_start(args, arg);
00150 while (arg) {
00151 if (egg_get_arg(text, &text, arg)) break;
00152 nargs++;
00153 arg = va_arg(args, char **);
00154 }
00155 while (arg) {
00156 *arg = NULL;
00157 arg = va_arg(args, char **);
00158 }
00159 va_end(args);
00160 if (next) *next = text;
00161 return(nargs);
00162 }
00163
00164 int egg_get_word_array(const char *text, const char **next, char **word, int nwords)
00165 {
00166 int i;
00167
00168 for (i = 0; i < nwords; i++) {
00169 if (egg_get_word(text, &text, word+i)) break;
00170 }
00171 while (i < nwords) word[i++] = NULL;
00172 if (next) *next = text;
00173 return(i);
00174 }
00175
00176 int egg_get_arg_array(const char *text, const char **next, char **args, int nargs)
00177 {
00178 int i;
00179
00180 for (i = 0; i < nargs; i++) {
00181 if (egg_get_arg(text, &text, args+i)) break;
00182 }
00183 while (i < nargs) args[i++] = NULL;
00184 if (next) *next = text;
00185 return(i);
00186 }
00187
00188 int egg_free_word_array(char **words, int nwords)
00189 {
00190 int i;
00191
00192 for (i = 0; i < nwords; i++) {
00193 if (words[i]) free(words[i]);
00194 words[i] = NULL;
00195 }
00196 return(0);
00197 }
00198
00199 int egg_free_arg_array(char **args, int nargs)
00200 {
00201 return egg_free_word_array(args, nargs);
00202 }
00203
00204
00205 void egg_append_static_str(char **dest, int *remaining, const char *src)
00206 {
00207 int len;
00208
00209 if (!src || *remaining <= 0) return;
00210
00211 len = strlen(src);
00212 if (len > *remaining) len = *remaining;
00213
00214 memmove(*dest, src, len);
00215 *remaining -= len;
00216 *dest += len;
00217 }
00218
00219
00220 void egg_append_str(char **dest, int *cur, int *max, const char *src)
00221 {
00222 int len;
00223
00224 if (!src) return;
00225
00226 len = strlen(src);
00227 if (*cur + len + 10 > *max) {
00228 *max += len + 128;
00229 *dest = realloc(*dest, *max+1);
00230 }
00231 memmove(*dest + *cur, src, len);
00232 *cur += len;
00233 }
00234
00235 int
00236 str_ends_with(const char *text, const char *str)
00237 {
00238 int len1, len2;
00239
00240 egg_return_val_if_fail (text != NULL, 0);
00241 egg_return_val_if_fail (str != NULL, 0);
00242
00243 len1 = strlen (text);
00244 len2 = strlen (str);
00245
00246 if (len2 > len1)
00247 return 0;
00248
00249 return (strcmp (text + (len1 - len2), str) == 0);
00250 }
00251
00252 int
00253 str_starts_with(const char *text, const char *str)
00254 {
00255 egg_return_val_if_fail (text != NULL, 0);
00256 egg_return_val_if_fail (str != NULL, 0);
00257
00258 while (*text && *str && *text++ == *str++)
00259 ;
00260
00261 return (*str);
00262 }
00263
00264 void str_tolower(char *str)
00265 {
00266 while (*str) {
00267 *str = tolower(*str);
00268 str++;
00269 }
00270 }
00271
00272
00273 void shuffleArray(char *array[], size_t n)
00274 {
00275 size_t j = 0, i = 0;
00276 char *temp = NULL;
00277
00278 for (i = 0; i < n; i++) {
00279 j = i + random() / (RAND_MAX / (n - i) + 1);
00280 temp = array[j];
00281 array[j] = array[i];
00282 array[i] = temp;
00283 }
00284 }
00285