From 2cd52ab17ee1b830bc53321b112411122dddc1c8 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Tue, 6 Jan 2015 17:31:21 -0800 Subject: Use Spanned while parsing config --- src/char/char.cpp | 131 +++++++++++++++++++++++++++-------------------------- src/char/inter.cpp | 21 +++++---- src/char/inter.hpp | 2 +- 3 files changed, 81 insertions(+), 73 deletions(-) (limited to 'src/char') diff --git a/src/char/char.cpp b/src/char/char.cpp index a82a692..ff9905f 100644 --- a/src/char/char.cpp +++ b/src/char/char.cpp @@ -52,6 +52,7 @@ #include "../io/extract.hpp" #include "../io/lock.hpp" #include "../io/read.hpp" +#include "../io/span.hpp" #include "../io/tty.hpp" #include "../io/write.hpp" @@ -2737,15 +2738,15 @@ void check_connect_login_server(TimerData *, tick_t) // Reading Lan Support configuration by [Yor] //------------------------------------------- static -bool char_lan_config(XString w1, ZString w2) +bool char_lan_config(io::Spanned w1, io::Spanned w2) { struct hostent *h = nullptr; { - if (w1 == "lan_map_ip"_s) + if (w1.data == "lan_map_ip"_s) { // Read map-server Lan IP Address - h = gethostbyname(w2.c_str()); + h = gethostbyname(w2.data.c_str()); if (h != nullptr) { lan_map_ip = IP4Address({ @@ -2757,17 +2758,17 @@ bool char_lan_config(XString w1, ZString w2) } else { - PRINTF("Bad IP value: %s\n"_fmt, w2); + PRINTF("Bad IP value: %s\n"_fmt, w2.data); return false; } PRINTF("LAN IP of map-server: %s.\n"_fmt, lan_map_ip); } - else if (w1 == "subnet"_s /*backward compatibility*/ - || w1 == "lan_subnet"_s) + else if (w1.data == "subnet"_s /*backward compatibility*/ + || w1.data == "lan_subnet"_s) { - if (!extract(w2, &lan_subnet)) + if (!extract(w2.data, &lan_subnet)) { - PRINTF("Bad IP mask: %s\n"_fmt, w2); + PRINTF("Bad IP mask: %s\n"_fmt, w2.data); return false; } PRINTF("Sub-network of the map-server: %s.\n"_fmt, @@ -2798,23 +2799,23 @@ bool lan_check() } static -bool char_config(XString w1, ZString w2) +bool char_config(io::Spanned w1, io::Spanned w2) { struct hostent *h = nullptr; { - if (w1 == "userid"_s) - userid = stringish(w2); - else if (w1 == "passwd"_s) - passwd = stringish(w2); - else if (w1 == "server_name"_s) + if (w1.data == "userid"_s) + userid = stringish(w2.data); + else if (w1.data == "passwd"_s) + passwd = stringish(w2.data); + else if (w1.data == "server_name"_s) { - server_name = stringish(w2); - PRINTF("%s server has been intialized\n"_fmt, w2); + server_name = stringish(w2.data); + PRINTF("%s server has been intialized\n"_fmt, w2.data); } - else if (w1 == "login_ip"_s) + else if (w1.data == "login_ip"_s) { - h = gethostbyname(w2.c_str()); + h = gethostbyname(w2.data.c_str()); if (h != nullptr) { login_ip = IP4Address({ @@ -2824,21 +2825,21 @@ bool char_config(XString w1, ZString w2) static_cast(h->h_addr[3]), }); PRINTF("Login server IP address : %s -> %s\n"_fmt, - w2, login_ip); + w2.data, login_ip); } else { - PRINTF("Bad IP value: %s\n"_fmt, w2); + PRINTF("Bad IP value: %s\n"_fmt, w2.data); return false; } } - else if (w1 == "login_port"_s) + else if (w1.data == "login_port"_s) { - login_port = atoi(w2.c_str()); + login_port = atoi(w2.data.c_str()); } - else if (w1 == "char_ip"_s) + else if (w1.data == "char_ip"_s) { - h = gethostbyname(w2.c_str()); + h = gethostbyname(w2.data.c_str()); if (h != nullptr) { char_ip = IP4Address({ @@ -2848,81 +2849,81 @@ bool char_config(XString w1, ZString w2) static_cast(h->h_addr[3]), }); PRINTF("Character server IP address : %s -> %s\n"_fmt, - w2, char_ip); + w2.data, char_ip); } else { - PRINTF("Bad IP value: %s\n"_fmt, w2); + PRINTF("Bad IP value: %s\n"_fmt, w2.data); return false; } } - else if (w1 == "char_port"_s) + else if (w1.data == "char_port"_s) { - char_port = atoi(w2.c_str()); + char_port = atoi(w2.data.c_str()); } - else if (w1 == "char_txt"_s) + else if (w1.data == "char_txt"_s) { - char_txt = w2; + char_txt = w2.data; } - else if (w1 == "max_connect_user"_s) + else if (w1.data == "max_connect_user"_s) { - max_connect_user = atoi(w2.c_str()); + max_connect_user = atoi(w2.data.c_str()); if (max_connect_user < 0) max_connect_user = 0; // unlimited online players } - else if (w1 == "autosave_time"_s) + else if (w1.data == "autosave_time"_s) { - autosave_time = std::chrono::seconds(atoi(w2.c_str())); + autosave_time = std::chrono::seconds(atoi(w2.data.c_str())); if (autosave_time <= std::chrono::seconds::zero()) autosave_time = DEFAULT_AUTOSAVE_INTERVAL; } - else if (w1 == "start_point"_s) + else if (w1.data == "start_point"_s) { - extract(w2, &start_point); + extract(w2.data, &start_point); } - else if (w1 == "unknown_char_name"_s) + else if (w1.data == "unknown_char_name"_s) { - unknown_char_name = stringish(w2); + unknown_char_name = stringish(w2.data); } - else if (w1 == "char_log_filename"_s) + else if (w1.data == "char_log_filename"_s) { - char_log_filename = w2; + char_log_filename = w2.data; } - else if (w1 == "char_name_letters"_s) + else if (w1.data == "char_name_letters"_s) { - if (!w2) + if (!w2.data) char_name_letters.reset(); else - for (uint8_t c : w2) + for (uint8_t c : w2.data) char_name_letters[c] = true; } - else if (w1 == "online_txt_filename"_s) + else if (w1.data == "online_txt_filename"_s) { - online_txt_filename = w2; + online_txt_filename = w2.data; } - else if (w1 == "online_html_filename"_s) + else if (w1.data == "online_html_filename"_s) { - online_html_filename = w2; + online_html_filename = w2.data; } - else if (w1 == "online_gm_display_min_level"_s) + else if (w1.data == "online_gm_display_min_level"_s) { // minimum GM level to display 'GM' when we want to display it - return extract(w2, &online_gm_display_min_level); + return extract(w2.data, &online_gm_display_min_level); } - else if (w1 == "online_refresh_html"_s) + else if (w1.data == "online_refresh_html"_s) { - online_refresh_html = atoi(w2.c_str()); + online_refresh_html = atoi(w2.data.c_str()); if (online_refresh_html < 1) online_refresh_html = 1; } - else if (w1 == "anti_freeze_enable"_s) + else if (w1.data == "anti_freeze_enable"_s) { - anti_freeze_enable = config_switch(w2); + anti_freeze_enable = config_switch(w2.data); } - else if (w1 == "anti_freeze_interval"_s) + else if (w1.data == "anti_freeze_interval"_s) { anti_freeze_interval = std::max( - std::chrono::seconds(atoi(w2.c_str())), + std::chrono::seconds(atoi(w2.data.c_str())), 5_s); } else @@ -2954,15 +2955,19 @@ void term_func(void) } static -bool char_confs(XString key, ZString value) +bool char_confs(io::Spanned key, io::Spanned value) { - unsigned sum = 0; - sum += char_config(key, value); - sum += char_lan_config(key, value); - sum += inter_config(key, value); - if (sum >= 2) - abort(); - return sum; + bool ok; + ok = char_config(key, value); + if (!ok) + return ok; + ok = char_lan_config(key, value); + if (!ok) + return ok; + ok = inter_config(key, value); + if (!ok) + return ok; + return ok; } int do_init(Slice argv) diff --git a/src/char/inter.cpp b/src/char/inter.cpp index 4b10dbc..df05434 100644 --- a/src/char/inter.cpp +++ b/src/char/inter.cpp @@ -37,8 +37,11 @@ #include "../io/extract.hpp" #include "../io/lock.hpp" #include "../io/read.hpp" +#include "../io/span.hpp" #include "../io/write.hpp" +#include "../mmo/config_parse.hpp" + #include "../proto2/char-map.hpp" #include "../high/extract_mmo.hpp" @@ -157,24 +160,24 @@ int inter_accreg_save(void) return 0; } -bool inter_config(XString w1, ZString w2) +bool inter_config(io::Spanned w1, io::Spanned w2) { { - if (w1 == "storage_txt"_s) + if (w1.data == "storage_txt"_s) { - storage_txt = w2; + storage_txt = w2.data; } - else if (w1 == "party_txt"_s) + else if (w1.data == "party_txt"_s) { - party_txt = w2; + party_txt = w2.data; } - else if (w1 == "accreg_txt"_s) + else if (w1.data == "accreg_txt"_s) { - accreg_txt = w2; + accreg_txt = w2.data; } - else if (w1 == "party_share_level"_s) + else if (w1.data == "party_share_level"_s) { - party_share_level = atoi(w2.c_str()); + party_share_level = atoi(w2.data.c_str()); if (party_share_level < 0) party_share_level = 0; } diff --git a/src/char/inter.hpp b/src/char/inter.hpp index 31d80b1..741bc8d 100644 --- a/src/char/inter.hpp +++ b/src/char/inter.hpp @@ -25,7 +25,7 @@ namespace tmwa { -bool inter_config(XString key, ZString value); +bool inter_config(io::Spanned key, io::Spanned value); void inter_init2(); void inter_save(void); RecvResult inter_parse_frommap(Session *ms, uint16_t packet_id); -- cgit v1.2.3-70-g09d2