OpenScop 0.8.1
|
00001 00002 /*+-----------------------------------------------------------------** 00003 ** OpenScop Library ** 00004 **-----------------------------------------------------------------** 00005 ** strings.c ** 00006 **-----------------------------------------------------------------** 00007 ** First version: 13/07/2011 ** 00008 **-----------------------------------------------------------------** 00009 00010 00011 ***************************************************************************** 00012 * OpenScop: Structures and formats for polyhedral tools to talk together * 00013 ***************************************************************************** 00014 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, * 00015 * / / / // // // // / / / // // / / // / /|,_, * 00016 * / / / // // // // / / / // // / / // / / / /\ * 00017 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ * 00018 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ * 00019 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ * 00020 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ * 00021 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ * 00022 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ * 00023 * | I | | | | e | | | | | | | | | | | | | \ \ \ * 00024 * | T | | | | | | | | | | | | | | | | | \ \ \ * 00025 * | E | | | | | | | | | | | | | | | | | \ \ \ * 00026 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ * 00027 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / * 00028 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' * 00029 * * 00030 * Copyright (C) 2008 University Paris-Sud 11 and INRIA * 00031 * * 00032 * (3-clause BSD license) * 00033 * Redistribution and use in source and binary forms, with or without * 00034 * modification, are permitted provided that the following conditions * 00035 * are met: * 00036 * * 00037 * 1. Redistributions of source code must retain the above copyright notice, * 00038 * this list of conditions and the following disclaimer. * 00039 * 2. Redistributions in binary form must reproduce the above copyright * 00040 * notice, this list of conditions and the following disclaimer in the * 00041 * documentation and/or other materials provided with the distribution. * 00042 * 3. The name of the author may not be used to endorse or promote products * 00043 * derived from this software without specific prior written permission. * 00044 * * 00045 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * 00046 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * 00047 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * 00048 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * 00049 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * 00050 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * 00051 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * 00052 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 00053 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * 00054 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 00055 * * 00056 * OpenScop Library, a library to manipulate OpenScop formats and data * 00057 * structures. Written by: * 00058 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and * 00059 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> * 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 * Structure display function * 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 // A blank line. 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 * Structure display function * 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 // Skip blank/commented lines and spaces before the strings. 00203 osl_util_sskip_blank_and_comments(input); 00204 00205 // Count the actual number of strings. 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 // Allocate the array of strings. Make it NULL-terminated. 00223 OSL_malloc(string, char **, sizeof(char *) * (nb_strings + 1)); 00224 string[nb_strings] = NULL; 00225 00226 // Read the desired number of strings. 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 // Update the input pointer to the end of the strings structure. 00238 *input = s; 00239 00240 // Build the strings structure 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 * Memory allocation/deallocation function * 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 * Processing functions * 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 clone->string[i] = strdup(strings->string[i]); 00341 if (clone->string[i] == NULL) 00342 OSL_error("memory overflow"); 00343 } 00344 00345 return clone; 00346 } 00347 00348 00357 int osl_strings_equal(osl_strings_p s1, osl_strings_p s2) { 00358 int i, nb_s1; 00359 00360 if (s1 == s2) 00361 return 1; 00362 00363 if (((s1 == NULL) && (s2 != NULL)) || 00364 ((s1 != NULL) && (s2 == NULL)) || 00365 ((nb_s1 = osl_strings_size(s1)) != osl_strings_size(s2))) 00366 return 0; 00367 00368 for (i = 0; i < nb_s1; i++) 00369 if (strcmp(s1->string[i], s2->string[i]) != 0) 00370 return 0; 00371 00372 return 1; 00373 } 00374 00375 00383 int osl_strings_size(osl_strings_p strings) { 00384 int size = 0; 00385 00386 if ((strings != NULL) && (strings->string != NULL)) { 00387 while (strings->string[size] != NULL) { 00388 size++; 00389 } 00390 } 00391 00392 return size; 00393 } 00394 00395 00403 osl_strings_p osl_strings_encapsulate(char * string) { 00404 osl_strings_p capsule = osl_strings_malloc(); 00405 00406 OSL_malloc(capsule->string, char **, 2 * sizeof(char *)); 00407 capsule->string[0] = string; 00408 capsule->string[1] = NULL; 00409 00410 return capsule; 00411 } 00412 00413 00420 osl_interface_p osl_strings_interface() { 00421 osl_interface_p interface = osl_interface_malloc(); 00422 00423 interface->URI = strdup(OSL_URI_STRINGS); 00424 interface->idump = (osl_idump_f)osl_strings_idump; 00425 interface->sprint = (osl_sprint_f)osl_strings_sprint; 00426 interface->sread = (osl_sread_f)osl_strings_sread; 00427 interface->malloc = (osl_malloc_f)osl_strings_malloc; 00428 interface->free = (osl_free_f)osl_strings_free; 00429 interface->clone = (osl_clone_f)osl_strings_clone; 00430 interface->equal = (osl_equal_f)osl_strings_equal; 00431 00432 return interface; 00433 } 00434 00435 00445 osl_strings_p osl_strings_generate(char * prefix, int nb_strings) { 00446 char ** strings = NULL; 00447 char buff[strlen(prefix) + 16]; // TODO: better (log10(INT_MAX) ?) :-D. 00448 int i; 00449 osl_strings_p generated; 00450 00451 if (nb_strings) { 00452 OSL_malloc(strings, char **, sizeof(char *) * (nb_strings + 1)); 00453 strings[nb_strings] = NULL; 00454 for (i = 0; i < nb_strings; i++) { 00455 sprintf(buff, "%s%d", prefix, i + 1); 00456 strings[i] = strdup(buff); 00457 if (strings[i] == NULL) 00458 OSL_error("memory overflow"); 00459 } 00460 } 00461 00462 generated = osl_strings_malloc(); 00463 generated->string = strings; 00464 return generated; 00465 }