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
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.
87 */
88void osl_comment_idump(FILE * file, osl_comment_p comment, int level) {
89  int j;
90  char * tmp;
91
92  // Go to the right level.
93  for (j = 0; j < level; j++)
94    fprintf(file, "|\t");
95
96  if (comment != NULL)
97    fprintf(file, "+-- osl_comment_t\n");
98  else
99    fprintf(file, "+-- NULL comment\n");
100
101  if (comment != NULL) {
102    // Go to the right level.
103    for(j = 0; j <= level; j++)
104      fprintf(file, "|\t");
105
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);
113  }
114
115  // The last line.
116  for (j = 0; j <= level; j++)
117    fprintf(file, "|\t");
118  fprintf(file, "\n");
119}
120
121
122/**
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.
128 */
129void osl_comment_dump(FILE * file, osl_comment_p comment) {
130  osl_comment_idump(file, comment, 0);
131}
132
133
134/**
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.
140 */
141char * 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];
145
146  if (comment != NULL) {
147    OSL_malloc(string, char *, high_water_mark * sizeof(char));
148    string[0] = '\0';
149
150    // Print the comment.
151    sprintf(buffer, "%s", comment->comment);
152    osl_util_safe_strcat(&string, buffer, &high_water_mark);
153
154    // Keep only the memory space we need.
155    OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
156  }
157
158  return string;
159}
160
161
162/*****************************************************************************
163 *                               Reading function                            *
164 *****************************************************************************/
165
166
167/**
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.
176 */
177osl_comment_p osl_comment_sread(char ** input) {
178  osl_comment_p comment;
179
180  if (*input == NULL) {
181    OSL_debug("no comment optional tag");
182    return NULL;
183  }
184
185  if (strlen(*input) > OSL_MAX_STRING)
186    OSL_error("comment too long");
187
188  // Build the comment structure
189  comment = osl_comment_malloc();
190  OSL_strdup(comment->comment, *input);
191
192  // Update the input pointer (everything has been read).
193  input += strlen(*input);
194
195  return comment;
196}
197
198
199/*+***************************************************************************
200 *                    Memory allocation/deallocation function                *
201 *****************************************************************************/
202
203
204/**
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.
211 */
212osl_comment_p osl_comment_malloc() {
213  osl_comment_p comment;
214
215  OSL_malloc(comment, osl_comment_p, sizeof(osl_comment_t));
216  comment->comment = NULL;
217
218  return comment;
219}
220
221
222/**
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.
227 */
228void osl_comment_free(osl_comment_p comment) {
229  if (comment != NULL) {
230    if(comment->comment != NULL)
231      free(comment->comment);
232    free(comment);
233  }
234}
235
236
237/*+***************************************************************************
238 *                            Processing functions                           *
239 *****************************************************************************/
240
241
242/**
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.
248 */
249osl_comment_p osl_comment_clone(osl_comment_p comment) {
250  osl_comment_p clone;
251
252  if (comment == NULL)
253    return NULL;
254
255  clone = osl_comment_malloc();
256  OSL_strdup(clone->comment, comment->comment);
257
258  return clone;
259}
260
261
262/**
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.
269 */
270int osl_comment_equal(osl_comment_p c1, osl_comment_p c2) {
271
272  if (c1 == c2)
273    return 1;
274
275  if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
276    return 0;
277
278  if (strcmp(c1->comment, c2->comment))
279    return 0;
280
281  return 1;
282}
283
284
285/**
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.
290 */
291osl_interface_p osl_comment_interface() {
292  osl_interface_p interface = osl_interface_malloc();
293
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;
302
303  return interface;
304}
305
306