Comments (2)
TOML Fortrans getter interface by default will eagerly create values which are absent, if you request a table and its key is not in use the table will be created and the pointer associated to it. In most cases that is what you would manually do if you find an entry is missing when reading values from a config file anyway, so it is the default. It has the advantage that serializing the root table afterwards reflects the full configuration as seen by the program.
You can add the requested=.false.
argument to not create a table if it is not present, you then can check by associated(ptr)
whether the table is actually present and insert your custom logic to populate the table pointer.
This should do more or less what you are looking for:
module demo
use tomlf
implicit none
type :: component_type
character(:), allocatable :: name
integer :: val
end type component_type
type :: system_type
type(component_type), allocatable :: component(:)
end type system_type
contains
subroutine load_main(system, table, stat)
type(system_type), intent(out) :: system
type(toml_table), intent(inout) :: table
integer, intent(out) :: stat
type(toml_table), allocatable, target :: extern
type(toml_table), pointer :: child
type(toml_array), pointer :: array
integer :: icomp
call get_value(table, "system", child, stat=stat)
if (stat /= toml_stat%success) return
call get_value(child, "components", array, stat=stat)
if (stat /= toml_stat%success) return
allocate(system%component(len(array)))
do icomp = 1, size(system%component)
call get_value(array, icomp, system%component(icomp)%name, stat=stat)
if (stat /= toml_stat%success) return
end do
do icomp = 1, size(system%component)
associate(component => system%component(icomp))
call get_value(table, component%name, child, &
& requested=.false., stat=stat)
if (stat /= toml_stat%success) return
! If component table is not provided fallback to read it from extra file
if (.not.associated(child)) then
call toml_load(extern, component%name // ".toml")
if (.not.allocated(extern)) then
stat = toml_stat%fatal
return
end if
child => extern
end if
call get_value(child, "value", component%val, stat=stat)
if (stat /= toml_stat%success) return
end associate
end do
end subroutine load_main
end module demo
from toml-f.
Presence of entries has been asked for a few times (#115, #110), so it might be good to have a how-to recipe on this case. Personally, I think with a carefully designed layout in the configuration file, checking for presence should never seldom be necessary, but there are of course edge cases where it can be useful.
from toml-f.
Related Issues (20)
- Compatibility of datetime data type HOT 2
- Reliable display of colored output in docs
- Compatibility with stdlib
- Return allocation on deletion from table
- child is always associated in get_value even if requested section is missing HOT 2
- Support non-English scripts for bare keys
- Changes implemented in toml-f 0.3.1 compare with 0.2.4 results in FPM tests fail HOT 1
- how to pass default value and check if a key exist in toml file HOT 4
- Internal compiler error detected when building the project HOT 5
- Release TOML Fortran version 0.4.0
- Continuously test fpm with latest TOML Fortran HOT 2
- Support null/none/nil value in parser
- Alternatives to ieee_value for creating exceptional values HOT 12
- Compiling with Cray Fortran 14 HOT 9
- cmake CMP0110 warning
- Infinite loop with """\"""
- Support newlines and trailing comma in inline tables
- Support \x escape code in strings
- Support optional seconds in datetime values
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from toml-f.