Skip to content

Commit

Permalink
small refactoring of api functions
Browse files Browse the repository at this point in the history
  • Loading branch information
toxa81 committed Jan 6, 2020
1 parent 17c7628 commit e239eb9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 80 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.4.2
6.4.3
2 changes: 1 addition & 1 deletion doc/doxygen.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "SIRIUS"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = "6.4.2"
PROJECT_NUMBER = "6.4.3"

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
38 changes: 19 additions & 19 deletions src/generated.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2754,54 +2754,54 @@ subroutine sirius_option_get_length_aux(section,length)&
call sirius_option_get_length_aux(section,length)
end subroutine sirius_option_get_length

!> @brief return the name and a type of an option from its index
!> @param [in] section name of the section
!> @param [out] elem_ index of the option
!> @param [out] key_name name of the option
!> @param [out] type type of the option (real, integer, boolean, string)
subroutine sirius_option_get_name_and_type(section,elem_,key_name,type)
!> @brief Return the name and a type of an option from its index.
!> @param [in] section Name of the section.
!> @param [in] elem Index of the option.
!> @param [out] key_name Name of the option.
!> @param [out] type Type of the option (real, integer, boolean, string).
subroutine sirius_option_get_name_and_type(section,elem,key_name,type)
implicit none
character(C_CHAR), dimension(*), intent(in) :: section
integer(C_INT), intent(out) :: elem_
integer(C_INT), intent(in) :: elem
character(C_CHAR), dimension(*), intent(out) :: key_name
integer(C_INT), intent(out) :: type
interface
subroutine sirius_option_get_name_and_type_aux(section,elem_,key_name,type)&
subroutine sirius_option_get_name_and_type_aux(section,elem,key_name,type)&
&bind(C, name="sirius_option_get_name_and_type")
use, intrinsic :: ISO_C_BINDING
character(C_CHAR), dimension(*), intent(in) :: section
integer(C_INT), intent(out) :: elem_
integer(C_INT), intent(in) :: elem
character(C_CHAR), dimension(*), intent(out) :: key_name
integer(C_INT), intent(out) :: type
end subroutine
end interface

call sirius_option_get_name_and_type_aux(section,elem_,key_name,type)
call sirius_option_get_name_and_type_aux(section,elem,key_name,type)
end subroutine sirius_option_get_name_and_type

!> @brief return the description and usage of a given option
!> @param [in] section name of the section
!> @param [in] name name of the option
!> @param [out] desc_ description of the option
!> @param [out] usage_ how to use the option
subroutine sirius_option_get_description_usage(section,name,desc_,usage_)
!> @param [out] desc description of the option
!> @param [out] usage how to use the option
subroutine sirius_option_get_description_usage(section,name,desc,usage)
implicit none
character(C_CHAR), dimension(*), intent(in) :: section
character(C_CHAR), dimension(*), intent(in) :: name
character(C_CHAR), dimension(*), intent(out) :: desc_
character(C_CHAR), dimension(*), intent(out) :: usage_
character(C_CHAR), dimension(*), intent(out) :: desc
character(C_CHAR), dimension(*), intent(out) :: usage
interface
subroutine sirius_option_get_description_usage_aux(section,name,desc_,usage_)&
subroutine sirius_option_get_description_usage_aux(section,name,desc,usage)&
&bind(C, name="sirius_option_get_description_usage")
use, intrinsic :: ISO_C_BINDING
character(C_CHAR), dimension(*), intent(in) :: section
character(C_CHAR), dimension(*), intent(in) :: name
character(C_CHAR), dimension(*), intent(out) :: desc_
character(C_CHAR), dimension(*), intent(out) :: usage_
character(C_CHAR), dimension(*), intent(out) :: desc
character(C_CHAR), dimension(*), intent(out) :: usage
end subroutine
end interface

call sirius_option_get_description_usage_aux(section,name,desc_,usage_)
call sirius_option_get_description_usage_aux(section,name,desc,usage)
end subroutine sirius_option_get_description_usage

!> @brief return the default value of the option
Expand Down
120 changes: 61 additions & 59 deletions src/sirius_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2625,125 +2625,127 @@ void sirius_update_atomic_potential(void* const* handler__)
}


/* @fortran begin function void sirius_option_get_length return the number of options in a given section
@fortran argument in required string section name of the seciton
@fortran argument out required int length number of options contained in the section
/* @fortran begin function void sirius_option_get_length return the number of options in a given section
@fortran argument in required string section name of the seciton
@fortran argument out required int length number of options contained in the section
@fortran end */

void sirius_option_get_length(char *section, int *length)
void sirius_option_get_length(char const* section__, int *length__)
{
const json &parser = sirius::get_options_dictionary();
// ugly as hell but fortran is a piece of ....
for ( char *p = section; *p; p++) *p = tolower(*p);
auto const& parser = sirius::get_options_dictionary();

auto section = std::string(section__);
std::transform(section.begin(), section.end(), section.begin(), ::tolower);

*length = parser[section].size();
*length__ = parser[section].size();
}

/* @fortran begin function void sirius_option_get_name_and_type Return the name and a type of an option from its index
@fortran argument in required string section name of the section
@fortran argument out required int elem_ index of the option
@fortran argument out required string key_name name of the option
@fortran argument out required int type type of the option (real, integer, boolean, string)
/* @fortran begin function void sirius_option_get_name_and_type Return the name and a type of an option from its index.
@fortran argument in required string section Name of the section.
@fortran argument in required int elem Index of the option.
@fortran argument out required string key_name Name of the option.
@fortran argument out required int type Type of the option (real, integer, boolean, string).
@fortran end */

