OpenScop  0.9.0
comment.c
Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                     extensions/comment.c                        **
00006      **-----------------------------------------------------------------**
00007      **                   First version: 07/12/2010                     **
00008      **-----------------------------------------------------------------**
00009 
00010  
00011  *****************************************************************************
00012  * OpenScop: Structures and formats for polyhedral tools to talk together    *
00013  *****************************************************************************
00014  *    ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__,                *
00015  *    /   / /  //  //  //  // /   / /  //  //   / /  // /  /|,_,             *
00016  *   /   / /  //  //  //  // /   / /  //  //   / /  // /  / / /\             *
00017  *  |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/  \            *
00018  *  | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\  \ /\           *
00019  *  | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\          *
00020  *  | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \  \         *
00021  *  | P |n| l | = | s | t |=| = |d| = | = | = | |   |=| o | | \# \  \        *
00022  *  | H | | y |   | e | o | | = |l|   |   | = | |   | | G | |  \  \  \       *
00023  *  | I | |   |   | e |   | |   | |   |   |   | |   | |   | |   \  \  \      *
00024  *  | T | |   |   |   |   | |   | |   |   |   | |   | |   | |    \  \  \     *
00025  *  | E | |   |   |   |   | |   | |   |   |   | |   | |   | |     \  \  \    *
00026  *  | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | /      \* \  \   *
00027  *  | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/        \  \ /   *
00028  *  '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---'          '--'    *
00029  *                                                                           *
00030  * Copyright (C) 2008 University Paris-Sud 11 and INRIA                      *
00031  *                                                                           *
00032  * (3-clause BSD license)                                                    *
00033  * Redistribution and use in source  and binary forms, with or without       *
00034  * modification, are permitted provided that the following conditions        *
00035  * are met:                                                                  *
00036  *                                                                           *
00037  * 1. Redistributions of source code must retain the above copyright notice, *
00038  *    this list of conditions and the following disclaimer.                  *
00039  * 2. Redistributions in binary form must reproduce the above copyright      *
00040  *    notice, this list of conditions and the following disclaimer in the    *
00041  *    documentation and/or other materials provided with the distribution.   *
00042  * 3. The name of the author may not be used to endorse or promote products  *
00043  *    derived from this software without specific prior written permission.  *
00044  *                                                                           *
00045  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      *
00046  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
00047  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   *
00048  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          *
00049  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  *
00050  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
00051  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     *
00052  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       *
00053  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  *
00054  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         *
00055  *                                                                           *
00056  * OpenScop Library, a library to manipulate OpenScop formats and data       *
00057  * structures. Written by:                                                   *
00058  * Cedric Bastoul     <Cedric.Bastoul@u-psud.fr> and                         *
00059  * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr>                          *
00060  *                                                                           *
00061  *****************************************************************************/
00062 
00063 #include <stdlib.h>
00064 #include <stdio.h>
00065 #include <string.h>
00066 
00067 #include <osl/macros.h>
00068 #include <osl/util.h>
00069 #include <osl/interface.h>
00070 #include <osl/extensions/comment.h>
00071 
00072 
00073 /*+***************************************************************************
00074  *                          Structure display function                       *
00075  *****************************************************************************/
00076 
00077 
00088 void osl_comment_idump(FILE * file, osl_comment_p comment, int level) {
00089   int j;
00090   size_t l;
00091   char * tmp;
00092 
00093   // Go to the right level.
00094   for (j = 0; j < level; j++)
00095     fprintf(file, "|\t");
00096 
00097   if (comment != NULL)
00098     fprintf(file, "+-- osl_comment_t\n");
00099   else
00100     fprintf(file, "+-- NULL comment\n");
00101 
00102   if (comment != NULL) {
00103     // Go to the right level.
00104     for(j = 0; j <= level; j++)
00105       fprintf(file, "|\t");
00106   
00107     // Display the comment message (without any carriage return).
00108     OSL_strdup(tmp, comment->comment);
00109     for (l = 0; l < strlen(tmp); l++)
00110       if (tmp[l] == '\n')
00111         tmp[l] = ' ';
00112     fprintf(file, "comment: %s\n", tmp);
00113     free(tmp);
00114   }
00115 
00116   // The last line.
00117   for (j = 0; j <= level; j++)
00118     fprintf(file, "|\t");
00119   fprintf(file, "\n");
00120 }
00121 
00122 
00130 void osl_comment_dump(FILE * file, osl_comment_p comment) {
00131   osl_comment_idump(file, comment, 0);
00132 }
00133 
00134 
00142 char * osl_comment_sprint(osl_comment_p comment) {
00143   int high_water_mark = OSL_MAX_STRING;
00144   char * string = NULL;
00145   char buffer[OSL_MAX_STRING];
00146 
00147   if (comment != NULL) {
00148     OSL_malloc(string, char *, high_water_mark * sizeof(char));
00149     string[0] = '\0';
00150    
00151     // Print the comment.
00152     sprintf(buffer, "%s", comment->comment);
00153     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00154 
00155     // Keep only the memory space we need.
00156     OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
00157   }
00158 
00159   return string;
00160 }
00161 
00162 
00163 /*****************************************************************************
00164  *                               Reading function                            *
00165  *****************************************************************************/
00166 
00167 
00178 osl_comment_p osl_comment_sread(char ** input) {
00179   osl_comment_p comment;
00180 
00181   if (*input == NULL) {
00182     OSL_debug("no comment optional tag");
00183     return NULL;
00184   }
00185 
00186   if (strlen(*input) > OSL_MAX_STRING) 
00187     OSL_error("comment too long");
00188 
00189   // Build the comment structure
00190   comment = osl_comment_malloc();
00191   OSL_strdup(comment->comment, *input);
00192 
00193   // Update the input pointer (everything has been read).
00194   input += strlen(*input);
00195 
00196   return comment;
00197 }
00198 
00199 
00200 /*+***************************************************************************
00201  *                    Memory allocation/deallocation function                *
00202  *****************************************************************************/
00203 
00204 
00213 osl_comment_p osl_comment_malloc() {
00214   osl_comment_p comment;
00215 
00216   OSL_malloc(comment, osl_comment_p, sizeof(osl_comment_t));
00217   comment->comment = NULL;
00218 
00219   return comment;
00220 }
00221 
00222 
00229 void osl_comment_free(osl_comment_p comment) {
00230   if (comment != NULL) {
00231     if(comment->comment != NULL)
00232       free(comment->comment);
00233     free(comment);
00234   }
00235 }
00236 
00237 
00238 /*+***************************************************************************
00239  *                            Processing functions                           *
00240  *****************************************************************************/
00241 
00242 
00250 osl_comment_p osl_comment_clone(osl_comment_p comment) {
00251   osl_comment_p clone;
00252 
00253   if (comment == NULL)
00254     return NULL;
00255 
00256   clone = osl_comment_malloc();
00257   OSL_strdup(clone->comment, comment->comment);
00258 
00259   return clone;
00260 }
00261 
00262 
00271 int osl_comment_equal(osl_comment_p c1, osl_comment_p c2) {
00272   if (c1 == c2)
00273     return 1;
00274 
00275   if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL))) {
00276     OSL_info("comments are not the same");
00277     return 0;
00278   }
00279 
00280   if (strcmp(c1->comment, c2->comment)) {
00281     // Well, the test does not apply well on textual content but the idea
00282     // is here (see osl_generic_sprint if you want to understand the problem).
00283     //OSL_info("comments are not the same");
00284     //return 0;
00285   }
00286 
00287   return 1;
00288 }
00289 
00290 
00297 osl_interface_p osl_comment_interface() {
00298   osl_interface_p interface = osl_interface_malloc();
00299   
00300   OSL_strdup(interface->URI, OSL_URI_COMMENT);
00301   interface->idump  = (osl_idump_f)osl_comment_idump;
00302   interface->sprint = (osl_sprint_f)osl_comment_sprint;
00303   interface->sread  = (osl_sread_f)osl_comment_sread;
00304   interface->malloc = (osl_malloc_f)osl_comment_malloc;
00305   interface->free   = (osl_free_f)osl_comment_free;
00306   interface->clone  = (osl_clone_f)osl_comment_clone;
00307   interface->equal  = (osl_equal_f)osl_comment_equal;
00308 
00309   return interface;
00310 }
00311