1 2 /*+-----------------------------------------------------------------** 3 ** OpenScop Library ** 4 **-----------------------------------------------------------------** 5 ** extensions/coordinates.c ** 6 **-----------------------------------------------------------------** 7 ** First version: 07/12/2010 ** 8 **-----------------------------------------------------------------** 9 10 11 ***************************************************************************** 12 * OpenScop: Structures and formats for polyhedral tools to talk together * 13 ***************************************************************************** 14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, * 15 * / / / // // // // / / / // // / / // / /|,_, * 16 * / / / // // // // / / / // // / / // / / / /\ * 17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ * 18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ * 19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ * 20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ * 21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ * 22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ * 23 * | I | | | | e | | | | | | | | | | | | | \ \ \ * 24 * | T | | | | | | | | | | | | | | | | | \ \ \ * 25 * | E | | | | | | | | | | | | | | | | | \ \ \ * 26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ * 27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / * 28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' * 29 * * 30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA * 31 * * 32 * (3-clause BSD license) * 33 * Redistribution and use in source and binary forms, with or without * 34 * modification, are permitted provided that the following conditions * 35 * are met: * 36 * * 37 * 1. Redistributions of source code must retain the above copyright notice, * 38 * this list of conditions and the following disclaimer. * 39 * 2. Redistributions in binary form must reproduce the above copyright * 40 * notice, this list of conditions and the following disclaimer in the * 41 * documentation and/or other materials provided with the distribution. * 42 * 3. The name of the author may not be used to endorse or promote products * 43 * derived from this software without specific prior written permission. * 44 * * 45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * 46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * 47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * 48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * 49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * 50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * 51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * 52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * 54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 55 * * 56 * OpenScop Library, a library to manipulate OpenScop formats and data * 57 * structures. Written by: * 58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and * 59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> * 60 * * 61 *****************************************************************************/ 62 63#include <stdlib.h> 64#include <stdio.h> 65#include <string.h> 66 67#include <osl/macros.h> 68#include <osl/util.h> 69#include <osl/interface.h> 70#include <osl/extensions/coordinates.h> 71 72 73/*+*************************************************************************** 74 * Structure display function * 75 *****************************************************************************/ 76 77 78/** 79 * osl_coordinates_idump function: 80 * this function displays an osl_coordinates_t structure (*coordinates) into a 81 * file (file, possibly stdout) in a way that trends to be understandable. It 82 * includes an indentation level (level) in order to work with others 83 * idump functions. 84 * \param file The file where the information has to be printed. 85 * \param coordinates The coordinates structure to print. 86 * \param level Number of spaces before printing, for each line. 87 */ 88void osl_coordinates_idump(FILE * file, osl_coordinates_p coordinates, 89 int level) { 90 int j; 91 92 // Go to the right level. 93 for (j = 0; j < level; j++) 94 fprintf(file, "|\t"); 95 96 if (coordinates != NULL) 97 fprintf(file, "+-- osl_coordinates_t\n"); 98 else 99 fprintf(file, "+-- NULL coordinates\n"); 100 101 if (coordinates != NULL) { 102 // Go to the right level. 103 for(j = 0; j <= level; j++) 104 fprintf(file, "|\t"); 105 106 // Display the file name. 107 if (coordinates->name != NULL) 108 fprintf(file, "File name__: %s\n", coordinates->name); 109 else 110 fprintf(file, "NULL file name\n"); 111 112 // Go to the right level. 113 for(j = 0; j <= level; j++) 114 fprintf(file, "|\t"); 115 116 // Display the lines. 117 fprintf(file, "Lines______: [%d, %d]\n", 118 coordinates->start, coordinates->end); 119 120 // Go to the right level. 121 for(j = 0; j <= level; j++) 122 fprintf(file, "|\t"); 123 124 // Display the indentation. 125 fprintf(file, "Indentation: %d\n", coordinates->indent); 126 } 127 128 // The last line. 129 for (j = 0; j <= level; j++) 130 fprintf(file, "|\t"); 131 fprintf(file, "\n"); 132} 133 134 135/** 136 * osl_coordinates_dump function: 137 * this function prints the content of an osl_coordinates_t structure 138 * (*coordinates) into a file (file, possibly stdout). 139 * \param file The file where the information has to be printed. 140 * \param coordinates The coordinates structure to print. 141 */ 142void osl_coordinates_dump(FILE * file, osl_coordinates_p coordinates) { 143 osl_coordinates_idump(file, coordinates, 0); 144} 145 146 147/** 148 * osl_coordinates_sprint function: 149 * this function prints the content of an osl_coordinates_t structure 150 * (*coordinates) into a string (returned) in the OpenScop textual format. 151 * \param coordinates The coordinates structure to be print. 152 * \return A string containing the OpenScop dump of the coordinates structure. 153 */ 154char * osl_coordinates_sprint(osl_coordinates_p coordinates) { 155 int high_water_mark = OSL_MAX_STRING; 156 char * string = NULL; 157 char buffer[OSL_MAX_STRING]; 158 159 if (coordinates != NULL) { 160 OSL_malloc(string, char *, high_water_mark * sizeof(char)); 161 string[0] = '\0'; 162 163 // Print the coordinates content. 164 sprintf(buffer, "# File name\n%s\n", coordinates->name); 165 osl_util_safe_strcat(&string, buffer, &high_water_mark); 166 167 sprintf(buffer, "# Starting line\n%d\n", coordinates->start); 168 osl_util_safe_strcat(&string, buffer, &high_water_mark); 169 170 sprintf(buffer, "# Ending line\n%d\n", coordinates->end); 171 osl_util_safe_strcat(&string, buffer, &high_water_mark); 172 173 sprintf(buffer, "# Indentation\n%d\n", coordinates->indent); 174 osl_util_safe_strcat(&string, buffer, &high_water_mark); 175 176 // Keep only the memory space we need. 177 OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char)); 178 } 179 180 return string; 181} 182 183 184/***************************************************************************** 185 * Reading function * 186 *****************************************************************************/ 187 188 189/** 190 * osl_coordinates_sread function: 191 * this function reads a coordinates structure from a string complying to the 192 * OpenScop textual format and returns a pointer to this structure. 193 * The input parameter is updated to the position in the input string this 194 * function reach right after reading the coordinates structure. 195 * \param[in,out] input The input string where to find coordinates. 196 * Updated to the position after what has been read. 197 * \return A pointer to the coordinates structure that has been read. 198 */ 199osl_coordinates_p osl_coordinates_sread(char ** input) { 200 osl_coordinates_p coordinates; 201 202 if (*input == NULL) { 203 OSL_debug("no coordinates optional tag"); 204 return NULL; 205 } 206 207 // Build the coordinates structure. 208 coordinates = osl_coordinates_malloc(); 209 210 // Read the file name (and path). 211 coordinates->name = osl_util_read_line(NULL, input); 212 213 // Read the number of the starting line. 214 coordinates->start = osl_util_read_int(NULL, input); 215 216 // Read the number of the ending line. 217 coordinates->end = osl_util_read_int(NULL, input); 218 219 // Read the indentation level. 220 coordinates->indent = osl_util_read_int(NULL, input); 221 222 return coordinates; 223} 224 225 226/*+*************************************************************************** 227 * Memory allocation/deallocation function * 228 *****************************************************************************/ 229 230 231/** 232 * osl_coordinates_malloc function: 233 * this function allocates the memory space for an osl_coordinates_t 234 * structure and sets its fields with default values. Then it returns a 235 * pointer to the allocated space. 236 * \return A pointer to an empty coordinates structure with fields set to 237 * default values. 238 */ 239osl_coordinates_p osl_coordinates_malloc() { 240 osl_coordinates_p coordinates; 241 242 OSL_malloc(coordinates, osl_coordinates_p, sizeof(osl_coordinates_t)); 243 coordinates->name = NULL; 244 coordinates->start = OSL_UNDEFINED; 245 coordinates->end = OSL_UNDEFINED; 246 coordinates->indent = OSL_UNDEFINED; 247 248 return coordinates; 249} 250 251 252/** 253 * osl_coordinates_free function: 254 * this function frees the allocated memory for an osl_coordinates_t 255 * structure. 256 * \param coordinates The pointer to the coordinates structure to free. 257 */ 258void osl_coordinates_free(osl_coordinates_p coordinates) { 259 if (coordinates != NULL) { 260 free(coordinates->name); 261 free(coordinates); 262 } 263} 264 265 266/*+*************************************************************************** 267 * Processing functions * 268 *****************************************************************************/ 269 270 271/** 272 * osl_coordinates_clone function: 273 * this function builds and returns a "hard copy" (not a pointer copy) of an 274 * osl_coordinates_t data structure. 275 * \param coordinates The pointer to the coordinates structure to clone. 276 * \return A pointer to the clone of the coordinates structure. 277 */ 278osl_coordinates_p osl_coordinates_clone(osl_coordinates_p coordinates) { 279 osl_coordinates_p clone; 280 281 if (coordinates == NULL) 282 return NULL; 283 284 clone = osl_coordinates_malloc(); 285 OSL_strdup(clone->name, coordinates->name); 286 clone->start = coordinates->start; 287 clone->end = coordinates->end; 288 clone->indent = coordinates->indent; 289 290 return clone; 291} 292 293 294/** 295 * osl_coordinates_equal function: 296 * this function returns true if the two coordinates structures are the same 297 * (content-wise), false otherwise. This functions considers two coordinates 298 * \param c1 The first coordinates structure. 299 * \param c2 The second coordinates structure. 300 * \return 1 if c1 and c2 are the same (content-wise), 0 otherwise. 301 */ 302int osl_coordinates_equal(osl_coordinates_p c1, osl_coordinates_p c2) { 303 if (c1 == c2) 304 return 1; 305 306 if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL))) 307 return 0; 308 309 if (strcmp(c1->name, c2->name)) { 310 OSL_info("file names are not the same"); 311 return 0; 312 } 313 314 if (c1->start != c2->start) { 315 OSL_info("starting lines are not the same"); 316 return 0; 317 } 318 319 if (c1->indent != c2->indent) { 320 OSL_info("indentations are not the same"); 321 return 0; 322 } 323 324 return 1; 325} 326 327 328/** 329 * osl_coordinates_interface function: 330 * this function creates an interface structure corresponding to the coordinates 331 * extension and returns it). 332 * \return An interface structure for the coordinates extension. 333 */ 334osl_interface_p osl_coordinates_interface() { 335 osl_interface_p interface = osl_interface_malloc(); 336 337 interface->URI = strdup(OSL_URI_COORDINATES); 338 interface->idump = (osl_idump_f)osl_coordinates_idump; 339 interface->sprint = (osl_sprint_f)osl_coordinates_sprint; 340 interface->sread = (osl_sread_f)osl_coordinates_sread; 341 interface->malloc = (osl_malloc_f)osl_coordinates_malloc; 342 interface->free = (osl_free_f)osl_coordinates_free; 343 interface->clone = (osl_clone_f)osl_coordinates_clone; 344 interface->equal = (osl_equal_f)osl_coordinates_equal; 345 346 return interface; 347} 348 349