1
2    /*+-----------------------------------------------------------------**
3     **                       OpenScop Library                          **
4     **-----------------------------------------------------------------**
5     **                    extensions/scatnames.c                       **
6     **-----------------------------------------------------------------**
7     **                   First version: 03/12/2011                     **
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/strings.h>
71#include <osl/extensions/scatnames.h>
72
73
74/*+***************************************************************************
75 *                          Structure display function                       *
76 *****************************************************************************/
77
78
79/**
80 * osl_scatnames_idump function:
81 * this function displays an osl_scatnames_t structure (*scatnames) into a
82 * file (file, possibly stdout) in a way that trends to be understandable. It
83 * includes an indentation level (level) in order to work with others
84 * idump functions.
85 * \param[in] file      The file where the information has to be printed.
86 * \param[in] scatnames Scatnames structure to print.
87 * \param[in] level     Number of spaces before printing, for each line.
88 */
89void osl_scatnames_idump(FILE * file, osl_scatnames_p scatnames, 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 (scatnames != NULL)
97    fprintf(file, "+-- osl_scatnames_t\n");
98  else
99    fprintf(file, "+-- NULL scatnames\n");
100
101  if (scatnames != NULL) {
102    // Go to the right level.
103    for(j = 0; j <= level + 1; j++)
104      fprintf(file, "|\t");
105    fprintf(file, "\n");
106
107    // Display the list of scattering names.
108    osl_strings_idump(file, scatnames->names, level + 1);
109  }
110
111  // The last line.
112  for (j = 0; j <= level; j++)
113    fprintf(file, "|\t");
114  fprintf(file, "\n");
115}
116
117
118/**
119 * osl_scatnames_dump function:
120 * this function prints the content of an osl_scatnames_t structure
121 * (*scatnames) into a file (file, possibly stdout).
122 * \param[in] file      The file where the information has to be printed.
123 * \param[in] scatnames The scatnames structure to print.
124 */
125void osl_scatnames_dump(FILE * file, osl_scatnames_p scatnames) {
126  osl_scatnames_idump(file, scatnames, 0);
127}
128
129
130/**
131 * osl_scatnames_sprint function:
132 * this function prints the content of an osl_scatnames_t structure
133 * (*scatnames) into a string (returned) in the OpenScop textual format.
134 * \param[in] scatnames The scatnames structure to print.
135 * \return A string containing the OpenScop dump of the scatnames structure.
136 */
137char * osl_scatnames_sprint(osl_scatnames_p scatnames) {
138  return osl_strings_sprint(scatnames->names);
139}
140
141
142/*****************************************************************************
143 *                               Reading function                            *
144 *****************************************************************************/
145
146
147/**
148 * osl_scatnames_sread function:
149 * this function reads a scatnames structure from a string complying to the
150 * OpenScop textual format and returns a pointer to this scatnames structure.
151 * The input parameter is updated to the position in the input string this
152 * function reach right after reading the scatnames structure. If there
153 * is nothing to read, the function returns NULL.
154 * \param[in,out] input The input string where to find a scatnames.
155 *                      Updated to the position after what has been read.
156 * \return A pointer to the scatnames structure that has been read.
157 */
158osl_scatnames_p osl_scatnames_sread(char ** input) {
159  osl_scatnames_p scatnames = NULL;
160  osl_strings_p names = NULL;
161
162  if (*input == NULL) {
163    OSL_debug("no scatnames optional tag");
164    return NULL;
165  }
166
167  // Build the scatnames structure
168  names = osl_strings_sread(input);
169  if (names != NULL) {
170    scatnames = osl_scatnames_malloc();
171    scatnames->names = names;
172  }
173
174  return scatnames;
175}
176
177
178/*+***************************************************************************
179 *                    Memory allocation/deallocation function                *
180 *****************************************************************************/
181
182
183/**
184 * osl_scatnames_malloc function:
185 * this function allocates the memory space for an osl_scatnames_t
186 * structure and sets its fields with default values. Then it returns a
187 * pointer to the allocated space.
188 * \return A pointer to an empty scatnames structure with fields set to
189 *         default values.
190 */
191osl_scatnames_p osl_scatnames_malloc() {
192  osl_scatnames_p scatnames;
193
194  OSL_malloc(scatnames, osl_scatnames_p, sizeof(osl_scatnames_t));
195  scatnames->names = NULL;
196
197  return scatnames;
198}
199
200
201/**
202 * osl_scatnames_free function:
203 * this function frees the allocated memory for an osl_scatnames_t
204 * structure.
205 * \param[in,out] scatnames The pointer to the scatnames structure to free.
206 */
207void osl_scatnames_free(osl_scatnames_p scatnames) {
208  if (scatnames != NULL) {
209    osl_strings_free(scatnames->names);
210    free(scatnames);
211  }
212}
213
214
215/*+***************************************************************************
216 *                            Processing functions                           *
217 *****************************************************************************/
218
219
220/**
221 * osl_scatnames_clone function:
222 * This function builds and returns a "hard copy" (not a pointer copy) of an
223 * osl_scatnames_t data structure.
224 * \param[in] scatnames The pointer to the scatnames structure to clone.
225 * \return A pointer to the clone of the scatnames structure.
226 */
227osl_scatnames_p osl_scatnames_clone(osl_scatnames_p scatnames) {
228  osl_scatnames_p clone;
229
230  if (scatnames == NULL)
231    return NULL;
232
233  clone = osl_scatnames_malloc();
234  clone->names = osl_strings_clone(scatnames->names);
235
236  return clone;
237}
238
239
240/**
241 * osl_scatnames_equal function:
242 * this function returns true if the two scatnames structures are the same
243 * (content-wise), false otherwise.
244 * \param[in] s1 The first scatnames structure.
245 * \param[in] s2 The second scatnames structure.
246 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
247 */
248int osl_scatnames_equal(osl_scatnames_p s1, osl_scatnames_p s2) {
249
250  if (s1 == s2)
251    return 1;
252
253  if (((s1 == NULL) && (s2 != NULL)) || ((s1 != NULL) && (s2 == NULL)))
254    return 0;
255
256  if (!osl_strings_equal(s1->names, s2->names))
257    return 0;
258
259  return 1;
260}
261
262
263/**
264 * osl_scatnames_interface function:
265 * this function creates an interface structure corresponding to the scatnames
266 * extension and returns it).
267 * \return An interface structure for the scatnames extension.
268 */
269osl_interface_p osl_scatnames_interface() {
270  osl_interface_p interface = osl_interface_malloc();
271
272  interface->URI    = strdup(OSL_URI_SCATNAMES);
273  interface->idump  = (osl_idump_f)osl_scatnames_idump;
274  interface->sprint = (osl_sprint_f)osl_scatnames_sprint;
275  interface->sread  = (osl_sread_f)osl_scatnames_sread;
276  interface->malloc = (osl_malloc_f)osl_scatnames_malloc;
277  interface->free   = (osl_free_f)osl_scatnames_free;
278  interface->clone  = (osl_clone_f)osl_scatnames_clone;
279  interface->equal  = (osl_equal_f)osl_scatnames_equal;
280
281  return interface;
282}
283