'Config file parser in Fortran [closed]
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!
Solution 1:[1]
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).
Solution 2:[2]
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.
Solution 3:[3]
Another option is TTUTIL used in SWAP hydrological model from Alterra. I have set up a rough R bindings around it.
Though I'm not sure how to deal with the last part of your example using this library either:(
Solution 4:[4]
I would recommend clear and easy plain C config-parser libconfig
You can easily link libconfig-powered module (written on C) with your Fortran code using gcc or Intel compiler.
Solution 5:[5]
Some years ago, when I started using Fortran, I was also surprised that there were no configuration file parsers. That's why I decided to write my own parser: fortran_config. The syntax was intentionally kept very simple:
name_of_variable = value1 [value2 value3 ...] # Optional comment
Where variables can be integers, reals, strings or logicals.
Solution 6:[6]
You can try this free Fortran Parser: https://github.com/nextnano/FortranInputParser
The idea is to make the input file readable for the human eye.
An example looks like this:
Syntax definition file
!--------------------------------------------------!
! This is a comment.
! CHOICE[...,...] defines allowed options, else arbitrary.
!--------------------------------------------------!
$magnetic-field optional !
magnetic-field-on character required CHOICE[yes,no]
magnetic-field-strength double required !
magnetic-field-direction integer_array required CHOICE[1 0 0,0 1 0,0 0 1]
$end_magnetic-field optional !
!--------------------------------------------------!
! Supported data types are
! - character (==> 'Use quotes for array of characters')
! - integer
! - integer_array
! - double
! - double_array
! - logical
Input file
# This is a comment.
!----------------------------------------!
$magnetic-field !
magnetic-field-on = yes !
magnetic-field-strength = 5.0 !
magnetic-field-direction = 0 0 1 ! array elements are separated by blanks
$end_magnetic-field !
!----------------------------------------!
// This is a comment.
/* This is also comment. */
< This is also comment. >
# Inside a keyword, several sequences can be defined.
!-----------------------------!
$material
material-number = 1
material-name = AlAs
band-gap = 1.7
material-number = 2
material-name = GaAs
band-gap = 1.42
material-number = 3
...
$end_material
!-----------------------------!
Variables and equations are supported:
%a = 2.1
%Field = sqrt(%a/3)
magnetic-field-strength = %Field
Conditional IF is supported:
%MagneticFieldOn = .TRUE.
#IF %MagneticFieldOn magnetic-field-strength = 1.0
! Line breaks are allowed
magnetic-field-direction = 0
0
1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Time Laird |
| Solution 2 | haraldkl |
| Solution 3 | mlt |
| Solution 4 | Vitaly Isaev |
| Solution 5 | Jannis Teunissen |
| Solution 6 |
