Rename string and _structure printing functions
[openscop.git] / source / names.c
blobe91985daf001bedd4459868a8d9bc7897c0ba66f
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** extensions/names.c **
6 **-----------------------------------------------------------------**
7 ** First version: 18/04/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 <string.h>
66 # include <openscop/names.h>
69 /*+***************************************************************************
70 * Structure display function *
71 *****************************************************************************/
74 /**
75 * openscop_names_idump function:
76 * this function displays an openscop_names_t structure (*names) into a
77 * file (file, possibly stdout) in a way that trends to be understandable. It
78 * includes an indentation level (level) in order to work with others
79 * print_structure functions.
80 * \param file The file where the information has to be printed.
81 * \param names The names structure whose information has to be printed.
82 * \param level Number of spaces before printing, for each line.
84 void openscop_names_idump(FILE * file, openscop_names_p names, int level) {
85 int j;
87 // Go to the right level.
88 for (j = 0; j < level; j++)
89 fprintf(file, "|\t");
91 if (names != NULL) {
92 if (names->textual == 1)
93 fprintf(file, "+-- openscop_names_t\n");
94 else
95 fprintf(file, "+-- openscop_names_t (non textual)\n");
97 else {
98 fprintf(file, "+-- NULL names\n");
101 if ((names != NULL) && (names->textual == 1)) {
102 // A blank line.
103 for (j = 0; j <= level+1; j++)
104 fprintf(file, "|\t");
105 fprintf(file, "\n");
107 // Print the original parameter names.
108 openscop_util_strings_idump(file, names->parameters,
109 names->nb_parameters, level,
110 "Parameter strings");
112 // Print the iterator names.
113 openscop_util_strings_idump(file, names->iterators,
114 names->nb_iterators, level,
115 "Iterator strings");
117 // Print the scattering dimension names.
118 openscop_util_strings_idump(file, names->scattdims,
119 names->nb_scattdims, level,
120 "Scattering dimension strings");
122 // Print the local dimension names.
123 openscop_util_strings_idump(file, names->localdims,
124 names->nb_localdims, level,
125 "Local dimension strings");
127 // Print the array names.
128 openscop_util_strings_idump(file, names->arrays,
129 names->nb_arrays, level,
130 "Array strings");
133 // The last line.
134 for (j = 0; j <= level; j++)
135 fprintf(file, "|\t");
136 fprintf(file, "\n");
141 * openscop_names_dump function:
142 * this function prints the content of an openscop_names_t structure
143 * (*names) into a file (file, possibly stdout).
144 * \param file The file where the information has to be printed.
145 * \param names The names structure whose information has to be printed.
147 void openscop_names_dump(FILE * file, openscop_names_p names) {
148 openscop_names_idump(file, names, 0);
153 * openscop_names_print function:
154 * this function prints the content of an openscop_names_t structure (*names)
155 * into a file (file, possibly stdout) in the OpenScop textual format.
156 * \param file The file where the information has to be printed.
157 * \param names The names structure whose information has to be printed.
159 void openscop_names_print(FILE * file, openscop_names_p names) {
160 int print = ((names != NULL) && (names->textual == 1));
162 openscop_util_strings_print(file,
163 names->parameters, names->nb_parameters,
164 print, "Parameter names");
166 openscop_util_strings_print(file,
167 names->iterators, names->nb_iterators,
168 print, "Iterator names");
170 openscop_util_strings_print(file,
171 names->scattdims, names->nb_scattdims,
172 print, "Scattering dimension names");
176 /*****************************************************************************
177 * Reading function *
178 *****************************************************************************/
182 * openscop_names_read function:
183 * this function reads a names structure from a file (possibly stdin)
184 * complying to the OpenScop textual format and returns a pointer to this
185 * names structure.
186 * \param file The file where the names has to be read.
187 * \return A pointer to the names structure that has been read.
189 openscop_names_p openscop_names_read(FILE * file) {
190 openscop_names_p names = openscop_names_malloc();
192 if (openscop_util_read_int(file, NULL) > 0) {
193 names->parameters = openscop_util_strings_read(file,
194 &(names->nb_parameters));
196 else {
197 names->nb_parameters = 0;
198 names->parameters = NULL;
201 if (openscop_util_read_int(file, NULL) > 0) {
202 names->iterators = openscop_util_strings_read(file,
203 &(names->nb_iterators));
205 else {
206 names->nb_iterators = 0;
207 names->iterators = NULL;
210 if (openscop_util_read_int(file, NULL) > 0) {
211 names->scattdims = openscop_util_strings_read(file,
212 &(names->nb_scattdims));
214 else {
215 names->nb_scattdims = 0;
216 names->scattdims = NULL;
219 return names;
223 /*+***************************************************************************
224 * Memory allocation/deallocation function *
225 *****************************************************************************/
229 * openscop_names_malloc function:
230 * this function allocates the memory space for an openscop_names_t
231 * structure and sets its fields with default values. Then it returns a
232 * pointer to the allocated space.
233 * \return A pointer to an empty names structure with fields set to
234 * default values.
236 openscop_names_p openscop_names_malloc() {
237 openscop_names_p names = (openscop_names_p)malloc(sizeof(openscop_names_t));
239 if (names == NULL) {
240 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
241 exit(1);
244 names->textual = 1;
245 names->nb_parameters = 0;
246 names->nb_iterators = 0;
247 names->nb_scattdims = 0;
248 names->parameters = NULL;
249 names->iterators = NULL;
250 names->scattdims = NULL;
252 names->nb_localdims = 0;
253 names->nb_arrays = 0;
254 names->localdims = NULL;
255 names->arrays = NULL;
257 return names;
262 * openscop_names_free function:
263 * This function frees the allocated memory for an openscop_names_t
264 * structure. If the names are not character strings, it is the
265 * responsibility of the user to free each array of elements (including
266 * the array itself), this function will only free the openscop_names_t shell.
267 * \param names The pointer to the names structure we want to free.
269 void openscop_names_free(openscop_names_p names) {
270 if (names != NULL) {
271 if (names->textual == 1) {
272 openscop_util_strings_free(names->parameters, names->nb_parameters);
273 openscop_util_strings_free(names->iterators, names->nb_iterators);
274 openscop_util_strings_free(names->scattdims, names->nb_scattdims);
276 openscop_util_strings_free(names->localdims, names->nb_localdims);
277 openscop_util_strings_free(names->arrays, names->nb_arrays);
280 free(names);
285 /*+***************************************************************************
286 * Processing functions *
287 *****************************************************************************/
291 * openscop_names_copy function:
292 * this function builds and returns a "hard copy" (not a pointer copy) of an
293 * openscop_names_t data structure provided as parameter.
294 * \param names The pointer to the names structure we want to copy.
295 * \return A pointer to the copy of the names structure provided as parameter.
297 openscop_names_p openscop_names_copy(openscop_names_p names) {
298 openscop_names_p copy = openscop_names_malloc();
300 copy->textual = names->textual;
301 copy->nb_parameters = names->nb_parameters;
302 copy->nb_iterators = names->nb_iterators;
303 copy->nb_scattdims = names->nb_scattdims;
304 copy->parameters = openscop_util_strings_copy(names->parameters,
305 names->nb_parameters);
306 copy->iterators = openscop_util_strings_copy(names->iterators,
307 names->nb_iterators);
308 copy->scattdims = openscop_util_strings_copy(names->scattdims,
309 names->nb_scattdims);
311 copy->nb_localdims = names->nb_localdims;
312 copy->nb_arrays = names->nb_arrays;
313 copy->localdims = openscop_util_strings_copy(names->localdims,
314 names->nb_localdims);
315 copy->arrays = openscop_util_strings_copy(names->arrays,
316 names->nb_arrays);
318 return copy;
323 * openscop_names_equal function:
324 * this function returns true if the two names structures are the same
325 * (content-wise), false otherwise.
326 * \param n1 The first names structure.
327 * \param n2 The second names structure.
328 * \return 1 if n1 and n2 are the same (content-wise), 0 otherwise.
330 int openscop_names_equal(openscop_names_p n1, openscop_names_p n2) {
331 if (n1 == n2)
332 return 1;
334 if (((n1 == NULL) && (n2 != NULL)) || ((n1 != NULL) && (n2 == NULL)))
335 return 0;
337 if (!openscop_util_strings_equal(n1->parameters, n1->nb_parameters,
338 n2->parameters, n1->nb_parameters)) {
339 fprintf(stderr, "[OpenScop] info: parameters are not the same.\n");
340 return 0;
343 if (!openscop_util_strings_equal(n1->iterators, n1->nb_iterators,
344 n2->iterators, n1->nb_iterators)) {
345 fprintf(stderr, "[OpenScop] info: iterators are not the same.\n");
346 return 0;
349 if (!openscop_util_strings_equal(n1->scattdims, n1->nb_scattdims,
350 n2->scattdims, n1->nb_scattdims)) {
351 fprintf(stderr, "[OpenScop] info: scattdims are not the same.\n");
352 return 0;
355 if (!openscop_util_strings_equal(n1->localdims, n1->nb_localdims,
356 n2->localdims, n1->nb_localdims)) {
357 fprintf(stderr, "[OpenScop] info: localdims are not the same.\n");
358 return 0;
361 if (!openscop_util_strings_equal(n1->arrays, n1->nb_arrays,
362 n2->arrays, n1->nb_arrays)) {
363 fprintf(stderr, "[OpenScop] info: arrays are not the same.\n");
364 return 0;
367 return 1;
372 * openscop_names_integrity_check function:
373 * This function checks that an openscop_names_t structure is "well formed".
374 * It returns 0 if the check failed or 1 if no problem has been detected.
375 * \param names The names structure we want to check.
376 * \param min_nb_parameters The minimum acceptable number of parameters.
377 * \param min_nb_iterators The minimum acceptable number of iterators.
378 * \param min_nb_scattdims The minimum acceptable number of scattdims.
379 * \return 0 if the integrity check fails, 1 otherwise.
381 int openscop_names_integrity_check(openscop_names_p names,
382 int min_nb_parameters,
383 int min_nb_iterators,
384 int min_nb_scattdims) {
385 if ((names->nb_parameters > 0) &&
386 (names->nb_parameters < min_nb_parameters)) {
387 fprintf(stderr, "[OpenScop] Warning: not enough parameter names.\n");
388 return 0;
391 if ((names->nb_iterators > 0) &&
392 (names->nb_iterators < min_nb_iterators)) {
393 fprintf(stderr, "[OpenScop] Warning: not enough iterator names.\n");
394 return 0;
397 if ((names->nb_scattdims > 0) &&
398 (names->nb_scattdims < min_nb_scattdims)) {
399 fprintf(stderr, "[OpenScop] Warning: not enough scattering "
400 "dimension names.\n");
401 return 0;
404 return 1;