Introduce coordinates extension
[openscop.git] / source / extensions / comment.c
blob907d4fab087ca514f9c6ba8a60ea5c0660d92e24
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 char * tmp;
92 // Go to the right level.
93 for (j = 0; j < level; j++)
94 fprintf(file, "|\t");
96 if (comment != NULL)
97 fprintf(file, "+-- osl_comment_t\n");
98 else
99 fprintf(file, "+-- NULL comment\n");
101 if (comment != NULL) {
102 // Go to the right level.
103 for(j = 0; j <= level; j++)
104 fprintf(file, "|\t");
106 // Display the comment message (without any carriage return).
107 tmp = strdup(comment->comment);
108 for (j = 0; j < strlen(tmp); j++)
109 if (tmp[j] == '\n')
110 tmp[j] = ' ';
111 fprintf(file, "comment: %s\n", tmp);
112 free(tmp);
115 // The last line.
116 for (j = 0; j <= level; j++)
117 fprintf(file, "|\t");
118 fprintf(file, "\n");
123 * osl_comment_dump function:
124 * this function prints the content of an osl_comment_t structure
125 * (*comment) into a file (file, possibly stdout).
126 * \param[in] file The file where the information has to be printed.
127 * \param[in] comment The comment structure to print.
129 void osl_comment_dump(FILE * file, osl_comment_p comment) {
130 osl_comment_idump(file, comment, 0);
135 * osl_comment_sprint function:
136 * this function prints the content of an osl_comment_t structure
137 * (*comment) into a string (returned) in the OpenScop textual format.
138 * \param[in] comment The comment structure to print.
139 * \return A string containing the OpenScop dump of the comment structure.
141 char * osl_comment_sprint(osl_comment_p comment) {
142 int high_water_mark = OSL_MAX_STRING;
143 char * string = NULL;
144 char buffer[OSL_MAX_STRING];
146 if (comment != NULL) {
147 OSL_malloc(string, char *, high_water_mark * sizeof(char));
148 string[0] = '\0';
150 // Print the comment.
151 sprintf(buffer, "%s", comment->comment);
152 osl_util_safe_strcat(&string, buffer, &high_water_mark);
154 // Keep only the memory space we need.
155 OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
158 return string;
162 /*****************************************************************************
163 * Reading function *
164 *****************************************************************************/
168 * osl_comment_sread function:
169 * this function reads a comment structure from a string complying to the
170 * OpenScop textual format and returns a pointer to this comment structure.
171 * The input parameter is updated to the position in the input string this
172 * function reach right after reading the comment structure.
173 * \param[in,out] input The input string where to find a comment.
174 * Updated to the position after what has been read.
175 * \return A pointer to the comment structure that has been read.
177 osl_comment_p osl_comment_sread(char ** input) {
178 osl_comment_p comment;
180 if (*input == NULL) {
181 OSL_debug("no comment optional tag");
182 return NULL;
185 if (strlen(*input) > OSL_MAX_STRING)
186 OSL_error("comment too long");
188 // Build the comment structure
189 comment = osl_comment_malloc();
190 OSL_strdup(comment->comment, *input);
192 // Update the input pointer (everything has been read).
193 input += strlen(*input);
195 return comment;
199 /*+***************************************************************************
200 * Memory allocation/deallocation function *
201 *****************************************************************************/
205 * osl_comment_malloc function:
206 * this function allocates the memory space for an osl_comment_t
207 * structure and sets its fields with default values. Then it returns a
208 * pointer to the allocated space.
209 * \return A pointer to an empty comment structure with fields set to
210 * default values.
212 osl_comment_p osl_comment_malloc() {
213 osl_comment_p comment;
215 OSL_malloc(comment, osl_comment_p, sizeof(osl_comment_t));
216 comment->comment = NULL;
218 return comment;
223 * osl_comment_free function:
224 * this function frees the allocated memory for an osl_comment_t
225 * structure.
226 * \param[in,out] comment The pointer to the comment structure to free.
228 void osl_comment_free(osl_comment_p comment) {
229 if (comment != NULL) {
230 if(comment->comment != NULL)
231 free(comment->comment);
232 free(comment);
237 /*+***************************************************************************
238 * Processing functions *
239 *****************************************************************************/
243 * osl_comment_clone function:
244 * this function builds and returns a "hard copy" (not a pointer copy) of an
245 * osl_comment_t data structure.
246 * \param[in] comment The pointer to the comment structure to clone.
247 * \return A pointer to the clone of the comment structure.
249 osl_comment_p osl_comment_clone(osl_comment_p comment) {
250 osl_comment_p clone;
252 if (comment == NULL)
253 return NULL;
255 clone = osl_comment_malloc();
256 OSL_strdup(clone->comment, comment->comment);
258 return clone;
263 * osl_comment_equal function:
264 * this function returns true if the two comment structures are the same
265 * (content-wise), false otherwise.
266 * \param[in] c1 The first comment structure.
267 * \param[in] c2 The second comment structure.
268 * \return 1 if c1 and c2 are the same (content-wise), 0 otherwise.
270 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 return 0;
278 if (strcmp(c1->comment, c2->comment))
279 return 0;
281 return 1;
286 * osl_comment_interface function:
287 * this function creates an interface structure corresponding to the comment
288 * extension and returns it).
289 * \return An interface structure for the comment extension.
291 osl_interface_p osl_comment_interface() {
292 osl_interface_p interface = osl_interface_malloc();
294 interface->URI = strdup(OSL_URI_COMMENT);
295 interface->idump = (osl_idump_f)osl_comment_idump;
296 interface->sprint = (osl_sprint_f)osl_comment_sprint;
297 interface->sread = (osl_sread_f)osl_comment_sread;
298 interface->malloc = (osl_malloc_f)osl_comment_malloc;
299 interface->free = (osl_free_f)osl_comment_free;
300 interface->clone = (osl_clone_f)osl_comment_clone;
301 interface->equal = (osl_equal_f)osl_comment_equal;
303 return interface;