OpenScop  0.9.0
coordinates.c
Go to the documentation of this file.
1 
2  /*+-----------------------------------------------------------------**
3  ** OpenScop Library **
4  **-----------------------------------------------------------------**
5  ** extensions/coordinates.c **
6  **-----------------------------------------------------------------**
7  ** First version: 07/12/2010 **
8  **-----------------------------------------------------------------**
9 
10 
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  *****************************************************************************/
62 
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 
67 #include <osl/macros.h>
68 #include <osl/util.h>
69 #include <osl/interface.h>
71 
72 
73 /*+***************************************************************************
74  * Structure display function *
75  *****************************************************************************/
76 
77 
88 void osl_coordinates_idump(FILE* file, osl_coordinates_p coordinates,
89  int level) {
90  int j;
91 
92  // Go to the right level.
93  for (j = 0; j < level; j++)
94  fprintf(file, "|\t");
95 
96  if (coordinates != NULL)
97  fprintf(file, "+-- osl_coordinates_t\n");
98  else
99  fprintf(file, "+-- NULL coordinates\n");
100 
101  if (coordinates != NULL) {
102  // Go to the right level.
103  for(j = 0; j <= level; j++)
104  fprintf(file, "|\t");
105 
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");
111 
112  // Go to the right level.
113  for(j = 0; j <= level; j++)
114  fprintf(file, "|\t");
115 
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);
120 
121  // Go to the right level.
122  for(j = 0; j <= level; j++)
123  fprintf(file, "|\t");
124 
125  // Display the indentation.
126  fprintf(file, "Indentation: %d\n", coordinates->indent);
127  }
128 
129  // The last line.
130  for (j = 0; j <= level; j++)
131  fprintf(file, "|\t");
132  fprintf(file, "\n");
133 }
134 
135 
143 void osl_coordinates_dump(FILE* file, osl_coordinates_p coordinates) {
144  osl_coordinates_idump(file, coordinates, 0);
145 }
146 
147 
156  int high_water_mark = OSL_MAX_STRING;
157  char* string = NULL;
158  char buffer[OSL_MAX_STRING];
159 
160  if (coordinates != NULL) {
161  OSL_malloc(string, char*, high_water_mark * sizeof(char));
162  string[0] = '\0';
163 
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);
167 
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);
171 
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);
175 
176  sprintf(buffer, "# Indentation\n%d\n", coordinates->indent);
177  osl_util_safe_strcat(&string, buffer, &high_water_mark);
178 
179  // Keep only the memory space we need.
180  OSL_realloc(string, char*, (strlen(string) + 1) * sizeof(char));
181  }
182 
183  return string;
184 }
185 
186 
187 /*****************************************************************************
188  * Reading function *
189  *****************************************************************************/
190 
191 
203  osl_coordinates_p coordinates;
204 
205  if (*input == NULL) {
206  OSL_debug("no coordinates optional tag");
207  return NULL;
208  }
209 
210  // Build the coordinates structure.
211  coordinates = osl_coordinates_malloc();
212 
213  // Read the file name (and path).
214  coordinates->name = osl_util_read_line(NULL, input);
215 
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);
221 
222  // Read the indentation level.
223  coordinates->indent = osl_util_read_int(NULL, input);
224 
225  return coordinates;
226 }
227 
228 
229 /*+***************************************************************************
230  * Memory allocation/deallocation function *
231  *****************************************************************************/
232 
233 
243  osl_coordinates_p coordinates;
244 
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;
252 
253  return coordinates;
254 }
255 
256 
264  if (coordinates != NULL) {
265  free(coordinates->name);
266  free(coordinates);
267  }
268 }
269 
270 
271 /*+***************************************************************************
272  * Processing functions *
273  *****************************************************************************/
274 
275 
284  osl_coordinates_p clone;
285 
286  if (coordinates == NULL)
287  return NULL;
288 
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;
296 
297  return clone;
298 }
299 
300 
310  if (c1 == c2)
311  return 1;
312 
313  if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
314  return 0;
315 
316  if (strcmp(c1->name, c2->name)) {
317  OSL_info("file names are not the same");
318  return 0;
319  }
320 
321  if (c1->line_start != c2->line_start) {
322  OSL_info("starting lines are not the same");
323  return 0;
324  }
325 
326  if (c1->column_start != c2->column_start) {
327  OSL_info("starting columns are not the same");
328  return 0;
329  }
330 
331  if (c1->line_end != c2->line_end) {
332  OSL_info("Ending lines are not the same");
333  return 0;
334  }
335 
336  if (c1->column_end != c2->column_end) {
337  OSL_info("Ending columns are not the same");
338  return 0;
339  }
340 
341  if (c1->indent != c2->indent) {
342  OSL_info("indentations are not the same");
343  return 0;
344  }
345 
346  return 1;
347 }
348 
349 
358 
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;
367 
368  return interface;
369 }
osl_coordinates_p osl_coordinates_sread(char **input)
Definition: coordinates.c:202
void *(* osl_clone_f)(void *)
Definition: interface.h:80
void osl_coordinates_dump(FILE *file, osl_coordinates_p coordinates)
Definition: coordinates.c:143
osl_coordinates_p osl_coordinates_malloc()
Definition: coordinates.c:242
int osl_coordinates_equal(osl_coordinates_p c1, osl_coordinates_p c2)
Definition: coordinates.c:309
osl_interface_p osl_interface_malloc()
Definition: interface.c:212
void osl_coordinates_free(osl_coordinates_p coordinates)
Definition: coordinates.c:263
osl_coordinates_p osl_coordinates_clone(osl_coordinates_p coordinates)
Definition: coordinates.c:283
void *(* osl_sread_f)(char **)
Definition: interface.h:77
void osl_util_safe_strcat(char **dst, char *src, int *hwm)
Definition: util.c:483
void(* osl_idump_f)(FILE *, void *, int)
Definition: interface.h:75
int osl_util_read_int(FILE *file, char **str)
Definition: util.c:140
char *(* osl_sprint_f)(void *)
Definition: interface.h:76
void *(* osl_malloc_f)()
Definition: interface.h:78
int(* osl_equal_f)(void *, void *)
Definition: interface.h:81
osl_interface_p osl_coordinates_interface()
Definition: coordinates.c:356
char * osl_util_read_line(FILE *file, char **str)
Definition: util.c:226
char * osl_coordinates_sprint(osl_coordinates_p coordinates)
Definition: coordinates.c:155
void(* osl_free_f)(void *)
Definition: interface.h:79
void osl_coordinates_idump(FILE *file, osl_coordinates_p coordinates, int level)
Definition: coordinates.c:88