OpenScop 0.8.1

interface.c

Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                          interface.c                            **
00006      **-----------------------------------------------------------------**
00007      **                   First version: 15/07/2011                     **
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 #include <osl/extensions/textual.h>
00067 #include <osl/extensions/comment.h>
00068 #include <osl/extensions/scatnames.h>
00069 #include <osl/extensions/arrays.h>
00070 #include <osl/extensions/lines.h>
00071 #include <osl/extensions/irregular.h>
00072 #include <osl/strings.h>
00073 #include <osl/body.h>
00074 #include <osl/interface.h>
00075 
00076 
00077 /*+***************************************************************************
00078  *                          Structure display function                       *
00079  *****************************************************************************/
00080 
00081 
00092 void osl_interface_idump(FILE * file, osl_interface_p interface, int level) {
00093   int j, first = 1;
00094 
00095   // Go to the right level.
00096   for (j = 0; j < level; j++)
00097     fprintf(file, "|\t");
00098 
00099   if (interface != NULL)
00100     fprintf(file, "+-- osl_interface_t: URI = %s\n", interface->URI);
00101   else
00102     fprintf(file, "+-- NULL interface\n");
00103 
00104   
00105   while (interface != NULL) {
00106     if (!first) {
00107       // Go to the right level.
00108       for (j = 0; j < level; j++)
00109         fprintf(file, "|\t");
00110 
00111       if (interface->URI != NULL)
00112         fprintf(file, "|   osl_interface_t: URI = %s\n", interface->URI);
00113       else
00114         fprintf(file, "|   osl_interface_t: URI = (NULL)\n");
00115     }
00116     else
00117       first = 0;
00118 
00119     interface = interface->next;
00120 
00121     // Next line.
00122     if (interface != NULL) {
00123       for (j = 0; j <= level + 1; j++)
00124         fprintf(file, "|\t");
00125       fprintf(file, "\n");
00126       for (j = 0; j <= level; j++)
00127         fprintf(file, "|\t");
00128       fprintf(file, "V\n");
00129     }
00130   }
00131   
00132   // The last line.
00133   for (j = 0; j <= level; j++)
00134     fprintf(file, "|\t");
00135   fprintf(file, "\n");
00136 }
00137 
00138 
00146 void osl_interface_dump(FILE * file, osl_interface_p interface) {
00147   osl_interface_idump(file, interface, 0); 
00148 }
00149 
00150 
00151 /*****************************************************************************
00152  *                               Reading function                            *
00153  *****************************************************************************/
00154 
00155 
00156 /*+***************************************************************************
00157  *                    Memory allocation/deallocation function                *
00158  *****************************************************************************/
00159 
00160 
00169 void osl_interface_add(osl_interface_p * list, osl_interface_p interface) {
00170   osl_interface_p tmp = *list, check_interface;
00171 
00172   if (interface != NULL) {
00173     // First, check that the interface list is OK.
00174     check_interface = interface;
00175     while (check_interface != NULL) {
00176       if (check_interface->URI == NULL)
00177         OSL_error("no URI in an interface to add to a list");
00178 
00179       if (osl_interface_lookup(*list, check_interface->URI) != NULL)
00180         OSL_error("only one interface with a given URI is allowed");
00181       check_interface = check_interface->next;
00182     }
00183 
00184     if (*list != NULL) {
00185       while (tmp->next != NULL)
00186         tmp = tmp->next;
00187       tmp->next = interface;
00188     }
00189     else {
00190       *list = interface;
00191     }
00192   }
00193 }
00194 
00195 
00204 osl_interface_p osl_interface_malloc() {
00205   osl_interface_p interface;
00206 
00207   OSL_malloc(interface, osl_interface_p,
00208                   sizeof(osl_interface_t));
00209   interface->URI    = NULL;
00210   interface->idump  = NULL; 
00211   interface->sprint = NULL; 
00212   interface->sread  = NULL; 
00213   interface->malloc = NULL; 
00214   interface->free   = NULL; 
00215   interface->clone  = NULL; 
00216   interface->equal  = NULL;
00217   interface->next   = NULL;
00218 
00219   return interface;
00220 }
00221 
00222 
00229 void osl_interface_free(osl_interface_p interface) {
00230   osl_interface_p tmp;
00231   int i = 0;
00232  
00233   if (interface == NULL)
00234     return;
00235 
00236   while (interface != NULL) {
00237     tmp = interface->next;
00238     if (interface->URI != NULL)
00239       free(interface->URI);
00240     free(interface);
00241     interface = tmp;
00242     i++;
00243   }
00244 }
00245 
00246 
00247 /*+***************************************************************************
00248  *                            Processing functions                           *
00249  *****************************************************************************/
00250 
00251 
00260 osl_interface_p osl_interface_nclone(osl_interface_p interface, int n) {
00261   osl_interface_p clone = NULL, new;
00262   int i = 0;
00263 
00264   while ((interface != NULL) && ((n == -1) || (i < n))) {
00265     new = osl_interface_malloc();
00266     OSL_strdup(new->URI, interface->URI);
00267     new->idump  = interface->idump;
00268     new->sprint = interface->sprint;
00269     new->sread  = interface->sread;
00270     new->malloc = interface->malloc;
00271     new->free   = interface->free;
00272     new->clone  = interface->clone;
00273     new->equal  = interface->equal;
00274     
00275     osl_interface_add(&clone, new);
00276     interface = interface->next;
00277     i++;
00278   }
00279 
00280   return clone;
00281 }
00282 
00283 
00291 osl_interface_p osl_interface_clone(osl_interface_p interface) {
00292 
00293   return osl_interface_nclone(interface, -1);
00294 }
00295 
00296 
00305 int osl_interface_equal(osl_interface_p interface1,
00306                         osl_interface_p interface2) {
00307 
00308   if (interface1 == interface2)
00309     return 1;
00310   
00311   if (((interface1 == NULL) && (interface2 != NULL)) ||
00312       ((interface1 != NULL) && (interface2 == NULL)))
00313     return 0;
00314 
00315   if (strcmp(interface1->URI, interface2->URI)   ||
00316       (interface1->idump  != interface2->idump)  ||
00317       (interface1->sprint != interface2->sprint) ||
00318       (interface1->sread  != interface2->sread)  ||
00319       (interface1->malloc != interface2->malloc) ||
00320       (interface1->free   != interface2->free)   ||
00321       (interface1->clone  != interface2->clone)  ||
00322       (interface1->equal  != interface2->equal))
00323     return 0;
00324 
00325   return 1;
00326 }
00327 
00328 
00338 osl_interface_p
00339 osl_interface_lookup(osl_interface_p list, char * URI) {
00340   while (list != NULL) {
00341     if ((list->URI != NULL) && (!strcmp(list->URI, URI)))
00342       return list;
00343 
00344     list = list->next;
00345   }
00346 
00347   return NULL;
00348 }
00349 
00350 
00357 osl_interface_p osl_interface_get_default_registry() {
00358   osl_interface_p registry = NULL;
00359 
00360   // Internal generics
00361   osl_interface_add(&registry, osl_strings_interface());
00362   osl_interface_add(&registry, osl_body_interface());
00363   
00364   // Extensions
00365   osl_interface_add(&registry, osl_textual_interface());
00366   osl_interface_add(&registry, osl_comment_interface());
00367   osl_interface_add(&registry, osl_scatnames_interface());
00368   //osl_interface_add(&registry, osl_arrays_interface());
00369   //osl_interface_add(&registry, osl_lines_interface());
00370   //osl_interface_add(&registry, osl_irregular_interface());
00371   
00372   return registry;
00373 }
00374 
00375 
00376