Skip to content

Commit

Permalink
ConfigParser: add blank_leading_zeros and minimum_digit_number options
Browse files Browse the repository at this point in the history
Signed-off-by: Norbert Takacs <[email protected]>
  • Loading branch information
norberttak committed Apr 26, 2024
1 parent 2690d57 commit 502b248
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
44 changes: 38 additions & 6 deletions src/core/ConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Configparser::Configparser()
process_functions[TOKEN_DYN_SPEED_HIGH] = &Configparser::handle_on_dynamic_speed;
process_functions[TOKEN_DISPLAY_LINE] = &Configparser::handle_on_line_add;
process_functions[TOKEN_BCD] = &Configparser::handle_on_set_bcd;
process_functions[TOKEN_BLANK_LEADING_ZEROS] = &Configparser::handle_on_set_blank_leading_zeros;
process_functions[TOKEN_ENCODER_INC] = &Configparser::handle_on_encoder_inc_or_dec;
process_functions[TOKEN_ENCODER_DEC] = &Configparser::handle_on_encoder_inc_or_dec;
process_functions[TOKEN_SERIAL] = &Configparser::handle_on_fip_serial;
Expand All @@ -58,6 +59,20 @@ std::vector<std::string> Configparser::tokenize(std::string line)
return tokens;
}

bool Configparser::get_and_remove_token_pair(std::vector<std::string>& tokens, std::string name, std::string& out_value)
{
for (int i = 0; i < tokens.size(); i += 2)
{
if ((i+1) < tokens.size() && tokens[i] == name)
{
out_value = tokens[i + 1];
tokens.erase(tokens.begin() + i, tokens.begin() + i + 2);
return true;
}
}
return false;
}

int Configparser::parse_file(std::string file_name, Configuration& config)
{
last_error_message = "";
Expand Down Expand Up @@ -619,14 +634,16 @@ int Configparser::handle_on_line_add(IniFileSectionHeader section_header, std::s
return EXIT_FAILURE;
}

std::string min_digit_number = "";
get_and_remove_token_pair(m, TOKEN_MIN_DIGIT_NUMBER, min_digit_number);
if (min_digit_number != "")
config.class_configs.back().multi_displays[section_header.id]->set_minimum_number_of_digits(stoi(min_digit_number));

std::string condition = "";
if (m[0] == TOKEN_ON_SELECT)
{
condition = m[1];
m.erase(m.begin(), m.begin() + 2);
}
get_and_remove_token_pair(m, TOKEN_ON_SELECT, condition);

if (m.size() < 2)
// at this point only the dataref/lua/const key-value pair shall remain
if (m.size() != 2)
{
Logger(TLogLevel::logERROR) << "parser: invalid syntax (section starts at line: " << section_header.line << "): " << value << std::endl;
return EXIT_FAILURE;
Expand Down Expand Up @@ -728,6 +745,21 @@ int Configparser::handle_on_set_bcd(IniFileSectionHeader section_header, std::st
return EXIT_SUCCESS;
}

int Configparser::handle_on_set_blank_leading_zeros(IniFileSectionHeader section_header, std::string key, std::string value, Configuration& config)
{
if (section_header.name == TOKEN_SECTION_MULTI_DISPLAY)
config.class_configs.back().multi_displays[section_header.id]->set_blank_leading_zeros(value == "yes" ? true : false);
else if (section_header.name == TOKEN_SECTION_DISPLAY)
config.class_configs.back().generic_displays[section_header.id]->set_blank_leading_zeros(value == "yes" ? true : false);
else
{
Logger(TLogLevel::logERROR) << "parser: 'blank leading zeros' option can be set for generic- or multidisplay items";
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

int Configparser::handle_on_encoder_inc_or_dec(IniFileSectionHeader section_header, std::string key, std::string value, Configuration& config)
{
//on_increment="commandref:sim/GPS/g1000n1_nav_inner_up:once"
Expand Down
4 changes: 4 additions & 0 deletions src/core/ConfigParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Configparser

std::map<std::string, f_handle_on_key_value> process_functions;
std::vector<std::string> tokenize(std::string line);
bool get_and_remove_token_pair(std::vector<std::string>& tokens, std::string name, std::string& out_value);
void check_and_get_array_index(std::string& dataref, int& index);
int process_ini_section(IniFileSection& section, Configuration& config);

Expand All @@ -32,6 +33,7 @@ class Configparser
int handle_on_script_file(IniFileSectionHeader section_header, std::string key, std::string value, Configuration& config);
int handle_on_line_add(IniFileSectionHeader section_header, std::string key, std::string value, Configuration& config);
int handle_on_set_bcd(IniFileSectionHeader section_header, std::string key, std::string value, Configuration& config);
int handle_on_set_blank_leading_zeros(IniFileSectionHeader section_header, std::string key, std::string value, Configuration& config);
int handle_on_encoder_inc_or_dec(IniFileSectionHeader section_header, std::string key, std::string value, Configuration& config);
int handle_on_fip_serial(IniFileSectionHeader section_header, std::string key, std::string value, Configuration& config);
int handle_on_fip_offset(IniFileSectionHeader section_header, std::string key, std::string value, Configuration& config);
Expand Down Expand Up @@ -80,6 +82,8 @@ class Configparser
const std::string TOKEN_LUA = "lua";
const std::string TOKEN_CONST = "const";
const std::string TOKEN_BCD = "bcd";
const std::string TOKEN_BLANK_LEADING_ZEROS = "blank_leading_zeros";
const std::string TOKEN_MIN_DIGIT_NUMBER = "minimum_digit_number";
const std::string TOKEN_ON_SELECT = "on_select";
const std::string TOKEN_BEGIN = "begin";
const std::string TOKEN_END = "end";
Expand Down
6 changes: 3 additions & 3 deletions test/test-valid-config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ trigger_lit="lua:get_led_status():1"
trigger_unlit="lua:get_led_status():0"

;-------- Multi purpose display ---
[multi_display:id="MULTI_DISPLAY_UP"]
[multi_display:id="MULTI_DISPLAY_UP", blank_leading_zeros="yes"]
line="on_select:SW_ALT,dataref:sim/custom/gauges/compas/pkp_helper_course_L"
line="on_select:SW_VS,dataref:sim/custom/gauges/compas/pkp_helper_course_L"
line="on_select:SW_HDG,dataref:sim/custom/gauges/compas/pkp_helper_course_L"
line="on_select:SW_HDG,dataref:sim/custom/gauges/compas/pkp_helper_course_L, minimum_digit_number: 3"

[multi_display:id="MULTI_DISPLAY_DOWN"]
[multi_display:id="MULTI_DISPLAY_DOWN", bcd="yes", blank_leading_zeros="yes"]
line="on_select:SW_ALT,dataref:sim/custom/gauges/compas/pkp_helper_course_L"
line="on_select:SW_VS,dataref:sim/custom/gauges/compas/pkp_helper_course_L"

0 comments on commit 502b248

Please sign in to comment.