From 783280c7a7ca0e26af3c3a34c615c1c722c955ba Mon Sep 17 00:00:00 2001 From: Cedric Bastoul Date: Tue, 10 Jan 2012 18:13:36 +0100 Subject: [PATCH] Introduce coordinates extension --- doc/openscop.texi | 69 +++++- include/Makefile.am | 2 +- include/osl/extensions/{lines.h => coordinates.h} | 48 ++-- include/osl/macros.h | 2 +- include/osl/osl.h | 4 +- include/osl/util.h | 1 + source/Makefile.am | 36 +-- source/body.c | 24 +- source/extensions/comment.c | 1 + source/extensions/{lines.c => coordinates.c} | 257 ++++++++++++---------- source/interface.c | 4 +- source/util.c | 42 +++- tests/Makefile.am | 1 + tests/test_coordinates.scop | 32 +++ 14 files changed, 338 insertions(+), 185 deletions(-) rename include/osl/extensions/{lines.h => coordinates.h} (80%) rename source/extensions/{lines.c => coordinates.c} (53%) create mode 100644 tests/test_coordinates.scop diff --git a/doc/openscop.texi b/doc/openscop.texi index c7969da..8e72a64 100644 --- a/doc/openscop.texi +++ b/doc/openscop.texi @@ -2416,7 +2416,7 @@ particular, the sender fully accepts the license and copyright notice. * Comment Extension:: * Arrays Extension:: * Scatnames Extension:: -* Lines Extension:: +* Coordinates Extension:: * Irregular Extension:: @end menu @@ -2584,8 +2584,71 @@ string @code{names[i]} and identifier @code{id[i]}. @c --------------------------------------------------------------------------- -@node Lines Extension -@subsection Lines Extension +@node Coordinates Extension +@subsection Coordinates Extension + +@noindent @strong{Description} +@itemize @bullet +@item URI: @code{coordinates}. +@item Author: C@'edric Bastoul . +@item Purpose: the @code{coordinates} extension provides the information +about the SCoP location in the original code: the original file name/path, +the starting and ending lines of the SCoP in this file (inclusives) and +the indentation level. +@end itemize + +@noindent @strong{File Format} + +@noindent The @code{coordinates} extension file format respects the following +grammar: +@example +Coordinates_generic ::= "" Coordinates "" +Coordinates ::= File_name Start_line End_line Indentation +File_name ::= _String +Start_line ::= _Integer +End_line ::= _Integer +Indentation ::= _Integer +@end example + +@noindent The original file name where the SCoP has been extracted is +provided on the first line, then the starting line number of the SCoP, +then the ending line number of the SCoP, and lastly the indentation level +(the number of spaces characters each line of the SCoP starts with). +For instance, the following example is a correct textual +@code{coordinates} extension: + +@example +@group + +# File name +./test/ax-do.c +# Starting line +9 +# Ending line +15 +# Indentation +2 + +@end group +@end example + +@noindent @strong{Data Structure} + +@noindent The @code{coordinates} extension data structure is the following: + +@example +@group +struct osl_coordinates @{ + char * name; /* File name */ + int start; /* First line of the SCoP in the source file */ + int end; /* Last line of the SCoP in the source file */ + int indent; /* Indentation */ +@}; +typedef struct osl_coordinates osl_coordinates_t; +typedef struct osl_coordinates * osl_coordinates_p; +@end group +@end example + @c --------------------------------------------------------------------------- diff --git a/include/Makefile.am b/include/Makefile.am index 52305c8..f9e69e7 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -97,6 +97,6 @@ pkgextensionsinclude_HEADERS = \ osl/extensions/comment.h \ osl/extensions/scatnames.h \ osl/extensions/arrays.h \ - osl/extensions/lines.h \ + osl/extensions/coordinates.h \ osl/extensions/irregular.h diff --git a/include/osl/extensions/lines.h b/include/osl/extensions/coordinates.h similarity index 80% rename from include/osl/extensions/lines.h rename to include/osl/extensions/coordinates.h index 9695e51..f417c70 100644 --- a/include/osl/extensions/lines.h +++ b/include/osl/extensions/coordinates.h @@ -2,7 +2,7 @@ /*+-----------------------------------------------------------------** ** OpenScop Library ** **-----------------------------------------------------------------** - ** extensions/lines.h ** + ** extensions/coordinates.h ** **-----------------------------------------------------------------** ** First version: 07/12/2010 ** **-----------------------------------------------------------------** @@ -61,8 +61,8 @@ *****************************************************************************/ -#ifndef OSL_LINES_H -# define OSL_LINES_H +#ifndef OSL_COORDINATES_H +# define OSL_COORDINATES_H # include # include @@ -73,55 +73,55 @@ extern "C" # endif -# define OSL_URI_LINES "lines" -# define OSL_TAG_LINES_START "<"OSL_URI_LINES">" -# define OSL_TAG_LINES_STOP "" +# define OSL_URI_COORDINATES "coordinates" /** - * The osl_lines_t structure stores a lines extention to the core - * OpenScop representation. It provides information about the line - * numbers of the SCoP in the original source file. + * The osl_coordinates_t structure stores a coordinates extention to the core + * OpenScop representation. It provides information about the SCoP location + * (file name, starting and ending lines, indentation level). */ -struct osl_lines { - int start; /**< First line of the SCoP in the original source file. */ - int end; /**< Last line of the SCoP in the original source file. */ +struct osl_coordinates { + char * name; /**< File name (may include the fils path as well). */ + int start; /**< First line of the SCoP in the original source file. */ + int end; /**< Last line of the SCoP in the original source file. */ + int indent; /**< Indentation (number of spaces starting each line). */ }; -typedef struct osl_lines osl_lines_t; -typedef struct osl_lines * osl_lines_p; +typedef struct osl_coordinates osl_coordinates_t; +typedef struct osl_coordinates * osl_coordinates_p; /*+*************************************************************************** * Structure display function * *****************************************************************************/ -void osl_lines_idump(FILE *, osl_lines_p, int); -void osl_lines_dump(FILE *, osl_lines_p); -char * osl_lines_sprint(osl_lines_p); +void osl_coordinates_idump(FILE *, osl_coordinates_p, int); +void osl_coordinates_dump(FILE *, osl_coordinates_p); +char * osl_coordinates_sprint(osl_coordinates_p); /***************************************************************************** * Reading function * *****************************************************************************/ -osl_lines_p osl_lines_sread(char **); +osl_coordinates_p osl_coordinates_sread(char **); /*+*************************************************************************** * Memory allocation/deallocation function * *****************************************************************************/ -osl_lines_p osl_lines_malloc(); -void osl_lines_free(osl_lines_p); +osl_coordinates_p osl_coordinates_malloc(); +void osl_coordinates_free(osl_coordinates_p); /*+*************************************************************************** * Processing functions * *****************************************************************************/ -osl_lines_p osl_lines_clone(osl_lines_p); -int osl_lines_equal(osl_lines_p, osl_lines_p); -osl_interface_p osl_lines_interface(); +osl_coordinates_p osl_coordinates_clone(osl_coordinates_p); +int osl_coordinates_equal(osl_coordinates_p, osl_coordinates_p); +osl_interface_p osl_coordinates_interface(); # if defined(__cplusplus) } # endif -#endif /* define OSL_LINES_H */ +#endif /* define OSL_COORDINATES_H */ diff --git a/include/osl/macros.h b/include/osl/macros.h index 536ed91..11a41c0 100644 --- a/include/osl/macros.h +++ b/include/osl/macros.h @@ -64,7 +64,7 @@ #ifndef OSL_MACROS_H # define OSL_MACROS_H -# define OSL_DEBUG 0 // 1 for debug mode, 0 otherwise. +# define OSL_DEBUG 1 // 1 for debug mode, 0 otherwise. # define OSL_TAG_START_SCOP "" # define OSL_TAG_END_SCOP "" diff --git a/include/osl/osl.h b/include/osl/osl.h index 9853e4d..b86db9c 100644 --- a/include/osl/osl.h +++ b/include/osl/osl.h @@ -72,7 +72,7 @@ * - int * - interface * - irregular - * - lines + * - coordinates * - openscop * - relation * - relation_list @@ -98,7 +98,7 @@ # include # include # include -# include +# include # include # include diff --git a/include/osl/util.h b/include/osl/util.h index e0dec59..4bc973f 100644 --- a/include/osl/util.h +++ b/include/osl/util.h @@ -80,6 +80,7 @@ char * osl_util_skip_blank_and_comments(FILE *, char *); void osl_util_sskip_blank_and_comments(char **); int osl_util_read_int(FILE *, char **); char * osl_util_read_string(FILE *, char **); +char * osl_util_read_line(FILE *, char **); char * osl_util_read_tag(FILE *, char **); char * osl_util_read_tail(FILE *); char * osl_util_read_uptotag(FILE *, char *); diff --git a/source/Makefile.am b/source/Makefile.am index 0a804f1..bb4e3a6 100644 --- a/source/Makefile.am +++ b/source/Makefile.am @@ -82,24 +82,24 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) \ lib_LTLIBRARIES = libosl.la -libosl_la_SOURCES = \ - scop.c \ - statement.c \ - extensions/textual.c \ - extensions/comment.c \ - extensions/scatnames.c \ - extensions/arrays.c \ - extensions/lines.c \ - extensions/irregular.c \ - interface.c \ - generic.c \ - relation.c \ - relation_list.c \ - vector.c \ - names.c \ - strings.c \ - body.c \ - int.c \ +libosl_la_SOURCES = \ + scop.c \ + statement.c \ + extensions/textual.c \ + extensions/comment.c \ + extensions/scatnames.c \ + extensions/arrays.c \ + extensions/coordinates.c \ + extensions/irregular.c \ + interface.c \ + generic.c \ + relation.c \ + relation_list.c \ + vector.c \ + names.c \ + strings.c \ + body.c \ + int.c \ util.c AM_CFLAGS = -Wall -fomit-frame-pointer -g diff --git a/source/body.c b/source/body.c index c627234..da721f3 100644 --- a/source/body.c +++ b/source/body.c @@ -245,28 +245,10 @@ osl_body_p osl_body_sread(char ** input) { } // Read the body: - // - Skip blank/commented lines and spaces before the body. - osl_util_sskip_blank_and_comments(input); - - // - Remove the comments after the body. - expression = *input; - while (*input && **input != '#' && **input != '\n') - (*input)++; - - if (*input && **input == '#') { - **input = '\0'; - while (**input != '\n') - (*input)++; - } - else { - if (*input && **input == '\n') { - **input = '\0'; - (*input)++; - } - } + expression = osl_util_read_line(NULL, input); - // - Copy the body. - body->expression = osl_strings_encapsulate(strdup(expression)); + // Insert the body. + body->expression = osl_strings_encapsulate(expression); } return body; diff --git a/source/extensions/comment.c b/source/extensions/comment.c index 2db62df..907d4fa 100644 --- a/source/extensions/comment.c +++ b/source/extensions/comment.c @@ -163,6 +163,7 @@ char * osl_comment_sprint(osl_comment_p comment) { * Reading function * *****************************************************************************/ + /** * osl_comment_sread function: * this function reads a comment structure from a string complying to the diff --git a/source/extensions/lines.c b/source/extensions/coordinates.c similarity index 53% rename from source/extensions/lines.c rename to source/extensions/coordinates.c index 2c1817a..4e5107d 100644 --- a/source/extensions/lines.c +++ b/source/extensions/coordinates.c @@ -2,7 +2,7 @@ /*+-----------------------------------------------------------------** ** OpenScop Library ** **-----------------------------------------------------------------** - ** extensions/lines.c ** + ** extensions/coordinates.c ** **-----------------------------------------------------------------** ** First version: 07/12/2010 ** **-----------------------------------------------------------------** @@ -67,7 +67,7 @@ #include #include #include -#include +#include /*+*************************************************************************** @@ -76,34 +76,53 @@ /** - * osl_lines_idump function: - * this function displays an osl_lines_t structure (*lines) into a + * osl_coordinates_idump function: + * this function displays an osl_coordinates_t structure (*coordinates) into a * file (file, possibly stdout) in a way that trends to be understandable. It * includes an indentation level (level) in order to work with others * idump functions. - * \param file The file where the information has to be printed. - * \param lines The lines structure whose information has to be printed. - * \param level Number of spaces before printing, for each line. + * \param file The file where the information has to be printed. + * \param coordinates The coordinates structure to print. + * \param level Number of spaces before printing, for each line. */ -void osl_lines_idump(FILE * file, osl_lines_p lines, int level) { +void osl_coordinates_idump(FILE * file, osl_coordinates_p coordinates, + int level) { int j; // Go to the right level. for (j = 0; j < level; j++) fprintf(file, "|\t"); - if (lines != NULL) - fprintf(file, "+-- osl_lines_t\n"); + if (coordinates != NULL) + fprintf(file, "+-- osl_coordinates_t\n"); else - fprintf(file, "+-- NULL lines\n"); + fprintf(file, "+-- NULL coordinates\n"); + + if (coordinates != NULL) { + // Go to the right level. + for(j = 0; j <= level; j++) + fprintf(file, "|\t"); + + // Display the file name. + if (coordinates->name != NULL) + fprintf(file, "File name__: %s\n", coordinates->name); + else + fprintf(file, "NULL file name\n"); + + // Go to the right level. + for(j = 0; j <= level; j++) + fprintf(file, "|\t"); + + // Display the lines. + fprintf(file, "Lines______: [%d, %d]\n", + coordinates->start, coordinates->end); - if (lines != NULL) { // Go to the right level. for(j = 0; j <= level; j++) fprintf(file, "|\t"); - // Display the lines content. - fprintf(file, "lines: %d - %d\n", lines->start,lines->end); + // Display the indentation. + fprintf(file, "Indentation: %d\n", coordinates->indent); } // The last line. @@ -114,49 +133,48 @@ void osl_lines_idump(FILE * file, osl_lines_p lines, int level) { /** - * osl_lines_dump function: - * this function prints the content of an osl_lines_t structure - * (*lines) into a file (file, possibly stdout). - * \param file The file where the information has to be printed. - * \param lines The lines structure whose information has to be printed. + * osl_coordinates_dump function: + * this function prints the content of an osl_coordinates_t structure + * (*coordinates) into a file (file, possibly stdout). + * \param file The file where the information has to be printed. + * \param coordinates The coordinates structure to print. */ -void osl_lines_dump(FILE * file, osl_lines_p lines) { - osl_lines_idump(file, lines, 0); +void osl_coordinates_dump(FILE * file, osl_coordinates_p coordinates) { + osl_coordinates_idump(file, coordinates, 0); } /** - * osl_lines_sprint function: - * this function prints the content of an osl_lines_t structure - * (*lines) into a string (returned) in the OpenScop textual format. - * \param lines The lines structure whose information has to be printed. - * \return A string containing the OpenScop dump of the lines structure. + * osl_coordinates_sprint function: + * this function prints the content of an osl_coordinates_t structure + * (*coordinates) into a string (returned) in the OpenScop textual format. + * \param coordinates The coordinates structure to be print. + * \return A string containing the OpenScop dump of the coordinates structure. */ -char * osl_lines_sprint(osl_lines_p lines) { +char * osl_coordinates_sprint(osl_coordinates_p coordinates) { int high_water_mark = OSL_MAX_STRING; char * string = NULL; - char * buffer; + char buffer[OSL_MAX_STRING]; - if (lines != NULL) { + if (coordinates != NULL) { OSL_malloc(string, char *, high_water_mark * sizeof(char)); - OSL_malloc(buffer, char *, OSL_MAX_STRING * sizeof(char)); string[0] = '\0'; - // Print the begin tag. - sprintf(buffer, OSL_TAG_LINES_START); + // Print the coordinates content. + sprintf(buffer, "# File name\n%s\n", coordinates->name); osl_util_safe_strcat(&string, buffer, &high_water_mark); - // Print the lines content. - sprintf(buffer, "\n%d - %d\n", lines->start, lines->end); + sprintf(buffer, "# Starting line\n%d\n", coordinates->start); osl_util_safe_strcat(&string, buffer, &high_water_mark); - // Print the end tag. - sprintf(buffer, OSL_TAG_LINES_STOP"\n"); + sprintf(buffer, "# Ending line\n%d\n", coordinates->end); + osl_util_safe_strcat(&string, buffer, &high_water_mark); + + sprintf(buffer, "# Indentation\n%d\n", coordinates->indent); osl_util_safe_strcat(&string, buffer, &high_water_mark); // Keep only the memory space we need. OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char)); - free(buffer); } return string; @@ -167,43 +185,41 @@ char * osl_lines_sprint(osl_lines_p lines) { * Reading function * *****************************************************************************/ + /** - * osl_lines_sread function: - * this function reads a lines structure from a string complying to the - * OpenScop textual format and returns a pointer to this lines structure. - * The string should contain only one textual format of a lines structure. - * \param extensions The input string where to find a lines structure. - * \return A pointer to the lines structure that has been read. + * osl_coordinates_sread function: + * this function reads a coordinates structure from a string complying to the + * OpenScop textual format and returns a pointer to this structure. + * The input parameter is updated to the position in the input string this + * function reach right after reading the coordinates structure. + * \param[in,out] input The input string where to find coordinates. + * Updated to the position after what has been read. + * \return A pointer to the coordinates structure that has been read. */ -osl_lines_p osl_lines_sread(char ** extensions_fixme) { - char * content, *tmp; - osl_lines_p lines; +osl_coordinates_p osl_coordinates_sread(char ** input) { + osl_coordinates_p coordinates; - // FIXME: this is a quick and dirty thing to accept char ** instead - // of char * in the parameter: really do it and update the - // pointer to after what has been read. - content = *extensions_fixme; - - if (content == NULL) { - OSL_debug("no lines optional tag"); + if (*input == NULL) { + OSL_debug("no coordinates optional tag"); return NULL; } - if (strlen(content) > OSL_MAX_STRING) - OSL_error("lines too long"); - - lines = osl_lines_malloc(); - tmp = strtok(content," -"); - lines->start = atoi(tmp); - if(lines->start == -1) - OSL_error("lines start NaN"); + // Build the coordinates structure. + coordinates = osl_coordinates_malloc(); - tmp = strtok(NULL," -"); - lines->end = atoi(tmp); - if(lines->end == -1) - OSL_error("lines end NaN"); + // Read the file name (and path). + coordinates->name = osl_util_read_line(NULL, input); + + // Read the number of the starting line. + coordinates->start = osl_util_read_int(NULL, input); + + // Read the number of the ending line. + coordinates->end = osl_util_read_int(NULL, input); + + // Read the indentation level. + coordinates->indent = osl_util_read_int(NULL, input); - return lines; + return coordinates; } @@ -213,33 +229,36 @@ osl_lines_p osl_lines_sread(char ** extensions_fixme) { /** - * osl_lines_malloc function: - * This function allocates the memory space for an osl_lines_t + * osl_coordinates_malloc function: + * this function allocates the memory space for an osl_coordinates_t * structure and sets its fields with default values. Then it returns a * pointer to the allocated space. - * \return A pointer to an empty lines structure with fields set to + * \return A pointer to an empty coordinates structure with fields set to * default values. */ -osl_lines_p osl_lines_malloc() { - osl_lines_p lines; +osl_coordinates_p osl_coordinates_malloc() { + osl_coordinates_p coordinates; - OSL_malloc(lines, osl_lines_p, sizeof(osl_lines_t)); - lines->start = OSL_UNDEFINED; - lines->end = OSL_UNDEFINED; + OSL_malloc(coordinates, osl_coordinates_p, sizeof(osl_coordinates_t)); + coordinates->name = NULL; + coordinates->start = OSL_UNDEFINED; + coordinates->end = OSL_UNDEFINED; + coordinates->indent = OSL_UNDEFINED; - return lines; + return coordinates; } /** - * osl_lines_free function: - * This function frees the allocated memory for an osl_lines_t + * osl_coordinates_free function: + * this function frees the allocated memory for an osl_coordinates_t * structure. - * \param lines The pointer to the lines structure we want to free. + * \param coordinates The pointer to the coordinates structure to free. */ -void osl_lines_free(osl_lines_p lines) { - if (lines != NULL) { - free(lines); +void osl_coordinates_free(osl_coordinates_p coordinates) { + if (coordinates != NULL) { + free(coordinates->name); + free(coordinates); } } @@ -250,65 +269,79 @@ void osl_lines_free(osl_lines_p lines) { /** - * osl_lines_clone function: - * This function builds and returns a "hard copy" (not a pointer copy) of an - * osl_lines_t data structure. - * \param lines The pointer to the lines structure we want to copy. - * \return A pointer to the copy of the lines structure. + * osl_coordinates_clone function: + * this function builds and returns a "hard copy" (not a pointer copy) of an + * osl_coordinates_t data structure. + * \param coordinates The pointer to the coordinates structure to clone. + * \return A pointer to the clone of the coordinates structure. */ -osl_lines_p osl_lines_clone(osl_lines_p lines) { - osl_lines_p copy; +osl_coordinates_p osl_coordinates_clone(osl_coordinates_p coordinates) { + osl_coordinates_p clone; - if (lines == NULL) + if (coordinates == NULL) return NULL; - copy = osl_lines_malloc(); - copy->start = lines->start; - copy->end = lines->end; + clone = osl_coordinates_malloc(); + OSL_strdup(clone->name, coordinates->name); + clone->start = coordinates->start; + clone->end = coordinates->end; + clone->indent = coordinates->indent; - return copy; + return clone; } /** - * osl_lines_equal function: - * this function returns true if the two lines structures are the same - * (content-wise), false otherwise. This functions considers two lines - * \param c1 The first lines structure. - * \param c2 The second lines structure. + * osl_coordinates_equal function: + * this function returns true if the two coordinates structures are the same + * (content-wise), false otherwise. This functions considers two coordinates + * \param c1 The first coordinates structure. + * \param c2 The second coordinates structure. * \return 1 if c1 and c2 are the same (content-wise), 0 otherwise. */ -int osl_lines_equal(osl_lines_p c1, osl_lines_p c2) { +int osl_coordinates_equal(osl_coordinates_p c1, osl_coordinates_p c2) { if (c1 == c2) return 1; if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL))) return 0; - if ((c1->start != c2->start) || (c1->end != c2->end)) + if (strcmp(c1->name, c2->name)) { + OSL_info("file names are not the same"); + return 0; + } + + if (c1->start != c2->start) { + OSL_info("starting lines are not the same"); + return 0; + } + + if (c1->indent != c2->indent) { + OSL_info("indentations are not the same"); return 0; + } return 1; } /** - * osl_lines_interface function: - * this function creates an interface structure corresponding to the lines + * osl_coordinates_interface function: + * this function creates an interface structure corresponding to the coordinates * extension and returns it). - * \return An interface structure for the lines extension. + * \return An interface structure for the coordinates extension. */ -osl_interface_p osl_lines_interface() { +osl_interface_p osl_coordinates_interface() { osl_interface_p interface = osl_interface_malloc(); - interface->URI = strdup(OSL_URI_LINES); - interface->idump = (osl_idump_f)osl_lines_idump; - interface->sprint = (osl_sprint_f)osl_lines_sprint; - interface->sread = (osl_sread_f)osl_lines_sread; - interface->malloc = (osl_malloc_f)osl_lines_malloc; - interface->free = (osl_free_f)osl_lines_free; - interface->clone = (osl_clone_f)osl_lines_clone; - interface->equal = (osl_equal_f)osl_lines_equal; + interface->URI = strdup(OSL_URI_COORDINATES); + interface->idump = (osl_idump_f)osl_coordinates_idump; + interface->sprint = (osl_sprint_f)osl_coordinates_sprint; + interface->sread = (osl_sread_f)osl_coordinates_sread; + interface->malloc = (osl_malloc_f)osl_coordinates_malloc; + interface->free = (osl_free_f)osl_coordinates_free; + interface->clone = (osl_clone_f)osl_coordinates_clone; + interface->equal = (osl_equal_f)osl_coordinates_equal; return interface; } diff --git a/source/interface.c b/source/interface.c index 97c1035..8c0a884 100644 --- a/source/interface.c +++ b/source/interface.c @@ -67,7 +67,7 @@ #include #include #include -#include +#include #include #include #include @@ -366,7 +366,7 @@ osl_interface_p osl_interface_get_default_registry() { osl_interface_add(®istry, osl_comment_interface()); osl_interface_add(®istry, osl_scatnames_interface()); osl_interface_add(®istry, osl_arrays_interface()); - //osl_interface_add(®istry, osl_lines_interface()); + osl_interface_add(®istry, osl_coordinates_interface()); //osl_interface_add(®istry, osl_irregular_interface()); return registry; diff --git a/source/util.c b/source/util.c index b3961d9..fe4b4d1 100644 --- a/source/util.c +++ b/source/util.c @@ -86,7 +86,7 @@ * \param[in] file The (opened) file to read. * \param[in] str Address of an allocated space to store the first line * that contains useful information. - * \return The address of the the first useful digit in str. + * \return The address of the first useful digit in str. */ char * osl_util_skip_blank_and_comments(FILE * file, char * str) { char * start; @@ -212,6 +212,46 @@ char * osl_util_read_string(FILE * file, char ** str) { /** + * osl_util_read_line function: + * reads a line on the input 'file' or the input string 'str' depending on + * which one is not NULL (exactly one of them must not be NULL). A line + * is defined as the array of characters before the comment tag or the end of + * line (it may include spaces). + * \param[in] file The file where to read a line (if not NULL). + * \param[in,out] str The string where to read a line (if not NULL). This + * pointer is updated to reflect the read and points + * after the line in the input string. + * \return The line that has been read. + */ +char * osl_util_read_line(FILE * file, char ** str) { + char s[OSL_MAX_STRING], * start; + char * res; + int i = 0; + + if ((file != NULL && str != NULL) || (file == NULL && str == NULL)) + OSL_error("one and only one of the two parameters can be non-NULL"); + + OSL_malloc(res, char *, OSL_MAX_STRING * sizeof(char)); + if (file != NULL) { + // Parse from a file. + start = osl_util_skip_blank_and_comments(file, s); + while (*start && *start != '\n' && *start != '#' && i < OSL_MAX_STRING) + res[i++] = (*start)++; + } + else { + // Parse from a string. + osl_util_sskip_blank_and_comments(str); + while (**str && **str != '\n' && **str != '#' && i < OSL_MAX_STRING) + res[i++] = *((*str)++); + } + + res[i] = '\0'; + OSL_realloc(res, char *, strlen(res) + 1); + return res; +} + + +/** * osl_util_read_int internal function: * reads a tag (the form of a tag with name "name" is \) on the input * 'file' or the input string 'str' depending on which one is not NULL (exactly diff --git a/tests/Makefile.am b/tests/Makefile.am index eca952e..42069f4 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -93,6 +93,7 @@ osl_test_SOURCES = \ test_just_body.scop \ test_just_domain.scop \ test_just_scattering.scop \ + test_coordinates.scop \ test_matmult.scop \ test_no_statement.scop \ test_scop_list.scop diff --git a/tests/test_coordinates.scop b/tests/test_coordinates.scop new file mode 100644 index 0000000..bfadfc2 --- /dev/null +++ b/tests/test_coordinates.scop @@ -0,0 +1,32 @@ + + +# =============================================== Global +# Language +C + +# Context +CONTEXT +0 2 0 0 0 0 + +# Parameter names are not provided +0 + +# No statement +0 + +# =============================================== Options + +# File name +./ax-do.c + +# Starting line +9 + +# Ending line +15 + +# Indentation +2 + + + -- 2.11.4.GIT