Set to 0.6.0
[openscop.git] / source / interface.c
blob828739c6fe58d44061e3a5bed2a7d6554c0d4f6b
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 <openscop/interface.h>
69 /*+***************************************************************************
70 * Structure display function *
71 *****************************************************************************/
74 /**
75 * openscop_interface_idump function:
76 * this function displays an openscop_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 openscop_interface_idump(FILE * file, openscop_interface_p interface,
85 int level) {
86 int j, first = 1;
88 // Go to the right level.
89 for (j = 0; j < level; j++)
90 fprintf(file, "|\t");
92 if (interface != NULL)
93 fprintf(file, "+-- openscop_interface_t: URI = %s\n", interface->URI);
94 else
95 fprintf(file, "+-- NULL interface\n");
98 while (interface != NULL) {
99 if (!first) {
100 // Go to the right level.
101 for (j = 0; j < level; j++)
102 fprintf(file, "|\t");
103 fprintf(file, "| openscop_interface_t: URI = %s\n", interface->URI);
105 else
106 first = 0;
108 interface = interface->next;
110 // Next line.
111 if (interface != NULL) {
112 for (j = 0; j <= level + 1; j++)
113 fprintf(file, "|\t");
114 fprintf(file, "\n");
115 for (j = 0; j <= level; j++)
116 fprintf(file, "|\t");
117 fprintf(file, "V\n");
121 // The last line.
122 for (j = 0; j <= level; j++)
123 fprintf(file, "|\t");
124 fprintf(file, "\n");
129 * openscop_interface_dump function:
130 * this function prints the content of a openscop_interface_t structure
131 * (*interface) into a file (file, possibly stdout).
132 * \param file File where informations are printed.
133 * \param extension The extension idstructure to print.
135 void openscop_interface_dump(FILE * file, openscop_interface_p interface) {
136 openscop_interface_idump(file, interface, 0);
140 /*****************************************************************************
141 * Reading function *
142 *****************************************************************************/
145 /*+***************************************************************************
146 * Memory allocation/deallocation function *
147 *****************************************************************************/
151 * openscop_interface_add function:
152 * this function adds an interface node (it may be a list as well) to a
153 * list of interfaces provided as parameter (list). The new node
154 * is inserted at the end of the list.
155 * \param list The list of interfaces to add a node (NULL if empty).
156 * \param interface The interface to add to the list.
158 void openscop_interface_add(openscop_interface_p * list,
159 openscop_interface_p interface) {
160 openscop_interface_p tmp = *list, check_interface;
162 if (interface != NULL) {
163 // First, check that the interface list is OK.
164 check_interface = interface;
165 while (check_interface != NULL) {
166 if (check_interface->URI == NULL)
167 OPENSCOP_error("no URI in an interface to add to a list");
169 if (openscop_interface_lookup(*list, check_interface->URI) != NULL)
170 OPENSCOP_error("only one interface with a given URI is allowed");
171 check_interface = check_interface->next;
174 if (*list != NULL) {
175 while (tmp->next != NULL)
176 tmp = tmp->next;
177 tmp->next = interface;
179 else {
180 *list = interface;
187 * openscop_interface_malloc function:
188 * This function allocates the memory space for a openscop_interface_t
189 * structure and sets its fields with default values. Then it returns a
190 * pointer to the allocated space.
191 * \return A pointer to an empty interface structure with fields set to
192 * default values.
194 openscop_interface_p openscop_interface_malloc() {
195 openscop_interface_p interface;
197 OPENSCOP_malloc(interface, openscop_interface_p,
198 sizeof(openscop_interface_t));
199 interface->URI = NULL;
200 interface->idump = NULL;
201 interface->dump = NULL;
202 interface->sprint = NULL;
203 interface->sread = NULL;
204 interface->malloc = NULL;
205 interface->free = NULL;
206 interface->clone = NULL;
207 interface->equal = NULL;
208 interface->next = NULL;
210 return interface;
215 * openscop_interface_free function:
216 * this function frees the allocated memory for an openscop_interface_t
217 * structure, and all the interfaces stored in the list.
218 * \param[in] id The pointer to the interface we want to free.
220 void openscop_interface_free(openscop_interface_p interface) {
221 openscop_interface_p tmp;
223 if (interface == NULL)
224 return;
226 while (interface != NULL) {
227 tmp = interface->next;
228 if (interface->URI != NULL)
229 free(interface->URI);
230 free(interface);
231 interface = tmp;
236 /*+***************************************************************************
237 * Processing functions *
238 *****************************************************************************/
242 * openscop_interface_nclone function:
243 * This function builds and returns a "hard copy" (not a pointer copy) of the
244 * n first elements of an openscop_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 openscop_interface_p openscop_interface_nclone(openscop_interface_p interface,
250 int n) {
251 openscop_interface_p clone = NULL, new;
252 int i = 0;
254 while ((interface != NULL) && ((n == -1) || (i < n))) {
255 new = openscop_interface_malloc();
256 new->URI = strdup(interface->URI);
257 new->idump = interface->idump;
258 new->dump = interface->dump;
259 new->sprint = interface->sprint;
260 new->sread = interface->sread;
261 new->malloc = interface->malloc;
262 new->free = interface->free;
263 new->clone = interface->clone;
264 new->equal = interface->equal;
265 openscop_interface_add(&clone, new);
266 interface = interface->next;
267 i++;
270 return clone;
275 * openscop_interface_clone function:
276 * This function builds and returns a "hard copy" (not a pointer copy) of an
277 * openscop_interface_t data structure.
278 * \param interface The pointer to the interface structure we want to copy.
279 * \return A pointer to the copy of the interface structure.
281 openscop_interface_p
282 openscop_interface_clone(openscop_interface_p interface) {
284 return openscop_interface_nclone(interface, -1);
289 * openscop_interface_equal function:
290 * this function returns true if the two interface structures are the same,
291 * (content-wise) false otherwise.
292 * \param interface1 The first interface structure.
293 * \param interface2 The second interface structure.
294 * \return 1 if interface1 and interface2 are the same, 0 otherwise.
296 int openscop_interface_equal(openscop_interface_p interface1,
297 openscop_interface_p interface2) {
299 if (interface1 == interface2)
300 return 1;
302 if (((interface1 == NULL) && (interface2 != NULL)) ||
303 ((interface1 != NULL) && (interface2 == NULL)))
304 return 0;
306 if (strcmp(interface1->URI, interface2->URI) ||
307 (interface1->idump != interface2->idump) ||
308 (interface1->dump != interface2->dump) ||
309 (interface1->sprint != interface2->sprint) ||
310 (interface1->sread != interface2->sread) ||
311 (interface1->malloc != interface2->malloc) ||
312 (interface1->free != interface2->free) ||
313 (interface1->clone != interface2->clone) ||
314 (interface1->equal != interface2->equal))
315 return 0;
317 return 1;
322 * openscop_interface_lookup function:
323 * this function returns the first interface with a given URI in the
324 * interface list provided as parameter and NULL if it doesn't find such
325 * an interface.
326 * \param list The interface list where to search a given interface URI.
327 * \param URI The URI of the interface we are looking for.
328 * \return The first interface of the requested URI in the list.
330 openscop_interface_p
331 openscop_interface_lookup(openscop_interface_p list, char * URI) {
332 while (list != NULL) {
333 if ((list->URI != NULL) && (!strcmp(list->URI, URI)))
334 return list;
336 list = list->next;
339 return NULL;