Introduce debug messages
[openscop.git] / source / extensions / comment.c
blob353153c0100ad3cd372b5458f5e38cab914a2ac5
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** extensions/comment.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 <osl/extensions/comment.h>
69 /*+***************************************************************************
70 * Structure display function *
71 *****************************************************************************/
74 /**
75 * osl_comment_idump function:
76 * this function displays an osl_comment_t structure (*comment) 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 comment The comment structure whose information has to be printed.
82 * \param level Number of spaces before printing, for each line.
84 void osl_comment_idump(FILE * file, osl_comment_p comment, int level) {
85 int j;
86 char * tmp;
88 // Go to the right level.
89 for (j = 0; j < level; j++)
90 fprintf(file, "|\t");
92 if (comment != NULL)
93 fprintf(file, "+-- osl_comment_t\n");
94 else
95 fprintf(file, "+-- NULL comment\n");
97 if (comment != NULL) {
98 // Go to the right level.
99 for(j = 0; j <= level; j++)
100 fprintf(file, "|\t");
102 // Display the comment message (without any carriage return).
103 tmp = strdup(comment->comment);
104 for (j = 0; j < strlen(tmp); j++)
105 if (tmp[j] == '\n')
106 tmp[j] = ' ';
107 fprintf(file, "comment: %s\n", tmp);
108 free(tmp);
111 // The last line.
112 for (j = 0; j <= level; j++)
113 fprintf(file, "|\t");
114 fprintf(file, "\n");
119 * osl_comment_dump function:
120 * this function prints the content of an osl_comment_t structure
121 * (*comment) into a file (file, possibly stdout).
122 * \param file The file where the information has to be printed.
123 * \param comment The comment structure whose information has to be printed.
125 void osl_comment_dump(FILE * file, osl_comment_p comment) {
126 osl_comment_idump(file, comment, 0);
131 * osl_comment_sprint function:
132 * this function prints the content of an osl_comment_t structure
133 * (*comment) into a string (returned) in the OpenScop textual format.
134 * \param comment The comment structure whose information has to be printed.
135 * \return A string containing the OpenScop dump of the comment structure.
137 char * osl_comment_sprint(osl_comment_p comment) {
138 int high_water_mark = OSL_MAX_STRING;
139 char * string = NULL;
140 char * buffer;
142 if (comment != NULL) {
143 OSL_malloc(string, char *, high_water_mark * sizeof(char));
144 OSL_malloc(buffer, char *, OSL_MAX_STRING * sizeof(char));
145 string[0] = '\0';
147 // Print the begin tag.
148 sprintf(buffer, OSL_TAG_COMMENT_START);
149 osl_util_safe_strcat(&string, buffer, &high_water_mark);
151 // Print the comment.
152 sprintf(buffer, "\n%s", comment->comment);
153 osl_util_safe_strcat(&string, buffer, &high_water_mark);
155 // Print the end tag.
156 sprintf(buffer, OSL_TAG_COMMENT_STOP"\n");
157 osl_util_safe_strcat(&string, buffer, &high_water_mark);
159 // Keep only the memory space we need.
160 OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
161 free(buffer);
164 return string;
168 /*****************************************************************************
169 * Reading function *
170 *****************************************************************************/
173 * osl_comment_sread function:
174 * this function reads a comment structure from a string complying to the
175 * OpenScop textual format and returns a pointer to this comment structure.
176 * The string should contain only one textual format of a comment structure.
177 * \param extensions The input string where to find a comment structure.
178 * \return A pointer to the comment structure that has been read.
180 osl_comment_p osl_comment_sread(char * extensions) {
181 char * content;
182 osl_comment_p comment;
184 content = osl_util_tag_content(extensions, OSL_TAG_COMMENT_START,
185 OSL_TAG_COMMENT_STOP);
186 if (content == NULL) {
187 OSL_debug("no comment optional tag");
188 return NULL;
191 if (strlen(content) > OSL_MAX_STRING)
192 OSL_error("comment too long");
194 comment = osl_comment_malloc();
195 comment->comment = content;
197 return comment;
201 /*+***************************************************************************
202 * Memory allocation/deallocation function *
203 *****************************************************************************/
207 * osl_comment_malloc function:
208 * This function allocates the memory space for an osl_comment_t
209 * structure and sets its fields with default values. Then it returns a
210 * pointer to the allocated space.
211 * \return A pointer to an empty comment structure with fields set to
212 * default values.
214 osl_comment_p osl_comment_malloc() {
215 osl_comment_p comment;
217 OSL_malloc(comment, osl_comment_p, sizeof(osl_comment_t));
218 comment->comment = NULL;
220 return comment;
225 * osl_comment_free function:
226 * This function frees the allocated memory for an osl_comment_t
227 * structure.
228 * \param comment The pointer to the comment structure we want to free.
230 void osl_comment_free(osl_comment_p comment) {
231 if (comment != NULL) {
232 if(comment->comment != NULL)
233 free(comment->comment);
234 free(comment);
239 /*+***************************************************************************
240 * Processing functions *
241 *****************************************************************************/
245 * osl_comment_clone function:
246 * This function builds and returns a "hard copy" (not a pointer copy) of an
247 * osl_comment_t data structure.
248 * \param comment The pointer to the comment structure we want to copy.
249 * \return A pointer to the copy of the comment structure.
251 osl_comment_p osl_comment_clone(osl_comment_p comment) {
252 osl_comment_p copy;
254 if (comment == NULL)
255 return NULL;
257 copy = osl_comment_malloc();
258 copy->comment = strdup(comment->comment);
259 if ((copy->comment == NULL) && (comment->comment != NULL))
260 OSL_error("memory overflow");
262 return copy;
267 * osl_comment_equal function:
268 * this function returns true if the two comment structures are the same
269 * (content-wise), false otherwise.
270 * \param c1 The first comment structure.
271 * \param c2 The second comment structure.
272 * \return 1 if c1 and c2 are the same (content-wise), 0 otherwise.
274 int osl_comment_equal(osl_comment_p c1, osl_comment_p c2) {
276 if (c1 == c2)
277 return 1;
279 if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
280 return 0;
282 if (strcmp(c1->comment, c2->comment))
283 return 0;
285 return 1;
290 * osl_comment_interface function:
291 * this function creates an interface structure corresponding to the comment
292 * extension and returns it).
293 * \return An interface structure for the comment extension.
295 osl_interface_p osl_comment_interface() {
296 osl_interface_p interface = osl_interface_malloc();
298 interface->URI = strdup(OSL_URI_COMMENT);
299 interface->idump = (osl_idump_f)osl_comment_idump;
300 interface->sprint = (osl_sprint_f)osl_comment_sprint;
301 interface->sread = (osl_sread_f)osl_comment_sread;
302 interface->malloc = (osl_malloc_f)osl_comment_malloc;
303 interface->free = (osl_free_f)osl_comment_free;
304 interface->clone = (osl_clone_f)osl_comment_clone;
305 interface->equal = (osl_equal_f)osl_comment_equal;
307 return interface;