summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/domain/converters.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gentoobrowse-api/domain/converters.cpp')
-rw-r--r--gentoobrowse-api/domain/converters.cpp69
1 files changed, 40 insertions, 29 deletions
diff --git a/gentoobrowse-api/domain/converters.cpp b/gentoobrowse-api/domain/converters.cpp
index 33a479f..aa51c26 100644
--- a/gentoobrowse-api/domain/converters.cpp
+++ b/gentoobrowse-api/domain/converters.cpp
@@ -6,6 +6,15 @@
#include <boost/algorithm/string/split.hpp>
namespace Slicer {
+ static
+ void
+ unpackPqTextArray(const std::string & s, ::Gentoo::StringList & list)
+ {
+ std::stringstream ss(s);
+ UnpackPqTextArray l(ss, list);
+ l.yylex();
+ }
+
template<typename T>
PqArray<T>::PqArray(std::vector<T> & l) :
ModelPartForSequence<std::vector<T>>(l)
@@ -24,11 +33,11 @@ namespace Slicer {
std::string v;
s->set(v);
if (v.size() < 3) return;
- typedef boost::algorithm::split_iterator<std::string::iterator> string_split_iterator;
+ typedef boost::algorithm::split_iterator<std::string::iterator> string_split_iterator;
string_split_iterator sfi(++v.begin(), --v.end(), boost::algorithm::first_finder(",", boost::algorithm::is_iequal()));
for(auto it = sfi; it != string_split_iterator(); ++it) {
this->Model.push_back(boost::lexical_cast<T>(*it));
- }
+ }
}
void PqArray<std::string>::SetValue(ValueSourcePtr s)
@@ -36,32 +45,20 @@ namespace Slicer {
Model.clear();
std::string v;
s->set(v);
- std::stringstream ss(v);
- ::Portage::Utils::UnpackPqTextArray l(ss, Model);
- l.yylex();
+ unpackPqTextArray(v, Model);
}
template<typename T>
- void PqArray<T>::GetValue(ValueTargetPtr s)
+ void
+ PqArray<T>::GetValue(ValueTargetPtr s)
{
- if (this->Model.empty()) s->get("{}");
- std::vector<std::string> l2;
- l2.reserve(this->Model.size());
- for(const auto & i : this->Model) {
- l2.push_back(boost::lexical_cast<std::string>(i));
- }
- s->get("{" + boost::algorithm::join(l2, ",") + "}");
+ s->get(packPqArray(this->Model));
}
- void PqArray<std::string>::GetValue(ValueTargetPtr s)
+ void
+ PqArray<std::string>::GetValue(ValueTargetPtr s)
{
- if (Model.empty()) s->get("{}");
- ::Gentoo::StringList l2(Model);
- for(auto & i : l2) {
- boost::algorithm::replace_all(i, "\"", "\\\"");
- i = "\"" + i + "\"";
- }
- s->get("{" + boost::algorithm::join(l2, ",") + "}");
+ s->get(packPqArray(this->Model));
}
template class PqArray<Ice::Int>;
@@ -70,23 +67,37 @@ namespace Slicer {
unpackPqTextArray(const std::string & s)
{
::Gentoo::StringList list;
- std::stringstream ss(s);
- ::Portage::Utils::UnpackPqTextArray l(ss, list);
- l.yylex();
+ unpackPqTextArray(s, list);
return list;
}
+ template<typename T>
std::string
- packPqTextArray(const ::Gentoo::StringList & l)
+ packPqArray(const T & l)
{
if (l.empty()) return "{}";
- ::Gentoo::StringList l2(l);
- for(auto & i : l2) {
- boost::algorithm::replace_all(i, "\"", "\\\"");
- i = "\"" + i + "\"";
+ std::vector<std::string> l2;
+ l2.reserve(l.size());
+ for(const auto & i : l) {
+ l2.push_back(packPqVar(i));
}
return "{" + boost::algorithm::join(l2, ",") + "}";
}
+ template std::string packPqArray<std::set<std::string>>(const std::set<std::string> &);
+
+ template<typename T>
+ std::string
+ packPqVar(const T & l)
+ {
+ return boost::lexical_cast<std::string>(l);
+ }
+
+ template<>
+ std::string
+ packPqVar(const std::string & l)
+ {
+ return "\"" + boost::algorithm::replace_all_copy(l, "\"", "\\\"") + "\"";
+ }
boost::posix_time::ptime
shortDateToPosixTime(const std::string & s)