void sirius_option_get_name_and_type(char const* section__, int const* elem__, char const* key_name__, int const * type__)
void sirius_option_get_name_and_type(char const* section__, int const* elem__, char* key_name__, int* type__)
{
const json &dict = sirius::get_options_dictionary();

// ugly as hell but fortran is a piece of ....
for ( char *p = section; *p; p++) *p = tolower(*p);
auto section = std::string(section__);
std::transform(section.begin(), section.end(), section.begin(), ::tolower);

int elem = 0;
*type = -1;
*type__ = -1;
for (auto& el : dict[section].items()) {
if (elem == *elem_) {
if (elem == *elem__) {
if (!dict[section][el.key()].count("default_value")) {
std::cout << "key : " << el.key() << "\n the default_value key is missing" << std::endl;
exit(0);
}
if (dict[section][el.key()]["default_value"].is_array()) {
*type = 10;
*type__ = 10;
if (dict[section][el.key()]["default_value"][0].is_number_integer()) {
*type += 1;
*type__ += 1;
}
if (dict[section][el.key()]["default_value"][0].is_number_float()) {
*type += 2;
*type__ += 2;
}
if (dict[section][el.key()]["default_value"][0].is_boolean()) {
*type += 3;
*type__ += 3;
}
if (dict[section][el.key()]["default_value"][0].is_string()) {
*type += 4;
*type__ += 4;
}
} else {
if (dict[section][el.key()]["default_value"].is_number_integer()) {
*type = 1;
*type__ = 1;
}
if (dict[section][el.key()]["default_value"].is_number_float()) {
*type = 2;
*type__ = 2;
}
if (dict[section][el.key()]["default_value"].is_boolean()) {
*type = 3;
*type__ = 3;
}
if (dict[section][el.key()]["default_value"].is_string()) {
*type = 4;
*type__ = 4;
}
}
std::memcpy(key_name, el.key().c_str(), el.key().size());
std::memcpy(key_name__, el.key().c_str(), el.key().size());
}
elem++;
}
}

/* @fortran begin function void sirius_option_get_description_usage return the description and usage of a given option
@fortran argument in required string section name of the section
@fortran argument in required string name name of the option
@fortran argument out required string desc_ description of the option
@fortran argument out required string usage_ how to use the option
/* @fortran begin function void sirius_option_get_description_usage return the description and usage of a given option
@fortran argument in required string section name of the section
@fortran argument in required string name name of the option
@fortran argument out required string desc description of the option
@fortran argument out required string usage how to use the option
@fortran end */

void sirius_option_get_description_usage(char * section, char * name, char *desc_, char *usage_)
void sirius_option_get_description_usage(char const* section__, char const* name__, char* desc__, char* usage__)
{
const json &parser = sirius::get_options_dictionary();

// ugly as hell but fortran is a piece of ....
for ( char *p = section; *p; p++) *p = tolower(*p);
// ugly as hell but fortran is a piece of ....
// for ( char *p = name; *p; p++) *p = tolower(*p);
auto section = std::string(section__);
std::transform(section.begin(), section.end(), section.begin(), ::tolower);

auto name = std::string(name__);

if (parser[section][name].count("description")) {
std::string description = parser[section][name].value("description", "");
std::copy(description.begin(), description.end(), desc_);
auto description = parser[section][name].value("description", "");
std::copy(description.begin(), description.end(), desc__);
}
if (parser[section][name].count("usage")) {
std::string usage = parser[section][name].value("usage","");
std::copy(usage.begin(), usage.end(), usage_);
auto usage = parser[section][name].value("usage", "");
std::copy(usage.begin(), usage.end(), usage__);
}
}

/* @fortran begin function void sirius_option_get_int return the default value of the option
@fortran argument in required string section name of the section of interest
@fortran argument in required string name name of the element
@fortran argument out required int default_value table containing the default values (if vector)
@fortran argument out required int length length of the table containing the default values
/* @fortran begin function void sirius_option_get_int return the default value of the option
@fortran argument in required string section name of the section of interest
@fortran argument in required string name name of the element
@fortran argument out required int default_value table containing the default values (if vector)
@fortran argument out required int length length of the table containing the default values
@fortran end */

void sirius_option_get_int(char * section, char * name, int *default_value, int *length)
void sirius_option_get_int(char const* section__, char const* name__, int *default_value__, int *length__)
{
const json &parser = sirius::get_options_dictionary();
auto const &parser = sirius::get_options_dictionary();

// ugly as hell but fortran is a piece of ....
for ( char *p = section; *p; p++) *p = tolower(*p);
// ugly as hell but fortran is a piece of ....
// for ( char *p = name; *p; p++) *p = tolower(*p);
if (!parser[section][name].count("default_value"))
std::cout << "default value is mossing" << std::endl;
auto section = std::string(section__);
std::transform(section.begin(), section.end(), section.begin(), ::tolower);

auto name = std::string(name__);

if (!parser[section][name].count("default_value")) {
std::cout << "default value is missing" << std::endl;
}
if (parser[section][name]["default_value"].is_array()) {
std::vector<int> v = parser[section][name]["default_value"].get<std::vector<int>>();
*length = v.size();
memcpy(default_value, &v[0], v.size() * sizeof(int));
*length__ = v.size();
std::memcpy(default_value__, &v[0], v.size() * sizeof(int));
} else {
*default_value = parser[section][name].value("default_value", -1);
*default_value__ = parser[section][name].value("default_value", -1);
}
}

Expand Down

0 comments on commit e239eb9

Please sign in to comment.