PLUTO
parse_file.c File Reference

File parser utilities. More...

#include "pluto.h"
Include dependency graph for parse_file.c:

Go to the source code of this file.

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
 The total number of lines (including empty ones) contained in the file. More...
 
static char ** fline
 All of the lines (including empty ones) in the file. More...
 

Detailed Description

File parser utilities.

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@p.nosp@m.h.uni.nosp@m.to.i.nosp@m.t)
Date
June 18, 2014

Definition in file parse_file.c.

Function Documentation

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.

Definition at line 159 of file parse_file.c.

166 {
167  int k;
168  char sline[512], *str;
169  const char delimiters[] = " \t\r\f";
170 
171 /* ---------------------------------------
172  search if label exists
173  --------------------------------------- */
174 
175  for (k = 0; k < nlines; k++) {
176  sprintf (sline,"%s",fline[k]);
177  str = strtok(sline,delimiters);
178  if (strcmp(str,label) == 0) return(1);
179  }
180 
181  return (0);
182 }
static char ** fline
All of the lines (including empty ones) in the file.
Definition: parse_file.c:49
static int nlines
The total number of lines (including empty ones) contained in the file.
Definition: parse_file.c:47
int k
Definition: analysis.c:2

Here is the caller graph for this function:

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

Definition at line 86 of file parse_file.c.

97 {
98  int k, nwords, nw;
99  static char **words;
100 
101 /* -------------------------------------------------------------
102  Allocate memory and initialize words[][] string array with
103  to avoid unpleasant situations occurring when there're
104  empty words.
105  ------------------------------------------------------------- */
106 
107  if (words == NULL) words = ARRAY_2D(128,128,char);
108  for (k = 0; k < 127; k++) sprintf (words[k],"\0");
109 
110  for (k = 0; k < nlines; k++) { /* Loop over lines */
111  nwords = ParamFileGetWords(fline[k],words); /* get words for k-th line */
112 
113  if (nwords > 0 && strcmp(words[0],label) == 0){ /* check if 1st word */
114  if (pos <= nwords) return words[pos]; /* matches label */
115  else {
116  printf ("! ParamFileGet: field # %d does not exist\n",pos);
117  QUIT_PLUTO(1);
118  }
119  }
120  }
121 
122  printf ("! ParamFileGet: label '%s' was not found\n",label);
123  QUIT_PLUTO(1);
124 
125  return NULL;
126 }
static int ParamFileGetWords(char *line, char **)
Definition: parse_file.c:185
static char ** fline
All of the lines (including empty ones) in the file.
Definition: parse_file.c:49
static int nlines
The total number of lines (including empty ones) contained in the file.
Definition: parse_file.c:47
int k
Definition: analysis.c:2
#define ARRAY_2D(nx, ny, type)
Definition: prototypes.h:171
#define QUIT_PLUTO(e_code)
Definition: macros.h:125

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Definition at line 185 of file parse_file.c.

190 {
191  int nw, nwords=0;
192  char *str, cstr[512];
193  const char delimiters[] = " \t\r\f";
194 
195 /* -- make a local copy of 'line' since strtok will modify it -- */
196 
197  strcpy (cstr,line);
198  str = strtok(cstr,delimiters);
199 
200 /* -- loop until end of line, copy and count words -- */
201 
202  while (str != NULL && str[0] != '\n') {
203  strcpy(words[nwords],str);
204  str = strtok(NULL, delimiters);
205  nwords++;
206  }
207 
208 /* -- get rid of newline character at the end of each word (if any) -- */
209 
210  for (nw = 0; nw < nwords; nw++){
211  strcpy (cstr, words[nw]);
212  sprintf (words[nw],"%s",strtok(cstr,"\n"));
213  }
214 
215  return nw;
216 }

Here is the caller graph for this function:

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.

Definition at line 129 of file parse_file.c.

139 {
140  int k, nwords, nw;
141  static char **words;
142 
143  if (words == NULL) words = ARRAY_2D(128,128,char);
144 
145  for (k = 0; k < nlines; k++) { /* Loop over lines */
146  nwords = ParamFileGetWords(fline[k],words); /* get words for k-th line */
147  if (strcmp(words[0],label1) == 0){
148  for (nw = 1; nw < nwords; nw++){
149  if (words[nw][0] == '#') break; /* comment detected:
150  no need to read any further */
151  if (strcmp(words[nw], label2) == 0) return 1;
152  }
153  }
154  }
155  return 0;
156 }
static int ParamFileGetWords(char *line, char **)
Definition: parse_file.c:185
static char ** fline
All of the lines (including empty ones) in the file.
Definition: parse_file.c:49
static int nlines
The total number of lines (including empty ones) contained in the file.
Definition: parse_file.c:47
int k
Definition: analysis.c:2
#define ARRAY_2D(nx, ny, type)
Definition: prototypes.h:171

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Definition at line 53 of file parse_file.c.

61 {
62  char sline[512];
63  FILE *fp;
64 
65  if (fline == NULL) fline = ARRAY_2D(128,128,char);
66 
67  fp = fopen(fname,"r");
68  if (fp == NULL) {
69  printf ("! ParamFileRead: file %s not found\n", fname);
70  QUIT_PLUTO(1);
71  }
72  nlines = 0;
73 
74  while ( fgets(sline, 512, fp) != NULL ) {
75  if (strlen(sline) > 0) {
76  strcpy (fline[nlines],sline);
77  nlines++;
78  }
79  }
80  fclose(fp);
81 
82  return(nlines);
83 }
static char ** fline
All of the lines (including empty ones) in the file.
Definition: parse_file.c:49
static int nlines
The total number of lines (including empty ones) contained in the file.
Definition: parse_file.c:47
FILE * fp
Definition: analysis.c:7
#define ARRAY_2D(nx, ny, type)
Definition: prototypes.h:171
#define QUIT_PLUTO(e_code)
Definition: macros.h:125

Here is the caller graph for this function:

Variable Documentation

char** fline
static

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

Definition at line 49 of file parse_file.c.

int nlines
static

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

Definition at line 47 of file parse_file.c.