Rename openscop_extension_id to more generic openscop_interface
[openscop.git] / source / extensions / comment.c
blob0c68addd843f62ddf6a62ed95c5b1069f1c15503
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 <openscop/extensions/comment.h>
69 /*+***************************************************************************
70 * Structure display function *
71 *****************************************************************************/
74 /**
75 * openscop_comment_idump function:
76 * this function displays an openscop_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 openscop_comment_idump(FILE * file, openscop_comment_p comment,
85 int level) {
86 int j;
87 char * tmp;
89 // Go to the right level.
90 for (j = 0; j < level; j++)
91 fprintf(file, "|\t");
93 if (comment != NULL)
94 fprintf(file, "+-- openscop_comment_t\n");
95 else
96 fprintf(file, "+-- NULL comment\n");
98 if (comment != NULL) {
99 // Go to the right level.
100 for(j = 0; j <= level; j++)
101 fprintf(file, "|\t");
103 // Display the comment message (without any carriage return).
104 tmp = strdup(comment->comment);
105 for (j = 0; j < strlen(tmp); j++)
106 if (tmp[j] == '\n')
107 tmp[j] = ' ';
108 fprintf(file, "comment: %s\n", tmp);
109 free(tmp);
112 // The last line.
113 for (j = 0; j <= level; j++)
114 fprintf(file, "|\t");
115 fprintf(file, "\n");
120 * openscop_comment_dump function:
121 * this function prints the content of an openscop_comment_t structure
122 * (*comment) into a file (file, possibly stdout).
123 * \param file The file where the information has to be printed.
124 * \param comment The comment structure whose information has to be printed.
126 void openscop_comment_dump(FILE * file, openscop_comment_p comment) {
127 openscop_comment_idump(file, comment, 0);
132 * openscop_comment_sprint function:
133 * this function prints the content of an openscop_comment_t structure
134 * (*comment) into a string (returned) in the OpenScop textual format.
135 * \param comment The comment structure whose information has to be printed.
136 * \return A string containing the OpenScop dump of the comment structure.
138 char * openscop_comment_sprint(openscop_comment_p comment) {
139 int high_water_mark = OPENSCOP_MAX_STRING;
140 char * string = NULL;
141 char * buffer;
143 if (comment != NULL) {
144 OPENSCOP_malloc(string, char *, high_water_mark * sizeof(char));
145 OPENSCOP_malloc(buffer, char *, OPENSCOP_MAX_STRING * sizeof(char));
146 string[0] = '\0';
148 // Print the begin tag.
149 sprintf(buffer, OPENSCOP_TAG_COMMENT_START);
150 openscop_util_safe_strcat(&string, buffer, &high_water_mark);
152 // Print the comment.
153 sprintf(buffer, "\n%s", comment->comment);
154 openscop_util_safe_strcat(&string, buffer, &high_water_mark);
156 // Print the end tag.
157 sprintf(buffer, OPENSCOP_TAG_COMMENT_STOP"\n");
158 openscop_util_safe_strcat(&string, buffer, &high_water_mark);
160 // Keep only the memory space we need.
161 OPENSCOP_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
162 free(buffer);
165 return string;
169 /*****************************************************************************
170 * Reading function *
171 *****************************************************************************/
174 * openscop_comment_sread function:
175 * this function reads a comment structure from a string complying to the
176 * OpenScop textual format and returns a pointer to this comment structure.
177 * The string should contain only one textual format of a comment structure.
178 * \param extensions The input string where to find a comment structure.
179 * \return A pointer to the comment structure that has been read.
181 openscop_comment_p openscop_comment_sread(char * extensions) {
182 char * content;
183 openscop_comment_p comment;
185 content = openscop_util_tag_content(extensions, OPENSCOP_TAG_COMMENT_START,
186 OPENSCOP_TAG_COMMENT_STOP);
187 if (content == NULL) {
188 OPENSCOP_info("no comment optional tag");
189 return NULL;
192 if (strlen(content) > OPENSCOP_MAX_STRING)
193 OPENSCOP_error("comment too long");
195 comment = openscop_comment_malloc();
196 comment->comment = content;
198 return comment;
202 /*+***************************************************************************
203 * Memory allocation/deallocation function *
204 *****************************************************************************/
208 * openscop_comment_malloc function:
209 * This function allocates the memory space for an openscop_comment_t
210 * structure and sets its fields with default values. Then it returns a
211 * pointer to the allocated space.
212 * \return A pointer to an empty comment structure with fields set to
213 * default values.
215 openscop_comment_p openscop_comment_malloc() {
216 openscop_comment_p comment;
218 OPENSCOP_malloc(comment, openscop_comment_p, sizeof(openscop_comment_t));
219 comment->comment = NULL;
221 return comment;
226 * openscop_comment_free function:
227 * This function frees the allocated memory for an openscop_comment_t
228 * structure.
229 * \param comment The pointer to the comment structure we want to free.
231 void openscop_comment_free(openscop_comment_p comment) {
232 if (comment != NULL) {
233 if(comment->comment != NULL)
234 free(comment->comment);
235 free(comment);
240 /*+***************************************************************************
241 * Processing functions *
242 *****************************************************************************/
246 * openscop_comment_clone function:
247 * This function builds and returns a "hard copy" (not a pointer copy) of an
248 * openscop_comment_t data structure.
249 * \param comment The pointer to the comment structure we want to copy.
250 * \return A pointer to the copy of the comment structure.
252 openscop_comment_p openscop_comment_clone(openscop_comment_p comment) {
253 openscop_comment_p copy;
255 if (comment == NULL)
256 return NULL;
258 copy = openscop_comment_malloc();
259 copy->comment = strdup(comment->comment);
260 if ((copy->comment == NULL) && (comment->comment != NULL))
261 OPENSCOP_error("memory overflow");
263 return copy;
268 * openscop_comment_equal function:
269 * this function returns true if the two comment structures are the same
270 * (content-wise), false otherwise.
271 * \param c1 The first comment structure.
272 * \param c2 The second comment structure.
273 * \return 1 if c1 and c2 are the same (content-wise), 0 otherwise.
275 int openscop_comment_equal(openscop_comment_p c1, openscop_comment_p c2) {
277 if (c1 == c2)
278 return 1;
280 if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
281 return 0;
283 if (strcmp(c1->comment, c2->comment))
284 return 0;
286 return 1;
291 * openscop_comment_interface function:
292 * this function creates an interface structure corresponding to the comment
293 * extension and returns it).
294 * \return An interface structure for the comment extension.
296 openscop_interface_p openscop_comment_interface() {
297 openscop_interface_p interface = openscop_interface_malloc();
299 interface->URI = strdup(OPENSCOP_URI_COMMENT);
300 interface->idump = (openscop_idump_f)openscop_comment_idump;
301 interface->dump = (openscop_dump_f)openscop_comment_dump;
302 interface->sprint = (openscop_sprint_f)openscop_comment_sprint;
303 interface->sread = (openscop_sread_f)openscop_comment_sread;
304 interface->malloc = (openscop_malloc_f)openscop_comment_malloc;
305 interface->free = (openscop_free_f)openscop_comment_free;
306 interface->clone = (openscop_clone_f)openscop_comment_clone;
307 interface->equal = (openscop_equal_f)openscop_comment_equal;
309 return interface;