Switch from matrix to relation data structure
[openscop.git] / source / extension.c
blob1023b365753e9664d2e347b3ce327f962fcf9fea
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** extension.c **
6 **-----------------------------------------------------------------**
7 ** First version: 26/11/2010 **
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 <string.h>
66 # include <openscop/extension.h>
69 /*+***************************************************************************
70 * Structure display function *
71 *****************************************************************************/
74 /**
75 * openscop_extension_print_structure function:
76 * Displays a openscop_extension_t structure (*extensions) into a file (file,
77 * possibly stdout) in a way that trends to be understandable. It includes an
78 * indentation level (level) in order to work with others print_structure
79 * functions.
80 * \param file File where informations are printed.
81 * \param extensions The extensions whose information have to be printed.
82 * \param level Number of spaces before printing, for each line.
84 void
85 openscop_extension_print_structure(FILE * file,
86 openscop_extension_p extension,
87 int level)
89 if (extension == NULL)
90 return;
92 switch (extension->type) {
93 case OPENSCOP_EXTENSION_COMMENT: {
94 openscop_comment_print_structure(file, extension->extension, level);
95 break;
97 case OPENSCOP_EXTENSION_ARRAYS: {
98 openscop_arrays_print_structure(file, extension->extension, level);
99 break;
101 default: {
102 fprintf(stderr, "[OpenScop] Error: unknown extension.\n");
103 exit(1);
109 void openscop_extension_print(FILE * file, openscop_extension_p extension)
114 void openscop_extension_print_openscop(FILE * file,
115 openscop_extension_p extension)
121 /*****************************************************************************
122 * Reading function *
123 *****************************************************************************/
125 char * openscop_extension_read_string(FILE * file)
127 int high_water_mark = OPENSCOP_MAX_STRING;
128 int nb_chars = 0;
129 char buff[OPENSCOP_MAX_STRING], *c;
130 char * extensions;
132 extensions = (char *)malloc(high_water_mark * sizeof(char));
133 if (extensions == NULL)
135 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
136 exit(1);
139 // - Skip blank/commented lines and spaces.
140 c = openscop_util_skip_blank_and_comments(file, buff);
141 strcpy(extensions, c);
142 nb_chars = strlen(c);
144 // - Copy everything else to the option tags field.
145 while (!feof(file))
147 extensions[nb_chars] = fgetc(file);
148 nb_chars++;
150 //printf("EXTENSIONS: %s", extensions);
151 if (nb_chars >= high_water_mark)
153 high_water_mark += high_water_mark;
154 extensions = (char *)realloc(extensions, high_water_mark * sizeof(char));
155 if (extensions == NULL)
157 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
158 exit(1);
163 // - 0-terminate the string.
164 extensions = (char *)realloc(extensions, nb_chars * sizeof(char));
165 extensions[nb_chars - 1] = '\0';
167 return extensions;
171 openscop_extension_p openscop_extension_read(FILE * file)
173 char * extension_string;
174 openscop_extension_p extension = NULL;
176 extension_string = openscop_extension_read_string(file);
178 openscop_extension_add(&extension, extension_string,
179 OPENSCOP_EXTENSION_COMMENT, openscop_comment_read);
180 openscop_extension_add(&extension, extension_string,
181 OPENSCOP_EXTENSION_ARRAYS, openscop_arrays_read);
183 return NULL;
188 /*+***************************************************************************
189 * Memory allocation/deallocation function *
190 *****************************************************************************/
192 void openscop_extension(openscop_extension_p * extension, char * string,
193 int type, (void *)read(char *))
195 void * new_ext;
197 new_ext = (void *)
198 if (*extension == NULL)
206 openscop_extension_p openscop_extension_malloc()
208 openscop_extension_p extension;
210 extension = (openscop_extension_p)malloc(sizeof(openscop_extension_p));
211 extension->type = OPENSCOP_EXTENSION_UNDEFINED;
212 extension->extension = NULL;
213 extension->next = NULL;
215 return extension;
219 void openscop_extension_free(openscop_extension_p extension)
226 /*+***************************************************************************
227 * Processing functions *
228 *****************************************************************************/
231 openscop_extension_p openscop_extension_copy(openscop_extension_p extension)
234 return NULL;
238 int openscop_extension_equal(openscop_extension_p x1, openscop_extension_p x2)
241 return 1;
245 /*+***************************************************************************
246 * Extension-Dependent Functions *
247 *****************************************************************************/
250 * For each extension called FOOEXT that a programmer wishes to add to
251 * OpenScop, the following functions must be provided in a dedicated
252 * section (follow the "Array Extension" example):
253 * - Display function (for internal use)
254 * - Print function (to OpenScop's file format as a string)
255 * - Read function (from OpenScop's file format as a string)
256 * - Malloc function (allocate the extension part in an extension structure)
257 * - Free function (free the extension part in an extension structure)
258 * - Copy function
259 * - Equal function
263 /*+***************************************************************************
264 * Array Extension *
265 *****************************************************************************/
269 char **
270 openscop_extension_read_tag_arrays(char * str, int * nb_arrays)
272 int i, k, nb_names, array_index, max_index = 0;
273 int high_water_mark = OPENSCOP_MAX_ARRAYS;
274 char ** arrays;
275 char ** tmpnames;
276 char * content;
277 char buff[OPENSCOP_MAX_STRING];
279 content = openscop_util_tag_content(str, OPENSCOP_TAG_ARRAY_START,
280 OPENSCOP_TAG_ARRAY_STOP);
282 if (content == NULL)
284 fprintf(stderr, "[OpenScop] Info: no array optional tag.\n");
285 *nb_arrays = 0;
286 return NULL;
289 // Allocate the array of names.
290 arrays = (char **)malloc(high_water_mark * sizeof(char *));
291 for (i = 0; i < high_water_mark; i++)
292 arrays[i] = NULL;
294 // Find the number of names provided.
295 nb_names = openscop_util_read_int(NULL, &content);
297 // Get each array name.
298 for (k = 0; k < nb_names; k++)
300 // Skip blank or commented lines.
301 while (*content == '#' || *content == '\n')
303 for (; *content != '\n'; ++content)
305 ++content;
308 // Get the array name index.
309 for (i = 0; *content && ! isspace(*content); ++i, ++content)
310 buff[i] = *content;
311 buff[i] = '\0';
312 sscanf(buff, "%d", &array_index);
313 max_index = (max_index > array_index) ? max_index : array_index;
314 if (array_index > high_water_mark)
316 fprintf(stderr, "[OpenScop] Info: array name indices sound high.\n");
317 high_water_mark += OPENSCOP_MAX_ARRAYS;
318 arrays = (char **)realloc(arrays, high_water_mark * sizeof(char *));
319 if (arrays == NULL)
321 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
322 exit(1);
324 for (i = high_water_mark - OPENSCOP_MAX_ARRAYS; i < high_water_mark; i++)
325 arrays[i] = NULL;
327 if (array_index <= 0)
329 fprintf(stderr, "[OpenScop] Error: array index must be > 0.\n");
330 exit(1);
333 // Get the array name in buff.
334 while (*content && isspace(*content))
335 ++content;
336 for (i = 0; *content && ! isspace(*content); ++i, ++content)
337 buff[i] = *content;
338 buff[i] = '\0';
340 // Array index is in 0-basis.
341 if (arrays[array_index - 1] != NULL)
343 fprintf(stderr, "[OpenScop] Warning: two array names have the "
344 "same index.\n");
345 free(arrays[array_index - 1]);
347 arrays[array_index - 1] = strdup(buff);
349 // Go to the end of line.
350 while (*content && *content != '\n')
351 ++content;
354 // Free unused memory.
355 arrays = (char **)realloc(arrays, max_index * sizeof(char *));
357 // Fill the missing names (and let's hope there is no need for higher index).
358 tmpnames = openscop_util_generate_names("var", max_index);
359 for (i = 0; i < max_index; i++)
361 if (arrays[i] == NULL || arrays[i][0] == '\0')
362 arrays[i] = tmpnames[i]; // Use a generated name.
363 else
364 free(tmpnames[i]); // Use a read name.
366 free(tmpnames);
369 if (OPENSCOP_DEBUG == 1)
371 printf("max_index: %d\n", max_index);
372 for (i = 0; i < max_index; i++)
373 printf("%s ", arrays[i]);
374 printf("\n");
377 *nb_arrays = max_index;
378 return arrays;