Solve a memory leak for cloned scops
[openscop.git] / source / extensions / textual.c
blob132c6233b88490cf235ecfd3ede3b0f9b35c766d
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>
66 # include <openscop/extensions/textual.h>
69 /* CAUTION : TEXTUAL IS A VERY SPECIAL CASE: DO NOT USE IT AS AN EXAMPLE !!! */
72 /*+***************************************************************************
73 * Structure display function *
74 *****************************************************************************/
77 /**
78 * openscop_textual_idump function:
79 * this function displays an openscop_textual_t structure (*textual) into a
80 * file (file, possibly stdout) in a way that trends to be understandable. It
81 * includes an indentation level (level) in order to work with others
82 * print_structure functions.
83 * \param file The file where the information has to be printed.
84 * \param textual The textual structure whose information has to be printed.
85 * \param level Number of spaces before printing, for each line.
87 void openscop_textual_idump(FILE * file, openscop_textual_p textual,
88 int level) {
89 int j;
90 char * tmp;
92 // Go to the right level.
93 for (j = 0; j < level; j++)
94 fprintf(file, "|\t");
96 if (textual != NULL) {
97 fprintf(file, "+-- openscop_textual_t: ");
99 // Display the textual message (without any carriage return).
100 tmp = strdup(textual->textual);
101 for (j = 0; j < strlen(tmp); j++)
102 if (tmp[j] == '\n')
103 tmp[j] = ' ';
105 if (strlen(tmp) > 40) {
106 for (j = 0; j < 20; j++)
107 fprintf(file, "%c", tmp[j]);
108 fprintf(file, " ... ");
109 for (j = strlen(tmp) - 20; j < strlen(tmp); j++)
110 fprintf(file, "%c", tmp[j]);
111 fprintf(file, "\n");
113 else {
114 fprintf(file,"%s\n", tmp);
116 free(tmp);
118 else {
119 fprintf(file, "+-- NULL textual\n");
122 // The last line.
123 for (j = 0; j <= level; j++)
124 fprintf(file, "|\t");
125 fprintf(file, "\n");
130 * openscop_textual_dump function:
131 * this function prints the content of an openscop_textual_t structure
132 * (*textual) into a file (file, possibly stdout).
133 * \param file The file where the information has to be printed.
134 * \param textual The textual structure whose information has to be printed.
136 void openscop_textual_dump(FILE * file, openscop_textual_p textual) {
137 openscop_textual_idump(file, textual, 0);
142 #if 0
144 * openscop_textual_sprint function:
145 * this function prints the content of an openscop_textual_t structure
146 * (*textual) into a string (returned) in the OpenScop textual format.
147 * \param textual The textual structure whose information has to be printed.
148 * \return A string containing the OpenScop dump of the textual structure.
150 char * openscop_textual_sprint(openscop_textual_p textual) {
151 char * string = NULL;
153 if ((textual != NULL) && (textual->textual != NULL)) {
154 if (strlen(textual->textual) > OPENSCOP_MAX_STRING)
155 OPENSCOP_error("textual too long");
157 string = strdup(textual->textual);
158 if (string == NULL)
159 OPENSCOP_error("memory overflow");
162 return string;
164 #else
166 * openscop_textual_sprint function:
167 * this function returns NULL. This is part of the special behavior of
168 * the textual option (printing it along with other options would double
169 * the options...).
170 * \param textual The textual structure whose information has to be printed.
171 * \return NULL.
173 char * openscop_textual_sprint(openscop_textual_p textual) {
175 return NULL;
177 #endif
180 /*****************************************************************************
181 * Reading function *
182 *****************************************************************************/
185 * openscop_textual_sread function:
186 * this function reads a textual structure from a string complying to the
187 * OpenScop textual format and returns a pointer to this textual structure.
188 * The string should contain only one textual format of a textual structure.
189 * \param extensions The input string where to find a textual structure.
190 * \return A pointer to the textual structure that has been read.
192 openscop_textual_p openscop_textual_sread(char * extensions) {
193 openscop_textual_p textual = NULL;
195 if (extensions != NULL) {
196 if (strlen(extensions) > OPENSCOP_MAX_STRING)
197 OPENSCOP_error("textual too long");
199 textual = openscop_textual_malloc();
200 textual->textual = strdup(extensions);
201 if (textual->textual == NULL)
202 OPENSCOP_error("memory overflow");
205 return textual;
209 /*+***************************************************************************
210 * Memory allocation/deallocation function *
211 *****************************************************************************/
215 * openscop_textual_malloc function:
216 * This function allocates the memory space for an openscop_textual_t
217 * structure and sets its fields with default values. Then it returns a
218 * pointer to the allocated space.
219 * \return A pointer to an empty textual structure with fields set to
220 * default values.
222 openscop_textual_p openscop_textual_malloc() {
223 openscop_textual_p textual;
225 OPENSCOP_malloc(textual, openscop_textual_p, sizeof(openscop_textual_t));
226 textual->textual = NULL;
228 return textual;
233 * openscop_textual_free function:
234 * This function frees the allocated memory for an openscop_textual_t
235 * structure.
236 * \param textual The pointer to the textual structure we want to free.
238 void openscop_textual_free(openscop_textual_p textual) {
239 if (textual != NULL) {
240 if(textual->textual != NULL)
241 free(textual->textual);
242 free(textual);
247 /*+***************************************************************************
248 * Processing functions *
249 *****************************************************************************/
253 * openscop_textual_clone function:
254 * This function builds and returns a "hard copy" (not a pointer copy) of an
255 * openscop_textual_t data structure.
256 * \param textual The pointer to the textual structure we want to copy.
257 * \return A pointer to the copy of the textual structure.
259 openscop_textual_p openscop_textual_clone(openscop_textual_p textual) {
260 openscop_textual_p copy;
262 if (textual == NULL)
263 return NULL;
265 copy = openscop_textual_malloc();
266 OPENSCOP_strdup(copy->textual, textual->textual);
268 return copy;
272 #if 0
274 * openscop_textual_equal function:
275 * this function returns true if the two textual structures are the same
276 * (content-wise), false otherwise.
277 * \param f1 The first textual structure.
278 * \param ff The second textual structure.
279 * \return 1 if f1 and f2 are the same (content-wise), 0 otherwise.
281 int openscop_textual_equal(openscop_textual_p f1, openscop_textual_p f2) {
283 if (f1 == f2)
284 return 1;
286 if (((f1 == NULL) && (f2 != NULL)) || ((f1 != NULL) && (f2 == NULL)))
287 return 0;
289 if (strcmp(f1->textual, f2->textual))
290 return 0;
292 return 1;
294 #else
296 * openscop_textual_equal function:
297 * this function returns 1. This is part of the special behavior of
298 * the textual option (the text string can be easily different while the
299 * options are actually identical.
300 * \param f1 The first textual structure.
301 * \param ff The second textual structure.
302 * \return 1.
304 int openscop_textual_equal(openscop_textual_p f1, openscop_textual_p f2) {
306 return 1;
308 #endif
312 * openscop_textual_interface function:
313 * this function creates an interface structure corresponding to the textual
314 * extension and returns it).
315 * \return An interface structure for the textual extension.
317 openscop_interface_p openscop_textual_interface() {
318 openscop_interface_p interface = openscop_interface_malloc();
320 interface->URI = strdup(OPENSCOP_URI_TEXTUAL);
321 interface->idump = (openscop_idump_f)openscop_textual_idump;
322 interface->dump = (openscop_dump_f)openscop_textual_dump;
323 interface->sprint = (openscop_sprint_f)openscop_textual_sprint;
324 interface->sread = (openscop_sread_f)openscop_textual_sread;
325 interface->malloc = (openscop_malloc_f)openscop_textual_malloc;
326 interface->free = (openscop_free_f)openscop_textual_free;
327 interface->clone = (openscop_clone_f)openscop_textual_clone;
328 interface->equal = (openscop_equal_f)openscop_textual_equal;
330 return interface;