OpenScop  0.9.0
comment.c
Go to the documentation of this file.
1 
2  /*+-----------------------------------------------------------------**
3  ** OpenScop Library **
4  **-----------------------------------------------------------------**
5  ** extensions/comment.c **
6  **-----------------------------------------------------------------**
7  ** First version: 07/12/2010 **
8  **-----------------------------------------------------------------**
9 
10 
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  *****************************************************************************/
62 
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 
67 #include <osl/macros.h>
68 #include <osl/util.h>
69 #include <osl/interface.h>
70 #include <osl/extensions/comment.h>
71 
72 
73 /*+***************************************************************************
74  * Structure display function *
75  *****************************************************************************/
76 
77 
88 void osl_comment_idump(FILE * file, osl_comment_p comment, int level) {
89  int j;
90  size_t l;
91  char * tmp;
92 
93  // Go to the right level.
94  for (j = 0; j < level; j++)
95  fprintf(file, "|\t");
96 
97  if (comment != NULL)
98  fprintf(file, "+-- osl_comment_t\n");
99  else
100  fprintf(file, "+-- NULL comment\n");
101 
102  if (comment != NULL) {
103  // Go to the right level.
104  for(j = 0; j <= level; j++)
105  fprintf(file, "|\t");
106 
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);
114  }
115 
116  // The last line.
117  for (j = 0; j <= level; j++)
118  fprintf(file, "|\t");
119  fprintf(file, "\n");
120 }
121 
122 
130 void osl_comment_dump(FILE * file, osl_comment_p comment) {
131  osl_comment_idump(file, comment, 0);
132 }
133 
134 
143  int high_water_mark = OSL_MAX_STRING;
144  char * string = NULL;
145  char buffer[OSL_MAX_STRING];
146 
147  if (comment != NULL) {
148  OSL_malloc(string, char *, high_water_mark * sizeof(char));
149  string[0] = '\0';
150 
151  // Print the comment.
152  sprintf(buffer, "%s", comment->comment);
153  osl_util_safe_strcat(&string, buffer, &high_water_mark);
154 
155  // Keep only the memory space we need.
156  OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
157  }
158 
159  return string;
160 }
161 
162 
163 /*****************************************************************************
164  * Reading function *
165  *****************************************************************************/
166 
167 
179  osl_comment_p comment;
180 
181  if (*input == NULL) {
182  OSL_debug("no comment optional tag");
183  return NULL;
184  }
185 
186  if (strlen(*input) > OSL_MAX_STRING)
187  OSL_error("comment too long");
188 
189  // Build the comment structure
190  comment = osl_comment_malloc();
191  OSL_strdup(comment->comment, *input);
192 
193  // Update the input pointer (everything has been read).
194  input += strlen(*input);
195 
196  return comment;
197 }
198 
199 
200 /*+***************************************************************************
201  * Memory allocation/deallocation function *
202  *****************************************************************************/
203 
204 
214  osl_comment_p comment;
215 
216  OSL_malloc(comment, osl_comment_p, sizeof(osl_comment_t));
217  comment->comment = NULL;
218 
219  return comment;
220 }
221 
222 
230  if (comment != NULL) {
231  if(comment->comment != NULL)
232  free(comment->comment);
233  free(comment);
234  }
235 }
236 
237 
238 /*+***************************************************************************
239  * Processing functions *
240  *****************************************************************************/
241 
242 
251  osl_comment_p clone;
252 
253  if (comment == NULL)
254  return NULL;
255 
256  clone = osl_comment_malloc();
257  OSL_strdup(clone->comment, comment->comment);
258 
259  return clone;
260 }
261 
262 
272  if (c1 == c2)
273  return 1;
274 
275  if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL))) {
276  OSL_info("comments are not the same");
277  return 0;
278  }
279 
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;
285  }
286 
287  return 1;
288 }
289 
290 
299 
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;
308 
309  return interface;
310 }
311 
osl_comment_p osl_comment_clone(osl_comment_p comment)
Definition: comment.c:250
osl_interface_p osl_comment_interface()
Definition: comment.c:297
void osl_comment_idump(FILE *file, osl_comment_p comment, int level)
Definition: comment.c:88
osl_comment_p osl_comment_malloc()
Definition: comment.c:213
void osl_comment_dump(FILE *file, osl_comment_p comment)
Definition: comment.c:130
void *(* osl_clone_f)(void *)
Definition: interface.h:80
char * comment
Definition: comment.h:84
osl_comment_p osl_comment_sread(char **input)
Definition: comment.c:178
osl_interface_p osl_interface_malloc()
Definition: interface.c:212
void osl_comment_free(osl_comment_p comment)
Definition: comment.c:229
void *(* osl_sread_f)(char **)
Definition: interface.h:77
void osl_util_safe_strcat(char **dst, char *src, int *hwm)
Definition: util.c:483
char * osl_comment_sprint(osl_comment_p comment)
Definition: comment.c:142
void(* osl_idump_f)(FILE *, void *, int)
Definition: interface.h:75
char *(* osl_sprint_f)(void *)
Definition: interface.h:76
void *(* osl_malloc_f)()
Definition: interface.h:78
int(* osl_equal_f)(void *, void *)
Definition: interface.h:81
int osl_comment_equal(osl_comment_p c1, osl_comment_p c2)
Definition: comment.c:271
void(* osl_free_f)(void *)
Definition: interface.h:79