diff options
Diffstat (limited to 'gentoobrowse-api/service/utils')
-rw-r--r-- | gentoobrowse-api/service/utils/dbUtils.cpp | 21 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/dbUtils.h | 15 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/ebuildCacheParser.cpp | 34 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/ebuildCacheParser.h | 28 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/entityWhereFilter.cpp | 25 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/entityWhereFilter.h | 22 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/fileUtils.cpp | 60 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/fileUtils.h | 45 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/splitEbuildProps.cpp | 29 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/splitEbuildProps.h | 25 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/xmlUtils.cpp | 26 | ||||
-rw-r--r-- | gentoobrowse-api/service/utils/xmlUtils.h | 20 |
12 files changed, 350 insertions, 0 deletions
diff --git a/gentoobrowse-api/service/utils/dbUtils.cpp b/gentoobrowse-api/service/utils/dbUtils.cpp new file mode 100644 index 0000000..f2b7f4d --- /dev/null +++ b/gentoobrowse-api/service/utils/dbUtils.cpp @@ -0,0 +1,21 @@ +#include "dbUtils.h" + +namespace Gentoo { + namespace Utils { + namespace Database { + bool + bindOptionalsS(DB::Command * db, unsigned int c, const std::vector<boost::optional<Glib::ustring> > & vs) + { + for(const auto & v : vs) { + if (v) { + db->bindParamS(c, *v); + return true; + } + } + db->bindNull(c); + return false; + } + } + } +} + diff --git a/gentoobrowse-api/service/utils/dbUtils.h b/gentoobrowse-api/service/utils/dbUtils.h new file mode 100644 index 0000000..e609c2d --- /dev/null +++ b/gentoobrowse-api/service/utils/dbUtils.h @@ -0,0 +1,15 @@ +#ifndef GENTOOBROWSE_API_SERVICE_DBUTILS_H +#define GENTOOBROWSE_API_SERVICE_DBUTILS_H + +#include <command.h> + +namespace Gentoo { + namespace Utils { + namespace Database { + bool bindOptionalsS(DB::Command * db, unsigned int c, const std::vector<boost::optional<Glib::ustring> > & vs); + } + } +} + +#endif + diff --git a/gentoobrowse-api/service/utils/ebuildCacheParser.cpp b/gentoobrowse-api/service/utils/ebuildCacheParser.cpp new file mode 100644 index 0000000..ad894db --- /dev/null +++ b/gentoobrowse-api/service/utils/ebuildCacheParser.cpp @@ -0,0 +1,34 @@ +#include "ebuildCacheParser.h" + +namespace U = Gentoo::Utils; + +namespace Gentoo { + namespace Utils { + EbuildCacheParser::EbuildCacheParser(const boost::filesystem::path & p) : + U::MemMap(p) + { + const char * chardata = (const char *)this->data; + while (const char * eq = strchr(chardata, '=')) { + if (const char * nl = strchr(eq + 1, '\n')) { + kvs.insert({ std::string(chardata, eq), { eq + 1, nl } }); + chardata = nl + 1; + } + else { + kvs.insert({ std::string(chardata, eq), { eq + 1, (const char *)this->data + st.st_size } }); + return; + } + } + } + + boost::optional<Glib::ustring> + EbuildCacheParser::get(const std::string & key) const + { + auto kvi = kvs.find(key); + if (kvi == kvs.end()) { + return boost::optional<Glib::ustring>(); + } + return Glib::ustring(kvi->second.first, kvi->second.second); + } + } +} + diff --git a/gentoobrowse-api/service/utils/ebuildCacheParser.h b/gentoobrowse-api/service/utils/ebuildCacheParser.h new file mode 100644 index 0000000..e9f1dfc --- /dev/null +++ b/gentoobrowse-api/service/utils/ebuildCacheParser.h @@ -0,0 +1,28 @@ +#ifndef GENTOOBROWSE_API_SERVICE_MAINTENANCE_EBUILDCACHEPARSER_H +#define GENTOOBROWSE_API_SERVICE_MAINTENANCE_EBUILDCACHEPARSER_H + +#include "fileUtils.h" +#include <map> +#include <boost/optional.hpp> +#include <boost/filesystem/path.hpp> +#include <glibmm/ustring.h> + +namespace Gentoo { + namespace Utils { + class EbuildCacheParser : public Gentoo::Utils::MemMap { + public: + typedef std::pair<const char *, const char *> Range; + typedef std::map<std::string, const Range> KVs; + + EbuildCacheParser(const boost::filesystem::path & p); + + boost::optional<Glib::ustring> get(const std::string & key) const; + + private: + KVs kvs; + }; + } +} + +#endif + diff --git a/gentoobrowse-api/service/utils/entityWhereFilter.cpp b/gentoobrowse-api/service/utils/entityWhereFilter.cpp new file mode 100644 index 0000000..8e29cfe --- /dev/null +++ b/gentoobrowse-api/service/utils/entityWhereFilter.cpp @@ -0,0 +1,25 @@ +#include "entityWhereFilter.h" +#include <command.h> + +namespace Gentoo { + namespace Utils { + EntityWhereFilter::EntityWhereFilter(const std::string & en, int64_t e) : + entityId(e), + entityColName(en) + { + } + + void + EntityWhereFilter::writeSql(AdHoc::Buffer & sql) + { + sql.appendbf("b.%s = ?", entityColName); + } + + void + EntityWhereFilter::bindParams(DB::Command * c, unsigned int & offset) + { + c->bindParamI(offset++, entityId); + } + } +} + diff --git a/gentoobrowse-api/service/utils/entityWhereFilter.h b/gentoobrowse-api/service/utils/entityWhereFilter.h new file mode 100644 index 0000000..20420c3 --- /dev/null +++ b/gentoobrowse-api/service/utils/entityWhereFilter.h @@ -0,0 +1,22 @@ +#ifndef GENTOOBROWSE_API_SERVICE_MAINTENANCE_ENTITYWHEREFILTER_H +#define GENTOOBROWSE_API_SERVICE_MAINTENANCE_ENTITYWHEREFILTER_H + +#include <sqlWriter.h> + +namespace Gentoo { + namespace Utils { + class EntityWhereFilter : public DB::SqlWriter { + public: + EntityWhereFilter(const std::string & en, int64_t e); + + void writeSql(AdHoc::Buffer & sql) override; + void bindParams(DB::Command * c, unsigned int & offset) override; + + const int64_t entityId; + const std::string entityColName; + }; + } +} + +#endif + diff --git a/gentoobrowse-api/service/utils/fileUtils.cpp b/gentoobrowse-api/service/utils/fileUtils.cpp new file mode 100644 index 0000000..1446b9f --- /dev/null +++ b/gentoobrowse-api/service/utils/fileUtils.cpp @@ -0,0 +1,60 @@ +#include "fileUtils.h" +#include <fcntl.h> +#include <unistd.h> +#include <sys/mman.h> + +namespace Gentoo { + namespace Utils { + namespace File { + boost::filesystem::path operator/(const boost::filesystem::path & p, unsigned int n) + { + auto pp = p.begin(); + while (n--) ++pp; + return *pp; + } + } + + FileHandle::FileHandle(const boost::filesystem::path & path) : + fh(open(path.c_str(), O_RDONLY)) + { + if (fh < 0) { + throw std::runtime_error("Failed to open " + path.string()); + } + } + + FileHandle::~FileHandle() + { + close(fh); + } + + FileHandleStat::FileHandleStat(const boost::filesystem::path & path) : + FileHandle(path) + { + if (fstat(fh, &st)) { + throw std::runtime_error("Failed to stat " + path.string()); + } + } + + const struct stat & + FileHandleStat::getStat() const + { + return st; + } + + MemMap::MemMap(const boost::filesystem::path & path) : + FileHandleStat(path), + data(mmap(0, st.st_size, PROT_READ, MAP_SHARED, fh, 0)) + { + if (data == (void*)-1) { + throw std::runtime_error("Failed to mmap " + path.string()); + } + } + + MemMap::~MemMap() + { + munmap(data, st.st_size); + } + + } +} + diff --git a/gentoobrowse-api/service/utils/fileUtils.h b/gentoobrowse-api/service/utils/fileUtils.h new file mode 100644 index 0000000..d73b1ef --- /dev/null +++ b/gentoobrowse-api/service/utils/fileUtils.h @@ -0,0 +1,45 @@ +#ifndef GENTOOBROWSE_API_SERVICE_FILEUTILS_H +#define GENTOOBROWSE_API_SERVICE_FILEUTILS_H + +#include <boost/filesystem/path.hpp> +#include <sys/stat.h> + +namespace Gentoo { + namespace Utils { + namespace File { + boost::filesystem::path operator/(const boost::filesystem::path & p, unsigned int n); + } + + class FileHandle { + public: + FileHandle(const boost::filesystem::path & path); + virtual ~FileHandle(); + + protected: + const int fh; + }; + + class FileHandleStat : public FileHandle { + public: + FileHandleStat(const boost::filesystem::path & path); + + const struct stat & getStat() const; + + protected: + struct stat st; + }; + + class MemMap : public FileHandleStat { + public: + MemMap(const boost::filesystem::path & path); + ~MemMap(); + + void * const data; + }; + + } +} + +#endif + + diff --git a/gentoobrowse-api/service/utils/splitEbuildProps.cpp b/gentoobrowse-api/service/utils/splitEbuildProps.cpp new file mode 100644 index 0000000..8116742 --- /dev/null +++ b/gentoobrowse-api/service/utils/splitEbuildProps.cpp @@ -0,0 +1,29 @@ +#include "splitEbuildProps.h" +#include <command.h> +#include "dbUtils.h" + +namespace Gentoo { + namespace Utils { + SplitEbuildProps::SplitEbuildProps(const std::string & ce, int e, const std::string & cp, const boost::optional<Glib::ustring> & p) : + entityId(e), + colEntityName(ce), + colPropName(cp), + props(p) + { + } + + void + SplitEbuildProps::writeSql(AdHoc::Buffer & sql) + { + sql.appendbf("(SELECT DISTINCT ?::int %s, trim(regexp_split_to_table(?, '\\s+'), '+') %s)", colEntityName, colPropName); + } + + void + SplitEbuildProps::bindParams(DB::Command * c, unsigned int & offset) + { + c->bindParamI(offset++, entityId); + Gentoo::Utils::Database::bindOptionalsS(c, offset++, { props }); + } + } +} + diff --git a/gentoobrowse-api/service/utils/splitEbuildProps.h b/gentoobrowse-api/service/utils/splitEbuildProps.h new file mode 100644 index 0000000..4739b7e --- /dev/null +++ b/gentoobrowse-api/service/utils/splitEbuildProps.h @@ -0,0 +1,25 @@ +#ifndef GENTOOBROWSE_API_SERVICE_MAINTENANCE_SPLITEBUILDPROPS_H +#define GENTOOBROWSE_API_SERVICE_MAINTENANCE_SPLITEBUILDPROPS_H + +#include <sqlWriter.h> +#include <glibmm/ustring.h> +#include <boost/optional.hpp> + +namespace Gentoo { + namespace Utils { + class SplitEbuildProps : public DB::SqlWriter { + public: + SplitEbuildProps(const std::string & ce, int e, const std::string & cp, const boost::optional<Glib::ustring> & p); + + void writeSql(AdHoc::Buffer & sql) override; + void bindParams(DB::Command * c, unsigned int & offset) override; + + const int entityId; + const std::string colEntityName, colPropName; + const boost::optional<Glib::ustring> props; + }; + } +} + +#endif + diff --git a/gentoobrowse-api/service/utils/xmlUtils.cpp b/gentoobrowse-api/service/utils/xmlUtils.cpp new file mode 100644 index 0000000..3fc3b44 --- /dev/null +++ b/gentoobrowse-api/service/utils/xmlUtils.cpp @@ -0,0 +1,26 @@ +#include "xmlUtils.h" + +namespace Gentoo { + namespace Utils { + XmlDoc::XmlDoc(const boost::filesystem::path & path) : + xmlpp::DomParser(path.string()) + { + } + + boost::optional<Glib::ustring> + XmlDoc::getXPathValue(const Glib::ustring & xp) + { + auto ns = get_document()->get_root_node()->find(xp); + if (ns.size() >= 1) { + if (auto cn = dynamic_cast<const xmlpp::ContentNode *>(ns.front())) { + return cn->get_content(); + } + } + else if (ns.size() > 1) { + throw std::logic_error("Ambiguous xpath " + xp); + } + return boost::optional<Glib::ustring>(); + } + } +} + diff --git a/gentoobrowse-api/service/utils/xmlUtils.h b/gentoobrowse-api/service/utils/xmlUtils.h new file mode 100644 index 0000000..158e134 --- /dev/null +++ b/gentoobrowse-api/service/utils/xmlUtils.h @@ -0,0 +1,20 @@ +#ifndef GENTOOBROWSE_API_SERVICE_XMLUTILS_H +#define GENTOOBROWSE_API_SERVICE_XMLUTILS_H + +#include <libxml++/parsers/domparser.h> +#include <boost/filesystem/path.hpp> +#include <boost/optional.hpp> + +namespace Gentoo { + namespace Utils { + class XmlDoc : public xmlpp::DomParser { + public: + XmlDoc(const boost::filesystem::path &); + + boost::optional<Glib::ustring> getXPathValue(const Glib::ustring &); + }; + } +} + +#endif + |