Add overflow detection
[openscop.git] / source / extensions / textual.c
blob288814f51f00916ac28e6120a8cf5434a5a025d7
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** extensions/textual.c **
6 **-----------------------------------------------------------------**
7 ** First version: 15/17/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/textual.h>
73 /* CAUTION : TEXTUAL IS A VERY SPECIAL CASE: DO NOT USE IT AS AN EXAMPLE !!! */
76 /*+***************************************************************************
77 * Structure display function *
78 *****************************************************************************/
81 /**
82 * osl_textual_idump function:
83 * this function displays an osl_textual_t structure (*textual) into a
84 * file (file, possibly stdout) in a way that trends to be understandable. It
85 * includes an indentation level (level) in order to work with others
86 * idump functions.
87 * \param[in] file The file where the information has to be printed.
88 * \param[in] textual The textual structure to be printed.
89 * \param[in] level Number of spaces before printing, for each line.
91 void osl_textual_idump(FILE * file, osl_textual_p textual, int level) {
92 int j;
93 char * tmp;
95 // Go to the right level.
96 for (j = 0; j < level; j++)
97 fprintf(file, "|\t");
99 if (textual != NULL) {
100 fprintf(file, "+-- osl_textual_t: ");
102 // Display the textual message (without any carriage return).
103 OSL_strdup(tmp, textual->textual);
104 for (j = 0; j < (int)strlen(tmp); j++)
105 if (tmp[j] == '\n')
106 tmp[j] = ' ';
108 if (strlen(tmp) > 40) {
109 for (j = 0; j < 20; j++)
110 fprintf(file, "%c", tmp[j]);
111 fprintf(file, " ... ");
112 for (j = (int)strlen(tmp) - 20; j < (int)strlen(tmp); j++)
113 fprintf(file, "%c", tmp[j]);
114 fprintf(file, "\n");
116 else {
117 fprintf(file,"%s\n", tmp);
119 free(tmp);
121 else {
122 fprintf(file, "+-- NULL textual\n");
125 // The last line.
126 for (j = 0; j <= level; j++)
127 fprintf(file, "|\t");
128 fprintf(file, "\n");
133 * osl_textual_dump function:
134 * this function prints the content of an osl_textual_t structure
135 * (*textual) into a file (file, possibly stdout).
136 * \param[in] file The file where the information has to be printed.
137 * \param[in] textual The textual structure to be printed.
139 void osl_textual_dump(FILE * file, osl_textual_p textual) {
140 osl_textual_idump(file, textual, 0);
145 #if 0
147 * osl_textual_sprint function:
148 * this function prints the content of an osl_textual_t structure
149 * (*textual) into a string (returned) in the OpenScop textual format.
150 * \param[in] textual The textual structure to be printed.
151 * \return A string containing the OpenScop dump of the textual structure.
153 char * osl_textual_sprint(osl_textual_p textual) {
154 char * string = NULL;
156 if ((textual != NULL) && (textual->textual != NULL)) {
157 if (strlen(textual->textual) > OSL_MAX_STRING)
158 OSL_error("textual too long");
160 string = strdup(textual->textual);
161 if (string == NULL)
162 OSL_error("memory overflow");
165 return string;
167 #else
169 * osl_textual_sprint function:
170 * this function returns NULL. This is part of the special behavior of
171 * the textual option (printing it along with other options would double
172 * the options...).
173 * \param[in] textual The textual structure to be printed.
174 * \return NULL.
176 char * osl_textual_sprint(osl_textual_p textual) {
178 return NULL;
180 #endif
183 /*****************************************************************************
184 * Reading function *
185 *****************************************************************************/
189 * osl_textual_sread function:
190 * this function reads a textual structure from a string complying to the
191 * OpenScop textual format and returns a pointer to this textual structure.
192 * The string should contain only one textual format of a textual structure.
193 * \param[in,out] extensions The input string where to find a textual struct.
194 * Updated to the position after what has been read.
195 * \return A pointer to the textual structure that has been read.
197 osl_textual_p osl_textual_sread(char ** extensions) {
198 osl_textual_p textual = NULL;
200 if (*extensions != NULL) {
201 textual = osl_textual_malloc();
202 OSL_strdup(textual->textual, *extensions);
204 // Update the input string pointer to the end of the string (since
205 // everything has been read).
206 *extensions = *extensions + strlen(*extensions);
209 return textual;
213 /*+***************************************************************************
214 * Memory allocation/deallocation function *
215 *****************************************************************************/
219 * osl_textual_malloc function:
220 * this function allocates the memory space for an osl_textual_t
221 * structure and sets its fields with default values. Then it returns a
222 * pointer to the allocated space.
223 * \return A pointer to an empty textual structure with fields set to
224 * default values.
226 osl_textual_p osl_textual_malloc() {
227 osl_textual_p textual;
229 OSL_malloc(textual, osl_textual_p, sizeof(osl_textual_t));
230 textual->textual = NULL;
232 return textual;
237 * osl_textual_free function:
238 * this function frees the allocated memory for an osl_textual_t
239 * structure.
240 * \param[in,out] textual The pointer to the textual structure to be freed.
242 void osl_textual_free(osl_textual_p textual) {
243 if (textual != NULL) {
244 if(textual->textual != NULL)
245 free(textual->textual);
246 free(textual);
251 /*+***************************************************************************
252 * Processing functions *
253 *****************************************************************************/
257 * osl_textual_clone function:
258 * this function builds and returns a "hard copy" (not a pointer copy) of an
259 * osl_textual_t data structure.
260 * \param[in] textual The pointer to the textual structure we want to clone.
261 * \return A pointer to the clone of the textual structure.
263 osl_textual_p osl_textual_clone(osl_textual_p textual) {
264 osl_textual_p clone;
266 if (textual == NULL)
267 return NULL;
269 clone = osl_textual_malloc();
270 OSL_strdup(clone->textual, textual->textual);
272 return clone;
276 #if 0
278 * osl_textual_equal function:
279 * this function returns true if the two textual structures are the same
280 * (content-wise), false otherwise.
281 * \param f1 The first textual structure.
282 * \param ff The second textual structure.
283 * \return 1 if f1 and f2 are the same (content-wise), 0 otherwise.
285 int osl_textual_equal(osl_textual_p f1, osl_textual_p f2) {
287 if (f1 == f2)
288 return 1;
290 if (((f1 == NULL) && (f2 != NULL)) || ((f1 != NULL) && (f2 == NULL)))
291 return 0;
293 if (strcmp(f1->textual, f2->textual))
294 return 0;
296 return 1;
298 #else
300 * osl_textual_equal function:
301 * this function returns 1. This is part of the special behavior of
302 * the textual option (the text string can be easily different while the
303 * options are actually identical.
304 * \param[in] f1 The first textual structure.
305 * \param[in] f2 The second textual structure.
306 * \return 1.
308 int osl_textual_equal(osl_textual_p f1, osl_textual_p f2) {
310 return 1;
312 #endif
316 * osl_textual_interface function:
317 * this function creates an interface structure corresponding to the textual
318 * extension and returns it).
319 * \return An interface structure for the textual extension.
321 osl_interface_p osl_textual_interface() {
322 osl_interface_p interface = osl_interface_malloc();
324 OSL_strdup(interface->URI, OSL_URI_TEXTUAL);
325 interface->idump = (osl_idump_f)osl_textual_idump;
326 interface->sprint = (osl_sprint_f)osl_textual_sprint;
327 interface->sread = (osl_sread_f)osl_textual_sread;
328 interface->malloc = (osl_malloc_f)osl_textual_malloc;
329 interface->free = (osl_free_f)osl_textual_free;
330 interface->clone = (osl_clone_f)osl_textual_clone;
331 interface->equal = (osl_equal_f)osl_textual_equal;
333 return interface;