Skip to content
Snippets Groups Projects

Parameter file

Merged James Willis requested to merge Parameter-File into master

First version of parameter file parser. Should be able to:

  • Read integers
  • Read floats
  • Read doubles
  • Read strings
  • Ignore comments that start with '#'
  • Validate that integers are not written as floats in the input file
  • Check whether two values are present on the same line
  • Print the contents of the structure that holds all parameters read from the file

I had to add ../src/parser.c to the Makefile.am in the tests directory to get make check working. Not sure if this is the correct way of doing it.

Merge request reports

Merged by avatar (May 26, 2025 12:10am UTC)

Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • src/parser.c 0 → 100644
    11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13 * GNU General Public License for more details.
    14 *
    15 * You should have received a copy of the GNU Lesser General Public License
    16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17 *
    18 ******************************************************************************/
    19
    20 #include "parser.h"
    21
    22 /* Needs to be included so that strtok returns char * instead of a int *. */
    23 #include <string.h>
    24 #include <stdlib.h>
    25 #include "error.h"
    26
    • Could you follow the same file inclusion policy/style as the other files please ? Especially including config.h is important as it is the file generated by the autotools.

  • src/parser.c 0 → 100644
    13 * GNU General Public License for more details.
    14 *
    15 * You should have received a copy of the GNU Lesser General Public License
    16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17 *
    18 ******************************************************************************/
    19
    20 #include "parser.h"
    21
    22 /* Needs to be included so that strtok returns char * instead of a int *. */
    23 #include <string.h>
    24 #include <stdlib.h>
    25 #include "error.h"
    26
    27 /* Reads an input file and stores each parameter in a structure.*/
    28 void parser_read_file(const char *file_name, struct swift_params *params) {
  • src/parser.c 0 → 100644
    36
    37 if(fp == NULL) {
    38 error("Error opening parameter file: %s",file_name);
    39 }
    40
    41 /* Read until the end of the file is reached.*/
    42 while(!feof(fp)) {
    43 parse_line(fp,params);
    44 }
    45
    46 fclose(fp);
    47 }
    48
    49 /* Counts the number of characters in a string.*/
    50 static int count_char(char *str, char val) {
    51
  • 39 39
    40 40 testVectorize_SOURCES = testVectorize.c
    41 41
    42 testParser_SOURCES = testParser.c ../src/parser.c
    43
  • 17 parser_read_file(input_file,&param_file);
    18
    19 /* Print the contents of the structure. */
    20 parser_print_params(&param_file);
    21
    22 /* Retrieve parameters and store them in variables defined above.
    23 * Have to specify the name of the parameter as it appears in the
    24 * input file: testParserInput.yaml.*/
    25 parser_get_param_int(&param_file,"no_of_threads",&no_of_threads);
    26 parser_get_param_int(&param_file,"no_of_time_steps",&no_of_time_steps);
    27 parser_get_param_float(&param_file,"max_h",&max_h);
    28 parser_get_param_string(&param_file,"ic_file",ic_file);
    29
    30 /* Print the variables to check their values are correct. */
    31 printf("no_of_threads: %d, no_of_time_steps: %d, max_h: %f, ic_file: %s\n",no_of_threads, no_of_time_steps, max_h, ic_file);
    32 }
  • You should also add the files to src/Makefile.am so that they are correctly linked everywhere. Also, parser.h should be added to src/swift.h.

    Thanks !

  • src/parser.c 0 → 100644
    36
    37 if(fp == NULL) {
    38 error("Error opening parameter file: %s",file_name);
    39 }
    40
    41 /* Read until the end of the file is reached.*/
    42 while(!feof(fp)) {
    43 parse_line(fp,params);
    44 }
    45
    46 fclose(fp);
    47 }
    48
    49 /* Counts the number of characters in a string.*/
    50 static int count_char(char *str, char val) {
    51
  • When I try to compile I get the following errors:

    make
    make check
    In file included from testParser.c:1:0:
    ../src/parser.h:52:13: error: ‘parse_line’ declared ‘static’ but never defined [-Werror=unused-function]
     static void parse_line(FILE *fp, struct swift_params *params);
                 ^
    ../src/parser.h:53:12: error: ‘count_char’ declared ‘static’ but never defined [-Werror=unused-function]
     static int count_char(char *str, char val); 
  • James Willis Added 6 commits:

    Added 6 commits:

    • 089c7a02 - Added parser.h and parser.c to lists so that they are linked correctly.
    • 224cff8c - Added parser.h to list.
    • fa69e5dd - Removed ../src/parser.c from testParsers sources.
    • 653193b8 - Added asserts at the end of the test to check that values are read correctly.
    • 11cd7eaf - Removed private static functions, prefixed constants with PARSER_ and changed name in copyright.
    • f87f7791 - Added doxygen comments to functions, changed implementation of count_char().
  • @jwillis do you want me to look at it again ? Or should I wait ?

  • I would wait just now and I will add the type checking for parsing an int

  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Please register or sign in to reply
    Loading