README file update
[openscop.git] / source / extensions / lines.c
blobeef1763442d2afb88ea9a827bbe28584643a0bed
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 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");
142 exit(1);
144 string[0] = '\0';
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));
160 free(buffer);
163 return string;
167 /*****************************************************************************
168 * Reading function *
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");
187 return NULL;
190 if (strlen(content) > OPENSCOP_MAX_STRING) {
191 fprintf(stderr, "[OpenScop] Error: lines too long.\n");
192 exit(1);
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");
199 exit(1);
201 tmp = strtok(NULL," -");
202 lines->end = atoi(tmp);
203 if(lines->end == -1) {
204 fprintf(stderr, "[OpenScop] Error: lines end NaN.\n");
205 exit(1);
207 return lines;
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
222 * default values.
224 openscop_lines_p openscop_lines_malloc() {
225 openscop_lines_p lines = (openscop_lines_p)malloc(sizeof(openscop_lines_t));
227 if (lines == NULL) {
228 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
229 exit(1);
232 lines->start = OPENSCOP_UNDEFINED;
233 lines->end = OPENSCOP_UNDEFINED;
235 return lines;
240 * openscop_lines_free function:
241 * This function frees the allocated memory for an openscop_lines_t
242 * structure.
243 * \param lines The pointer to the lines structure we want to free.
245 void openscop_lines_free(openscop_lines_p lines) {
246 if (lines != NULL) {
247 free(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;
267 if (lines == NULL)
268 return NULL;
270 copy = openscop_lines_malloc();
271 if (copy == NULL) {
272 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
273 exit(1);
275 copy->start = lines->start;
276 copy->end = lines->end;
278 return copy;
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) {
291 if (c1 == c2)
292 return 1;
294 if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
295 return 0;
297 if ((c1->start != c2->start) || (c1->end != c2->end))
298 return 0;
300 return 1;