Skip to content
Snippets Groups Projects
Commit 970fdc31 authored by Evgeny Mortikov's avatar Evgeny Mortikov
Browse files

major configuration file parser update

parent 018be472
No related branches found
No related tags found
No related merge requests found
Showing
with 224 additions and 13 deletions
/drag.exe
/build/
/srcF/*.mod
/parser/*.mod
/config-parser/*.mod
.idea
......@@ -30,7 +30,7 @@ endif ()
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/modules)
if(USE_CONFIG_PARSER)
add_subdirectory(parser/)
add_subdirectory(config-parser/)
add_definitions(-DUSE_CONFIG_PARSER)
# list(APPEND RUN_MACRO -DUSE_CONFIG_PARSER)
set(USE_CXX ON)
......@@ -75,7 +75,6 @@ set(SOURCES_F
srcF/sfx_sheba.f90
srcF/sfx_sheba_param.f90
srcF/sfx_fc_wrapper.F90
srcF/parser_subfunctions.f90
)
set(HEADERS_F
......@@ -153,7 +152,7 @@ set_property(TARGET sfx PROPERTY LINKER_LANGUAGE Fortran)
target_include_directories(sfx PUBLIC ${CMAKE_BINARY_DIR}/modules/)
if(USE_CONFIG_PARSER)
target_link_libraries(sfx parser_F parser_CXX)
target_link_libraries(sfx config_parser_F config_parser_CXX)
endif(USE_CONFIG_PARSER)
# copy data & configs on post build
......
......@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.19)
# option(INCLUDE_CXX "CXX build in mode" OFF)
project(Pars)
project(config_parser)
enable_language(Fortran)
enable_language(C)
......@@ -10,7 +10,7 @@ enable_language(CXX)
set(CMAKE_CXX_STANDARD 11)
set(SOURCES_CXX
call_parser.cpp
c-config-parser.cpp
cfg-cmd.cpp
cfg-value.cpp
cfg-vec.cpp
......@@ -21,7 +21,7 @@ set(SOURCES_CXX
lexeme-parser.cpp
)
set(HEADERS_CXX
call_parser.h
c-config-parser.h
cfg-cmd.h
cfg-value.h
cfg-vec.h
......@@ -36,11 +36,11 @@ set(HEADERS_CXX
)
set(SOURCES_C
call_parser.c
c-config-parser.c
)
set(SOURCES_F
call_parser.F90
config-parser.f90
)
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/modules)
......@@ -48,7 +48,7 @@ set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/modules)
set(SOURCES_CXX ${SOURCES_CXX} ${SOURCES_C})
set(HEADERS_CXX ${HEADERS_CXX})
add_library(parser_CXX STATIC ${HEADERS_CXX} ${SOURCES_CXX})
add_library(parser_F STATIC ${SOURCES_F})
set_property(TARGET parser_F PROPERTY LINKER_LANGUAGE Fortran)
target_link_libraries(parser_F parser_CXX)
add_library(config_parser_CXX STATIC ${HEADERS_CXX} ${SOURCES_CXX})
add_library(config_parser_F STATIC ${SOURCES_F})
set_property(TARGET config_parser_F PROPERTY LINKER_LANGUAGE Fortran)
target_link_libraries(config_parser_F config_parser_CXX)
#include "c-config-parser.h"
#include <stdio.h>
void c_config_run(const char* filename, int* status) {
(*status) = config_run(filename);
}
void c_config_is_varname(const char* name, int* status) {
(*status) = config_is_varname(name);
}
void c_config_get_int(const char* name, int* value, int* status) {
(*status) = config_get_int(name, value);
}
void c_config_get_float(const char* name, float* value, int* status) {
(*status) = config_get_float(name, value);
}
void c_config_get_double(const char* name, double* value, int* status) {
(*status) = config_get_double(name, value);
}
void c_config_get_string_len(const char* name, int *length) {
(*length) = config_get_string_len(name);
}
void c_config_get_string_unsafe(const char* name,
char* value, int* status)
{
(*status) = config_get_string_unsafe(name, value);
}
#include "c-config-parser.h"
#include "config-parser.h"
#include <string>
// *: static class
static scm::ConfigParser config;
#ifdef __cplusplus
extern "C" {
#endif
int config_run(const char* filename) {
return (int)config.run(filename);
}
int config_is_varname(const char* name) {
return (int)config.is_varname(name);
}
int config_get_int(const char* name, int* value) {
return (int)config.get_value(name, value);
}
int config_get_float(const char* name, float* value) {
return (int)config.get_value(name, value);
}
int config_get_double(const char* name, double* value) {
return (int)config.get_value(name, value);
}
int config_get_string_len(const char* name)
{
std::string str;
bool status = config.get_value(name, str);
if (!status) return 0;
return (int)str.size();
}
int config_get_string_unsafe(const char* name, char* c_str)
{
std::string str;
bool status = config.get_value(name, str);
if (!status) return 0;
memcpy(c_str, str.c_str(), (int)str.size() * sizeof(char));
return 1;
}
#ifdef __cplusplus
}
#endif
#pragma once
/* class [configParser] static C++ wrapper for C interface */
#ifdef __cplusplus
extern "C" {
#endif
/* main run call */
int config_run(const char* filename);
/* check calls */
int config_is_varname(const char* name);
/* get calls */
int config_get_int(const char* name, int* value);
int config_get_float(const char* name, float* value);
int config_get_double(const char* name, double* value);
int config_get_string_len(const char* name);
/* --- unsafe call: assumes 'c_str' is allocated to [len] elements */
int config_get_string_unsafe(const char* name, char* c_str);
#ifdef __cplusplus
}
#endif
File moved
File moved
File moved
File moved
File moved
File moved
File moved
module config_parser
!< @brief C->Fortran interface
interface
!< process configuration file [filename], status = [0 (ERROR), 1 (OK)]
subroutine c_config_run(filename, status) BIND(C)
use, intrinsic :: iso_c_binding, only: C_CHAR, C_INT
implicit none
character (kind=C_CHAR), intent(in) :: filename(*)
integer (kind=C_INT), intent(out) :: status
end subroutine c_config_run
!< check if variable [name] is defined, status = [0 (FALSE), 1 (TRUE)]
subroutine c_config_is_varname(name, status) BIND(C)
use, intrinsic :: iso_c_binding, only: C_CHAR, C_INT
implicit none
character (kind=C_CHAR), intent(in) :: name(*)
integer (kind=C_INT), intent(out) :: status
end subroutine c_config_is_varname
!< get (int) [value] of variable [name], status = [0 (ERROR), 1 (OK)]
subroutine c_config_get_int(name, value, status) BIND(C)
use, intrinsic :: iso_c_binding, only: C_CHAR, C_INT
implicit none
character (kind=C_CHAR), intent(in) :: name(*)
integer (kind=C_INT), intent(out) :: value
integer (kind=C_INT), intent(out) :: status
end subroutine c_config_get_int
!< get (float) [value] of variable [name], status = [0 (ERROR), 1 (OK)]
subroutine c_config_get_float(name, value, status) BIND(C)
use, intrinsic :: iso_c_binding, only: C_CHAR, C_INT, C_FLOAT
implicit none
character (kind=C_CHAR), intent(in) :: name(*)
real (kind=C_FLOAT), intent(out) :: value
integer (kind=C_INT), intent(out) :: status
end subroutine c_config_get_float
!< get (double) [value] of variable [name], status = [0 (ERROR), 1 (OK)]
subroutine c_config_get_double(name, value, status) BIND(C)
use, intrinsic :: iso_c_binding, only: C_CHAR, C_INT, C_DOUBLE
implicit none
character (kind=C_CHAR), intent(in) :: name(*)
real (kind=C_DOUBLE), intent(out) :: value
integer (kind=C_INT), intent(out) :: status
end subroutine c_config_get_double
!< get (int) [len] of string variable [name]
!< [len] = 0 if variable is not defined or not a string
subroutine c_config_get_string_len(name, len) BIND(C)
use, intrinsic :: iso_c_binding, only: C_CHAR, C_INT
implicit none
character (kind=C_CHAR), intent(in) :: name(*)
integer (kind=C_INT), intent(out) :: len
end subroutine c_config_get_string_len
!< get (char*) [c_str] of string variable [name], status = [0 (ERROR), 1 (OK)]
!< *: [c_str] should be allocated up to hold at least [len] bytes
!< *: [c_str] output doesn't contain the null terminated character
subroutine c_config_get_string_unsafe(name, c_str, status) BIND(C)
use, intrinsic :: iso_c_binding, only: C_CHAR, C_INT
implicit none
character (kind=C_CHAR), intent(in) :: name(*)
character (kind=C_CHAR), intent(out) :: c_str(*)
integer (kind=C_INT), intent(out) :: status
end subroutine c_config_get_string_unsafe
end interface
contains
!< @brief additional C->Fortran wrappers
! --------------------------------------------------------------------------------
!< get (char*) [c_str] of string variable [name], status = [0 (ERROR), 1 (OK)]
! *: [c_str] allocation is performed in-place
! *: [c_str] output doesn't contain the null terminated character
subroutine c_config_get_string(name, c_str, status)
use, intrinsic :: iso_c_binding, only: C_CHAR, C_INT
implicit none
character (kind=C_CHAR), intent(in) :: name(*)
character (kind=C_CHAR), allocatable, intent(out) :: c_str(:)
integer (kind=C_INT), intent(out) :: status
! local variables
! --------------------------------------------------------------------------------
integer :: len
! --------------------------------------------------------------------------------
call c_config_get_string_len(name, len)
if (allocated(c_str)) deallocate(c_str)
if (len > 0) allocate(c_str(len))
call c_config_get_string_unsafe(name, c_str, status)
end subroutine c_config_get_string
! --------------------------------------------------------------------------------
!< @brief Fortran nice wrappers
! --------------------------------------------------------------------------------
! --------------------------------------------------------------------------------
end module config_parser
\ No newline at end of file
File moved
File moved
File moved
File moved
File moved
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment