Replace openscop_ prefix with osl_ to ease 80 columns programming
[openscop.git] / source / strings.c
blobaaaee3e005c04c005e4575d35bba8251a89990f2
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** strings.c **
6 **-----------------------------------------------------------------**
7 ** First version: 13/07/2011 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
29 * *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
31 * *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
35 * are met: *
36 * *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
44 * *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
55 * *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
60 * *
61 *****************************************************************************/
63 # include <stdlib.h>
64 # include <stdio.h>
65 # include <ctype.h>
66 # include <string.h>
67 # include <osl/strings.h>
70 /*+***************************************************************************
71 * Structure display function *
72 *****************************************************************************/
75 /**
76 * osl_strings_idump function:
77 * this function displays an array of strings into a file (file, possibly
78 * stdout) in a way that trends to be understandable. It includes an
79 * indentation level (level) in order to work with others
80 * print_structure functions.
81 * \param[in] file The file where the information has to be printed.
82 * \param[in] strings The array of strings that has to be printed.
83 * \param[in] level Number of spaces before printing, for each line.
85 void osl_strings_idump(FILE * file, osl_strings_p strings, int level) {
86 int i, nb_strings;
88 for (i = 0; i < level; i++)
89 fprintf(file, "|\t");
91 if (strings != NULL) {
92 nb_strings = osl_strings_size(strings);
93 fprintf(file, "+-- osl_strings_t:");
94 for (i = 0; i < nb_strings; i++)
95 fprintf(file, " %s", strings->string[i]);
96 fprintf(file, "\n");
98 else
99 fprintf(file, "+-- NULL strings\n");
101 // A blank line.
102 for (i = 0; i <= level; i++)
103 fprintf(file, "|\t");
104 fprintf(file, "\n");
109 * osl_strings_dump function:
110 * this function prints the content of an osl_strings_t structure
111 * (*strings) into a file (file, possibly stdout).
112 * \param[in] file The file where the information has to be printed.
113 * \param[in] strings The strings structure which has to be printed.
115 void osl_strings_dump(FILE * file, osl_strings_p strings) {
116 osl_strings_idump(file, strings, 0);
121 * osl_strings_sprint function:
122 * this function prints the content of an osl_strings_t structure
123 * (*strings) into a string (returned) in the OpenScop textual format.
124 * \param[in] strings The strings structure which has to be printed.
125 * \return A string containing the OpenScop dump of the strings structure.
127 char * osl_strings_sprint(osl_strings_p strings) {
128 int i;
129 int high_water_mark = OSL_MAX_STRING;
130 char * string = NULL;
131 char * buffer;
133 if (strings != NULL) {
134 OSL_malloc(string, char *, high_water_mark * sizeof(char));
135 OSL_malloc(buffer, char *, OSL_MAX_STRING * sizeof(char));
136 string[0] = '\0';
138 // Print the begin tag.
139 for (i = 0; i < osl_strings_size(strings); i++) {
140 sprintf(buffer, "%s", strings->string[i]);
141 osl_util_safe_strcat(&string, buffer, &high_water_mark);
142 if (i < osl_strings_size(strings) - 1)
143 osl_util_safe_strcat(&string, " ", &high_water_mark);
145 sprintf(buffer, "\n");
146 osl_util_safe_strcat(&string, buffer, &high_water_mark);
147 free(buffer);
150 return string;
155 * osl_strings_print function:
156 * this function prints the content of an osl_strings_t structure
157 * (*body) into a file (file, possibly stdout) in the OpenScop format.
158 * \param file[in] File where informations are printed.
159 * \param strings[in] The strings whose information has to be printed.
161 void osl_strings_print(FILE * file, osl_strings_p strings) {
162 char * string;
164 string = osl_strings_sprint(strings);
165 if (string != NULL) {
166 fprintf(file, "%s", string);
167 free(string);
172 /*+***************************************************************************
173 * Structure display function *
174 *****************************************************************************/
178 * osl_strings_sread function:
179 * this function reads a strings structure from a string complying to the
180 * OpenScop textual format and returns a pointer to this strings structure.
181 * The input string should only contain the list of strings this function
182 * has to read (comments at the end of the line are accepted).
183 * \param[in] input The input string where to find a strings structure.
184 * \return A pointer to the strings structure that has been read.
186 osl_strings_p osl_strings_sread(char * input) {
187 char tmp[OSL_MAX_STRING];
188 char * s;
189 char ** string = NULL;
190 int nb_strings;
191 int i, count;
192 osl_strings_p strings = NULL;
194 // Count the actual number of strings.
195 nb_strings = 0;
196 s = input;
197 while (1) {
198 for (count = 0; *s && ! isspace(*s) && *s != '#'; count++)
199 s++;
201 if (count != 0)
202 nb_strings++;
204 if ((*s == '#') || (*s == '\n'))
205 break;
206 else
207 ++s;
210 if (nb_strings > 0) {
211 // Allocate the array of strings. Make it NULL-terminated.
212 OSL_malloc(string, char **, sizeof(char *) * (nb_strings + 1));
213 string[nb_strings] = NULL;
215 // Read the desired number of strings.
216 s = input;
217 for (i = 0; i < nb_strings; i++) {
218 for (count = 0; *s && ! isspace(*s) && *s != '#'; count++)
219 tmp[count] = *(s++);
220 tmp[count] = '\0';
221 string[i] = strdup(tmp);
222 if (string[i] == NULL)
223 OSL_error("memory overflow");
224 if (*s != '#')
225 ++s;
228 // Build the strings structure
229 strings = osl_strings_malloc();
230 strings->string = string;
233 return strings;
238 * osl_strings_read function.
239 * this function reads a strings structure from a file (possibly stdin)
240 * complying to the OpenScop textual format and returns a pointer to this
241 * structure.
242 * parameter nb_strings).
243 * \param[in] file The file where to read the strings structure.
244 * \return The strings structure that has been read.
246 osl_strings_p osl_strings_read(FILE * file) {
247 char buffer[OSL_MAX_STRING], * start;
248 osl_strings_p strings;
250 start = osl_util_skip_blank_and_comments(file, buffer);
251 strings = osl_strings_sread(start);
253 return strings;
257 /*+***************************************************************************
258 * Memory allocation/deallocation function *
259 *****************************************************************************/
263 * osl_strings_malloc function:
264 * This function allocates the memory space for an osl_strings_t
265 * structure and sets its fields with default values. Then it returns a
266 * pointer to the allocated space.
267 * \return A pointer to an empty strings structure with fields set to
268 * default values.
270 osl_strings_p osl_strings_malloc() {
271 osl_strings_p strings;
273 OSL_malloc(strings, osl_strings_p, sizeof(osl_strings_t));
274 strings->string = NULL;
276 return strings;
281 * osl_strings_free function:
282 * this function frees the allocated memory for a strings data structure.
283 * \param[in] strings The strings structure we want to free.
285 void osl_strings_free(osl_strings_p strings) {
286 int i;
288 if (strings != NULL) {
289 if (strings->string != NULL) {
290 i = 0;
291 while(strings->string[i] != NULL) {
292 free(strings->string[i]);
293 i++;
295 free(strings->string);
297 free(strings);
302 /*+***************************************************************************
303 * Processing functions *
304 *****************************************************************************/
308 * osl_strings_clone function.
309 * this function builds and return a "hard copy" (not a pointer copy) of an
310 * strings structure provided as parameter.
311 * \param[in] strings The strings structure to clone.
312 * \return The clone of the strings structure.
314 osl_strings_p osl_strings_clone(osl_strings_p strings) {
315 int i, nb_strings;
316 osl_strings_p clone = NULL;
318 if (strings == NULL)
319 return NULL;
321 clone = osl_strings_malloc();
322 if ((nb_strings = osl_strings_size(strings)) == 0)
323 return clone;
325 OSL_malloc(clone->string, char **, (nb_strings + 1) * sizeof(char *));
326 clone->string[nb_strings] = NULL;
327 for (i = 0; i < nb_strings; i++) {
328 clone->string[i] = strdup(strings->string[i]);
329 if (clone->string[i] == NULL)
330 OSL_error("memory overflow");
333 return clone;
338 * osl_strings_equal function:
339 * this function returns true if the two strings structures are the same
340 * (content-wise), false otherwise.
341 * \param[in] s1 The first strings structure.
342 * \param[in] s2 The second strings structure.
343 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
345 int osl_strings_equal(osl_strings_p s1, osl_strings_p s2) {
346 int i, nb_s1;
348 if (s1 == s2)
349 return 1;
351 if (((s1 == NULL) && (s2 != NULL)) ||
352 ((s1 != NULL) && (s2 == NULL)) ||
353 ((nb_s1 = osl_strings_size(s1)) != osl_strings_size(s2)))
354 return 0;
356 for (i = 0; i < nb_s1; i++)
357 if (strcmp(s1->string[i], s2->string[i]) != 0)
358 return 0;
360 return 1;
365 * osl_strings_size function:
366 * this function returns the number of elements in the NULL-terminated
367 * strings array of the strings structure.
368 * \param[in] strings The strings structure we need to know the size.
369 * \return The number of strings in the strings structure.
371 int osl_strings_size(osl_strings_p strings) {
372 int size = 0;
374 if ((strings != NULL) && (strings->string != NULL)) {
375 while (strings->string[size] != NULL) {
376 size++;
380 return size;
385 * osl_strings_encapsulate function:
386 * this function builds a new strings structure to encapsulate the string
387 * provided as a parameter (the reference to this string is used directly).
388 * \param[in] string The string to encapsulate in a strings structure.
389 * \return A new strings structure containing only the provided string.
391 osl_strings_p osl_strings_encapsulate(char * string) {
392 osl_strings_p capsule = osl_strings_malloc();
394 OSL_malloc(capsule->string, char **, 2 * sizeof(char *));
395 capsule->string[0] = string;
396 capsule->string[1] = NULL;
398 return capsule;
403 * osl_strings_interface function:
404 * this function creates an interface structure corresponding to the strings
405 * structure and returns it).
406 * \return An interface structure for the strings structure.
408 osl_interface_p osl_strings_interface() {
409 osl_interface_p interface = osl_interface_malloc();
411 interface->URI = strdup(OSL_URI_STRINGS);
412 interface->idump = (osl_idump_f)osl_strings_idump;
413 interface->dump = (osl_dump_f)osl_strings_dump;
414 interface->sprint = (osl_sprint_f)osl_strings_sprint;
415 interface->sread = (osl_sread_f)osl_strings_sread;
416 interface->malloc = (osl_malloc_f)osl_strings_malloc;
417 interface->free = (osl_free_f)osl_strings_free;
418 interface->clone = (osl_clone_f)osl_strings_clone;
419 interface->equal = (osl_equal_f)osl_strings_equal;
421 return interface;
426 * osl_strings_generate function:
427 * this function generates a new strings structure containing
428 * 'nb_strings' strings of the form "prefixXX" where XX goes from 1 to
429 * nb_strings.
430 * \param[in] prefix The prefix of the generated strings.
431 * \param[in] nb_strings The number of strings to generate.
432 * \return A new strings structure containing generated strings.
434 osl_strings_p osl_strings_generate(char * prefix, int nb_strings) {
435 char ** strings = NULL;
436 char buff[strlen(prefix) + 16]; // TODO: better (log10(INT_MAX) ?) :-D.
437 int i;
438 osl_strings_p generated;
440 if (nb_strings) {
441 OSL_malloc(strings, char **, sizeof(char *) * (nb_strings + 1));
442 strings[nb_strings] = NULL;
443 for (i = 0; i < nb_strings; i++) {
444 sprintf(buff, "%s%d", prefix, i + 1);
445 strings[i] = strdup(buff);
446 if (strings[i] == NULL)
447 OSL_error("memory overflow");
451 generated = osl_strings_malloc();
452 generated->string = strings;
453 return generated;
457 #if 0
459 * osl_strings_print function:
460 * this function prints the content of an array of strings
461 * into a file (file, possibly stdout) in the OpenScop textual format.
462 * \param[in] file The file where the information has to be printed.
463 * \param[in] strings The array of strings that has to be printed.
464 * \param[in] provided Boolean set to 1 to print a "provided" boolean
465 * before the string list, 0 otherwise.
466 * \param[in] print Boolean set to 1 to print the strings, 0 otherwise.
467 * \param[in] title A string to use as a title for the array of strings.
469 void osl_strings_print(FILE * file, char ** strings,
470 int provided, int print, char * title) {
471 int i, nb_strings;
473 if ((print != 0) &&
474 ((nb_strings = osl_strings_size(strings)) > 0)) {
475 if (provided) {
476 fprintf(file, "# %s are provided\n", title);
477 fprintf(file, "1\n");
479 fprintf(file, "# %s\n", title);
480 for (i = 0; i < nb_strings; i++)
481 fprintf(file, "%s ", strings[i]);
482 fprintf(file, "\n\n");
484 else {
485 if (provided) {
486 fprintf(file, "# %s are not provided\n", title);
487 fprintf(file, "0\n\n");
496 * osl_strings_complete function:
497 * this function completes an array of strings with generated strings.
498 * \param[in,out] strings Pointer to the initial array of strings.
499 * \param[in] prefix The prefix of the generated names.
500 * \param[in] nb_complete The desired number of strings in the new array.
502 void osl_strings_complete(char *** strings, char * prefix,
503 int nb_complete) {
504 int i, nb_new;
505 char ** completion;
507 nb_new = nb_complete - osl_strings_size(*strings);
508 if (nb_new < 0) {
509 fprintf(stderr, "[OpenScop] Error: asked to complete the following "
510 "string array but it would be smaller than the original "
511 "(desired length: %d):\n", nb_complete);
512 osl_strings_idump(stderr, *strings, -1, "To be completed");
513 exit(1);
516 if (nb_new > 0) {
517 OSL_realloc(*strings, char **, (nb_complete + 1) * sizeof(char *));
518 *strings[nb_complete] = NULL;
519 completion = osl_strings_generate(prefix, nb_new);
520 for (i = 0; i < nb_new; i++)
521 (*strings)[i + osl_strings_size(*strings)] = completion[i];
522 free(completion);
525 #endif