00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #include <malloc.h>
00044 #include "string_utils.h"
00045
00046 #define BLANK ' '
00047 #define DOT '.'
00048 #define SLASH '/'
00049 #define PERCENT '%'
00050
00051 char *
00052 string_copy (char *s)
00053 {
00054 char *new;
00055 if (s == NULL) return NULL;
00056 new = (char *) malloc(strlen(s)+1);
00057 strcpy(new, s);
00058 return new;
00059 }
00060
00061
00062 char *
00063 substring_copy (char *s, int i, int len)
00064 {
00065 char *new;
00066 if (s == NULL) return NULL;
00067 new = (char *) malloc(len+1);
00068 strncpy(new, s+i, len);
00069 new[len] = '\0';
00070 return new;
00071 }
00072
00073
00074 char *
00075 concat_strings (const char *a, const char *b)
00076 {
00077
00078 char *new = (char *) malloc(strlen(a) + strlen(b) + 1);
00079 strcpy(new, a);
00080 strcat(new, b);
00081 return new;
00082 }
00083
00084
00085 char *
00086 get_suffix (const char *s)
00087 {
00088 char *suffix;
00089 suffix = strrchr (s, DOT);
00090 if (suffix == NULL) return NULL;
00091 suffix++;
00092 return suffix;
00093 }
00094
00095
00096
00097 char *
00098 change_suffix (char *s, char *suffix)
00099 {
00100 char *new = string_copy(s);
00101 char *p = get_suffix(new);
00102 if (p == NULL) return s;
00103 if (strlen(suffix) <= strlen(p)) {
00104
00105 strcpy(p, suffix);
00106 return new;
00107 }
00108 else {
00109
00110 *p = NIL;
00111 return concat_strings(new, suffix);
00112 }
00113 }
00114
00115
00116
00117 boolean
00118 has_blank (char *s)
00119 {
00120 char *p;
00121 for (p = s; *p != NIL; p++) {
00122 if (*p == BLANK) return TRUE;
00123 }
00124 return FALSE;
00125 }
00126
00127
00128
00129 void
00130 replace_substring (char *base, char *old_pattern, char *new_pattern)
00131 {
00132 char *p = strstr (base, old_pattern);
00133 strncpy(p,new_pattern,strlen(old_pattern));
00134 }
00135
00136
00137
00138
00139
00140
00141 char *
00142 expand_template_string (char *template, char *arg)
00143 {
00144 char *percent;
00145 char *new = string_copy(template);
00146 percent = strchr (new, PERCENT);
00147 if (percent == NULL) return new;
00148 *percent = NIL;
00149 return concat_strings (new, arg);
00150 }
00151
00152
00153 string_list_t *
00154 init_string_list (void)
00155 {
00156 string_list_t *p;
00157 p = (string_list_t *) malloc(sizeof(string_list_t));
00158 p->head = p->tail = NULL;
00159 return p;
00160 }
00161
00162
00163 static void
00164 add_existing_string (string_list_t *list, char *s)
00165 {
00166 string_item_t *p;
00167 p = (string_item_t *) malloc(sizeof(string_item_t));
00168 p->name = s;
00169 p->next = NULL;
00170 if (list->head == NULL) {
00171 list->head = list->tail = p;
00172 } else {
00173 list->tail->next = p;
00174 list->tail = p;
00175 }
00176 }
00177
00178
00179 void
00180 add_after_string (string_list_t *list, string_item_t *item, char *s)
00181 {
00182 string_item_t *p;
00183 p = (string_item_t *) malloc(sizeof(string_item_t));
00184 p->name = s;
00185 p->next = item->next;
00186 item->next = p;
00187 if (list->tail == item) {
00188 list->tail = p;
00189 }
00190 }
00191
00192
00193 void
00194 add_string (string_list_t *list, char *s)
00195 {
00196
00197 add_existing_string (list, string_copy(s));
00198 }
00199
00200
00201 void
00202 add_multi_strings (string_list_t *list, char *s
00203 #ifdef KEY
00204 , boolean only_one
00205 #endif
00206 )
00207 {
00208
00209 char *new = string_copy(s);
00210 if (has_blank(new)) {
00211 char *t;
00212
00213 for (t = new; *t != NIL; t++) {
00214 if (*t == BLANK) {
00215 *t = NIL;
00216 add_existing_string(list, new);
00217 new = t+1;
00218 #ifdef KEY
00219 if (only_one)
00220 break;
00221 #endif
00222 }
00223 }
00224 }
00225 add_existing_string (list, new);
00226 }
00227
00228
00229 void
00230 add_string_if_new (string_list_t *list, char *s)
00231 {
00232 string_item_t *p;
00233 for (p = list->head; p != NULL; p = p->next) {
00234 if (strcmp(p->name, s) == 0)
00235 return;
00236 }
00237
00238 add_string (list, s);
00239 }
00240
00241
00242 void
00243 replace_string (string_list_t *list, char *old, char *new)
00244 {
00245 string_item_t *p;
00246 for (p = list->head; p != NULL; p = p->next) {
00247 if (strcmp(p->name, old) == 0) {
00248 p->name = string_copy(new);
00249 return;
00250 }
00251 }
00252
00253 }
00254
00255
00256 void
00257 append_string_lists (string_list_t *a, string_list_t *b)
00258 {
00259 if (a->head == NULL) {
00260
00261 a->head = b->head;
00262 a->tail = b->tail;
00263 } else if (b->head == NULL) {
00264
00265 } else {
00266 a->tail->next = b->head;
00267 a->tail = b->tail;
00268 }
00269 }
00270
00271 void
00272 print_string_list (FILE *f, string_list_t *list)
00273 {
00274 string_item_t *p;
00275 for (p = list->head; p != NULL; p = p->next) {
00276 fprintf(f, "%s ", p->name);
00277 }
00278 fprintf(f, "\n");
00279 }
00280
00281 const char *
00282 ends_with(const char *s, const char *sfx)
00283 {
00284 int len = strlen(s);
00285 int slen = strlen(sfx);
00286
00287 if (len >= slen && strcmp(s + len - slen, sfx) == 0)
00288 return s + len - slen;
00289
00290 return NULL;
00291 }
00292
00293
00294 string_pair_list_t *
00295 init_string_pair_list (void)
00296 {
00297 string_pair_list_t *p;
00298 p = (string_pair_list_t *) malloc(sizeof(string_pair_list_t));
00299 p->head = p->tail = NULL;
00300 return p;
00301 }
00302
00303
00304 void
00305 add_string_pair (string_pair_list_t *list, char *key, char *val)
00306 {
00307 string_pair_item_t *p;
00308 p = (string_pair_item_t *) malloc(sizeof(string_pair_item_t));
00309 p->key = string_copy(key);
00310 p->val = string_copy(val);
00311 p->next = NULL;
00312 if (list->head == NULL) {
00313 list->head = list->tail = p;
00314 } else {
00315 list->tail->next = p;
00316 list->tail = p;
00317 }
00318 }
00319
00320 int string_list_size(const string_list_t *l)
00321 {
00322 const string_item_t *i;
00323 int len = 0;
00324
00325 FOREACH_STRING(i, l) {
00326 len += 1;
00327 }
00328
00329 return len;
00330 }