]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/configuration/sgconfig/parser_tariff.cpp
COmpose CXXFLAGS for tests correctly.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_tariff.cpp
index bb855fafc07ba679c057b7bfbda772ae8424f0c6..e083f85292b0ea60929bbdf54be2d948b6b8ee4a 100644 (file)
@@ -1,8 +1,11 @@
-#include <cstdio> // snprintf
-#include <cstring>
+#include "parser.h"
 
 #include "stg/tariffs.h"
 
 #include "stg/tariffs.h"
-#include "parser.h"
+#include "stg/users.h"
+#include "stg/common.h"
+
+#include <cstdio> // snprintf
+#include <cstring>
 
 const int pt_mega = 1024 * 1024;
 //-----------------------------------------------------------------------------
 
 const int pt_mega = 1024 * 1024;
 //-----------------------------------------------------------------------------
@@ -29,127 +32,131 @@ return -1;
 //-----------------------------------------------------------------------------
 void PARSER_GET_TARIFFS::CreateAnswer()
 {
 //-----------------------------------------------------------------------------
 void PARSER_GET_TARIFFS::CreateAnswer()
 {
-string s;
-char vs[100];
-int hd, hn, md, mn;
-
-answerList->erase(answerList->begin(), answerList->end());
-
-answerList->push_back("<Tariffs>");
+answer = "<Tariffs>";
 
 std::list<TARIFF_DATA> dataList;
 tariffs->GetTariffsData(&dataList);
 std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
 for (; it != dataList.end(); ++it)
     {
 
 std::list<TARIFF_DATA> dataList;
 tariffs->GetTariffsData(&dataList);
 std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
 for (; it != dataList.end(); ++it)
     {
-    s = "<tariff name=\"" + it->tariffConf.name + "\">";
-    answerList->push_back(s);
+    answer += "<tariff name=\"" + it->tariffConf.name + "\">";
 
 
-    for (int j = 0; j < DIR_NUM; j++)
-        {
-        hd = it->dirPrice[j].hDay;
-        md = it->dirPrice[j].mDay;
-
-        hn = it->dirPrice[j].hNight;
-        mn = it->dirPrice[j].mNight;
-
-        strprintf(&s, "<Time%d value=\"%d:%d-%d:%d\"/>", j, hd, md, hn, mn);
-        answerList->push_back(s);
-        }
+    for (size_t i = 0; i < DIR_NUM; i++)
+        answer += "<Time" + x2str(i) + " value=\"" +
+            x2str(it->dirPrice[i].hDay)   + ":" + x2str(it->dirPrice[i].mDay)   + "-" +
+            x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
 
 
-    strprintf(&s, "    <PriceDayA value=\"");
-    for (int i = 0; i < DIR_NUM; i++)
+    answer += "<PriceDayA value=\"";
+    bool first = true;
+    for (size_t i = 0; i < DIR_NUM; i++)
         {
         {
-        snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceDayA * pt_mega, i+1 == DIR_NUM?"":"/");
-        s += vs;
+        if (first)
+            first = false;
+        else
+            answer += "/";
+        answer += x2str(it->dirPrice[i].priceDayA * pt_mega);
         }
         }
-    s += "\"/>";
-    answerList->push_back(s);
+    answer += "\"/>";
 
 
-    strprintf(&s, "    <PriceDayB value=\"");
-    for (int i = 0; i < DIR_NUM; i++)
+    answer += "<PriceDayB value=\"";
+    first = true;
+    for (size_t i = 0; i < DIR_NUM; i++)
         {
         {
-        snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceDayB * pt_mega, i+1 == DIR_NUM?"":"/");
-        s += vs;
+        if (first)
+            first = false;
+        else
+            answer += "/";
+        answer += x2str(it->dirPrice[i].priceDayB * pt_mega);
         }
         }
-    s += "\"/>";
-    answerList->push_back(s);
+    answer += "\"/>";
 
 
-    strprintf(&s, "    <PriceNightA value=\"");
-    for (int i = 0; i < DIR_NUM; i++)
+    answer += "<PriceNightA value=\"";
+    first = true;
+    for (size_t i = 0; i < DIR_NUM; i++)
         {
         {
-        snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceNightA * pt_mega, i+1 == DIR_NUM?"":"/");
-        s += vs;
+        if (first)
+            first = false;
+        else
+            answer += "/";
+        answer += x2str(it->dirPrice[i].priceNightA * pt_mega);
         }
         }
-    s += "\"/>";
-    answerList->push_back(s);
+    answer += "\"/>";
 
 
-    strprintf(&s, "    <PriceNightB value=\"");
-    for (int i = 0; i < DIR_NUM; i++)
+    answer += "<PriceNightB value=\"";
+    first = true;
+    for (size_t i = 0; i < DIR_NUM; i++)
         {
         {
-        snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceNightB * pt_mega, i+1 == DIR_NUM?"":"/");
-        s += vs;
+        if (first)
+            first = false;
+        else
+            answer += "/";
+        answer += x2str(it->dirPrice[i].priceNightB * pt_mega);
         }
         }
