Simple parser to read inputs from ASCII files. These files should be organized like this: Label 1 : 0.1 # A scalar value Label 2 : 0.1 0.2 0.3 # A vector value Label 3 : Test # A string Label 4 : Test1 Test2 # An array of strings Label 5 : .true. # A logical !Label 6 : .false. # This line is ignored because of '!' Label 7 : 0.1 ! 0.2 0.3 # A scalar value
In this example, the symbol ':' separates labels and values. All characters that appear after a comment symbol (!, #, or %) are ignored.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| character(len=3), | private, | parameter | :: | COMMENT_SYMBOLS | = | '!#%' |
Allowed comment symbols |
| integer, | private, | parameter | :: | MAX_LABEL_SIZE | = | 128 |
Maximum number of characters in a label |
| integer, | private, | parameter | :: | MAX_LINE_SIZE | = | 4096 |
Maximum number of characters in a line |
| character(len=1), | private, | parameter | :: | SEPARATOR | = | ':' |
Character used to seperate label form value |
| integer, | private, | parameter | :: | TAB_ICHAR | = | 9 |
ichar of a tab character |
A utility to parse information in an ASCII file.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| type(entry_obj), | public, | allocatable | :: | entries(:) |
Entries in file |
| procedure, public :: Finalize => parser_obj_final | |
| generic, public :: Get => parser_obj_read0D, parser_obj_read1D | |
| procedure, public :: Initialize => parser_obj_init | |
| procedure, public :: IsDefined => parser_obj_IsDefined | |
| procedure, public :: ParseFile => parser_obj_ParseFile | |
| procedure, public :: Print => parser_obj_print | |
| procedure, private :: AddEntry => parser_obj_AddEntry | |
| generic, private :: AssignDefault => parser_obj_AssignDefault0D, parser_obj_AssignDefault1D | |
| procedure, private :: FetchLabelID => parser_obj_FetchLabelID | |
| procedure, private :: ParseLine => parser_obj_ParseLine | |
| procedure, private, nopass :: parser_obj_AssignDefault0D | |
| procedure, private, nopass :: parser_obj_AssignDefault1D | |
| procedure, private, nopass :: parser_obj_ReformatLine | |
| procedure, private :: parser_obj_read0D | |
| procedure, private :: parser_obj_read1D |
Object representing an entry in the input file.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| character(len=:), | public, | allocatable | :: | label |
Column label |
||
| character(len=:), | public, | allocatable | :: | value |
Value |
Returns ID of label in entries array. Returns 0 if label not found.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(parser_obj), | intent(in) | :: | this |
Parser object |
||
| character(len=*), | intent(in) | :: | label |
label to look for |
Checks whether a label was found when parsing the file.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(parser_obj), | intent(in) | :: | this |
Parser object |
||
| character(len=*), | intent(in) | :: | label |
label to look for |
Result
Resizes entries array to add a new entry.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(parser_obj), | intent(inout) | :: | this |
Parser object |
||
| type(entry_obj), | intent(in) | :: | entry |
New entrysize |
Sets 0D (scalar) value equal to 0D (scalar) default.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(*), | intent(out) | :: | value |
Value to return |
||
| class(*), | intent(in) | :: | default |
Default value to return in case label is not found |
Sets 1D (array) values equal to 1D (array) defaults.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(*), | intent(out) | :: | value(:) |
Value to return |
||
| class(*), | intent(in) | :: | default(:) |
Default value to return in case label is not found |
Parses an ASCII file. This subroutine will identify all entries in the file (label-value pairs) and store them internally. Users can specify the filename using the optional optInput variable. Otherwise, the default filename is 'input'.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(parser_obj), | intent(inout) | :: | this |
Parser object |
||
| character(len=*), | intent(in), | optional | :: | optInput |
Optional input file name |
Parses a line by finding the label-value pair and storing it as a new entry.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(parser_obj), | intent(inout) | :: | this |
Parser object |
||
| character(len=MAX_LINE_SIZE), | intent(inout) | :: | line |
A line from the input file |
Fomats a line by removing comments and replacing tabs with spaces.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=MAX_LINE_SIZE), | intent(inout) | :: | line |
A line from the input file |
Finalizes object and frees allocated memory.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(parser_obj), | intent(inout) | :: | this |
Parser object |
Initializes the parser object.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(parser_obj), | intent(inout) | :: | this |
Parser object |
Prints all variables found in the parsed file.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(parser_obj), | intent(inout) | :: | this |
Parser object |
Reads 0D (scalar) values of kind real, integer, logical, or character string. If label is not found and the optional default is provided, the value will be set to the default value. If the label is not found and there is no optional default, this subroutine will return an error.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(parser_obj), | intent(inout) | :: | this |
Parser object |
||
| character(len=*), | intent(in) | :: | label |
Label |
||
| class(*), | intent(out) | :: | value |
Value to return |
||
| class(*), | intent(in), | optional | :: | default |
Default value to return in case label is not found |
Reads 1D (array) values of kind real, integer, logical, or character string. If label is not found and the optional default is provided, the value will be set to the default value. If the label is not found and there is no optional default, this subroutine will return an error.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(parser_obj), | intent(inout) | :: | this |
Parser object |
||
| character(len=*), | intent(in) | :: | label |
Label |
||
| class(*), | intent(out) | :: | value(:) |
Value to return |
||
| class(*), | intent(in), | optional | :: | default(:) |
Default value to return in case label is not found |