Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Config file parser in Fortran

I would like to be able to use a simple configuration file to pass parameters to my program. This configuration file would consist of a list of arguments with values that can be of different types (integer, real, logical, list of words, etc.).

Here is an example of a configuration that I would like to be able to parse in a simple way:

! This is a first comment
param1 = 1234;
param2 = TRUE;                    ! This is a second comment

param3 = abc def ghi jkl mno   
             pqr stu vwx yz;

! Another type of instruction, 
! i.e. to specify that var1-var3 depend on var4-var10:
var1 var2 var3 ~ var4 var5 var6
                 var7 var8 var9 var10;

Line breaks would be allowed, and different types of instructions could be passed to the program.

I am aware that NAMELIST could somehow allow to do that (except for the last part of the config file in this example), however it does not appear to be flexible enough for my needs. For instance, I don't think it allows to insert comments in the configuration file.

I found many libraries in C and C++ offering such a configuration file parser, but quite surprisingly, nothing in Fortran. Does anybody know of such a library?

Thanks in advance for your help!

like image 247
remek Avatar asked Feb 24 '11 03:02

remek


2 Answers

I would always recommend using a standard format for your configuration files. This can pay dividends later if you want to say, script your program, or generate or read your config files in another programming language. There are now (in 2015) several good standard configuration file parsers available for Fortran, so there is no need to use other languages. See, for example:

  • JSON -- JSON-Fortran
  • INI -- FiNeR

Note that these are both for modern Fortran, and require one of the newer compilers (say, gfortran 4.9 or later).

like image 66
Time Laird Avatar answered Nov 01 '22 02:11

Time Laird


we have a library called aotus, which allows the usage of Lua scripts as configuration files in Fortran applications: AOTUS Might be that this is useful for you.

like image 21
haraldkl Avatar answered Nov 01 '22 02:11

haraldkl