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
00064 # include <stdlib.h>
00065 # include <stdio.h>
00066 # include <string.h>
00067 # include <ctype.h>
00068 # include <osl/macros.h>
00069 # include <osl/util.h>
00070 # include <osl/strings.h>
00071 # include <osl/interface.h>
00072 # include <osl/body.h>
00073
00074
00075
00076
00077
00078
00079
00090 void osl_body_idump(FILE * file, osl_body_p body, int level) {
00091 int j;
00092
00093
00094 for (j = 0; j < level; j++)
00095 fprintf(file, "|\t");
00096
00097 if (body != NULL) {
00098 fprintf(file, "+-- osl_body_t\n");
00099
00100
00101 for (j = 0; j <= level+1; j++)
00102 fprintf(file, "|\t");
00103 fprintf(file, "\n");
00104
00105
00106 osl_strings_idump(file, body->iterators, level + 1);
00107
00108
00109 osl_strings_idump(file, body->expression, level + 1);
00110 }
00111 else {
00112 fprintf(file, "+-- NULL body\n");
00113 }
00114
00115
00116 for (j = 0; j <= level; j++)
00117 fprintf(file, "|\t");
00118 fprintf(file, "\n");
00119 }
00120
00121
00129 void osl_body_dump(FILE * file, osl_body_p body) {
00130 osl_body_idump(file, body, 0);
00131 }
00132
00133
00141 void osl_body_print(FILE * file, osl_body_p body) {
00142 int nb_iterators;
00143
00144 if (body != NULL) {
00145 nb_iterators = osl_strings_size(body->iterators);
00146 fprintf(file, "# Number of original iterators\n");
00147 fprintf(file, "%d\n", nb_iterators);
00148
00149 if (nb_iterators > 0) {
00150 fprintf(file, "\n# List of original iterators\n");
00151 osl_strings_print(file, body->iterators);
00152 }
00153
00154 fprintf(file, "\n# Statement body expression\n");
00155 osl_strings_print(file, body->expression);
00156 }
00157 else {
00158 fprintf(file, "# NULL statement body\n");
00159 }
00160 }
00161
00162
00170 char * osl_body_sprint(osl_body_p body) {
00171 int nb_iterators;
00172 int high_water_mark = OSL_MAX_STRING;
00173 char * string = NULL;
00174 char buffer[OSL_MAX_STRING];
00175 char * iterators, * expression;
00176
00177 OSL_malloc(string, char *, high_water_mark * sizeof(char));
00178 string[0] = '\0';
00179
00180 if (body != NULL) {
00181 nb_iterators = osl_strings_size(body->iterators);
00182 sprintf(buffer, "# Number of original iterators\n%d\n", nb_iterators);
00183 osl_util_safe_strcat(&string, buffer, &high_water_mark);
00184
00185 if (nb_iterators > 0) {
00186 sprintf(buffer, "# List of original iterators\n");
00187 osl_util_safe_strcat(&string, buffer, &high_water_mark);
00188 iterators = osl_strings_sprint(body->iterators);
00189 osl_util_safe_strcat(&string, iterators, &high_water_mark);
00190 free(iterators);
00191 }
00192
00193 sprintf(buffer, "# Statement body expression\n");
00194 osl_util_safe_strcat(&string, buffer, &high_water_mark);
00195 expression = osl_strings_sprint(body->expression);
00196 osl_util_safe_strcat(&string, expression, &high_water_mark);
00197 free(expression);
00198 }
00199 else {
00200 sprintf(buffer, "# NULL body\n");
00201 osl_util_safe_strcat(&string, buffer, &high_water_mark);
00202 }
00203
00204 return string;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00226 osl_body_p osl_body_sread(char ** input) {
00227 osl_body_p body = NULL;
00228 char * expression;
00229 int nb_iterators;
00230
00231 if (input) {
00232 body = osl_body_malloc();
00233
00234
00235 nb_iterators = osl_util_read_int(NULL, input);
00236
00237
00238 if (nb_iterators > 0) {
00239 body->iterators = osl_strings_sread(input);
00240 }
00241 else {
00242 body->iterators = osl_strings_malloc();
00243 OSL_malloc(body->iterators->string, char **, sizeof(char *));
00244 body->iterators->string[0] = NULL;
00245 }
00246
00247
00248 expression = osl_util_read_line(NULL, input);
00249
00250
00251 body->expression = osl_strings_encapsulate(expression);
00252 }
00253
00254 return body;
00255 }
00256
00257
00258
00259
00260
00261
00262
00270 osl_body_p osl_body_malloc() {
00271 osl_body_p body;
00272
00273 OSL_malloc(body, osl_body_p, sizeof(osl_body_t));
00274 body->iterators = NULL;
00275 body->expression = NULL;
00276
00277 return body;
00278 }
00279
00280
00287 void osl_body_free(osl_body_p body) {
00288
00289 if (body != NULL) {
00290 osl_strings_free(body->iterators);
00291 osl_strings_free(body->expression);
00292 free(body);
00293 }
00294 }
00295
00296
00297
00298
00299
00300
00301
00310 osl_body_p osl_body_clone(osl_body_p body) {
00311 osl_body_p copy = NULL;
00312
00313 if (body != NULL) {
00314 copy = osl_body_malloc();
00315 copy->iterators = osl_strings_clone(body->iterators);
00316 copy->expression = osl_strings_clone(body->expression);
00317 }
00318
00319 return copy;
00320 }
00321
00322
00332 int osl_body_equal(osl_body_p b1, osl_body_p b2) {
00333
00334 if (b1 == b2)
00335 return 1;
00336
00337 if (((b1 != NULL) && (b2 == NULL)) ||
00338 ((b1 == NULL) && (b2 != NULL))) {
00339 OSL_info("bodies are not the same");
00340 return 0;
00341 }
00342
00343 if (!osl_strings_equal(b1->iterators, b2->iterators)) {
00344 OSL_info("body iterators are not the same");
00345 return 0;
00346 }
00347
00348 if (!osl_strings_equal(b1->expression, b2->expression)) {
00349 OSL_info("body expressions are not the same");
00350 return 0;
00351 }
00352
00353 return 1;
00354 }
00355
00356
00363 osl_interface_p osl_body_interface() {
00364 osl_interface_p interface = osl_interface_malloc();
00365
00366 interface->URI = strdup(OSL_URI_BODY);
00367 interface->idump = (osl_idump_f)osl_body_idump;
00368 interface->sprint = (osl_sprint_f)osl_body_sprint;
00369 interface->sread = (osl_sread_f)osl_body_sread;
00370 interface->malloc = (osl_malloc_f)osl_body_malloc;
00371 interface->free = (osl_free_f)osl_body_free;
00372 interface->clone = (osl_clone_f)osl_body_clone;
00373 interface->equal = (osl_equal_f)osl_body_equal;
00374
00375 return interface;
00376 }
00377