From b77eb73caa4d389a195b4a78962a2610eb4b051a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 19 Dec 2015 21:00:12 +0000 Subject: Add support for unpacking PostgreSQL text arrays from their string form --- gentoobrowse-api/domain/portage-models.ice | 1 + gentoobrowse-api/service/Jamfile.jam | 3 ++ gentoobrowse-api/service/converters.cpp | 22 +++++++++ gentoobrowse-api/service/unpackPqTextArray.h | 27 +++++++++++ gentoobrowse-api/service/unpackPqTextArray.ll | 64 +++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 gentoobrowse-api/service/converters.cpp create mode 100644 gentoobrowse-api/service/unpackPqTextArray.h create mode 100644 gentoobrowse-api/service/unpackPqTextArray.ll diff --git a/gentoobrowse-api/domain/portage-models.ice b/gentoobrowse-api/domain/portage-models.ice index 744cbf5..4f98ce3 100644 --- a/gentoobrowse-api/domain/portage-models.ice +++ b/gentoobrowse-api/domain/portage-models.ice @@ -5,6 +5,7 @@ module Gentoo { sequence Image; + sequence StringList; class Category { ["slicer:db:pkey"] diff --git a/gentoobrowse-api/service/Jamfile.jam b/gentoobrowse-api/service/Jamfile.jam index b29063e..777c453 100644 --- a/gentoobrowse-api/service/Jamfile.jam +++ b/gentoobrowse-api/service/Jamfile.jam @@ -1,8 +1,10 @@ import generators ; import type ; +import lex ; lib gentoobrowse-service : [ glob *.cpp ] + [ glob *.ll ] [ glob-tree *.sql ] [ glob ../domain/*.ice ] : @@ -18,6 +20,7 @@ lib gentoobrowse-service : ..//boost_thread ..//boost_date_time ../..//glibmm + . : : . ../api//gentoobrowse-api diff --git a/gentoobrowse-api/service/converters.cpp b/gentoobrowse-api/service/converters.cpp new file mode 100644 index 0000000..b0ffacf --- /dev/null +++ b/gentoobrowse-api/service/converters.cpp @@ -0,0 +1,22 @@ +#include +#include "unpackPqTextArray.h" +#include + +namespace Slicer { + ::Gentoo::StringList + unpackPqTextArray(const std::string & s) + { + ::Gentoo::StringList list; + std::stringstream ss(s); + ::Portage::Utils::UnpackPqTextArray l(ss, list); + l.yylex(); + return list; + } + + std::string + packPqTextArray(const ::Gentoo::StringList &) + { + return std::string(); + } +} + diff --git a/gentoobrowse-api/service/unpackPqTextArray.h b/gentoobrowse-api/service/unpackPqTextArray.h new file mode 100644 index 0000000..246be71 --- /dev/null +++ b/gentoobrowse-api/service/unpackPqTextArray.h @@ -0,0 +1,27 @@ +#ifndef JSONFLEXLEXER_H +#define JSONFLEXLEXER_H + +#include +#ifndef yyFlexLexer +#define yyFlexLexer pqBaseFlexLexer +#include +#endif + +namespace Portage { + namespace Utils { + class UnpackPqTextArray : public yyFlexLexer { + public: + UnpackPqTextArray(std::istream &, ::Gentoo::StringList & sl); + + int yylex() override; + void LexerError(const char * msg) override; + + private: + ::Gentoo::StringList & list; + }; + } +} + +#endif + + diff --git a/gentoobrowse-api/service/unpackPqTextArray.ll b/gentoobrowse-api/service/unpackPqTextArray.ll new file mode 100644 index 0000000..6281063 --- /dev/null +++ b/gentoobrowse-api/service/unpackPqTextArray.ll @@ -0,0 +1,64 @@ +%option batch +%option c++ +%option noyywrap +%option 8bit +%option stack +%option yylineno +%option yyclass="Portage::Utils::UnpackPqTextArray" +%option prefix="pqBase" + +%{ +#include "unpackPqTextArray.h" +#include +#pragma GCC diagnostic ignored "-Wsign-compare" +%} + +begin "{" +end "}" +comma , +unquoted [^,\"}]+ +quoted (\"[^\"]+\") + +%x FIRST +%x AFTER +%x NEXT + +%% + +{begin} { + yy_push_state(FIRST); +} +{end} { + yy_pop_state(); +} +{unquoted} { + list.push_back(YYText()); + BEGIN(AFTER); +} +{quoted} { + list.push_back(YYText()); + boost::algorithm::trim_if(list.back(), [](auto c){ return c == '"'; }); + BEGIN(AFTER); +} +{comma} { + BEGIN(NEXT); +} + +%% + +namespace Portage { + namespace Utils { + UnpackPqTextArray::UnpackPqTextArray(std::istream & f, ::Gentoo::StringList & sl) : + yyFlexLexer(&f, NULL), + list(sl) + { + } + + void + UnpackPqTextArray::LexerError(const char * msg) + { + throw std::runtime_error(msg); + } + } +} + -- cgit v1.2.3