X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/0c9d10cb3d9399280fe5eca715287d2454788654..7f830439cde870faa88929eade481eb38c0ef382:/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp?ds=inline diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp index 8d54b1d5..867181af 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp @@ -23,7 +23,6 @@ #include "stg/tariffs.h" #include "stg/users.h" -#include "stg/common.h" #include "stg/resetable.h" #include // snprintf @@ -34,6 +33,11 @@ using STG::PARSER::ADD_TARIFF; using STG::PARSER::DEL_TARIFF; using STG::PARSER::CHG_TARIFF; +const char * GET_TARIFFS::tag = "GetTariffs"; +const char * ADD_TARIFF::tag = "AddTariff"; +const char * DEL_TARIFF::tag = "DelTariff"; +const char * CHG_TARIFF::tag = "SetTariff"; + namespace { @@ -52,6 +56,16 @@ std::string AOS2String(const A & array, size_t size, const F C::* field, F multi return res; } +template +bool str2res(const std::string& source, RESETABLE& dest, T divisor) +{ + T value = 0; + if (str2x(source, value)) + return false; + dest = value / divisor; + return true; +} + template bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE C::* field, F divisor) { @@ -60,15 +74,13 @@ bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE std::string::size_type pos = 0; while (index < size && (pos = source.find('/', from)) != std::string::npos) { - if (str2x(source.substr(from, pos - from), (array[index].*field).data())) + if (!str2res(source.substr(from, pos - from), array[index].*field, divisor)) return false; - (array[index].*field).data() /= divisor; from = pos + 1; ++index; } - if (str2x(source.substr(from), (array[index].*field).data())) + if (str2res(source.substr(from), array[index].*field, divisor)) return false; - (array[index].*field).data() /= divisor; return true; } @@ -76,21 +88,21 @@ bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE void GET_TARIFFS::CreateAnswer() { - answer = GetOpenTag(); + m_answer = ""; std::list dataList; m_tariffs.GetTariffsData(&dataList); std::list::const_iterator it = dataList.begin(); for (; it != dataList.end(); ++it) { - answer += "tariffConf.name + "\">"; + m_answer += "tariffConf.name + "\">"; for (size_t i = 0; i < DIR_NUM; i++) - answer += "dirPrice[i].hDay) + ":" + x2str(it->dirPrice[i].mDay) + "-" + x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>"; - answer += "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" + + m_answer += "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" + "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayB, pt_mega) + "\"/>" + "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightA, pt_mega) + "\"/>" + "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightB, pt_mega) + "\"/>" + @@ -102,15 +114,16 @@ void GET_TARIFFS::CreateAnswer() "tariffConf.free) + "\"/>" + "tariffConf.traffType) + "\"/>" + "tariffConf.period) + "\"/>" + + "tariffConf.changePolicy) + "\"/>" + ""; } - answer += GetCloseTag(); + m_answer += ""; } int ADD_TARIFF::Start(void *, const char * el, const char ** attr) { - if (strcasecmp(el, tag.c_str()) != 0) + if (strcasecmp(el, m_tag.c_str()) != 0) return -1; if (attr[1] == NULL) @@ -122,15 +135,15 @@ int ADD_TARIFF::Start(void *, const char * el, const char ** attr) void ADD_TARIFF::CreateAnswer() { - if (m_tariffs.Add(tariff, &currAdmin) == 0) - answer = "<" + tag + " Result=\"Ok\"/>"; + if (m_tariffs.Add(tariff, &m_currAdmin) == 0) + m_answer = "<" + m_tag + " Result=\"Ok\"/>"; else - answer = "<" + tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>"; + m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>"; } int DEL_TARIFF::Start(void *, const char * el, const char ** attr) { - if (strcasecmp(el, tag.c_str()) != 0) + if (strcasecmp(el, m_tag.c_str()) != 0) return -1; if (attr[1] == NULL) @@ -143,22 +156,26 @@ int DEL_TARIFF::Start(void *, const char * el, const char ** attr) void DEL_TARIFF::CreateAnswer() { if (m_users.TariffInUse(tariff)) - answer = "<" + tag + " Result=\"Error. Tariff \'" + tariff + "\' cannot be deleted, it is in use.\"/>"; - else if (m_tariffs.Del(tariff, &currAdmin) == 0) - answer = "<" + tag + " Result=\"Ok\"/>"; + m_answer = "<" + m_tag + " Result=\"Error. Tariff \'" + tariff + "\' cannot be deleted, it is in use.\"/>"; + else if (m_tariffs.Del(tariff, &m_currAdmin) == 0) + m_answer = "<" + m_tag + " Result=\"Ok\"/>"; else - answer = "<" + tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>"; + m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>"; } int CHG_TARIFF::Start(void *, const char * el, const char ** attr) { - depth++; + m_depth++; - if (depth == 1) + if (m_depth == 1) { - if (strcasecmp(el, tag.c_str()) == 0) + if (strcasecmp(el, m_tag.c_str()) == 0) { - td.tariffConf.name = attr[1]; + const TARIFF * tariff = m_tariffs.FindByName(attr[1]); + if (tariff != NULL) + td = tariff->GetTariffData(); + else + return -1; return 0; } } @@ -276,21 +293,14 @@ int CHG_TARIFF::Start(void *, const char * el, const char ** attr) td.tariffConf.period = TARIFF::StringToPeriod(attr[1]); return 0; } - } - return -1; -} -int CHG_TARIFF::End(void *, const char * el) -{ - if (depth == 1) - { - if (strcasecmp(el, tag.c_str()) != 0) - return -1; - CreateAnswer(); + if (strcasecmp(el, "ChangePolicy") == 0) + { + td.tariffConf.changePolicy = TARIFF::StringToChangePolicy(attr[1]); + return 0; + } } - - --depth; - return 0; + return -1; } void CHG_TARIFF::CreateAnswer() @@ -298,11 +308,11 @@ void CHG_TARIFF::CreateAnswer() if (!td.tariffConf.name.data().empty()) { TARIFF_DATA tariffData = td.GetData(); - if (m_tariffs.Chg(tariffData, &currAdmin) == 0) - answer = "<" + tag + " Result=\"ok\"/>"; + if (m_tariffs.Chg(tariffData, &m_currAdmin) == 0) + m_answer = "<" + m_tag + " Result=\"ok\"/>"; else - answer = "<" + tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>"; + m_answer = "<" + m_tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>"; } else - answer = "<" + tag + " Result=\"Change tariff error!\"/>"; + m_answer = "<" + m_tag + " Result=\"Change tariff error!\"/>"; }