OpenScop 0.8.1

lines.c

Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                     extensions/lines.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/lines.h>
00071 
00072 
00073 /*+***************************************************************************
00074  *                          Structure display function                       *
00075  *****************************************************************************/
00076 
00077 
00088 void osl_lines_idump(FILE * file, osl_lines_p lines, int level) {
00089   int j;
00090 
00091   // Go to the right level.
00092   for (j = 0; j < level; j++)
00093     fprintf(file, "|\t");
00094 
00095   if (lines != NULL)
00096     fprintf(file, "+-- osl_lines_t\n");
00097   else
00098     fprintf(file, "+-- NULL lines\n");
00099 
00100   if (lines != NULL) {
00101     // Go to the right level.
00102     for(j = 0; j <= level; j++)
00103       fprintf(file, "|\t");
00104   
00105     // Display the lines content.
00106     fprintf(file, "lines: %d - %d\n", lines->start,lines->end);
00107   }
00108 
00109   // The last line.
00110   for (j = 0; j <= level; j++)
00111     fprintf(file, "|\t");
00112   fprintf(file, "\n");
00113 }
00114 
00115 
00123 void osl_lines_dump(FILE * file, osl_lines_p lines) {
00124   osl_lines_idump(file, lines, 0);
00125 }
00126 
00127 
00135 char * osl_lines_sprint(osl_lines_p lines) {
00136   int high_water_mark = OSL_MAX_STRING;
00137   char * string = NULL;
00138   char * buffer;
00139 
00140   if (lines != NULL) {
00141     OSL_malloc(string, char *, high_water_mark * sizeof(char));
00142     OSL_malloc(buffer, char *, OSL_MAX_STRING * sizeof(char));
00143     string[0] = '\0';
00144    
00145     // Print the begin tag.
00146     sprintf(buffer, OSL_TAG_LINES_START);
00147     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00148 
00149     // Print the lines content.
00150     sprintf(buffer, "\n%d - %d\n", lines->start, lines->end);
00151     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00152 
00153     // Print the end tag.
00154     sprintf(buffer, OSL_TAG_LINES_STOP"\n");
00155     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00156   
00157     // Keep only the memory space we need.
00158     OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
00159     free(buffer);
00160   }
00161 
00162   return string;
00163 }
00164 
00165 
00166 /*****************************************************************************
00167  *                               Reading function                            *
00168  *****************************************************************************/
00169 
00178 osl_lines_p osl_lines_sread(char ** extensions_fixme) {
00179   char * content, *tmp;
00180   osl_lines_p lines;
00181 
00182   // FIXME: this is a quick and dirty thing to accept char ** instead
00183   //        of char * in the parameter: really do it and update the
00184   //        pointer to after what has been read.
00185   content = *extensions_fixme;
00186 
00187   if (content == NULL) {
00188     OSL_debug("no lines optional tag");
00189     return NULL;
00190   }
00191 
00192   if (strlen(content) > OSL_MAX_STRING) 
00193     OSL_error("lines too long");
00194   
00195   lines = osl_lines_malloc();
00196   tmp = strtok(content," -");
00197   lines->start = atoi(tmp);
00198   if(lines->start == -1)
00199     OSL_error("lines start NaN");
00200   
00201   tmp = strtok(NULL," -");
00202   lines->end = atoi(tmp);
00203   if(lines->end == -1)
00204     OSL_error("lines end NaN");
00205   
00206   return lines;
00207 }
00208 
00209 
00210 /*+***************************************************************************
00211  *                    Memory allocation/deallocation function                *
00212  *****************************************************************************/
00213 
00214 
00223 osl_lines_p osl_lines_malloc() {
00224   osl_lines_p lines;
00225   
00226   OSL_malloc(lines, osl_lines_p, sizeof(osl_lines_t));
00227   lines->start = OSL_UNDEFINED;
00228   lines->end   = OSL_UNDEFINED;
00229 
00230   return lines;
00231 }
00232 
00233 
00240 void osl_lines_free(osl_lines_p lines) {
00241   if (lines != NULL) {
00242     free(lines);
00243   }
00244 }
00245 
00246 
00247 /*+***************************************************************************
00248  *                            Processing functions                           *
00249  *****************************************************************************/
00250 
00251 
00259 osl_lines_p osl_lines_clone(osl_lines_p lines) {
00260   osl_lines_p copy;
00261 
00262   if (lines == NULL)
00263     return NULL;
00264 
00265   copy = osl_lines_malloc();
00266   copy->start = lines->start;
00267   copy->end = lines->end;
00268 
00269   return copy;
00270 }
00271 
00272 
00281 int osl_lines_equal(osl_lines_p c1, osl_lines_p c2) {
00282   if (c1 == c2)
00283     return 1;
00284 
00285   if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
00286     return 0;
00287 
00288   if ((c1->start != c2->start) || (c1->end != c2->end))
00289     return 0;
00290 
00291   return 1;
00292 }
00293 
00294 
00301 osl_interface_p osl_lines_interface() {
00302   osl_interface_p interface = osl_interface_malloc();
00303   
00304   interface->URI    = strdup(OSL_URI_LINES);
00305   interface->idump  = (osl_idump_f)osl_lines_idump;
00306   interface->sprint = (osl_sprint_f)osl_lines_sprint;
00307   interface->sread  = (osl_sread_f)osl_lines_sread;
00308   interface->malloc = (osl_malloc_f)osl_lines_malloc;
00309   interface->free   = (osl_free_f)osl_lines_free;
00310   interface->clone  = (osl_clone_f)osl_lines_clone;
00311   interface->equal  = (osl_equal_f)osl_lines_equal;
00312 
00313   return interface;
00314 }
00315