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