Merge branch 'master' of github.com:periscop/openscop
[openscop.git] / source / extensions / comment.c
blobba465bd3eeaa227eb36668c6801dd9dfde81bee7
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>
67 #include <osl/macros.h>
68 #include <osl/util.h>
69 #include <osl/interface.h>
70 #include <osl/extensions/comment.h>
73 /*+***************************************************************************
74 * Structure display function *
75 *****************************************************************************/
78 /**
79 * osl_comment_idump function:
80 * this function displays an osl_comment_t structure (*comment) 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[in] file The file where the information has to be printed.
85 * \param[in] comment The comment structure to print.
86 * \param[in] level Number of spaces before printing, for each line.
88 void osl_comment_idump(FILE * file, osl_comment_p comment, int level) {
89 int j;
90 size_t l;
91 char * tmp;
93 // Go to the right level.
94 for (j = 0; j < level; j++)
95 fprintf(file, "|\t");
97 if (comment != NULL)
98 fprintf(file, "+-- osl_comment_t\n");
99 else
100 fprintf(file, "+-- NULL comment\n");
102 if (comment != NULL) {
103 // Go to the right level.
104 for(j = 0; j <= level; j++)
105 fprintf(file, "|\t");
107 // Display the comment message (without any carriage return).
108 OSL_strdup(tmp, comment->comment);
109 for (l = 0; l < strlen(tmp); l++)
110 if (tmp[l] == '\n')
111 tmp[l] = ' ';
112 fprintf(file, "comment: %s\n", tmp);
113 free(tmp);
116 // The last line.
117 for (j = 0; j <= level; j++)
118 fprintf(file, "|\t");
119 fprintf(file, "\n");
124 * osl_comment_dump function:
125 * this function prints the content of an osl_comment_t structure
126 * (*comment) into a file (file, possibly stdout).
127 * \param[in] file The file where the information has to be printed.
128 * \param[in] comment The comment structure to print.
130 void osl_comment_dump(FILE * file, osl_comment_p comment) {
131 osl_comment_idump(file, comment, 0);
136 * osl_comment_sprint function:
137 * this function prints the content of an osl_comment_t structure
138 * (*comment) into a string (returned) in the OpenScop textual format.
139 * \param[in] comment The comment structure to print.
140 * \return A string containing the OpenScop dump of the comment structure.
142 char * osl_comment_sprint(osl_comment_p comment) {
143 int high_water_mark = OSL_MAX_STRING;
144 char * string = NULL;
145 char buffer[OSL_MAX_STRING];
147 if (comment != NULL) {
148 OSL_malloc(string, char *, high_water_mark * sizeof(char));
149 string[0] = '\0';
151 // Print the comment.
152 sprintf(buffer, "%s", comment->comment);
153 osl_util_safe_strcat(&string, buffer, &high_water_mark);
155 // Keep only the memory space we need.
156 OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
159 return string;
163 /*****************************************************************************
164 * Reading function *
165 *****************************************************************************/
169 * osl_comment_sread function:
170 * this function reads a comment structure from a string complying to the
171 * OpenScop textual format and returns a pointer to this comment structure.
172 * The input parameter is updated to the position in the input string this
173 * function reach right after reading the comment structure.
174 * \param[in,out] input The input string where to find a comment.
175 * Updated to the position after what has been read.
176 * \return A pointer to the comment structure that has been read.
178 osl_comment_p osl_comment_sread(char ** input) {
179 osl_comment_p comment;
181 if (*input == NULL) {
182 OSL_debug("no comment optional tag");
183 return NULL;
186 if (strlen(*input) > OSL_MAX_STRING)
187 OSL_error("comment too long");
189 // Build the comment structure
190 comment = osl_comment_malloc();
191 OSL_strdup(comment->comment, *input);
193 // Update the input pointer (everything has been read).
194 input += strlen(*input);
196 return comment;
200 /*+***************************************************************************
201 * Memory allocation/deallocation function *
202 *****************************************************************************/
206 * osl_comment_malloc function:
207 * this function allocates the memory space for an osl_comment_t
208 * structure and sets its fields with default values. Then it returns a
209 * pointer to the allocated space.
210 * \return A pointer to an empty comment structure with fields set to
211 * default values.
213 osl_comment_p osl_comment_malloc() {
214 osl_comment_p comment;
216 OSL_malloc(comment, osl_comment_p, sizeof(osl_comment_t));
217 comment->comment = NULL;
219 return comment;
224 * osl_comment_free function:
225 * this function frees the allocated memory for an osl_comment_t
226 * structure.
227 * \param[in,out] comment The pointer to the comment structure to free.
229 void osl_comment_free(osl_comment_p comment) {
230 if (comment != NULL) {
231 if(comment->comment != NULL)
232 free(comment->comment);
233 free(comment);
238 /*+***************************************************************************
239 * Processing functions *
240 *****************************************************************************/
244 * osl_comment_clone function:
245 * this function builds and returns a "hard copy" (not a pointer copy) of an
246 * osl_comment_t data structure.
247 * \param[in] comment The pointer to the comment structure to clone.
248 * \return A pointer to the clone of the comment structure.
250 osl_comment_p osl_comment_clone(osl_comment_p comment) {
251 osl_comment_p clone;
253 if (comment == NULL)
254 return NULL;
256 clone = osl_comment_malloc();
257 OSL_strdup(clone->comment, comment->comment);
259 return clone;
264 * osl_comment_equal function:
265 * this function returns true if the two comment structures are the same
266 * (content-wise), false otherwise.
267 * \param[in] c1 The first comment structure.
268 * \param[in] c2 The second comment structure.
269 * \return 1 if c1 and c2 are the same (content-wise), 0 otherwise.
271 int osl_comment_equal(osl_comment_p c1, osl_comment_p c2) {
272 if (c1 == c2)
273 return 1;
275 if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL))) {
276 OSL_info("comments are not the same");
277 return 0;
280 if (strcmp(c1->comment, c2->comment)) {
281 // Well, the test does not apply well on textual content but the idea
282 // is here (see osl_generic_sprint if you want to understand the problem).
283 //OSL_info("comments are not the same");
284 //return 0;
287 return 1;
292 * osl_comment_interface function:
293 * this function creates an interface structure corresponding to the comment
294 * extension and returns it).
295 * \return An interface structure for the comment extension.
297 osl_interface_p osl_comment_interface() {
298 osl_interface_p interface = osl_interface_malloc();
300 OSL_strdup(interface->URI, OSL_URI_COMMENT);
301 interface->idump = (osl_idump_f)osl_comment_idump;
302 interface->sprint = (osl_sprint_f)osl_comment_sprint;
303 interface->sread = (osl_sread_f)osl_comment_sread;
304 interface->malloc = (osl_malloc_f)osl_comment_malloc;
305 interface->free = (osl_free_f)osl_comment_free;
306 interface->clone = (osl_clone_f)osl_comment_clone;
307 interface->equal = (osl_equal_f)osl_comment_equal;
309 return interface;