OpenScop  0.9.0
coordinates.c
Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                    extensions/coordinates.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/coordinates.h>
00071 
00072 
00073 /*+***************************************************************************
00074  *                          Structure display function                       *
00075  *****************************************************************************/
00076 
00077 
00088 void osl_coordinates_idump(FILE* file, osl_coordinates_p coordinates,
00089                            int level) {
00090   int j;
00091 
00092   // Go to the right level.
00093   for (j = 0; j < level; j++)
00094     fprintf(file, "|\t");
00095 
00096   if (coordinates != NULL)
00097     fprintf(file, "+-- osl_coordinates_t\n");
00098   else
00099     fprintf(file, "+-- NULL coordinates\n");
00100 
00101   if (coordinates != NULL) {
00102     // Go to the right level.
00103     for(j = 0; j <= level; j++)
00104       fprintf(file, "|\t");
00105 
00106     // Display the file name.
00107     if (coordinates->name != NULL)
00108       fprintf(file, "File name__: %s\n", coordinates->name);
00109     else
00110       fprintf(file, "NULL file name\n");
00111 
00112     // Go to the right level.
00113     for(j = 0; j <= level; j++)
00114       fprintf(file, "|\t");
00115 
00116     // Display the lines.
00117     fprintf(file, "Coordinates: [%d,%d -> %d,%d]\n",
00118             coordinates->line_start, coordinates->column_start,
00119             coordinates->line_end,   coordinates->column_end);
00120 
00121     // Go to the right level.
00122     for(j = 0; j <= level; j++)
00123       fprintf(file, "|\t");
00124   
00125     // Display the indentation.
00126     fprintf(file, "Indentation: %d\n", coordinates->indent);
00127   }
00128 
00129   // The last line.
00130   for (j = 0; j <= level; j++)
00131     fprintf(file, "|\t");
00132   fprintf(file, "\n");
00133 }
00134 
00135 
00143 void osl_coordinates_dump(FILE* file, osl_coordinates_p coordinates) {
00144   osl_coordinates_idump(file, coordinates, 0);
00145 }
00146 
00147 
00155 char* osl_coordinates_sprint(osl_coordinates_p coordinates) {
00156   int high_water_mark = OSL_MAX_STRING;
00157   char* string = NULL;
00158   char buffer[OSL_MAX_STRING];
00159 
00160   if (coordinates != NULL) {
00161     OSL_malloc(string, char*, high_water_mark * sizeof(char));
00162     string[0] = '\0';
00163    
00164     // Print the coordinates content.
00165     sprintf(buffer, "# File name\n%s\n", coordinates->name);
00166     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00167 
00168     sprintf(buffer, "# Starting line and column\n%d %d\n",
00169             coordinates->line_start, coordinates->column_start);
00170     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00171 
00172     sprintf(buffer, "# Ending line and column\n%d %d\n",
00173             coordinates->line_end, coordinates->column_end);
00174     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00175 
00176     sprintf(buffer, "# Indentation\n%d\n", coordinates->indent);
00177     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00178   
00179     // Keep only the memory space we need.
00180     OSL_realloc(string, char*, (strlen(string) + 1) * sizeof(char));
00181   }
00182 
00183   return string;
00184 }
00185 
00186 
00187 /*****************************************************************************
00188  *                               Reading function                            *
00189  *****************************************************************************/
00190 
00191 
00202 osl_coordinates_p osl_coordinates_sread(char** input) {
00203   osl_coordinates_p coordinates;
00204 
00205   if (*input == NULL) {
00206     OSL_debug("no coordinates optional tag");
00207     return NULL;
00208   }
00209 
00210   // Build the coordinates structure.
00211   coordinates = osl_coordinates_malloc();
00212   
00213   // Read the file name (and path).
00214   coordinates->name = osl_util_read_line(NULL, input);
00215 
00216   // Read the coordinates.
00217   coordinates->line_start   = osl_util_read_int(NULL, input);
00218   coordinates->column_start = osl_util_read_int(NULL, input);
00219   coordinates->line_end     = osl_util_read_int(NULL, input);
00220   coordinates->column_end   = osl_util_read_int(NULL, input);
00221 
00222   // Read the indentation level.
00223   coordinates->indent = osl_util_read_int(NULL, input);
00224   
00225   return coordinates;
00226 }
00227 
00228 
00229 /*+***************************************************************************
00230  *                    Memory allocation/deallocation function                *
00231  *****************************************************************************/
00232 
00233 
00242 osl_coordinates_p osl_coordinates_malloc() {
00243   osl_coordinates_p coordinates;
00244   
00245   OSL_malloc(coordinates, osl_coordinates_p, sizeof(osl_coordinates_t));
00246   coordinates->name         = NULL;
00247   coordinates->line_start   = OSL_UNDEFINED;
00248   coordinates->column_start = OSL_UNDEFINED;
00249   coordinates->line_end     = OSL_UNDEFINED;
00250   coordinates->column_end   = OSL_UNDEFINED;
00251   coordinates->indent       = OSL_UNDEFINED;
00252 
00253   return coordinates;
00254 }
00255 
00256 
00263 void osl_coordinates_free(osl_coordinates_p coordinates) {
00264   if (coordinates != NULL) {
00265     free(coordinates->name);
00266     free(coordinates);
00267   }
00268 }
00269 
00270 
00271 /*+***************************************************************************
00272  *                            Processing functions                           *
00273  *****************************************************************************/
00274 
00275 
00283 osl_coordinates_p osl_coordinates_clone(osl_coordinates_p coordinates) {
00284   osl_coordinates_p clone;
00285 
00286   if (coordinates == NULL)
00287     return NULL;
00288 
00289   clone = osl_coordinates_malloc();
00290   OSL_strdup(clone->name, coordinates->name);
00291   clone->line_start   = coordinates->line_start;
00292   clone->column_start = coordinates->column_start;
00293   clone->line_end     = coordinates->line_end;
00294   clone->column_end   = coordinates->column_end;
00295   clone->indent       = coordinates->indent;
00296 
00297   return clone;
00298 }
00299 
00300 
00309 int osl_coordinates_equal(osl_coordinates_p c1, osl_coordinates_p c2) {
00310   if (c1 == c2)
00311     return 1;
00312 
00313   if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
00314     return 0;
00315 
00316   if (strcmp(c1->name, c2->name)) {
00317     OSL_info("file names are not the same");
00318     return 0;
00319   }
00320 
00321   if (c1->line_start != c2->line_start) {
00322     OSL_info("starting lines are not the same");
00323     return 0;
00324   }
00325 
00326   if (c1->column_start != c2->column_start) {
00327     OSL_info("starting columns are not the same");
00328     return 0;
00329   }
00330 
00331   if (c1->line_end != c2->line_end) {
00332     OSL_info("Ending lines are not the same");
00333     return 0;
00334   }
00335 
00336   if (c1->column_end != c2->column_end) {
00337     OSL_info("Ending columns are not the same");
00338     return 0;
00339   }
00340 
00341   if (c1->indent != c2->indent) {
00342     OSL_info("indentations are not the same");
00343     return 0;
00344   }
00345 
00346   return 1;
00347 }
00348 
00349 
00356 osl_interface_p osl_coordinates_interface() {
00357   osl_interface_p interface = osl_interface_malloc();
00358   
00359   OSL_strdup(interface->URI, OSL_URI_COORDINATES);
00360   interface->idump  = (osl_idump_f)osl_coordinates_idump;
00361   interface->sprint = (osl_sprint_f)osl_coordinates_sprint;
00362   interface->sread  = (osl_sread_f)osl_coordinates_sread;
00363   interface->malloc = (osl_malloc_f)osl_coordinates_malloc;
00364   interface->free   = (osl_free_f)osl_coordinates_free;
00365   interface->clone  = (osl_clone_f)osl_coordinates_clone;
00366   interface->equal  = (osl_equal_f)osl_coordinates_equal;
00367 
00368   return interface;
00369 }