summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gentoobrowse-api/domain/converters.cpp79
-rw-r--r--gentoobrowse-api/domain/converters.h26
-rw-r--r--gentoobrowse-api/domain/converters.impl.h81
-rw-r--r--gentoobrowse-api/domain/unpackPqTextArray.h6
-rw-r--r--gentoobrowse-api/domain/unpackPqTextArray.ll14
-rw-r--r--gentoobrowse-api/service/maintenanceGitOperations.cpp2
6 files changed, 98 insertions, 110 deletions
diff --git a/gentoobrowse-api/domain/converters.cpp b/gentoobrowse-api/domain/converters.cpp
index 073c545..b4748cd 100644
--- a/gentoobrowse-api/domain/converters.cpp
+++ b/gentoobrowse-api/domain/converters.cpp
@@ -1,51 +1,24 @@
#include <portage-models.h>
#include "converters.h"
+#include "converters.impl.h"
#include "unpackPqTextArray.h"
#include <string>
#include <boost/algorithm/string/join.hpp>
#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)
{
}
- PqArray<std::string>::PqArray(::Gentoo::StringList * sl) :
- ModelPartForSequence<::Gentoo::StringList>(sl)
- {
- }
-
template<typename T>
void PqArray<T>::SetValue(ValueSourcePtr s)
{
- this->Model->clear();
- std::string v;
- s->set(v);
- if (v.size() < 3) return;
- 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)
- {
- Model->clear();
- std::string v;
- s->set(v);
- unpackPqTextArray(v, *Model);
+ std::string in;
+ s->set(in);
+ *this->Model = unpackPqArray<T>(in);
}
template<typename T>
@@ -56,50 +29,8 @@ namespace Slicer {
return true;
}
- bool
- PqArray<std::string>::GetValue(ValueTargetPtr s)
- {
- s->get(packPqArray(*this->Model));
- return true;
- }
-
template class PqArray<Ice::Int>;
-
- ::Gentoo::StringList
- unpackPqTextArray(const std::string & s)
- {
- ::Gentoo::StringList list;
- unpackPqTextArray(s, list);
- return list;
- }
-
- template<typename T>
- std::string
- packPqArray(const T & l)
- {
- if (l.empty()) return "{}";
- 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, "\"", "\\\"") + "\"";
- }
+ template class PqArray<std::string>;
boost::posix_time::ptime
shortDateToPosixTime(const std::string & s)
diff --git a/gentoobrowse-api/domain/converters.h b/gentoobrowse-api/domain/converters.h
index 8449a65..404b9a1 100644
--- a/gentoobrowse-api/domain/converters.h
+++ b/gentoobrowse-api/domain/converters.h
@@ -16,32 +16,6 @@ namespace Slicer {
bool GetValue(ValueTargetPtr) override;
};
- template<>
- class DLL_PUBLIC PqArray<std::string> : public ModelPartForSequence<::Gentoo::StringList> {
- public:
- PqArray(::Gentoo::StringList *);
-
- void SetValue(ValueSourcePtr) override;
- bool GetValue(ValueTargetPtr) override;
- };
-
- DLL_PUBLIC
- ::Gentoo::StringList
- unpackPqTextArray(const std::string & s);
-
- template<typename T>
- DLL_PUBLIC
- std::string
- packPqArray(const T & l);
-
- template<typename T>
- std::string
- packPqVar(const T & l);
-
- template<>
- std::string
- packPqVar(const std::string & l);
-
DLL_PUBLIC
boost::posix_time::ptime
shortDateToPosixTime(const std::string & s);
diff --git a/gentoobrowse-api/domain/converters.impl.h b/gentoobrowse-api/domain/converters.impl.h
new file mode 100644
index 0000000..25f9266
--- /dev/null
+++ b/gentoobrowse-api/domain/converters.impl.h
@@ -0,0 +1,81 @@
+#ifndef GENTOOBROWSE_API_DOMAIN_CONVERTERS_IMPL_H
+#define GENTOOBROWSE_API_DOMAIN_CONVERTERS_IMPL_H
+
+#include "unpackPqTextArray.h"
+
+namespace Slicer {
+ template <typename T>
+ class UnpackPqTextArrayInto : public UnpackPqTextArray {
+ public:
+ UnpackPqTextArrayInto(std::istream & s, std::vector<T> & l) :
+ UnpackPqTextArray(s),
+ list(l)
+ {
+ }
+
+ virtual void consume(const std::string & s) override
+ {
+ list.push_back(boost::lexical_cast<T>(s));
+ }
+
+ private:
+ std::vector<T> & list;
+ };
+
+ template<typename T>
+ typename std::enable_if<std::is_arithmetic<T>::value>::type
+ packPqVar(std::ostream & s, const T & l)
+ {
+ s << l;
+ }
+
+ std::string
+ escapePqChar(char c)
+ {
+ if (c == '"') {
+ return std::string("\\\"", 2);
+ }
+ return std::string(1, c);
+ }
+
+ void
+ packPqVar(std::ostream & s, const std::string & l)
+ {
+ s << "\"";
+ std::transform(l.begin(), l.end(), std::ostream_iterator<std::string>(s), escapePqChar);
+ s << "\"";
+ }
+
+ template <typename T>
+ std::vector<T>
+ unpackPqArray(const std::string & s)
+ {
+ std::vector<T> rtn;
+ std::stringstream ss(s);
+ UnpackPqTextArrayInto<T> u(ss, rtn);
+ u.yylex();
+ return rtn;
+ }
+
+ template<typename T>
+ std::string
+ packPqArray(const T & l)
+ {
+ std::stringstream ss;
+ ss << "{";
+ if (!l.empty()) {
+ auto i = l.cbegin();
+ packPqVar(ss, *i);
+ i++;
+ while (i != l.cend()) {
+ ss << ",";
+ packPqVar(ss, *i++);
+ }
+ }
+ ss << "}";
+ return ss.str();
+ }
+}
+
+#endif
+
diff --git a/gentoobrowse-api/domain/unpackPqTextArray.h b/gentoobrowse-api/domain/unpackPqTextArray.h
index aefaddd..5f639b4 100644
--- a/gentoobrowse-api/domain/unpackPqTextArray.h
+++ b/gentoobrowse-api/domain/unpackPqTextArray.h
@@ -10,13 +10,15 @@
namespace Slicer {
class UnpackPqTextArray : public yyFlexLexer {
public:
- UnpackPqTextArray(std::istream &, ::Gentoo::StringList & sl);
+ UnpackPqTextArray(std::istream &);
+ virtual ~UnpackPqTextArray() = default;
int yylex() override;
void LexerError(const char * msg) override;
+ virtual void consume(const std::string &) = 0;
private:
- ::Gentoo::StringList & list;
+ std::string buffer;
};
}
diff --git a/gentoobrowse-api/domain/unpackPqTextArray.ll b/gentoobrowse-api/domain/unpackPqTextArray.ll
index f8cce5f..224ffc0 100644
--- a/gentoobrowse-api/domain/unpackPqTextArray.ll
+++ b/gentoobrowse-api/domain/unpackPqTextArray.ll
@@ -35,32 +35,32 @@ char [^"]
yy_pop_state();
}
<FIRST,NEXT>{unquoted} {
- list.push_back(YYText());
+ consume(YYText());
BEGIN(AFTER);
}
<FIRST,NEXT>{quote} {
- list.push_back(std::string());
+ buffer.clear();
BEGIN(QUOTED);
}
<AFTER>{comma} {
BEGIN(NEXT);
}
<QUOTED>{quote} {
+ consume(buffer);
BEGIN(AFTER);
}
<QUOTED>{esc} {
- list.back().push_back(*(YYText() + 1));
+ buffer.push_back(*(YYText() + 1));
}
<QUOTED>{char} {
- list.back().append(YYText());
+ buffer.append(YYText());
}
%%
namespace Slicer {
- UnpackPqTextArray::UnpackPqTextArray(std::istream & f, ::Gentoo::StringList & sl) :
- yyFlexLexer(&f, NULL),
- list(sl)
+ UnpackPqTextArray::UnpackPqTextArray(std::istream & f) :
+ yyFlexLexer(&f, NULL)
{
}
diff --git a/gentoobrowse-api/service/maintenanceGitOperations.cpp b/gentoobrowse-api/service/maintenanceGitOperations.cpp
index 59470c6..d532926 100644
--- a/gentoobrowse-api/service/maintenanceGitOperations.cpp
+++ b/gentoobrowse-api/service/maintenanceGitOperations.cpp
@@ -11,7 +11,7 @@
#include <sql/maintenance/repoSetLastCommit.sql.h>
#include <portage-models.h>
#include "utils/git.h"
-#include "converters.h"
+#include "converters.impl.h"
#include <compileTimeFormatter.h>
namespace Gentoo {