2 /*+-----------------------------------------------------------------**
4 **-----------------------------------------------------------------**
5 ** extensions/textual.c **
6 **-----------------------------------------------------------------**
7 ** First version: 15/17/2010 **
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 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
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 *
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. *
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. *
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> *
61 *****************************************************************************/
67 #include <osl/macros.h>
69 #include <osl/interface.h>
70 #include <osl/extensions/textual.h>
73 /* CAUTION : TEXTUAL IS A VERY SPECIAL CASE: DO NOT USE IT AS AN EXAMPLE !!! */
76 /*+***************************************************************************
77 * Structure display function *
78 *****************************************************************************/
82 * osl_textual_idump function:
83 * this function displays an osl_textual_t structure (*textual) into a
84 * file (file, possibly stdout) in a way that trends to be understandable. It
85 * includes an indentation level (level) in order to work with others
87 * \param[in] file The file where the information has to be printed.
88 * \param[in] textual The textual structure to be printed.
89 * \param[in] level Number of spaces before printing, for each line.
91 void osl_textual_idump(FILE * file
, osl_textual_p textual
, int level
) {
95 // Go to the right level.
96 for (j
= 0; j
< level
; j
++)
99 if (textual
!= NULL
) {
100 fprintf(file
, "+-- osl_textual_t: ");
102 // Display the textual message (without any carriage return).
103 OSL_strdup(tmp
, textual
->textual
);
104 for (j
= 0; j
< (int)strlen(tmp
); j
++)
108 if (strlen(tmp
) > 40) {
109 for (j
= 0; j
< 20; j
++)
110 fprintf(file
, "%c", tmp
[j
]);
111 fprintf(file
, " ... ");
112 for (j
= (int)strlen(tmp
) - 20; j
< (int)strlen(tmp
); j
++)
113 fprintf(file
, "%c", tmp
[j
]);
117 fprintf(file
,"%s\n", tmp
);
122 fprintf(file
, "+-- NULL textual\n");
126 for (j
= 0; j
<= level
; j
++)
127 fprintf(file
, "|\t");
133 * osl_textual_dump function:
134 * this function prints the content of an osl_textual_t structure
135 * (*textual) into a file (file, possibly stdout).
136 * \param[in] file The file where the information has to be printed.
137 * \param[in] textual The textual structure to be printed.
139 void osl_textual_dump(FILE * file
, osl_textual_p textual
) {
140 osl_textual_idump(file
, textual
, 0);
147 * osl_textual_sprint function:
148 * this function prints the content of an osl_textual_t structure
149 * (*textual) into a string (returned) in the OpenScop textual format.
150 * \param[in] textual The textual structure to be printed.
151 * \return A string containing the OpenScop dump of the textual structure.
153 char * osl_textual_sprint(osl_textual_p textual
) {
154 char * string
= NULL
;
156 if ((textual
!= NULL
) && (textual
->textual
!= NULL
)) {
157 if (strlen(textual
->textual
) > OSL_MAX_STRING
)
158 OSL_error("textual too long");
160 string
= strdup(textual
->textual
);
162 OSL_error("memory overflow");
169 * osl_textual_sprint function:
170 * this function returns NULL. This is part of the special behavior of
171 * the textual option (printing it along with other options would double
173 * \param[in] textual The textual structure to be printed.
176 char * osl_textual_sprint(osl_textual_p textual
) {
183 /*****************************************************************************
185 *****************************************************************************/
189 * osl_textual_sread function:
190 * this function reads a textual structure from a string complying to the
191 * OpenScop textual format and returns a pointer to this textual structure.
192 * The string should contain only one textual format of a textual structure.
193 * \param[in,out] extensions The input string where to find a textual struct.
194 * Updated to the position after what has been read.
195 * \return A pointer to the textual structure that has been read.
197 osl_textual_p
osl_textual_sread(char ** extensions
) {
198 osl_textual_p textual
= NULL
;
200 if (*extensions
!= NULL
) {
201 textual
= osl_textual_malloc();
202 OSL_strdup(textual
->textual
, *extensions
);
204 // Update the input string pointer to the end of the string (since
205 // everything has been read).
206 *extensions
= *extensions
+ strlen(*extensions
);
213 /*+***************************************************************************
214 * Memory allocation/deallocation function *
215 *****************************************************************************/
219 * osl_textual_malloc function:
220 * this function allocates the memory space for an osl_textual_t
221 * structure and sets its fields with default values. Then it returns a
222 * pointer to the allocated space.
223 * \return A pointer to an empty textual structure with fields set to
226 osl_textual_p
osl_textual_malloc() {
227 osl_textual_p textual
;
229 OSL_malloc(textual
, osl_textual_p
, sizeof(osl_textual_t
));
230 textual
->textual
= NULL
;
237 * osl_textual_free function:
238 * this function frees the allocated memory for an osl_textual_t
240 * \param[in,out] textual The pointer to the textual structure to be freed.
242 void osl_textual_free(osl_textual_p textual
) {
243 if (textual
!= NULL
) {
244 if(textual
->textual
!= NULL
)
245 free(textual
->textual
);
251 /*+***************************************************************************
252 * Processing functions *
253 *****************************************************************************/
257 * osl_textual_clone function:
258 * this function builds and returns a "hard copy" (not a pointer copy) of an
259 * osl_textual_t data structure.
260 * \param[in] textual The pointer to the textual structure we want to clone.
261 * \return A pointer to the clone of the textual structure.
263 osl_textual_p
osl_textual_clone(osl_textual_p textual
) {
269 clone
= osl_textual_malloc();
270 OSL_strdup(clone
->textual
, textual
->textual
);
278 * osl_textual_equal function:
279 * this function returns true if the two textual structures are the same
280 * (content-wise), false otherwise.
281 * \param f1 The first textual structure.
282 * \param ff The second textual structure.
283 * \return 1 if f1 and f2 are the same (content-wise), 0 otherwise.
285 int osl_textual_equal(osl_textual_p f1
, osl_textual_p f2
) {
290 if (((f1
== NULL
) && (f2
!= NULL
)) || ((f1
!= NULL
) && (f2
== NULL
)))
293 if (strcmp(f1
->textual
, f2
->textual
))
300 * osl_textual_equal function:
301 * this function returns 1. This is part of the special behavior of
302 * the textual option (the text string can be easily different while the
303 * options are actually identical.
304 * \param[in] f1 The first textual structure.
305 * \param[in] f2 The second textual structure.
308 int osl_textual_equal(osl_textual_p f1
, osl_textual_p f2
) {
316 * osl_textual_interface function:
317 * this function creates an interface structure corresponding to the textual
318 * extension and returns it).
319 * \return An interface structure for the textual extension.
321 osl_interface_p
osl_textual_interface() {
322 osl_interface_p interface
= osl_interface_malloc();
324 OSL_strdup(interface
->URI
, OSL_URI_TEXTUAL
);
325 interface
->idump
= (osl_idump_f
)osl_textual_idump
;
326 interface
->sprint
= (osl_sprint_f
)osl_textual_sprint
;
327 interface
->sread
= (osl_sread_f
)osl_textual_sread
;
328 interface
->malloc
= (osl_malloc_f
)osl_textual_malloc
;
329 interface
->free
= (osl_free_f
)osl_textual_free
;
330 interface
->clone
= (osl_clone_f
)osl_textual_clone
;
331 interface
->equal
= (osl_equal_f
)osl_textual_equal
;