Set to 0.8.1
[openscop.git] / source / interface.c
blob136fcdc67f0c3025545cc74c0e80ce8e54fc781d
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** interface.c **
6 **-----------------------------------------------------------------**
7 ** First version: 15/07/2011 **
8 **-----------------------------------------------------------------**
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 *****************************************************************************/
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <osl/extensions/textual.h>
67 #include <osl/extensions/comment.h>
68 #include <osl/extensions/scatnames.h>
69 #include <osl/extensions/arrays.h>
70 #include <osl/extensions/lines.h>
71 #include <osl/extensions/irregular.h>
72 #include <osl/strings.h>
73 #include <osl/body.h>
74 #include <osl/interface.h>
77 /*+***************************************************************************
78 * Structure display function *
79 *****************************************************************************/
82 /**
83 * osl_interface_idump function:
84 * this function displays an osl_interface_t structure (*interface) into
85 * a file (file, possibly stdout) in a way that trends to be understandable.
86 * It includes an indentation level (level) in order to work with others
87 * idump functions.
88 * \param file The file where the information has to be printed.
89 * \param interface The interface structure which has to be printed.
90 * \param level Number of spaces before printing, for each line.
92 void osl_interface_idump(FILE * file, osl_interface_p interface, int level) {
93 int j, first = 1;
95 // Go to the right level.
96 for (j = 0; j < level; j++)
97 fprintf(file, "|\t");
99 if (interface != NULL)
100 fprintf(file, "+-- osl_interface_t: URI = %s\n", interface->URI);
101 else
102 fprintf(file, "+-- NULL interface\n");
105 while (interface != NULL) {
106 if (!first) {
107 // Go to the right level.
108 for (j = 0; j < level; j++)
109 fprintf(file, "|\t");
111 if (interface->URI != NULL)
112 fprintf(file, "| osl_interface_t: URI = %s\n", interface->URI);
113 else
114 fprintf(file, "| osl_interface_t: URI = (NULL)\n");
116 else
117 first = 0;
119 interface = interface->next;
121 // Next line.
122 if (interface != NULL) {
123 for (j = 0; j <= level + 1; j++)
124 fprintf(file, "|\t");
125 fprintf(file, "\n");
126 for (j = 0; j <= level; j++)
127 fprintf(file, "|\t");
128 fprintf(file, "V\n");
132 // The last line.
133 for (j = 0; j <= level; j++)
134 fprintf(file, "|\t");
135 fprintf(file, "\n");
140 * osl_interface_dump function:
141 * this function prints the content of a osl_interface_t structure
142 * (*interface) into a file (file, possibly stdout).
143 * \param file File where informations are printed.
144 * \param interface The interface structure to print.
146 void osl_interface_dump(FILE * file, osl_interface_p interface) {
147 osl_interface_idump(file, interface, 0);
151 /*****************************************************************************
152 * Reading function *
153 *****************************************************************************/
156 /*+***************************************************************************
157 * Memory allocation/deallocation function *
158 *****************************************************************************/
162 * osl_interface_add function:
163 * this function adds an interface node (it may be a list as well) to a
164 * list of interfaces provided as parameter (list). The new node
165 * is inserted at the end of the list.
166 * \param list The list of interfaces to add a node (NULL if empty).
167 * \param interface The interface to add to the list.
169 void osl_interface_add(osl_interface_p * list, osl_interface_p interface) {
170 osl_interface_p tmp = *list, check_interface;
172 if (interface != NULL) {
173 // First, check that the interface list is OK.
174 check_interface = interface;
175 while (check_interface != NULL) {
176 if (check_interface->URI == NULL)
177 OSL_error("no URI in an interface to add to a list");
179 if (osl_interface_lookup(*list, check_interface->URI) != NULL)
180 OSL_error("only one interface with a given URI is allowed");
181 check_interface = check_interface->next;
184 if (*list != NULL) {
185 while (tmp->next != NULL)
186 tmp = tmp->next;
187 tmp->next = interface;
189 else {
190 *list = interface;
197 * osl_interface_malloc function:
198 * This function allocates the memory space for a osl_interface_t
199 * structure and sets its fields with default values. Then it returns a
200 * pointer to the allocated space.
201 * \return A pointer to an empty interface structure with fields set to
202 * default values.
204 osl_interface_p osl_interface_malloc() {
205 osl_interface_p interface;
207 OSL_malloc(interface, osl_interface_p,
208 sizeof(osl_interface_t));
209 interface->URI = NULL;
210 interface->idump = NULL;
211 interface->sprint = NULL;
212 interface->sread = NULL;
213 interface->malloc = NULL;
214 interface->free = NULL;
215 interface->clone = NULL;
216 interface->equal = NULL;
217 interface->next = NULL;
219 return interface;
224 * osl_interface_free function:
225 * this function frees the allocated memory for an osl_interface_t
226 * structure, and all the interfaces stored in the list.
227 * \param[in] interface The pointer to the interface we want to free.
229 void osl_interface_free(osl_interface_p interface) {
230 osl_interface_p tmp;
231 int i = 0;
233 if (interface == NULL)
234 return;
236 while (interface != NULL) {
237 tmp = interface->next;
238 if (interface->URI != NULL)
239 free(interface->URI);
240 free(interface);
241 interface = tmp;
242 i++;
247 /*+***************************************************************************
248 * Processing functions *
249 *****************************************************************************/
253 * osl_interface_nclone function:
254 * This function builds and returns a "hard copy" (not a pointer copy) of the
255 * n first elements of an osl_interface_t list.
256 * \param interface The pointer to the interface structure we want to clone.
257 * \param n The number of nodes we want to copy (-1 for infinity).
258 * \return The clone of the n first nodes of the interface list.
260 osl_interface_p osl_interface_nclone(osl_interface_p interface, int n) {
261 osl_interface_p clone = NULL, new;
262 int i = 0;
264 while ((interface != NULL) && ((n == -1) || (i < n))) {
265 new = osl_interface_malloc();
266 OSL_strdup(new->URI, interface->URI);
267 new->idump = interface->idump;
268 new->sprint = interface->sprint;
269 new->sread = interface->sread;
270 new->malloc = interface->malloc;
271 new->free = interface->free;
272 new->clone = interface->clone;
273 new->equal = interface->equal;
275 osl_interface_add(&clone, new);
276 interface = interface->next;
277 i++;
280 return clone;
285 * osl_interface_clone function:
286 * This function builds and returns a "hard copy" (not a pointer copy) of an
287 * osl_interface_t data structure.
288 * \param interface The pointer to the interface structure we want to copy.
289 * \return A pointer to the copy of the interface structure.
291 osl_interface_p osl_interface_clone(osl_interface_p interface) {
293 return osl_interface_nclone(interface, -1);
298 * osl_interface_equal function:
299 * this function returns true if the two interface structures are the same,
300 * (content-wise) false otherwise.
301 * \param interface1 The first interface structure.
302 * \param interface2 The second interface structure.
303 * \return 1 if interface1 and interface2 are the same, 0 otherwise.
305 int osl_interface_equal(osl_interface_p interface1,
306 osl_interface_p interface2) {
308 if (interface1 == interface2)
309 return 1;
311 if (((interface1 == NULL) && (interface2 != NULL)) ||
312 ((interface1 != NULL) && (interface2 == NULL)))
313 return 0;
315 if (strcmp(interface1->URI, interface2->URI) ||
316 (interface1->idump != interface2->idump) ||
317 (interface1->sprint != interface2->sprint) ||
318 (interface1->sread != interface2->sread) ||
319 (interface1->malloc != interface2->malloc) ||
320 (interface1->free != interface2->free) ||
321 (interface1->clone != interface2->clone) ||
322 (interface1->equal != interface2->equal))
323 return 0;
325 return 1;
330 * osl_interface_lookup function:
331 * this function returns the first interface with a given URI in the
332 * interface list provided as parameter and NULL if it doesn't find such
333 * an interface.
334 * \param list The interface list where to search a given interface URI.
335 * \param URI The URI of the interface we are looking for.
336 * \return The first interface of the requested URI in the list.
338 osl_interface_p
339 osl_interface_lookup(osl_interface_p list, char * URI) {
340 while (list != NULL) {
341 if ((list->URI != NULL) && (!strcmp(list->URI, URI)))
342 return list;
344 list = list->next;
347 return NULL;
352 * osl_interface_get_default_registry function:
353 * this function creates the list of known interfaces (of all generic types,
354 * including extensions) and returns it.
355 * \return The list of known interfaces.
357 osl_interface_p osl_interface_get_default_registry() {
358 osl_interface_p registry = NULL;
360 // Internal generics
361 osl_interface_add(&registry, osl_strings_interface());
362 osl_interface_add(&registry, osl_body_interface());
364 // Extensions
365 osl_interface_add(&registry, osl_textual_interface());
366 osl_interface_add(&registry, osl_comment_interface());
367 osl_interface_add(&registry, osl_scatnames_interface());
368 //osl_interface_add(&registry, osl_arrays_interface());
369 //osl_interface_add(&registry, osl_lines_interface());
370 //osl_interface_add(&registry, osl_irregular_interface());
372 return registry;