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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 # include <stdlib.h>
00064 # include <stdio.h>
00065 # include <ctype.h>
00066 # include <string.h>
00067
00068 # include <osl/macros.h>
00069 # include <osl/util.h>
00070 # include <osl/interface.h>
00071 # include <osl/strings.h>
00072
00073
00074
00075
00076
00077
00078
00089 void osl_strings_idump(FILE * file, osl_strings_p strings, int level) {
00090 int i, nb_strings;
00091
00092 for (i = 0; i < level; i++)
00093 fprintf(file, "|\t");
00094
00095 if (strings != NULL) {
00096 nb_strings = osl_strings_size(strings);
00097 fprintf(file, "+-- osl_strings_t:");
00098 for (i = 0; i < nb_strings; i++)
00099 fprintf(file, " %s", strings->string[i]);
00100 fprintf(file, "\n");
00101 }
00102 else
00103 fprintf(file, "+-- NULL strings\n");
00104
00105
00106 for (i = 0; i <= level; i++)
00107 fprintf(file, "|\t");
00108 fprintf(file, "\n");
00109 }
00110
00111
00119 void osl_strings_dump(FILE * file, osl_strings_p strings) {
00120 osl_strings_idump(file, strings, 0);
00121 }
00122
00123
00131 char * osl_strings_sprint(osl_strings_p strings) {
00132 int i;
00133 int high_water_mark = OSL_MAX_STRING;
00134 char * string = NULL;
00135 char buffer[OSL_MAX_STRING];
00136
00137 OSL_malloc(string, char *, high_water_mark * sizeof(char));
00138 string[0] = '\0';
00139
00140 if (strings != NULL) {
00141 for (i = 0; i < osl_strings_size(strings); i++) {
00142 sprintf(buffer, "%s", strings->string[i]);
00143 osl_util_safe_strcat(&string, buffer, &high_water_mark);
00144 if (i < osl_strings_size(strings) - 1)
00145 osl_util_safe_strcat(&string, " ", &high_water_mark);
00146 }
00147 sprintf(buffer, "\n");
00148 osl_util_safe_strcat(&string, buffer, &high_water_mark);
00149 }
00150 else {
00151 sprintf(buffer, "# NULL strings\n");
00152 osl_util_safe_strcat(&string, buffer, &high_water_mark);
00153 }
00154
00155 return string;
00156 }
00157
00158
00166 void osl_strings_print(FILE * file, osl_strings_p strings) {
00167 char * string;
00168
00169 string = osl_strings_sprint(strings);
00170 if (string != NULL) {
00171 fprintf(file, "%s", string);
00172 free(string);
00173 }
00174 }
00175
00176
00177
00178
00179
00180
00181
00194 osl_strings_p osl_strings_sread(char ** input) {
00195 char tmp[OSL_MAX_STRING];
00196 char * s;
00197 char ** string = NULL;
00198 int nb_strings;
00199 int i, count;
00200 osl_strings_p strings = NULL;
00201
00202
00203 osl_util_sskip_blank_and_comments(input);
00204
00205
00206 nb_strings = 0;
00207 s = *input;
00208 while (1) {
00209 for (count = 0; *s && !isspace(*s) && *s != '#'; count++)
00210 s++;
00211
00212 if (count != 0)
00213 nb_strings++;
00214
00215 if ((!*s) || (*s == '#') || (*s == '\n'))
00216 break;
00217 else
00218 s++;
00219 }
00220
00221 if (nb_strings > 0) {
00222
00223 OSL_malloc(string, char **, sizeof(char *) * (nb_strings + 1));
00224 string[nb_strings] = NULL;
00225
00226
00227 s = *input;
00228 for (i = 0; i < nb_strings; i++) {
00229 for (count = 0; *s && !isspace(*s) && *s != '#'; count++)
00230 tmp[count] = *(s++);
00231 tmp[count] = '\0';
00232 OSL_strdup(string[i], tmp);
00233 if (*s != '#')
00234 s++;
00235 }
00236
00237
00238 *input = s;
00239
00240
00241 strings = osl_strings_malloc();
00242 strings->string = string;
00243 }
00244
00245 return strings;
00246 }
00247
00248
00258 osl_strings_p osl_strings_read(FILE * file) {
00259 char buffer[OSL_MAX_STRING], * start;
00260 osl_strings_p strings;
00261
00262 start = osl_util_skip_blank_and_comments(file, buffer);
00263 strings = osl_strings_sread(&start);
00264
00265 return strings;
00266 }
00267
00268
00269
00270
00271
00272
00273
00282 osl_strings_p osl_strings_malloc() {
00283 osl_strings_p strings;
00284
00285 OSL_malloc(strings, osl_strings_p, sizeof(osl_strings_t));
00286 strings->string = NULL;
00287
00288 return strings;
00289 }
00290
00291
00297 void osl_strings_free(osl_strings_p strings) {
00298 int i;
00299
00300 if (strings != NULL) {
00301 if (strings->string != NULL) {
00302 i = 0;
00303 while (strings->string[i] != NULL) {
00304 free(strings->string[i]);
00305 i++;
00306 }
00307 free(strings->string);
00308 }
00309 free(strings);
00310 }
00311 }
00312
00313
00314
00315
00316
00317
00318
00326 osl_strings_p osl_strings_clone(osl_strings_p strings) {
00327 int i, nb_strings;
00328 osl_strings_p clone = NULL;
00329
00330 if (strings == NULL)
00331 return NULL;
00332
00333 clone = osl_strings_malloc();
00334 if ((nb_strings = osl_strings_size(strings)) == 0)
00335 return clone;
00336
00337 OSL_malloc(clone->string, char **, (nb_strings + 1) * sizeof(char *));
00338 clone->string[nb_strings] = NULL;
00339 for (i = 0; i < nb_strings; i++)
00340 OSL_strdup(clone->string[i], strings->string[i]);
00341
00342 return clone;
00343 }
00344
00345
00354 int osl_strings_equal(osl_strings_p s1, osl_strings_p s2) {
00355 int i, nb_s1;
00356
00357 if (s1 == s2)
00358 return 1;
00359
00360 if (((s1 == NULL) && (s2 != NULL)) ||
00361 ((s1 != NULL) && (s2 == NULL)) ||
00362 ((nb_s1 = osl_strings_size(s1)) != osl_strings_size(s2)))
00363 return 0;
00364
00365 for (i = 0; i < nb_s1; i++)
00366 if (strcmp(s1->string[i], s2->string[i]) != 0)
00367 return 0;
00368
00369 return 1;
00370 }
00371
00372
00380 int osl_strings_size(osl_strings_p strings) {
00381 int size = 0;
00382
00383 if ((strings != NULL) && (strings->string != NULL)) {
00384 while (strings->string[size] != NULL) {
00385 size++;
00386 }
00387 }
00388
00389 return size;
00390 }
00391
00392
00400 osl_strings_p osl_strings_encapsulate(char * string) {
00401 osl_strings_p capsule = osl_strings_malloc();
00402
00403 OSL_malloc(capsule->string, char **, 2 * sizeof(char *));
00404 capsule->string[0] = string;
00405 capsule->string[1] = NULL;
00406
00407 return capsule;
00408 }
00409
00410
00417 osl_interface_p osl_strings_interface() {
00418 osl_interface_p interface = osl_interface_malloc();
00419
00420 interface->URI = strdup(OSL_URI_STRINGS);
00421 interface->idump = (osl_idump_f)osl_strings_idump;
00422 interface->sprint = (osl_sprint_f)osl_strings_sprint;
00423 interface->sread = (osl_sread_f)osl_strings_sread;
00424 interface->malloc = (osl_malloc_f)osl_strings_malloc;
00425 interface->free = (osl_free_f)osl_strings_free;
00426 interface->clone = (osl_clone_f)osl_strings_clone;
00427 interface->equal = (osl_equal_f)osl_strings_equal;
00428
00429 return interface;
00430 }
00431
00432
00442 osl_strings_p osl_strings_generate(char * prefix, int nb_strings) {
00443 char ** strings = NULL;
00444 char buff[strlen(prefix) + 16];
00445 int i;
00446 osl_strings_p generated;
00447
00448 if (nb_strings) {
00449 OSL_malloc(strings, char **, sizeof(char *) * (nb_strings + 1));
00450 strings[nb_strings] = NULL;
00451 for (i = 0; i < nb_strings; i++) {
00452 sprintf(buff, "%s%d", prefix, i + 1);
00453 strings[i] = strdup(buff);
00454 if (strings[i] == NULL)
00455 OSL_error("memory overflow");
00456 }
00457 }
00458
00459 generated = osl_strings_malloc();
00460 generated->string = strings;
00461 return generated;
00462 }