2 /*+-----------------------------------------------------------------**
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 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
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 *
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. *
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. *
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> *
61 *****************************************************************************/
67 #include <osl/macros.h>
69 #include <osl/interface.h>
70 #include <osl/extensions/comment.h>
73 /*+***************************************************************************
74 * Structure display function *
75 *****************************************************************************/
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
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
) {
93 // Go to the right level.
94 for (j
= 0; j
< level
; j
++)
98 fprintf(file
, "+-- osl_comment_t\n");
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
++)
112 fprintf(file
, "comment: %s\n", tmp
);
117 for (j
= 0; j
<= level
; j
++)
118 fprintf(file
, "|\t");
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));
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));
163 /*****************************************************************************
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");
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
);
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
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
;
224 * osl_comment_free function:
225 * this function frees the allocated memory for an osl_comment_t
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
);
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
) {
256 clone
= osl_comment_malloc();
257 OSL_strdup(clone
->comment
, comment
->comment
);
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
) {
275 if (((c1
== NULL
) && (c2
!= NULL
)) || ((c1
!= NULL
) && (c2
== NULL
))) {
276 OSL_info("comments are not the same");
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");
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
;