2 /*+-----------------------------------------------------------------**
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 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
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 *
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. *
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. *
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> *
61 *****************************************************************************/
66 # include <openscop/names.h>
69 /*+***************************************************************************
70 * Structure display function *
71 *****************************************************************************/
75 * openscop_names_print_structure 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_print_structure(FILE * file
, openscop_names_p names
,
88 // Go to the right level.
89 for (j
= 0; j
< level
; j
++)
93 if (names
->textual
== 1)
94 fprintf(file
, "+-- openscop_names_t\n");
96 fprintf(file
, "+-- openscop_names_t (non textual)\n");
99 fprintf(file
, "+-- NULL names\n");
102 if ((names
!= NULL
) && (names
->textual
== 1)) {
104 for (j
= 0; j
<= level
+1; j
++)
105 fprintf(file
, "|\t");
108 // Print the original parameter names.
109 openscop_util_strings_print_structure(file
, names
->parameters
,
110 names
->nb_parameters
, level
,
111 "Parameter strings");
113 // Print the iterator names.
114 openscop_util_strings_print_structure(file
, names
->iterators
,
115 names
->nb_iterators
, level
,
118 // Print the scattering dimension names.
119 openscop_util_strings_print_structure(file
, names
->scattdims
,
120 names
->nb_scattdims
, level
,
121 "Scattering dimension strings");
123 // Print the local dimension names.
124 openscop_util_strings_print_structure(file
, names
->localdims
,
125 names
->nb_localdims
, level
,
126 "Local dimension strings");
128 // Print the array names.
129 openscop_util_strings_print_structure(file
, names
->arrays
,
130 names
->nb_arrays
, level
,
135 for (j
= 0; j
<= level
; j
++)
136 fprintf(file
, "|\t");
142 * openscop_names_print function:
143 * this function prints the content of an openscop_names_t structure
144 * (*names) into a file (file, possibly stdout).
145 * \param file The file where the information has to be printed.
146 * \param names The names structure whose information has to be printed.
148 void openscop_names_print(FILE * file
, openscop_names_p names
) {
149 openscop_names_print_structure(file
, names
, 0);
154 * openscop_names_print_openscop function:
155 * this function prints the content of an openscop_names_t structure (*names)
156 * into a file (file, possibly stdout) in the OpenScop textual format.
157 * \param file The file where the information has to be printed.
158 * \param names The names structure whose information has to be printed.
160 void openscop_names_print_openscop(FILE * file
, openscop_names_p names
) {
161 int print
= ((names
!= NULL
) && (names
->textual
== 1));
163 openscop_util_strings_print_openscop(file
,
164 names
->parameters
, names
->nb_parameters
,
165 print
, "Parameter names");
167 openscop_util_strings_print_openscop(file
,
168 names
->iterators
, names
->nb_iterators
,
169 print
, "Iterator names");
171 openscop_util_strings_print_openscop(file
,
172 names
->scattdims
, names
->nb_scattdims
,
173 print
, "Scattering dimension names");
177 /*****************************************************************************
179 *****************************************************************************/
183 * openscop_names_read function:
184 * this function reads a names structure from a file (possibly stdin)
185 * complying to the OpenScop textual format and returns a pointer to this
187 * \param file The file where the names has to be read.
188 * \return A pointer to the names structure that has been read.
190 openscop_names_p
openscop_names_read(FILE * file
) {
191 openscop_names_p names
= openscop_names_malloc();
193 if (openscop_util_read_int(file
, NULL
) > 0) {
194 names
->parameters
= openscop_util_strings_read(file
,
195 &(names
->nb_parameters
));
198 names
->nb_parameters
= 0;
199 names
->parameters
= NULL
;
202 if (openscop_util_read_int(file
, NULL
) > 0) {
203 names
->iterators
= openscop_util_strings_read(file
,
204 &(names
->nb_iterators
));
207 names
->nb_iterators
= 0;
208 names
->iterators
= NULL
;
211 if (openscop_util_read_int(file
, NULL
) > 0) {
212 names
->scattdims
= openscop_util_strings_read(file
,
213 &(names
->nb_scattdims
));
216 names
->nb_scattdims
= 0;
217 names
->scattdims
= NULL
;
224 /*+***************************************************************************
225 * Memory allocation/deallocation function *
226 *****************************************************************************/
230 * openscop_names_malloc function:
231 * this function allocates the memory space for an openscop_names_t
232 * structure and sets its fields with default values. Then it returns a
233 * pointer to the allocated space.
234 * \return A pointer to an empty names structure with fields set to
237 openscop_names_p
openscop_names_malloc() {
238 openscop_names_p names
= (openscop_names_p
)malloc(sizeof(openscop_names_t
));
241 fprintf(stderr
, "[OpenScop] Error: memory overflow.\n");
246 names
->nb_parameters
= 0;
247 names
->nb_iterators
= 0;
248 names
->nb_scattdims
= 0;
249 names
->parameters
= NULL
;
250 names
->iterators
= NULL
;
251 names
->scattdims
= NULL
;
253 names
->nb_localdims
= 0;
254 names
->nb_arrays
= 0;
255 names
->localdims
= NULL
;
256 names
->arrays
= NULL
;
263 * openscop_names_free function:
264 * This function frees the allocated memory for an openscop_names_t
265 * structure. If the names are not character strings, it is the
266 * responsibility of the user to free each array of elements (including
267 * the array itself), this function will only free the openscop_names_t shell.
268 * \param names The pointer to the names structure we want to free.
270 void openscop_names_free(openscop_names_p names
) {
272 if (names
->textual
== 1) {
273 openscop_util_strings_free(names
->parameters
, names
->nb_parameters
);
274 openscop_util_strings_free(names
->iterators
, names
->nb_iterators
);
275 openscop_util_strings_free(names
->scattdims
, names
->nb_scattdims
);
277 openscop_util_strings_free(names
->localdims
, names
->nb_localdims
);
278 openscop_util_strings_free(names
->arrays
, names
->nb_arrays
);
286 /*+***************************************************************************
287 * Processing functions *
288 *****************************************************************************/
292 * openscop_names_copy function:
293 * this function builds and returns a "hard copy" (not a pointer copy) of an
294 * openscop_names_t data structure provided as parameter.
295 * \param names The pointer to the names structure we want to copy.
296 * \return A pointer to the copy of the names structure provided as parameter.
298 openscop_names_p
openscop_names_copy(openscop_names_p names
) {
299 openscop_names_p copy
= openscop_names_malloc();
301 copy
->textual
= names
->textual
;
302 copy
->nb_parameters
= names
->nb_parameters
;
303 copy
->nb_iterators
= names
->nb_iterators
;
304 copy
->nb_scattdims
= names
->nb_scattdims
;
305 copy
->parameters
= openscop_util_strings_copy(names
->parameters
,
306 names
->nb_parameters
);
307 copy
->iterators
= openscop_util_strings_copy(names
->iterators
,
308 names
->nb_iterators
);
309 copy
->scattdims
= openscop_util_strings_copy(names
->scattdims
,
310 names
->nb_scattdims
);
312 copy
->nb_localdims
= names
->nb_localdims
;
313 copy
->nb_arrays
= names
->nb_arrays
;
314 copy
->localdims
= openscop_util_strings_copy(names
->localdims
,
315 names
->nb_localdims
);
316 copy
->arrays
= openscop_util_strings_copy(names
->arrays
,
324 * openscop_names_equal function:
325 * this function returns true if the two names structures are the same
326 * (content-wise), false otherwise.
327 * \param n1 The first names structure.
328 * \param n2 The second names structure.
329 * \return 1 if n1 and n2 are the same (content-wise), 0 otherwise.
331 int openscop_names_equal(openscop_names_p n1
, openscop_names_p n2
) {
335 if (((n1
== NULL
) && (n2
!= NULL
)) || ((n1
!= NULL
) && (n2
== NULL
)))
338 if (!openscop_util_strings_equal(n1
->parameters
, n1
->nb_parameters
,
339 n2
->parameters
, n1
->nb_parameters
)) {
340 fprintf(stderr
, "[OpenScop] info: parameters are not the same.\n");
344 if (!openscop_util_strings_equal(n1
->iterators
, n1
->nb_iterators
,
345 n2
->iterators
, n1
->nb_iterators
)) {
346 fprintf(stderr
, "[OpenScop] info: iterators are not the same.\n");
350 if (!openscop_util_strings_equal(n1
->scattdims
, n1
->nb_scattdims
,
351 n2
->scattdims
, n1
->nb_scattdims
)) {
352 fprintf(stderr
, "[OpenScop] info: scattdims are not the same.\n");
356 if (!openscop_util_strings_equal(n1
->localdims
, n1
->nb_localdims
,
357 n2
->localdims
, n1
->nb_localdims
)) {
358 fprintf(stderr
, "[OpenScop] info: localdims are not the same.\n");
362 if (!openscop_util_strings_equal(n1
->arrays
, n1
->nb_arrays
,
363 n2
->arrays
, n1
->nb_arrays
)) {
364 fprintf(stderr
, "[OpenScop] info: arrays are not the same.\n");
373 * openscop_names_integrity_check function:
374 * This function checks that an openscop_names_t structure is "well formed".
375 * It returns 0 if the check failed or 1 if no problem has been detected.
376 * \param names The names structure we want to check.
377 * \param min_nb_parameters The minimum acceptable number of parameters.
378 * \param min_nb_iterators The minimum acceptable number of iterators.
379 * \param min_nb_scattdims The minimum acceptable number of scattdims.
380 * \return 0 if the integrity check fails, 1 otherwise.
382 int openscop_names_integrity_check(openscop_names_p names
,
383 int min_nb_parameters
,
384 int min_nb_iterators
,
385 int min_nb_scattdims
) {
386 if ((names
->nb_parameters
> 0) &&
387 (names
->nb_parameters
< min_nb_parameters
)) {
388 fprintf(stderr
, "[OpenScop] Warning: not enough parameter names.\n");
392 if ((names
->nb_iterators
> 0) &&
393 (names
->nb_iterators
< min_nb_iterators
)) {
394 fprintf(stderr
, "[OpenScop] Warning: not enough iterator names.\n");
398 if ((names
->nb_scattdims
> 0) &&
399 (names
->nb_scattdims
< min_nb_scattdims
)) {
400 fprintf(stderr
, "[OpenScop] Warning: not enough scattering "
401 "dimension names.\n");