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
00071
00072
00073
00074
00075
00076
00091 char * osl_util_skip_blank_and_comments(FILE * file, char * str) {
00092 char * start;
00093
00094 do {
00095 start = fgets(str, OSL_MAX_STRING, file);
00096 while ((start != NULL) && isspace(*start) && (*start != '\n'))
00097 start++;
00098 }
00099 while (start != NULL && (*start == '#' || *start == '\n'));
00100
00101 return start;
00102 }
00103
00104
00113 void osl_util_sskip_blank_and_comments(char ** str) {
00114 do {
00115
00116 while (*str && **str && isspace(**str))
00117 (*str)++;
00118
00119
00120 if (*str && **str && **str == '#') {
00121 while (**str && **str != '\n') {
00122 (*str)++;
00123 }
00124 }
00125 }
00126 while (*str && **str && **str == '\n');
00127 }
00128
00129
00140 int osl_util_read_int(FILE * file, char ** str) {
00141 char s[OSL_MAX_STRING], * start;
00142 int res;
00143 int i = 0;
00144
00145 if ((file != NULL && str != NULL) || (file == NULL && str == NULL))
00146 OSL_error("one and only one of the two parameters can be non-NULL");
00147
00148 if (file != NULL) {
00149
00150 start = osl_util_skip_blank_and_comments(file, s);
00151 if (sscanf(start, " %d", &res) != 1)
00152 OSL_error("an int was expected");
00153 }
00154 else {
00155
00156
00157 osl_util_sskip_blank_and_comments(str);
00158
00159
00160 while (**str && !isspace(**str) && **str != '\n' && **str != '#')
00161 s[i++] = *((*str)++);
00162 s[i] = '\0';
00163 if (sscanf(s, "%d", &res) != 1)
00164 OSL_error("an int was expected");
00165 }
00166
00167 return res;
00168 }
00169
00170
00181 char * osl_util_read_string(FILE * file, char ** str) {
00182 char s[OSL_MAX_STRING], * start;
00183 char * res;
00184 int i = 0;
00185
00186 if ((file != NULL && str != NULL) || (file == NULL && str == NULL))
00187 OSL_error("one and only one of the two parameters can be non-NULL");
00188
00189 OSL_malloc(res, char *, OSL_MAX_STRING * sizeof(char));
00190 if (file != NULL) {
00191
00192 start = osl_util_skip_blank_and_comments(file, s);
00193 if (sscanf(start, " %s", res) != 1)
00194 OSL_error("a string was expected");
00195 }
00196 else {
00197
00198
00199 osl_util_sskip_blank_and_comments(str);
00200
00201
00202 while (**str && !isspace(**str) && **str != '\n' && **str != '#')
00203 s[i++] = *((*str)++);
00204 s[i] = '\0';
00205 if (sscanf(s, "%s", res) != 1)
00206 OSL_error("a string was expected");
00207 }
00208
00209 OSL_realloc(res, char *, strlen(res) + 1);
00210 return res;
00211 }
00212
00213
00226 char * osl_util_read_line(FILE * file, char ** str) {
00227 char s[OSL_MAX_STRING], * start;
00228 char * res;
00229 int i = 0;
00230
00231 if ((file != NULL && str != NULL) || (file == NULL && str == NULL))
00232 OSL_error("one and only one of the two parameters can be non-NULL");
00233
00234 OSL_malloc(res, char *, OSL_MAX_STRING * sizeof(char));
00235 if (file != NULL) {
00236
00237 start = osl_util_skip_blank_and_comments(file, s);
00238 while (*start && *start != '\n' && *start != '#' && i < OSL_MAX_STRING)
00239 res[i++] = (*start)++;
00240 }
00241 else {
00242
00243 osl_util_sskip_blank_and_comments(str);
00244 while (**str && **str != '\n' && **str != '#' && i < OSL_MAX_STRING)
00245 res[i++] = *((*str)++);
00246 }
00247
00248 res[i] = '\0';
00249 OSL_realloc(res, char *, strlen(res) + 1);
00250 return res;
00251 }
00252
00253
00267 char * osl_util_read_tag(FILE * file, char ** str) {
00268 char s[OSL_MAX_STRING], * start;
00269 char * res;
00270 int i = 0;
00271
00272 if ((file != NULL && str != NULL) || (file == NULL && str == NULL))
00273 OSL_error("one and only one of the two parameters can be non-NULL");
00274
00275
00276 if (file != NULL) {
00277 start = osl_util_skip_blank_and_comments(file, s);
00278 str = &start;
00279 }
00280 else {
00281 osl_util_sskip_blank_and_comments(str);
00282 }
00283
00284
00285 if (**str != '<')
00286 OSL_error("a \"<\" to start a tag was expected");
00287 (*str)++;
00288
00289
00290 OSL_malloc(res, char *, (OSL_MAX_STRING + 1) * sizeof(char));
00291 res[OSL_MAX_STRING] = '\0';
00292
00293 while (**str && **str != '>') {
00294 if (((**str >= 'A') && (**str <= 'Z')) ||
00295 ((**str >= 'a') && (**str <= 'z')) ||
00296 ((**str == '/') && (i == 0)) ||
00297 (**str == '_')) {
00298 res[i++] = *((*str)++);
00299 res[i] = '\0';
00300 }
00301 else {
00302 OSL_error("illegal character in the tag name");
00303 }
00304 }
00305
00306
00307 if (**str != '>')
00308 OSL_error("a \">\" to end a tag was expected");
00309 (*str)++;
00310
00311 return res;
00312 }
00313
00314
00324 char * osl_util_read_uptotag(FILE * file, char * tag) {
00325 int high_water_mark = OSL_MAX_STRING;
00326 int nb_chars = 0;
00327 int lentag = strlen(tag);
00328 int tag_found = 0;
00329 char * res;
00330
00331 OSL_malloc(res, char *, high_water_mark * sizeof(char));
00332
00333
00334 while (!feof(file)) {
00335 res[nb_chars] = fgetc(file);
00336 nb_chars++;
00337
00338 if ((nb_chars >= lentag) &&
00339 (!strncmp(&res[nb_chars - lentag], tag, lentag))) {
00340 tag_found = 1;
00341 break;
00342 }
00343
00344 if (nb_chars >= high_water_mark) {
00345 high_water_mark += high_water_mark;
00346 OSL_realloc(res, char *, high_water_mark * sizeof(char));
00347 }
00348 }
00349
00350 if (!tag_found) {
00351 OSL_debug("tag was not found, end of file reached");
00352 free(res);
00353 return NULL;
00354 }
00355
00356
00357 OSL_realloc(res, char *, (nb_chars - strlen(tag) + 1) * sizeof(char));
00358 res[nb_chars - strlen(tag)] = '\0';
00359
00360 return res;
00361 }
00362
00363
00374 char * osl_util_read_uptoendtag(FILE * file, char * name) {
00375 char tag[strlen(name) + 4];
00376
00377 sprintf(tag, "</%s>", name);
00378 return osl_util_read_uptotag(file, tag);
00379 }
00380
00381
00391 char * osl_util_tag_content(char * str, char * name) {
00392 int i;
00393 char * start;
00394 char * stop;
00395 char tag[strlen(name) + 3];
00396 char endtag[strlen(name) + 4];
00397 int size = 0;
00398 int lentag;
00399 char * res = NULL;
00400
00401 sprintf(tag, "<%s>", name);
00402 sprintf(endtag, "</%s>", name);
00403
00404 if (str) {
00405 start = str;
00406 lentag = strlen(tag);
00407 for (; start && *start && strncmp(start, tag, lentag); ++start)
00408 continue;
00409
00410
00411 if (! *start)
00412 return NULL;
00413 start += lentag;
00414 stop = start;
00415 lentag = strlen(endtag);
00416 for (size = 0; *stop && strncmp(stop, endtag, lentag); ++stop, ++size)
00417 continue;
00418
00419
00420 if (! *stop)
00421 return NULL;
00422 OSL_malloc(res, char *, (size + 1) * sizeof(char));
00423
00424
00425 for (++start, i = 0; start != stop; ++start, ++i)
00426 res[i] = *start;
00427 res[i] = '\0';
00428 }
00429
00430 return res;
00431 }
00432
00433
00444 void osl_util_safe_strcat(char ** dst, char * src, int * hwm) {
00445
00446 while (strlen(*dst) + strlen(src) >= *hwm) {
00447 *hwm += OSL_MAX_STRING;
00448 OSL_realloc(*dst, char *, *hwm * sizeof(char));
00449 }
00450
00451 strcat(*dst, src);
00452 }
00453
00454
00461 int osl_util_get_precision() {
00462 int precision = OSL_PRECISION_DP;
00463 char * precision_env;
00464
00465 #ifdef OSL_GMP_IS_HERE
00466 precision = OSL_PRECISION_MP;
00467 #endif
00468
00469 precision_env = getenv(OSL_PRECISION_ENV);
00470 if (precision_env != NULL) {
00471 if (!strcmp(precision_env, OSL_PRECISION_ENV_SP))
00472 precision = OSL_PRECISION_SP;
00473 else if (!strcmp(precision_env, OSL_PRECISION_ENV_DP))
00474 precision = OSL_PRECISION_DP;
00475 else if (!strcmp(precision_env, OSL_PRECISION_ENV_MP)) {
00476 #ifndef OSL_GMP_IS_HERE
00477 OSL_warning("$OSL_PRECISION says GMP but osl not compiled with "
00478 "GMP support, switching to double precision");
00479 precision = OSL_PRECISION_DP;
00480 #else
00481 precision = OSL_PRECISION_MP;
00482 #endif
00483 }
00484 else
00485 OSL_warning("bad OSL_PRECISION environment value, see osl's manual");
00486 }
00487
00488 return precision;
00489 }
00490
00491
00500 void osl_util_print_provided(FILE * file, int provided, char * title) {
00501 if (provided) {
00502 fprintf(file, "# %s provided\n", title);
00503 fprintf(file, "1\n");
00504 }
00505 else {
00506 fprintf(file, "# %s not provided\n", title);
00507 fprintf(file, "0\n\n");
00508 }
00509 }
00510
00511
00521 static
00522 int osl_util_identifier_is_here(char * expression, char * identifier,
00523 int index) {
00524
00525 if (strlen(identifier) + index > strlen(expression))
00526 return 0;
00527
00528
00529 if ((index > 0) &&
00530 (((expression[index - 1] >= 'A') && (expression[index - 1] <= 'Z')) ||
00531 ((expression[index - 1] >= 'a') && (expression[index - 1] <= 'z')) ||
00532 ((expression[index - 1] >= '0') && (expression[index - 1] <= '9'))))
00533 return 0;
00534
00535
00536 if ((strlen(identifier) + index < strlen(expression)) &&
00537 (((expression[strlen(identifier) + index] >= 'A') &&
00538 (expression[strlen(identifier) + index] <= 'Z')) ||
00539 ((expression[strlen(identifier) + index] >= 'a') &&
00540 (expression[strlen(identifier) + index] <= 'z')) ||
00541 ((expression[strlen(identifier) + index] >= '0') &&
00542 (expression[strlen(identifier) + index] <= '9'))))
00543 return 0;
00544
00545
00546 if (strncmp(expression + index, identifier, strlen(identifier)))
00547 return 0;
00548
00549 return 1;
00550 }
00551
00552
00567 static
00568 int osl_util_lazy_isolated_identifier(char * expression, char * identifier,
00569 int index) {
00570 int look;
00571
00572
00573 look = index - 1;
00574 while (look >= 0) {
00575 if (isspace(expression[look]))
00576 look--;
00577 else
00578 break;
00579 }
00580
00581 if ((look >= 0) &&
00582 (expression[look] != '[') &&
00583 (expression[look] != '(') &&
00584 (expression[look] != '+') &&
00585 (expression[look] != '=') &&
00586 (expression[look] != ','))
00587 return 0;
00588
00589
00590 look = index + strlen(identifier);
00591 while (look < strlen(expression)) {
00592 if (isspace(expression[look]))
00593 look++;
00594 else
00595 break;
00596 }
00597
00598 if ((look < strlen(expression)) &&
00599 (expression[look] != ']') &&
00600 (expression[look] != ')') &&
00601 (expression[look] != '+') &&
00602 (expression[look] != ',') &&
00603 (expression[look] != ';'))
00604 return 0;
00605
00606 return 1;
00607 }
00608
00609
00625 char * osl_util_identifier_substitution(char * expression,
00626 char ** identifiers) {
00627 int index, j, found;
00628 int high_water_mark = OSL_MAX_STRING;
00629 char buffer[OSL_MAX_STRING];
00630 char * string;
00631
00632 OSL_malloc(string, char *, high_water_mark * sizeof(char));
00633 string[0] = '\0';
00634
00635 index = 0;
00636 while (index < strlen(expression)) {
00637 j = 0;
00638 found = 0;
00639 while (identifiers[j] != NULL) {
00640 if (osl_util_identifier_is_here(expression, identifiers[j], index)) {
00641 if (osl_util_lazy_isolated_identifier(expression,identifiers[j],index))
00642 sprintf(buffer, "@%d@", j);
00643 else
00644 sprintf(buffer, "(@%d@)", j);
00645 osl_util_safe_strcat(&string, buffer, &high_water_mark);
00646 index += strlen(identifiers[j]);
00647 found = 1;
00648 break;
00649 }
00650 j++;
00651 }
00652 if (!found) {
00653 sprintf(buffer, "%c", expression[index]);
00654 osl_util_safe_strcat(&string, buffer, &high_water_mark);
00655 index++;
00656 }
00657 }
00658
00659 return string;
00660 }
00661
00662
00663