From 94f07acffb4a61731a30f08336e79dcf3ad772d7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 19 Apr 2016 00:22:55 +0100 Subject: Add a swaith of utility classes --- gentoobrowse-api/service/Jamfile.jam | 1 + gentoobrowse-api/service/dbUtils.cpp | 21 ++++++++++++++++ gentoobrowse-api/service/dbUtils.h | 15 ++++++++++++ gentoobrowse-api/service/fileUtils.cpp | 45 ++++++++++++++++++++++++++++++++++ gentoobrowse-api/service/fileUtils.h | 40 ++++++++++++++++++++++++++++++ gentoobrowse-api/service/xmlUtils.cpp | 26 ++++++++++++++++++++ gentoobrowse-api/service/xmlUtils.h | 20 +++++++++++++++ 7 files changed, 168 insertions(+) create mode 100644 gentoobrowse-api/service/dbUtils.cpp create mode 100644 gentoobrowse-api/service/dbUtils.h create mode 100644 gentoobrowse-api/service/fileUtils.cpp create mode 100644 gentoobrowse-api/service/fileUtils.h create mode 100644 gentoobrowse-api/service/xmlUtils.cpp create mode 100644 gentoobrowse-api/service/xmlUtils.h diff --git a/gentoobrowse-api/service/Jamfile.jam b/gentoobrowse-api/service/Jamfile.jam index c4b1bad..c5a19a2 100644 --- a/gentoobrowse-api/service/Jamfile.jam +++ b/gentoobrowse-api/service/Jamfile.jam @@ -23,6 +23,7 @@ lib gentoobrowse-service : ..//boost_filesystem ..//boost_date_time ../..//glibmm + ../..//libxmlpp Gentoo::Service . : : diff --git a/gentoobrowse-api/service/dbUtils.cpp b/gentoobrowse-api/service/dbUtils.cpp new file mode 100644 index 0000000..0bdb279 --- /dev/null +++ b/gentoobrowse-api/service/dbUtils.cpp @@ -0,0 +1,21 @@ +#include "dbUtils.h" + +namespace Gentoo { + namespace Utils { + namespace Database { + bool + bindOptionalsS(const DB::CommandPtr & db, unsigned int c, const std::vector > & 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/dbUtils.h b/gentoobrowse-api/service/dbUtils.h new file mode 100644 index 0000000..1f3e2f3 --- /dev/null +++ b/gentoobrowse-api/service/dbUtils.h @@ -0,0 +1,15 @@ +#ifndef GENTOOBROWSE_API_SERVICE_DBUTILS_H +#define GENTOOBROWSE_API_SERVICE_DBUTILS_H + +#include + +namespace Gentoo { + namespace Utils { + namespace Database { + bool bindOptionalsS(const DB::CommandPtr & db, unsigned int c, const std::vector > & vs); + } + } +} + +#endif + diff --git a/gentoobrowse-api/service/fileUtils.cpp b/gentoobrowse-api/service/fileUtils.cpp new file mode 100644 index 0000000..cfa02bc --- /dev/null +++ b/gentoobrowse-api/service/fileUtils.cpp @@ -0,0 +1,45 @@ +#include "fileUtils.h" +#include +#include +#include + +namespace Gentoo { + namespace Utils { + FileHandle::FileHandle(const std::string & path) : + fh(open(path.c_str(), O_RDONLY)) + { + if (fh < 0) { + throw std::runtime_error("Failed to open " + path); + } + } + + FileHandle::~FileHandle() + { + close(fh); + } + + FileHandleStat::FileHandleStat(const std::string & path) : + FileHandle(path) + { + if (fstat(fh, &st)) { + throw std::runtime_error("Failed to stat " + path); + } + } + + MemMap::MemMap(const std::string & 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); + } + } + + MemMap::~MemMap() + { + munmap(data, st.st_size); + } + + } +} + diff --git a/gentoobrowse-api/service/fileUtils.h b/gentoobrowse-api/service/fileUtils.h new file mode 100644 index 0000000..58d235f --- /dev/null +++ b/gentoobrowse-api/service/fileUtils.h @@ -0,0 +1,40 @@ +#ifndef GENTOOBROWSE_API_SERVICE_FILEUTILS_H +#define GENTOOBROWSE_API_SERVICE_FILEUTILS_H + +#include +#include + +namespace Gentoo { + namespace Utils { + class FileHandle { + public: + FileHandle(const std::string & path); + ~FileHandle(); + + protected: + const int fh; + }; + + class FileHandleStat : public FileHandle { + public: + FileHandleStat(const std::string & path); + + protected: + struct stat st; + }; + + class MemMap : public FileHandleStat { + public: + MemMap(const std::string & path); + ~MemMap(); + + protected: + void * data; + }; + + } +} + +#endif + + diff --git a/gentoobrowse-api/service/xmlUtils.cpp b/gentoobrowse-api/service/xmlUtils.cpp new file mode 100644 index 0000000..3fc3b44 --- /dev/null +++ b/gentoobrowse-api/service/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 + XmlDoc::getXPathValue(const Glib::ustring & xp) + { + auto ns = get_document()->get_root_node()->find(xp); + if (ns.size() >= 1) { + if (auto cn = dynamic_cast(ns.front())) { + return cn->get_content(); + } + } + else if (ns.size() > 1) { + throw std::logic_error("Ambiguous xpath " + xp); + } + return boost::optional(); + } + } +} + diff --git a/gentoobrowse-api/service/xmlUtils.h b/gentoobrowse-api/service/xmlUtils.h new file mode 100644 index 0000000..158e134 --- /dev/null +++ b/gentoobrowse-api/service/xmlUtils.h @@ -0,0 +1,20 @@ +#ifndef GENTOOBROWSE_API_SERVICE_XMLUTILS_H +#define GENTOOBROWSE_API_SERVICE_XMLUTILS_H + +#include +#include +#include + +namespace Gentoo { + namespace Utils { + class XmlDoc : public xmlpp::DomParser { + public: + XmlDoc(const boost::filesystem::path &); + + boost::optional getXPathValue(const Glib::ustring &); + }; + } +} + +#endif + -- cgit v1.2.3