diff --git a/src/core/ConfigParser.cpp b/src/core/ConfigParser.cpp index a6a1c81..4ef8e0a 100644 --- a/src/core/ConfigParser.cpp +++ b/src/core/ConfigParser.cpp @@ -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; @@ -58,6 +59,20 @@ std::vector Configparser::tokenize(std::string line) return tokens; } +bool Configparser::get_and_remove_token_pair(std::vector& 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 = ""; @@ -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; @@ -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" diff --git a/src/core/ConfigParser.h b/src/core/ConfigParser.h index 65b0211..67fd91b 100644 --- a/src/core/ConfigParser.h +++ b/src/core/ConfigParser.h @@ -17,6 +17,7 @@ class Configparser std::map process_functions; std::vector tokenize(std::string line); + bool get_and_remove_token_pair(std::vector& 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); @@ -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); @@ -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"; diff --git a/test/test-valid-config.ini b/test/test-valid-config.ini index de15fa8..5afd148 100644 --- a/test/test-valid-config.ini +++ b/test/test-valid-config.ini @@ -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"