Add overflow detection
[openscop.git] / source / extensions / coordinates.c
blob79173a0ff9e3176bb0a61a827c786f9b69ec19e8
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** extensions/coordinates.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>
67 #include <osl/macros.h>
68 #include <osl/util.h>
69 #include <osl/interface.h>
70 #include <osl/extensions/coordinates.h>
73 /*+***************************************************************************
74 * Structure display function *
75 *****************************************************************************/
78 /**
79 * osl_coordinates_idump function:
80 * this function displays an osl_coordinates_t structure (*coordinates) into a
81 * file (file, possibly stdout) in a way that trends to be understandable. It
82 * includes an indentation level (level) in order to work with others
83 * idump functions.
84 * \param file The file where the information has to be printed.
85 * \param coordinates The coordinates structure to print.
86 * \param level Number of spaces before printing, for each line.
88 void osl_coordinates_idump(FILE* file, osl_coordinates_p coordinates,
89 int level) {
90 int j;
92 // Go to the right level.
93 for (j = 0; j < level; j++)
94 fprintf(file, "|\t");
96 if (coordinates != NULL)
97 fprintf(file, "+-- osl_coordinates_t\n");
98 else
99 fprintf(file, "+-- NULL coordinates\n");
101 if (coordinates != NULL) {
102 // Go to the right level.
103 for(j = 0; j <= level; j++)
104 fprintf(file, "|\t");
106 // Display the file name.
107 if (coordinates->name != NULL)
108 fprintf(file, "File name__: %s\n", coordinates->name);
109 else
110 fprintf(file, "NULL file name\n");
112 // Go to the right level.
113 for(j = 0; j <= level; j++)
114 fprintf(file, "|\t");
116 // Display the lines.
117 fprintf(file, "Coordinates: [%d,%d -> %d,%d]\n",
118 coordinates->line_start, coordinates->column_start,
119 coordinates->line_end, coordinates->column_end);
121 // Go to the right level.
122 for(j = 0; j <= level; j++)
123 fprintf(file, "|\t");
125 // Display the indentation.
126 fprintf(file, "Indentation: %d\n", coordinates->indent);
129 // The last line.
130 for (j = 0; j <= level; j++)
131 fprintf(file, "|\t");
132 fprintf(file, "\n");
137 * osl_coordinates_dump function:
138 * this function prints the content of an osl_coordinates_t structure
139 * (*coordinates) into a file (file, possibly stdout).
140 * \param file The file where the information has to be printed.
141 * \param coordinates The coordinates structure to print.
143 void osl_coordinates_dump(FILE* file, osl_coordinates_p coordinates) {
144 osl_coordinates_idump(file, coordinates, 0);
149 * osl_coordinates_sprint function:
150 * this function prints the content of an osl_coordinates_t structure
151 * (*coordinates) into a string (returned) in the OpenScop textual format.
152 * \param coordinates The coordinates structure to be print.
153 * \return A string containing the OpenScop dump of the coordinates structure.
155 char* osl_coordinates_sprint(osl_coordinates_p coordinates) {
156 int high_water_mark = OSL_MAX_STRING;
157 char* string = NULL;
158 char buffer[OSL_MAX_STRING];
160 if (coordinates != NULL) {
161 OSL_malloc(string, char*, high_water_mark * sizeof(char));
162 string[0] = '\0';
164 // Print the coordinates content.
165 sprintf(buffer, "# File name\n%s\n", coordinates->name);
166 osl_util_safe_strcat(&string, buffer, &high_water_mark);
168 sprintf(buffer, "# Starting line and column\n%d %d\n",
169 coordinates->line_start, coordinates->column_start);
170 osl_util_safe_strcat(&string, buffer, &high_water_mark);
172 sprintf(buffer, "# Ending line and column\n%d %d\n",
173 coordinates->line_end, coordinates->column_end);
174 osl_util_safe_strcat(&string, buffer, &high_water_mark);
176 sprintf(buffer, "# Indentation\n%d\n", coordinates->indent);
177 osl_util_safe_strcat(&string, buffer, &high_water_mark);
179 // Keep only the memory space we need.
180 OSL_realloc(string, char*, (strlen(string) + 1) * sizeof(char));
183 return string;
187 /*****************************************************************************
188 * Reading function *
189 *****************************************************************************/
193 * osl_coordinates_sread function:
194 * this function reads a coordinates structure from a string complying to the
195 * OpenScop textual format and returns a pointer to this structure.
196 * The input parameter is updated to the position in the input string this
197 * function reach right after reading the coordinates structure.
198 * \param[in,out] input The input string where to find coordinates.
199 * Updated to the position after what has been read.
200 * \return A pointer to the coordinates structure that has been read.
202 osl_coordinates_p osl_coordinates_sread(char** input) {
203 osl_coordinates_p coordinates;
205 if (*input == NULL) {
206 OSL_debug("no coordinates optional tag");
207 return NULL;
210 // Build the coordinates structure.
211 coordinates = osl_coordinates_malloc();
213 // Read the file name (and path).
214 coordinates->name = osl_util_read_line(NULL, input);
216 // Read the coordinates.
217 coordinates->line_start = osl_util_read_int(NULL, input);
218 coordinates->column_start = osl_util_read_int(NULL, input);
219 coordinates->line_end = osl_util_read_int(NULL, input);
220 coordinates->column_end = osl_util_read_int(NULL, input);
222 // Read the indentation level.
223 coordinates->indent = osl_util_read_int(NULL, input);
225 return coordinates;
229 /*+***************************************************************************
230 * Memory allocation/deallocation function *
231 *****************************************************************************/
235 * osl_coordinates_malloc function:
236 * this function allocates the memory space for an osl_coordinates_t
237 * structure and sets its fields with default values. Then it returns a
238 * pointer to the allocated space.
239 * \return A pointer to an empty coordinates structure with fields set to
240 * default values.
242 osl_coordinates_p osl_coordinates_malloc() {
243 osl_coordinates_p coordinates;
245 OSL_malloc(coordinates, osl_coordinates_p, sizeof(osl_coordinates_t));
246 coordinates->name = NULL;
247 coordinates->line_start = OSL_UNDEFINED;
248 coordinates->column_start = OSL_UNDEFINED;
249 coordinates->line_end = OSL_UNDEFINED;
250 coordinates->column_end = OSL_UNDEFINED;
251 coordinates->indent = OSL_UNDEFINED;
253 return coordinates;
258 * osl_coordinates_free function:
259 * this function frees the allocated memory for an osl_coordinates_t
260 * structure.
261 * \param coordinates The pointer to the coordinates structure to free.
263 void osl_coordinates_free(osl_coordinates_p coordinates) {
264 if (coordinates != NULL) {
265 free(coordinates->name);
266 free(coordinates);
271 /*+***************************************************************************
272 * Processing functions *
273 *****************************************************************************/
277 * osl_coordinates_clone function:
278 * this function builds and returns a "hard copy" (not a pointer copy) of an
279 * osl_coordinates_t data structure.
280 * \param coordinates The pointer to the coordinates structure to clone.
281 * \return A pointer to the clone of the coordinates structure.
283 osl_coordinates_p osl_coordinates_clone(osl_coordinates_p coordinates) {
284 osl_coordinates_p clone;
286 if (coordinates == NULL)
287 return NULL;
289 clone = osl_coordinates_malloc();
290 OSL_strdup(clone->name, coordinates->name);
291 clone->line_start = coordinates->line_start;
292 clone->column_start = coordinates->column_start;
293 clone->line_end = coordinates->line_end;
294 clone->column_end = coordinates->column_end;
295 clone->indent = coordinates->indent;
297 return clone;
302 * osl_coordinates_equal function:
303 * this function returns true if the two coordinates structures are the same
304 * (content-wise), false otherwise. This functions considers two coordinates
305 * \param c1 The first coordinates structure.
306 * \param c2 The second coordinates structure.
307 * \return 1 if c1 and c2 are the same (content-wise), 0 otherwise.
309 int osl_coordinates_equal(osl_coordinates_p c1, osl_coordinates_p c2) {
310 if (c1 == c2)
311 return 1;
313 if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
314 return 0;
316 if (strcmp(c1->name, c2->name)) {
317 OSL_info("file names are not the same");
318 return 0;
321 if (c1->line_start != c2->line_start) {
322 OSL_info("starting lines are not the same");
323 return 0;
326 if (c1->column_start != c2->column_start) {
327 OSL_info("starting columns are not the same");
328 return 0;
331 if (c1->line_end != c2->line_end) {
332 OSL_info("Ending lines are not the same");
333 return 0;
336 if (c1->column_end != c2->column_end) {
337 OSL_info("Ending columns are not the same");
338 return 0;
341 if (c1->indent != c2->indent) {
342 OSL_info("indentations are not the same");
343 return 0;
346 return 1;
351 * osl_coordinates_interface function:
352 * this function creates an interface structure corresponding to the coordinates
353 * extension and returns it).
354 * \return An interface structure for the coordinates extension.
356 osl_interface_p osl_coordinates_interface() {
357 osl_interface_p interface = osl_interface_malloc();
359 OSL_strdup(interface->URI, OSL_URI_COORDINATES);
360 interface->idump = (osl_idump_f)osl_coordinates_idump;
361 interface->sprint = (osl_sprint_f)osl_coordinates_sprint;
362 interface->sread = (osl_sread_f)osl_coordinates_sread;
363 interface->malloc = (osl_malloc_f)osl_coordinates_malloc;
364 interface->free = (osl_free_f)osl_coordinates_free;
365 interface->clone = (osl_clone_f)osl_coordinates_clone;
366 interface->equal = (osl_equal_f)osl_coordinates_equal;
368 return interface;