GRVY-0.38.0
Input Parsing Example using C API

An illustrative code example in C using the input parsing API is shown below (and is also available in the examples directory from a top-level install). This C example is configured to parse the example pecos-input.txt file shown here and uses many of the available parsing routines including a demonstration of default value registration.

//-----------------------------------------------------------------------bl-
//--------------------------------------------------------------------------
//
// libGRVY - a utility library for scientific computing.
//
// Copyright (C) 2008-2013,2018-2022 The PECOS Development Team
// Additional Copyright (C) 2018 individual authors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Version 2.1 GNU Lesser General
// Public License as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc. 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301 USA
//
//-----------------------------------------------------------------------el-
//
// C example illustrating basic input file parsing via libGRVY.
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <grvy.h>
int main(int argc, char **argv)
{
int igot;
float reyn,mach,aoa,A[3];
int iter_max,turbulent;
char *gridfile;
char *koomie;
char *key1, *key2;
/* Initialize/read the file */
igot = grvy_input_fopen("./pecos-input.txt");
/* Read specific variables and echo locally */
if(grvy_input_fread_float("reyn",&reyn))
printf("--> %-10s = %f\n","reyn",reyn);
if(grvy_input_fread_float("mach",&mach))
printf("--> %-10s = %f\n","mach",mach);
if(grvy_input_fread_float("aoa",&aoa))
printf("--> %-10s = %f\n","aoa",aoa);
if(grvy_input_fread_int("iter_max",&iter_max))
printf("--> %-10s = %i\n","iter_max",iter_max);
if(grvy_input_fread_char("gridfile",&gridfile))
printf("--> %-10s = %s\n","gridfile",gridfile);
/* Note that for convenience, libGRVY will allocate memory for the
* string based on the size of the string found in the input file;
* you should call free() when done with the variable. */
free(gridfile);
/* Read from the solver section */
if(grvy_input_fread_int("solver/turbulence",&turbulent))
printf("--> %-10s = %i\n","turbulent",turbulent);
if(grvy_input_fread_char_ivec("solver/keywords",&key1,0))
printf("--> %-10s = %s \n","solver/keyword 1",key1);
if(grvy_input_fread_char_ivec("solver/keywords",&key2,1))
printf("--> %-10s = %s \n","solver/keyword 2",key2);
if(grvy_input_fread_float_vec("turbulence/A",A,3))
printf("--> %-10s = %f %f %f\n","turbulence/A",A[0],A[1],A[2]);
/* Attempt to read unknown variable */
if(grvy_input_fread_char("koomie",&koomie))
printf("--> %-10s = %s\n","koomie",koomie);
free(koomie);
/* Disable error messages if you want to control them yourself
* If you query the unknown variable again, stdout should be quiet */
grvy_log_setlevel(GRVY_NOLOG);
if(grvy_input_fread_char("koomie",&koomie))
printf("--> %-10s = %s\n","koomie",koomie);
free(koomie);
grvy_log_setlevel(GRVY_INFO);
/* Register variable default values (can be used for
* backwards-compatible input file support) */
grvy_input_register_int ("solver/fast_button",1);
grvy_input_register_float ("solver/afloat",456.789);
grvy_input_register_double("solver/adouble",1.23e10);
grvy_input_register_char ("solver/astring","shoplifters-unite");
/* Dump the whole file to stdout */
printf("\n ------ Full Dump ------\n\n");
printf(" ---- End Full Dump ----\n\n");
printf("\n ------ Full Dump (delimited) ------\n\n");
printf(" ---- End Full Dump ----\n\n");
/* Dump the whole file to a file */
printf("\n ------ Full Dump to test.out ------\n\n");
grvy_input_fdump_file("% ","test.out");
printf(" ------- End Full Dump -------\n\n");
int igot2;
float fgot;
double dgot;
char *sgot;
if(grvy_input_register_get_int("solver/fast_button",&igot2))
printf("registered int = %i\n",igot2);
if(grvy_input_register_get_float("solver/afloat",&fgot))
printf("registered float = %f\n",fgot);
if(grvy_input_register_get_double("solver/adouble",&dgot))
printf("registered double = %e\n",dgot);
if(grvy_input_register_get_char("solver/astring",&sgot))
printf("registered string = %s\n",sgot);
/* Read in variable that has a registered default but is not present
in the input file */
if(grvy_input_fread_int("solver/fast_button",&igot))
printf("fread_int: fast_button = %i\n",igot);
fgot=-1.;
dgot=-1.;
if(grvy_input_fread_float("solver/afloat",&fgot))
printf("fread_float: afloat = %f\n",fgot);
if(grvy_input_fread_double("solver/adouble",&dgot))
printf("fread_double: adouble = %e\n",dgot);
sgot=NULL;
if(grvy_input_fread_char("solver/astring",&sgot))
printf("fread_char: astring = %s\n",sgot);
/* Close the file */
return 0;
}
grvy_input_fdump_file
int grvy_input_fdump_file(const char *prefix, const char *filename)
Dump input file settings to an open file with a provided prefix delimiter.
grvy_input_fclose
int grvy_input_fclose()
Close the currently open libGRVY style input file.
grvy_input_fread_float
int grvy_input_fread_float(const char *keyword, float *value)
Read a single input float value corresponding to the keyword path.
grvy_input_fread_int
int grvy_input_fread_int(const char *keyword, int *value)
Read a single input integer value corresponding to the keyword path.
grvy_input_register_char
int grvy_input_register_char(const char *keyword, char *value)
Register default character string value corresponding to the keyword path.
grvy_input_register_int
int grvy_input_register_int(const char *keyword, int value)
Register default integer value corresponding to the keyword path.
grvy_input_fdump_delim
int grvy_input_fdump_delim(const char *prefix)
Dump input file settings to stdout prefixed by a delimiter.
grvy_input_fread_char_ivec
int grvy_input_fread_char_ivec(const char *keyword, char **values, int elem)
Read a character array of size nelems corresponding to the keyword path.
grvy_input_fread_char
int grvy_input_fread_char(const char *keyword, char **value)
Read a single input character string value corresponding to the keyword path. For convenience,...
grvy_input_register_double
int grvy_input_register_double(const char *keyword, double value)
Register default double value corresponding to the keyword path.
grvy_input_register_get_double
int grvy_input_register_get_double(const char *keyword, double *value)
Retrieve registered default double value corresponding to the keyword path.
grvy_input_fopen
int grvy_input_fopen(const char *filename)
Open a libGRVY style input file.
grvy_log_setlevel
void grvy_log_setlevel(int priority)
Set current logging priority level.
grvy_input_register_get_char
int grvy_input_register_get_char(const char *keyword, char **value)
Retrieve registered default character string value corresponding to the keyword path.
grvy_input_register_float
int grvy_input_register_float(const char *keyword, float value)
Register default float value corresponding to the keyword path.
grvy.h
grvy_input_fread_double
int grvy_input_fread_double(const char *keyword, double *value)
Read a single input double value corresponding to the keyword path.
grvy_input_fread_float_vec
int grvy_input_fread_float_vec(const char *keyword, float *values, int nelems)
Read a float array of size nelems corresponding to the keyword path.
grvy_input_register_get_float
int grvy_input_register_get_float(const char *keyword, float *value)
Retrieve registered default float value corresponding to the keyword path.
grvy_input_register_get_int
int grvy_input_register_get_int(const char *keyword, int *value)
Retrieve registered default integer value corresponding to the keyword path.
grvy_input_fdump
int grvy_input_fdump()
Dump input file settings to stdout.

Generated on Wed Jul 12 2023 16:53:23 for GRVY-0.38.0 by  doxygen 1.8.17