OpenScop  0.9.0
textual.c
Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                     extensions/textual.c                        **
00006      **-----------------------------------------------------------------**
00007      **                   First version: 15/17/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/textual.h>
00071 
00072 
00073 /* CAUTION : TEXTUAL IS A VERY SPECIAL CASE: DO NOT USE IT AS AN EXAMPLE !!! */
00074 
00075 
00076 /*+***************************************************************************
00077  *                          Structure display function                       *
00078  *****************************************************************************/
00079 
00080 
00091 void osl_textual_idump(FILE * file, osl_textual_p textual, int level) {
00092   int j;
00093   char * tmp;
00094 
00095   // Go to the right level.
00096   for (j = 0; j < level; j++)
00097     fprintf(file, "|\t");
00098 
00099   if (textual != NULL) {
00100     fprintf(file, "+-- osl_textual_t: ");
00101     
00102     // Display the textual message (without any carriage return).
00103     OSL_strdup(tmp, textual->textual);
00104     for (j = 0; j < (int)strlen(tmp); j++)
00105       if (tmp[j] == '\n')
00106         tmp[j] = ' ';
00107 
00108     if (strlen(tmp) > 40) {
00109       for (j = 0; j < 20; j++)
00110         fprintf(file, "%c", tmp[j]);
00111       fprintf(file, "   ...   ");
00112       for (j = (int)strlen(tmp) - 20; j < (int)strlen(tmp); j++)
00113         fprintf(file, "%c", tmp[j]);
00114       fprintf(file, "\n");
00115     }
00116     else {
00117       fprintf(file,"%s\n", tmp);
00118     }
00119     free(tmp);
00120   }
00121   else {
00122     fprintf(file, "+-- NULL textual\n");
00123   }
00124 
00125   // The last line.
00126   for (j = 0; j <= level; j++)
00127     fprintf(file, "|\t");
00128   fprintf(file, "\n");
00129 }
00130 
00131 
00139 void osl_textual_dump(FILE * file, osl_textual_p textual) {
00140   osl_textual_idump(file, textual, 0);
00141 }
00142 
00143 
00144 
00145 #if 0
00146 
00153 char * osl_textual_sprint(osl_textual_p textual) {
00154   char * string = NULL;
00155 
00156   if ((textual != NULL) && (textual->textual != NULL)) {
00157     if (strlen(textual->textual) > OSL_MAX_STRING) 
00158       OSL_error("textual too long");
00159     
00160     string = strdup(textual->textual);
00161     if (string == NULL)
00162       OSL_error("memory overflow");
00163   }
00164 
00165   return string;
00166 }
00167 #else
00168 
00176 char * osl_textual_sprint(osl_textual_p textual) {
00177 
00178   return NULL;
00179 }
00180 #endif
00181 
00182 
00183 /*****************************************************************************
00184  *                               Reading function                            *
00185  *****************************************************************************/
00186 
00187 
00197 osl_textual_p osl_textual_sread(char ** extensions) {
00198   osl_textual_p textual = NULL;
00199 
00200   if (*extensions != NULL) {
00201     textual = osl_textual_malloc();
00202     OSL_strdup(textual->textual, *extensions);
00203     
00204     // Update the input string pointer to the end of the string (since
00205     // everything has been read).
00206     *extensions = *extensions + strlen(*extensions);
00207   }
00208 
00209   return textual;
00210 }
00211 
00212 
00213 /*+***************************************************************************
00214  *                    Memory allocation/deallocation function                *
00215  *****************************************************************************/
00216 
00217 
00226 osl_textual_p osl_textual_malloc() {
00227   osl_textual_p textual;
00228 
00229   OSL_malloc(textual, osl_textual_p, sizeof(osl_textual_t));
00230   textual->textual = NULL;
00231 
00232   return textual;
00233 }
00234 
00235 
00242 void osl_textual_free(osl_textual_p textual) {
00243   if (textual != NULL) {
00244     if(textual->textual != NULL)
00245       free(textual->textual);
00246     free(textual);
00247   }
00248 }
00249 
00250 
00251 /*+***************************************************************************
00252  *                            Processing functions                           *
00253  *****************************************************************************/
00254 
00255 
00263 osl_textual_p osl_textual_clone(osl_textual_p textual) {
00264   osl_textual_p clone;
00265 
00266   if (textual == NULL)
00267     return NULL;
00268 
00269   clone = osl_textual_malloc();
00270   OSL_strdup(clone->textual, textual->textual);
00271 
00272   return clone;
00273 }
00274 
00275 
00276 #if 0
00277 
00285 int osl_textual_equal(osl_textual_p f1, osl_textual_p f2) {
00286  
00287   if (f1 == f2)
00288     return 1;
00289 
00290   if (((f1 == NULL) && (f2 != NULL)) || ((f1 != NULL) && (f2 == NULL)))
00291     return 0;
00292 
00293   if (strcmp(f1->textual, f2->textual))
00294     return 0;
00295 
00296   return 1;
00297 }
00298 #else
00299 
00308 int osl_textual_equal(osl_textual_p f1, osl_textual_p f2) {
00309 
00310   return 1;
00311 }
00312 #endif
00313 
00314 
00321 osl_interface_p osl_textual_interface() {
00322   osl_interface_p interface = osl_interface_malloc();
00323   
00324   OSL_strdup(interface->URI, OSL_URI_TEXTUAL);
00325   interface->idump  = (osl_idump_f)osl_textual_idump;
00326   interface->sprint = (osl_sprint_f)osl_textual_sprint;
00327   interface->sread  = (osl_sread_f)osl_textual_sread;
00328   interface->malloc = (osl_malloc_f)osl_textual_malloc;
00329   interface->free   = (osl_free_f)osl_textual_free;
00330   interface->clone  = (osl_clone_f)osl_textual_clone;
00331   interface->equal  = (osl_equal_f)osl_textual_equal;
00332 
00333   return interface;
00334 }
00335