PLUTO  4.4-patch2
Functions | Variables
parse_file.c File Reference

File parser utilities. More...

#include "pluto.h"

Functions

static int ParamFileGetWords (char *line, char **)
 
int ParamFileRead (char *fname)
 
char * ParamFileGet (const char *label, int pos)
 
int ParamFileHasBoth (const char *label1, const char *label2)
 
int ParamExist (const char *label)
 

Variables

static int nlines
 
static char ** fline
 

Detailed Description

This file provides a set of useful functions to open / read and parse the content of a parameter file (typically pluto.ini). The parameter file can contain a number of lines with the general structure

label value1 value2 ...

where the number of values following the label can be different for each line.

ParamFileRead() reads the file and store its content into an array of lines (**fline). ParamFileGet() can be used to retrieve the n-th parameter value following a given label, while ParQuery() check whether a parameter actually exists.

As an example consider the following file "myparam.txt":

 ---- File myparam.txt ----- 
 nx        100                
 xdomain   15.0  30.0         
 ---------------------------

The parameter can be read with the following code snippet:

int nlines,nx;
double xbeg, xend;
nlines = ParamFileRead("myparam.txt");
nx = atoi(ParamFileGet("nx", 1));
xbeg = atof(ParamFileGet("xdomain", 1));
xend = atof(ParamFileGet("xdomain", 2));
Authors
A. Mignone (migno.nosp@m.ne@t.nosp@m.o.inf.nosp@m.n.it)
Date
Aug 21, 2020

Function Documentation

◆ ParamExist()

int ParamExist ( const char *  label)

Check whether *label exists in any of the lines (**fline).

Parameters
[in]label
Returns
0 on success, 1 if label cannot be found.

◆ ParamFileGet()

char* ParamFileGet ( const char *  label,
int  pos 
)

Search for *label in all the lines pointed to by **fline. If label exists, return a pointer to the string located pos words after label. Issue an error if this string cannot be located.

Parameters
[in]labelthe first word of a line to be searched
[in]posan integer giving the position of the word
Returns
the n-th word contained in the same line

◆ ParamFileGetWords()

int ParamFileGetWords ( char *  line,
char **  words 
)
static

Return the words and their number contained in a single line.

◆ ParamFileHasBoth()

int ParamFileHasBoth ( const char *  label1,
const char *  label2 
)

Locate the line beginning with label1 and return 1 if label2 can be found on the same line. Return 0 otherwise.

Parameters
[in]label1The first word of the line to be searched
[in]label2A word containined in the same line beginning with label1
Returns
1 If label2 and label1 are in the same line. 0 otherwise.

◆ ParamFileRead()

int ParamFileRead ( char *  fname)

Parse file *fname and store its content line by line in *fline. Blank lines are excluded.

Parameters
[in]fnamethe name of the file to be read
Returns
the number of liens successfully read.

Variable Documentation

◆ fline

char** fline
static

All of the lines (including empty ones) in the file

◆ nlines

int nlines
static

The total number of lines (including empty ones) contained in the file