-    s += "\"/>";
-    answerList->push_back(s);
+    answer += "\"/>";
 
 
-    strprintf(&s, "    <Threshold value=\"");
-    for (int i = 0; i < DIR_NUM; i++)
+    answer += "<Threshold value=\"";
+    first = true;
+    for (size_t i = 0; i < DIR_NUM; i++)
         {
         {
-        snprintf(vs, 100, "%d%s", it->dirPrice[i].threshold, i+1 == DIR_NUM?"":"/");
-        s += vs;
+        if (first)
+            first = false;
+        else
+            answer += "/";
+        answer += x2str(it->dirPrice[i].threshold);
         }
         }
-    s += "\"/>";
-    answerList->push_back(s);
+    answer += "\"/>";
 
 
-    strprintf(&s, "    <SinglePrice value=\"");
-    for (int i = 0; i < DIR_NUM; i++)
+    answer += "<SinglePrice value=\"";
+    first = true;
+    for (size_t i = 0; i < DIR_NUM; i++)
         {
         {
-        snprintf(vs, 100, "%d%s", it->dirPrice[i].singlePrice, i+1 == DIR_NUM?"":"/");
-        s += vs;
+        if (first)
+            first = false;
+        else
+            answer += "/";
+        answer += (it->dirPrice[i].singlePrice ? "1" : "0");
         }
         }
-    s += "\"/>";
-    answerList->push_back(s);
+    answer += "\"/>";
 
 
-    strprintf(&s, "    <NoDiscount value=\"");
-    for (int i = 0; i < DIR_NUM; i++)
+    answer += "<NoDiscount value=\"";
+    first = true;
+    for (size_t i = 0; i < DIR_NUM; i++)
         {
         {
-        snprintf(vs, 100, "%d%s", it->dirPrice[i].noDiscount, i+1 == DIR_NUM?"":"/");
-        s += vs;
+        if (first)
+            first = false;
+        else
+            answer += "/";
+        answer += (it->dirPrice[i].noDiscount ? "1" : "0");
         }
         }
-    s += "\"/>";
-    answerList->push_back(s);
+    answer += "\"/>";
 
 
-    strprintf(&s, "    <Fee value=\"%.5f\"/>", it->tariffConf.fee);
-    answerList->push_back(s);
+    answer += "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>";
 
 
-    strprintf(&s, "    <PassiveCost value=\"%.5f\"/>", it->tariffConf.passiveCost);
-    answerList->push_back(s);
+    answer += "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>";
 
 
-    strprintf(&s, "    <Free value=\"%.5f\"/>", it->tariffConf.free);
-    answerList->push_back(s);
+    answer += "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>";
 
     switch (it->tariffConf.traffType)
         {
         case TRAFF_UP:
 
     switch (it->tariffConf.traffType)
         {
         case TRAFF_UP:
-            answerList->push_back("<TraffType value=\"up\"/>");
+            answer += "<TraffType value=\"up\"/>";
             break;
         case TRAFF_DOWN:
             break;
         case TRAFF_DOWN:
-            answerList->push_back("<TraffType value=\"down\"/>");
+            answer += "<TraffType value=\"down\"/>";
             break;
         case TRAFF_UP_DOWN:
             break;
         case TRAFF_UP_DOWN:
-            answerList->push_back("<TraffType value=\"up+down\"/>");
+            answer += "<TraffType value=\"up+down\"/>";
             break;
         case TRAFF_MAX:
             break;
         case TRAFF_MAX:
-            answerList->push_back("<TraffType value=\"max\"/>");
+            answer += "<TraffType value=\"max\"/>";
             break;
         }
 
             break;
         }
 
-    answerList->push_back("<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>");
+    answer += "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>";
 
 
-    answerList->push_back("</tariff>");
+    answer += "</tariff>";
     }
     }
-answerList->push_back("</Tariffs>");
+answer += "</Tariffs>";
 }
 //-----------------------------------------------------------------------------
 //  ADD TARIFF
 }
 //-----------------------------------------------------------------------------
 //  ADD TARIFF
