OpenScop  0.9.0
strings.c
Go to the documentation of this file.
1 
2  /*+-----------------------------------------------------------------**
3  ** OpenScop Library **
4  **-----------------------------------------------------------------**
5  ** strings.c **
6  **-----------------------------------------------------------------**
7  ** First version: 13/07/2011 **
8  **-----------------------------------------------------------------**
9 
10 
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  *****************************************************************************/
62 
63 # include <stdlib.h>
64 # include <stdio.h>
65 # include <ctype.h>
66 # include <string.h>
67 
68 # include <osl/macros.h>
69 # include <osl/util.h>
70 # include <osl/interface.h>
71 # include <osl/strings.h>
72 
73 
74 /*+***************************************************************************
75  * Structure display function *
76  *****************************************************************************/
77 
78 
89 void osl_strings_idump(FILE * file, osl_strings_p strings, int level) {
90  int i, nb_strings;
91 
92  for (i = 0; i < level; i++)
93  fprintf(file, "|\t");
94 
95  if (strings != NULL) {
96  nb_strings = osl_strings_size(strings);
97  fprintf(file, "+-- osl_strings_t:");
98  for (i = 0; i < nb_strings; i++)
99  fprintf(file, " %s", strings->string[i]);
100  fprintf(file, "\n");
101  }
102  else
103  fprintf(file, "+-- NULL strings\n");
104 
105  // A blank line.
106  for (i = 0; i <= level; i++)
107  fprintf(file, "|\t");
108  fprintf(file, "\n");
109 }
110 
111 
119 void osl_strings_dump(FILE * file, osl_strings_p strings) {
120  osl_strings_idump(file, strings, 0);
121 }
122 
123 
132  size_t i;
133  int high_water_mark = OSL_MAX_STRING;
134  char * string = NULL;
135  char buffer[OSL_MAX_STRING];
136 
137  OSL_malloc(string, char *, high_water_mark * sizeof(char));
138  string[0] = '\0';
139 
140  if (strings != NULL) {
141  for (i = 0; i < osl_strings_size(strings); i++) {
142  sprintf(buffer, "%s", strings->string[i]);
143  osl_util_safe_strcat(&string, buffer, &high_water_mark);
144  if (i < osl_strings_size(strings) - 1)
145  osl_util_safe_strcat(&string, " ", &high_water_mark);
146  }
147  sprintf(buffer, "\n");
148  osl_util_safe_strcat(&string, buffer, &high_water_mark);
149  }
150  else {
151  sprintf(buffer, "# NULL strings\n");
152  osl_util_safe_strcat(&string, buffer, &high_water_mark);
153  }
154 
155  return string;
156 }
157 
158 
166 void osl_strings_print(FILE * file, osl_strings_p strings) {
167  char * string;
168 
169  string = osl_strings_sprint(strings);
170  if (string != NULL) {
171  fprintf(file, "%s", string);
172  free(string);
173  }
174 }
175 
176 
177 /*+***************************************************************************
178  * Structure display function *
179  *****************************************************************************/
180 
181 
195  char tmp[OSL_MAX_STRING];
196  char * s;
197  char ** string = NULL;
198  int nb_strings;
199  int i, count;
200  osl_strings_p strings = NULL;
201 
202  // Skip blank/commented lines and spaces before the strings.
204 
205  // Count the actual number of strings.
206  nb_strings = 0;
207  s = *input;
208  while (1) {
209  for (count = 0; *s && !isspace(*s) && *s != '#'; count++)
210  s++;
211 
212  if (count != 0)
213  nb_strings++;
214 
215  if ((!*s) || (*s == '#') || (*s == '\n'))
216  break;
217  else
218  s++;
219  }
220 
221  if (nb_strings > 0) {
222  // Allocate the array of strings. Make it NULL-terminated.
223  OSL_malloc(string, char **, sizeof(char *) * (nb_strings + 1));
224  string[nb_strings] = NULL;
225 
226  // Read the desired number of strings.
227  s = *input;
228  for (i = 0; i < nb_strings; i++) {
229  for (count = 0; *s && !isspace(*s) && *s != '#'; count++)
230  tmp[count] = *(s++);
231  tmp[count] = '\0';
232  OSL_strdup(string[i], tmp);
233  if (*s != '#')
234  s++;
235  }
236 
237  // Update the input pointer to the end of the strings structure.
238  *input = s;
239 
240  // Build the strings structure
241  strings = osl_strings_malloc();
242  free(strings->string);
243  strings->string = string;
244  }
245 
246  return strings;
247 }
248 
249 
260  char buffer[OSL_MAX_STRING], * start;
261  osl_strings_p strings;
262 
263  start = osl_util_skip_blank_and_comments(file, buffer);
264  strings = osl_strings_sread(&start);
265 
266  return strings;
267 }
268 
269 
270 /*+***************************************************************************
271  * Memory allocation/deallocation function *
272  *****************************************************************************/
273 
274 
284  osl_strings_p strings;
285 
286  OSL_malloc(strings, osl_strings_p, sizeof(osl_strings_t));
287  OSL_malloc(strings->string, char**, sizeof(char*));
288  strings->string[0] = NULL;
289 
290  return strings;
291 }
292 
293 
300  int i;
301 
302  if (strings != NULL) {
303  if (strings->string != NULL) {
304  i = 0;
305  while (strings->string[i] != NULL) {
306  free(strings->string[i]);
307  i++;
308  }
309  free(strings->string);
310  }
311  free(strings);
312  }
313 }
314 
315 
316 /*+***************************************************************************
317  * Processing functions *
318  *****************************************************************************/
319 
320 
329  int i, nb_strings;
330  osl_strings_p clone = NULL;
331 
332  if (strings == NULL)
333  return NULL;
334 
335  clone = osl_strings_malloc();
336  if ((nb_strings = osl_strings_size(strings)) == 0)
337  return clone;
338 
339  free(clone->string);
340  OSL_malloc(clone->string, char **, (nb_strings + 1) * sizeof(char *));
341  clone->string[nb_strings] = NULL;
342  for (i = 0; i < nb_strings; i++)
343  OSL_strdup(clone->string[i], strings->string[i]);
344 
345  return clone;
346 }
347 
355 size_t osl_strings_find(osl_strings_p strings, char const * const string) {
356  size_t i;
357  for (i = 0; i < osl_strings_size(strings); ++i) {
358  if (strcmp(strings->string[i], string) == 0) { return i; }
359  }
360  return i;
361 }
362 
363 
370 void osl_strings_add(osl_strings_p strings, char const * const string) {
371  size_t original_size = osl_strings_size(strings);
372  OSL_realloc(strings->string, char**, sizeof(char*) * (original_size + 1 + 1));
373  strings->string[original_size + 1] = NULL;
374  strings->string[original_size] = malloc(sizeof(char) * (strlen(string) + 1));
375  strcpy(strings->string[original_size], string);
376 }
377 
378 
388  size_t i, nb_s1;
389 
390  if (s1 == s2)
391  return 1;
392 
393  if (((s1 == NULL) && (s2 != NULL)) ||
394  ((s1 != NULL) && (s2 == NULL)) ||
395  ((nb_s1 = osl_strings_size(s1)) != osl_strings_size(s2)))
396  return 0;
397 
398  for (i = 0; i < nb_s1; i++)
399  if (strcmp(s1->string[i], s2->string[i]) != 0)
400  return 0;
401 
402  return 1;
403 }
404 
405 
414  size_t size = 0;
415 
416  if ((strings != NULL) && (strings->string != NULL)) {
417  while (strings->string[size] != NULL) {
418  size++;
419  }
420  }
421 
422  return size;
423 }
424 
425 
434  osl_strings_p capsule = osl_strings_malloc();
435  free(capsule->string);
436  OSL_malloc(capsule->string, char **, 2 * sizeof(char *));
437  capsule->string[0] = string;
438  capsule->string[1] = NULL;
439 
440  return capsule;
441 }
442 
443 
452 
453  OSL_strdup(interface->URI, OSL_URI_STRINGS);
454  interface->idump = (osl_idump_f)osl_strings_idump;
455  interface->sprint = (osl_sprint_f)osl_strings_sprint;
456  interface->sread = (osl_sread_f)osl_strings_sread;
457  interface->malloc = (osl_malloc_f)osl_strings_malloc;
458  interface->free = (osl_free_f)osl_strings_free;
459  interface->clone = (osl_clone_f)osl_strings_clone;
460  interface->equal = (osl_equal_f)osl_strings_equal;
461 
462  return interface;
463 }
464 
465 
475 osl_strings_p osl_strings_generate(char * prefix, int nb_strings) {
476  char ** strings = NULL;
477  char buff[strlen(prefix) + 16]; // TODO: better (log10(INT_MAX) ?) :-D.
478  int i;
479  osl_strings_p generated;
480 
481  if (nb_strings) {
482  OSL_malloc(strings, char **, sizeof(char *) * (nb_strings + 1));
483  strings[nb_strings] = NULL;
484  for (i = 0; i < nb_strings; i++) {
485  sprintf(buff, "%s%d", prefix, i + 1);
486  OSL_strdup(strings[i], buff);
487  if (strings[i] == NULL)
488  OSL_error("memory overflow");
489  }
490  }
491 
492  generated = osl_strings_malloc();
493  free(generated->string);
494  generated->string = strings;
495  return generated;
496 }
osl_strings_p osl_strings_malloc()
Definition: strings.c:283
void *(* osl_clone_f)(void *)
Definition: interface.h:80
osl_strings_p osl_strings_clone(osl_strings_p strings)
Definition: strings.c:328
char ** string
Definition: strings.h:82
size_t osl_strings_find(osl_strings_p strings, char const *const string)
Definition: strings.c:355
osl_interface_p osl_interface_malloc()
Definition: interface.c:212
void *(* osl_sread_f)(char **)
Definition: interface.h:77
osl_strings_p osl_strings_sread(char **input)
Definition: strings.c:194
void osl_strings_idump(FILE *file, osl_strings_p strings, int level)
Definition: strings.c:89
void osl_util_safe_strcat(char **dst, char *src, int *hwm)
Definition: util.c:483
struct osl_strings const *const osl_const_strings_const_p
Definition: strings.h:89
void osl_strings_dump(FILE *file, osl_strings_p strings)
Definition: strings.c:119
osl_strings_p osl_strings_read(FILE *file)
Definition: strings.c:259
size_t osl_strings_size(osl_const_strings_const_p strings)
Definition: strings.c:413
void osl_util_sskip_blank_and_comments(char **str)
Definition: util.c:113
void(* osl_idump_f)(FILE *, void *, int)
Definition: interface.h:75
osl_strings_p osl_strings_encapsulate(char *string)
Definition: strings.c:433
char *(* osl_sprint_f)(void *)
Definition: interface.h:76
char * osl_util_skip_blank_and_comments(FILE *file, char *str)
Definition: util.c:91
osl_interface_p osl_strings_interface()
Definition: strings.c:450
void osl_strings_print(FILE *file, osl_strings_p strings)
Definition: strings.c:166
char * osl_strings_sprint(osl_strings_p strings)
Definition: strings.c:131
void *(* osl_malloc_f)()
Definition: interface.h:78
void osl_strings_free(osl_strings_p strings)
Definition: strings.c:299
int osl_strings_equal(osl_strings_p s1, osl_strings_p s2)
Definition: strings.c:387
int(* osl_equal_f)(void *, void *)
Definition: interface.h:81
void osl_strings_add(osl_strings_p strings, char const *const string)
Definition: strings.c:370
osl_strings_p osl_strings_generate(char *prefix, int nb_strings)
Definition: strings.c:475
void(* osl_free_f)(void *)
Definition: interface.h:79