Introduce utility macros for messages and allocation
[openscop.git] / source / extensions / lines.c
blob00f7d4bff04dc1b95aecd1f7c3d6aa03bbc5d614
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
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 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
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_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,
85 int level) {
86 int j;
88 // Go to the right level.
89 for (j = 0; j < level; j++)
90 fprintf(file, "|\t");
92 if (lines != NULL)
93 fprintf(file, "+-- openscop_lines_t\n");
94 else
95 fprintf(file, "+-- NULL lines\n");
97 if (lines != NULL) {
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);
106 // The last line.
107 for (j = 0; j <= level; j++)
108 fprintf(file, "|\t");
109 fprintf(file, "\n");
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;
135 char * buffer;
137 if (lines != NULL) {
138 OPENSCOP_malloc(string, char *, high_water_mark * sizeof(char));
139 OPENSCOP_malloc(buffer, char *, OPENSCOP_MAX_STRING * sizeof(char));
140 string[0] = '\0';
142 // Print the begin tag.
143 sprintf(buffer, OPENSCOP_TAG_LINES_START);
144 openscop_util_safe_strcat(&string, buffer, &high_water_mark);
146 // Print the lines content.
147 sprintf(buffer, "\n%d - %d\n", lines->start, lines->end);
148 openscop_util_safe_strcat(&string, buffer, &high_water_mark);
150 // Print the end tag.
151 sprintf(buffer, OPENSCOP_TAG_LINES_STOP"\n");
152 openscop_util_safe_strcat(&string, buffer, &high_water_mark);
154 // Keep only the memory space we need.
155 OPENSCOP_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
156 free(buffer);
159 return string;
163 /*****************************************************************************
164 * Reading function *
165 *****************************************************************************/
168 * openscop_lines_sread function:
169 * this function reads a lines structure from a string complying to the
170 * OpenScop textual format and returns a pointer to this lines structure.
171 * The string should contain only one textual format of a lines structure.
172 * \param extensions The input string where to find a lines structure.
173 * \return A pointer to the lines structure that has been read.
175 openscop_lines_p openscop_lines_sread(char * extensions) {
176 char * content, *tmp;
177 openscop_lines_p lines;
179 content = openscop_util_tag_content(extensions, OPENSCOP_TAG_LINES_START,
180 OPENSCOP_TAG_LINES_STOP);
181 if (content == NULL) {
182 OPENSCOP_info("no lines optional tag");
183 return NULL;
186 if (strlen(content) > OPENSCOP_MAX_STRING)
187 OPENSCOP_error("lines too long");
189 lines = openscop_lines_malloc();
190 tmp = strtok(content," -");
191 lines->start = atoi(tmp);
192 if(lines->start == -1)
193 OPENSCOP_error("lines start NaN");
195 tmp = strtok(NULL," -");
196 lines->end = atoi(tmp);
197 if(lines->end == -1)
198 OPENSCOP_error("lines end NaN");
200 return lines;
204 /*+***************************************************************************
205 * Memory allocation/deallocation function *
206 *****************************************************************************/
210 * openscop_lines_malloc function:
211 * This function allocates the memory space for an openscop_lines_t
212 * structure and sets its fields with default values. Then it returns a
213 * pointer to the allocated space.
214 * \return A pointer to an empty lines structure with fields set to
215 * default values.
217 openscop_lines_p openscop_lines_malloc() {
218 openscop_lines_p lines;
220 OPENSCOP_malloc(lines, openscop_lines_p, sizeof(openscop_lines_t));
221 lines->start = OPENSCOP_UNDEFINED;
222 lines->end = OPENSCOP_UNDEFINED;
224 return lines;
229 * openscop_lines_free function:
230 * This function frees the allocated memory for an openscop_lines_t
231 * structure.
232 * \param lines The pointer to the lines structure we want to free.
234 void openscop_lines_free(openscop_lines_p lines) {
235 if (lines != NULL) {
236 free(lines);
241 /*+***************************************************************************
242 * Processing functions *
243 *****************************************************************************/
247 * openscop_lines_copy function:
248 * This function builds and returns a "hard copy" (not a pointer copy) of an
249 * openscop_lines_t data structure.
250 * \param lines The pointer to the lines structure we want to copy.
251 * \return A pointer to the copy of the lines structure.
253 openscop_lines_p openscop_lines_copy(openscop_lines_p lines) {
254 openscop_lines_p copy;
256 if (lines == NULL)
257 return NULL;
259 copy = openscop_lines_malloc();
260 copy->start = lines->start;
261 copy->end = lines->end;
263 return copy;
268 * openscop_lines_equal function:
269 * this function returns true if the two lines structures are the same
270 * (content-wise), false otherwise. This functions considers two lines
271 * \param c1 The first lines structure.
272 * \param c2 The second lines structure.
273 * \return 1 if c1 and c2 are the same (content-wise), 0 otherwise.
275 int openscop_lines_equal(openscop_lines_p c1, openscop_lines_p c2) {
276 if (c1 == c2)
277 return 1;
279 if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
280 return 0;
282 if ((c1->start != c2->start) || (c1->end != c2->end))
283 return 0;
285 return 1;