@@ -179,19 +186,10 @@ return -1;
 //-----------------------------------------------------------------------------
 void PARSER_ADD_TARIFF::CreateAnswer()
 {
 //-----------------------------------------------------------------------------
 void PARSER_ADD_TARIFF::CreateAnswer()
 {
-//answerList->clear();
-answerList->erase(answerList->begin(), answerList->end());
-
 if (tariffs->Add(tariffToAdd, currAdmin) == 0)
 if (tariffs->Add(tariffToAdd, currAdmin) == 0)
-    {
-    answerList->push_back("<AddTariff Result=\"Ok\"/>");
-    }
+    answer = "<AddTariff Result=\"Ok\"/>";
 else
 else
-    {
-    string s;
-    strprintf(&s, "<AddTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
-    answerList->push_back(s);
-    }
+    answer = "<AddTariff Result=\"Error. " + tariffs->GetStrError() + "\"/>";
 }
 //-----------------------------------------------------------------------------
 //  DEL TARIFF
 }
 //-----------------------------------------------------------------------------
 //  DEL TARIFF
@@ -219,34 +217,19 @@ return -1;
 //-----------------------------------------------------------------------------
 void PARSER_DEL_TARIFF::CreateAnswer()
 {
 //-----------------------------------------------------------------------------
 void PARSER_DEL_TARIFF::CreateAnswer()
 {
-//answerList->clear();
-answerList->erase(answerList->begin(), answerList->end());
-
 if (users->TariffInUse(tariffToDel))
 if (users->TariffInUse(tariffToDel))
-    {
-    string s;
-    strprintf(&s, "<DelTariff Result=\"Error. Tariff \'%s\' cannot be deleted. Tariff in use.\"/>", tariffToDel.c_str());
-    answerList->push_back(s);
-    return;
-    }
-
-if (tariffs->Del(tariffToDel, currAdmin) == 0)
-    {
-    answerList->push_back("<DelTariff Result=\"Ok\"/>");
-    }
+    answer = "<DelTariff Result=\"Error. Tariff \'" + tariffToDel + "\' cannot be deleted. Tariff in use.\"/>";
+else if (tariffs->Del(tariffToDel, currAdmin) == 0)
+    answer = "<DelTariff Result=\"Ok\"/>";
 else
 else
-    {
-    string s;
-    strprintf(&s, "<DelTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
-    answerList->push_back(s);
-    }
+    answer = "<DelTariff Result=\"Error. " + tariffs->GetStrError() + "\"/>";
 }
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //  CHG TARIFF
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 }
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //  CHG TARIFF
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-int PARSER_CHG_TARIFF::ParseSlashedIntParams(int paramsNum, const string & s, int * params)
+int PARSER_CHG_TARIFF::ParseSlashedIntParams(int paramsNum, const std::string & s, int * params)
 {
 char * str = new char[s.size() + 1];
 char * p;
 {
 char * str = new char[s.size() + 1];
 char * p;
@@ -274,7 +257,7 @@ delete[] str;
 return 0;
 }
 //-----------------------------------------------------------------------------
 return 0;
 }
 //-----------------------------------------------------------------------------
-int PARSER_CHG_TARIFF::ParseSlashedDoubleParams(int paramsNum, const string & s, double * params)
+int PARSER_CHG_TARIFF::ParseSlashedDoubleParams(int paramsNum, const std::string & s, double * params)
 {
 char * str = new char[s.size() + 1];
 char * p;
 {
 char * str = new char[s.size() + 1];
 char * p;
@@ -304,7 +287,6 @@ return 0;
 //-----------------------------------------------------------------------------
 int PARSER_CHG_TARIFF::ParseStart(void *, const char * el, const char ** attr)
 {
 //-----------------------------------------------------------------------------
 int PARSER_CHG_TARIFF::ParseStart(void *, const char * el, const char ** attr)
 {
-char st[50];
 double price[DIR_NUM];
 int t[DIR_NUM];
 depth++;
 double price[DIR_NUM];
 int t[DIR_NUM];
 depth++;
@@ -319,7 +301,7 @@ if (depth == 1)
     }
 else
     {
     }
 else
     {
-    string s;
+    std::string s;
 
     if (strcasecmp(el, "PriceDayA") == 0)
         {
 
     if (strcasecmp(el, "PriceDayA") == 0)
         {
@@ -387,6 +369,7 @@ else
 
     for (int j = 0; j < DIR_NUM; j++)
         {
 
     for (int j = 0; j < DIR_NUM; j++)
         {
+        char st[50];
         snprintf(st, 50, "Time%d", j);
         if (strcasecmp(el, st) == 0)
             {
         snprintf(st, 50, "Time%d", j);
         if (strcasecmp(el, st) == 0)
             {
@@ -481,24 +464,20 @@ return -1;
 //-----------------------------------------------------------------------------
 void PARSER_CHG_TARIFF::CreateAnswer()
 {
 //-----------------------------------------------------------------------------
 void PARSER_CHG_TARIFF::CreateAnswer()
 {
-answerList->erase(answerList->begin(), answerList->end());
-
 if (!td.tariffConf.name.data().empty())
     {
     TARIFF_DATA tariffData = td.GetData();
     if (tariffs->Chg(tariffData, currAdmin) == 0)
         {
 if (!td.tariffConf.name.data().empty())
     {
     TARIFF_DATA tariffData = td.GetData();
     if (tariffs->Chg(tariffData, currAdmin) == 0)
         {
-        answerList->push_back("<SetTariff Result=\"ok\"/>");
+        answer = "<SetTariff Result=\"ok\"/>";
         return;
         }
     else
         {
         return;
         }
     else
         {
-        string s;
-        strprintf(&s, "<SetTariff Result=\"Change tariff error! %s\"/>", tariffs->GetStrError().c_str());
-        answerList->push_back(s);
+        answer = "<SetTariff Result=\"Change tariff error! " + tariffs->GetStrError() + "\"/>";
         return;
         }
     }
         return;
         }
     }
-answerList->push_back("<SetTariff Result=\"Change tariff error!\"/>");
+answer = "<SetTariff Result=\"Change tariff error!\"/>";
 }
 //-----------------------------------------------------------------------------
 }
 //-----------------------------------------------------------------------------