Add minor functionalities on relations
[openscop.git] / source / interface.c
blobf81a196d72d064baaa9e300edcd766223f5e3dd8
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/interface.h>
69 /*+***************************************************************************
70 * Structure display function *
71 *****************************************************************************/
74 /**
75 * osl_interface_idump function:
76 * this function displays an osl_interface_t structure (*interface) into
77 * a file (file, possibly stdout) in a way that trends to be understandable.
78 * It includes an indentation level (level) in order to work with others
79 * print_structure functions.
80 * \param file The file where the information has to be printed.
81 * \param interface The interface structure which has to be printed.
82 * \param level Number of spaces before printing, for each line.
84 void osl_interface_idump(FILE * file, osl_interface_p interface, int level) {
85 int j, first = 1;
87 // Go to the right level.
88 for (j = 0; j < level; j++)
89 fprintf(file, "|\t");
91 if (interface != NULL)
92 fprintf(file, "+-- osl_interface_t: URI = %s\n", interface->URI);
93 else
94 fprintf(file, "+-- NULL interface\n");
97 while (interface != NULL) {
98 if (!first) {
99 // Go to the right level.
100 for (j = 0; j < level; j++)
101 fprintf(file, "|\t");
102 fprintf(file, "| osl_interface_t: URI = %s\n", interface->URI);
104 else
105 first = 0;
107 interface = interface->next;
109 // Next line.
110 if (interface != NULL) {
111 for (j = 0; j <= level + 1; j++)
112 fprintf(file, "|\t");
113 fprintf(file, "\n");
114 for (j = 0; j <= level; j++)
115 fprintf(file, "|\t");
116 fprintf(file, "V\n");
120 // The last line.
121 for (j = 0; j <= level; j++)
122 fprintf(file, "|\t");
123 fprintf(file, "\n");
128 * osl_interface_dump function:
129 * this function prints the content of a osl_interface_t structure
130 * (*interface) into a file (file, possibly stdout).
131 * \param file File where informations are printed.
132 * \param extension The extension idstructure to print.
134 void osl_interface_dump(FILE * file, osl_interface_p interface) {
135 osl_interface_idump(file, interface, 0);
139 /*****************************************************************************
140 * Reading function *
141 *****************************************************************************/
144 /*+***************************************************************************
145 * Memory allocation/deallocation function *
146 *****************************************************************************/
150 * osl_interface_add function:
151 * this function adds an interface node (it may be a list as well) to a
152 * list of interfaces provided as parameter (list). The new node
153 * is inserted at the end of the list.
154 * \param list The list of interfaces to add a node (NULL if empty).
155 * \param interface The interface to add to the list.
157 void osl_interface_add(osl_interface_p * list,
158 osl_interface_p interface) {
159 osl_interface_p tmp = *list, check_interface;
161 if (interface != NULL) {
162 // First, check that the interface list is OK.
163 check_interface = interface;
164 while (check_interface != NULL) {
165 if (check_interface->URI == NULL)
166 OSL_error("no URI in an interface to add to a list");
168 if (osl_interface_lookup(*list, check_interface->URI) != NULL)
169 OSL_error("only one interface with a given URI is allowed");
170 check_interface = check_interface->next;
173 if (*list != NULL) {
174 while (tmp->next != NULL)
175 tmp = tmp->next;
176 tmp->next = interface;
178 else {
179 *list = interface;
186 * osl_interface_malloc function:
187 * This function allocates the memory space for a osl_interface_t
188 * structure and sets its fields with default values. Then it returns a
189 * pointer to the allocated space.
190 * \return A pointer to an empty interface structure with fields set to
191 * default values.
193 osl_interface_p osl_interface_malloc() {
194 osl_interface_p interface;
196 OSL_malloc(interface, osl_interface_p,
197 sizeof(osl_interface_t));
198 interface->URI = NULL;
199 interface->idump = NULL;
200 interface->sprint = NULL;
201 interface->sread = NULL;
202 interface->malloc = NULL;
203 interface->free = NULL;
204 interface->clone = NULL;
205 interface->equal = NULL;
206 interface->next = NULL;
208 return interface;
213 * osl_interface_free function:
214 * this function frees the allocated memory for an osl_interface_t
215 * structure, and all the interfaces stored in the list.
216 * \param[in] id The pointer to the interface we want to free.
218 void osl_interface_free(osl_interface_p interface) {
219 osl_interface_p tmp;
220 int i = 0;
222 if (interface == NULL)
223 return;
225 while (interface != NULL) {
226 tmp = interface->next;
227 if (interface->URI != NULL)
228 free(interface->URI);
229 free(interface);
230 interface = tmp;
231 i++;
236 /*+***************************************************************************
237 * Processing functions *
238 *****************************************************************************/
242 * osl_interface_nclone function:
243 * This function builds and returns a "hard copy" (not a pointer copy) of the
244 * n first elements of an osl_interface_t list.
245 * \param interface The pointer to the interface structure we want to clone.
246 * \param n The number of nodes we want to copy (-1 for infinity).
247 * \return The clone of the n first nodes of the interface list.
249 osl_interface_p osl_interface_nclone(osl_interface_p interface, int n) {
250 osl_interface_p clone = NULL, new;
251 int i = 0;
253 while ((interface != NULL) && ((n == -1) || (i < n))) {
254 new = osl_interface_malloc();
255 OSL_strdup(new->URI, interface->URI);
256 new->idump = interface->idump;
257 new->sprint = interface->sprint;
258 new->sread = interface->sread;
259 new->malloc = interface->malloc;
260 new->free = interface->free;
261 new->clone = interface->clone;
262 new->equal = interface->equal;
264 osl_interface_add(&clone, new);
265 interface = interface->next;
266 i++;
269 return clone;
274 * osl_interface_clone function:
275 * This function builds and returns a "hard copy" (not a pointer copy) of an
276 * osl_interface_t data structure.
277 * \param interface The pointer to the interface structure we want to copy.
278 * \return A pointer to the copy of the interface structure.
280 osl_interface_p osl_interface_clone(osl_interface_p interface) {
282 return osl_interface_nclone(interface, -1);
287 * osl_interface_equal function:
288 * this function returns true if the two interface structures are the same,
289 * (content-wise) false otherwise.
290 * \param interface1 The first interface structure.
291 * \param interface2 The second interface structure.
292 * \return 1 if interface1 and interface2 are the same, 0 otherwise.
294 int osl_interface_equal(osl_interface_p interface1,
295 osl_interface_p interface2) {
297 if (interface1 == interface2)
298 return 1;
300 if (((interface1 == NULL) && (interface2 != NULL)) ||
301 ((interface1 != NULL) && (interface2 == NULL)))
302 return 0;
304 if (strcmp(interface1->URI, interface2->URI) ||
305 (interface1->idump != interface2->idump) ||
306 (interface1->sprint != interface2->sprint) ||
307 (interface1->sread != interface2->sread) ||
308 (interface1->malloc != interface2->malloc) ||
309 (interface1->free != interface2->free) ||
310 (interface1->clone != interface2->clone) ||
311 (interface1->equal != interface2->equal))
312 return 0;
314 return 1;
319 * osl_interface_lookup function:
320 * this function returns the first interface with a given URI in the
321 * interface list provided as parameter and NULL if it doesn't find such
322 * an interface.
323 * \param list The interface list where to search a given interface URI.
324 * \param URI The URI of the interface we are looking for.
325 * \return The first interface of the requested URI in the list.
327 osl_interface_p
328 osl_interface_lookup(osl_interface_p list, char * URI) {
329 while (list != NULL) {
330 if ((list->URI != NULL) && (!strcmp(list->URI, URI)))
331 return list;
333 list = list->next;
336 return NULL;