2 /*+-----------------------------------------------------------------**
4 **-----------------------------------------------------------------**
5 ** extensions/lines.c **
6 **-----------------------------------------------------------------**
7 ** First version: 07/12/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 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
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/extension.h>
69 /*+***************************************************************************
70 * Structure display function *
71 *****************************************************************************/
75 * openscop_lines_idump function:
76 * this function displays an openscop_lines_t structure (*lines) 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 lines The lines structure whose information has to be printed.
82 * \param level Number of spaces before printing, for each line.
84 void openscop_lines_idump(FILE * file
, openscop_lines_p lines
,
88 // Go to the right level.
89 for (j
= 0; j
< level
; j
++)
93 fprintf(file
, "+-- openscop_lines_t\n");
95 fprintf(file
, "+-- NULL lines\n");
98 // Go to the right level.
99 for(j
= 0; j
<= level
; j
++)
100 fprintf(file
, "|\t");
102 // Display the lines content.
103 fprintf(file
, "lines: %d - %d\n", lines
->start
,lines
->end
);
107 for (j
= 0; j
<= level
; j
++)
108 fprintf(file
, "|\t");
114 * openscop_lines_dump function:
115 * this function prints the content of an openscop_lines_t structure
116 * (*lines) into a file (file, possibly stdout).
117 * \param file The file where the information has to be printed.
118 * \param lines The lines structure whose information has to be printed.
120 void openscop_lines_dump(FILE * file
, openscop_lines_p lines
) {
121 openscop_lines_idump(file
, lines
, 0);
126 * openscop_lines_sprint function:
127 * this function prints the content of an openscop_lines_t structure
128 * (*lines) into a string (returned) in the OpenScop textual format.
129 * \param lines The lines structure whose information has to be printed.
130 * \return A string containing the OpenScop dump of the lines structure.
132 char * openscop_lines_sprint(openscop_lines_p lines
) {
133 int high_water_mark
= OPENSCOP_MAX_STRING
;
134 char * string
= NULL
;
138 string
= (char *)malloc(high_water_mark
* sizeof(char));
139 buffer
= (char *)malloc(OPENSCOP_MAX_STRING
* sizeof(char));
140 if ((string
== NULL
) || (buffer
== NULL
)) {
141 fprintf(stderr
, "[OpenScop] Error: memory overflow.\n");
146 // Print the begin tag.
147 sprintf(buffer
, OPENSCOP_TAG_LINES_START
);
148 openscop_util_safe_strcat(&string
, buffer
, &high_water_mark
);
150 // Print the lines content.
151 sprintf(buffer
, "\n%d - %d\n", lines
->start
, lines
->end
);
152 openscop_util_safe_strcat(&string
, buffer
, &high_water_mark
);
154 // Print the end tag.
155 sprintf(buffer
, OPENSCOP_TAG_LINES_STOP
"\n");
156 openscop_util_safe_strcat(&string
, buffer
, &high_water_mark
);
158 // Keep only the memory space we need.
159 string
= (char *)realloc(string
, (strlen(string
) + 1) * sizeof(char));
167 /*****************************************************************************
169 *****************************************************************************/
172 * openscop_lines_sread function:
173 * this function reads a lines structure from a string complying to the
174 * OpenScop textual format and returns a pointer to this lines structure.
175 * The string should contain only one textual format of a lines structure.
176 * \param extensions The input string where to find a lines structure.
177 * \return A pointer to the lines structure that has been read.
179 openscop_lines_p
openscop_lines_sread(char * extensions
) {
180 char * content
, *tmp
;
181 openscop_lines_p lines
;
183 content
= openscop_util_tag_content(extensions
, OPENSCOP_TAG_LINES_START
,
184 OPENSCOP_TAG_LINES_STOP
);
185 if (content
== NULL
) {
186 fprintf(stderr
, "[OpenScop] Info: no lines optional tag.\n");
190 if (strlen(content
) > OPENSCOP_MAX_STRING
) {
191 fprintf(stderr
, "[OpenScop] Error: lines too long.\n");
194 lines
= openscop_lines_malloc();
195 tmp
= strtok(content
," -");
196 lines
->start
= atoi(tmp
);
197 if(lines
->start
== -1) {
198 fprintf(stderr
, "[OpenScop] Error: lines start NaN.\n");
201 tmp
= strtok(NULL
," -");
202 lines
->end
= atoi(tmp
);
203 if(lines
->end
== -1) {
204 fprintf(stderr
, "[OpenScop] Error: lines end NaN.\n");
211 /*+***************************************************************************
212 * Memory allocation/deallocation function *
213 *****************************************************************************/
217 * openscop_lines_malloc function:
218 * This function allocates the memory space for an openscop_lines_t
219 * structure and sets its fields with default values. Then it returns a
220 * pointer to the allocated space.
221 * \return A pointer to an empty lines structure with fields set to
224 openscop_lines_p
openscop_lines_malloc() {
225 openscop_lines_p lines
= (openscop_lines_p
)malloc(sizeof(openscop_lines_t
));
228 fprintf(stderr
, "[OpenScop] Error: memory overflow.\n");
232 lines
->start
= OPENSCOP_UNDEFINED
;
233 lines
->end
= OPENSCOP_UNDEFINED
;
240 * openscop_lines_free function:
241 * This function frees the allocated memory for an openscop_lines_t
243 * \param lines The pointer to the lines structure we want to free.
245 void openscop_lines_free(openscop_lines_p lines
) {
252 /*+***************************************************************************
253 * Processing functions *
254 *****************************************************************************/
258 * openscop_lines_copy function:
259 * This function builds and returns a "hard copy" (not a pointer copy) of an
260 * openscop_lines_t data structure.
261 * \param lines The pointer to the lines structure we want to copy.
262 * \return A pointer to the copy of the lines structure.
264 openscop_lines_p
openscop_lines_copy(openscop_lines_p lines
) {
265 openscop_lines_p copy
;
270 copy
= openscop_lines_malloc();
272 fprintf(stderr
, "[OpenScop] Error: memory overflow.\n");
275 copy
->start
= lines
->start
;
276 copy
->end
= lines
->end
;
283 * openscop_lines_equal function:
284 * this function returns true if the two lines structures are the same
285 * (content-wise), false otherwise. This functions considers two lines
286 * \param c1 The first lines structure.
287 * \param c2 The second lines structure.
288 * \return 1 if c1 and c2 are the same (content-wise), 0 otherwise.
290 int openscop_lines_equal(openscop_lines_p c1
, openscop_lines_p c2
) {
294 if (((c1
== NULL
) && (c2
!= NULL
)) || ((c1
!= NULL
) && (c2
== NULL
)))
297 if ((c1
->start
!= c2
->start
) || (c1
->end
!= c2
->end
))