summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-04-19 00:22:55 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2016-04-25 21:25:33 +0100
commit7f63b402365746e61be288688826760ba7c86a93 (patch)
tree450650cf9e9b9aa7cafb8111be5ef2eedba4254b
parentSet up basic file processing from table patch audit callbacks (diff)
downloadgentoobrowse-api-7f63b402365746e61be288688826760ba7c86a93.tar.bz2
gentoobrowse-api-7f63b402365746e61be288688826760ba7c86a93.tar.xz
gentoobrowse-api-7f63b402365746e61be288688826760ba7c86a93.zip
Add a swaith of utility classes
-rw-r--r--gentoobrowse-api/service/Jamfile.jam1
-rw-r--r--gentoobrowse-api/service/dbUtils.cpp21
-rw-r--r--gentoobrowse-api/service/dbUtils.h15
-rw-r--r--gentoobrowse-api/service/fileUtils.cpp45
-rw-r--r--gentoobrowse-api/service/fileUtils.h40
-rw-r--r--gentoobrowse-api/service/xmlUtils.cpp26
-rw-r--r--gentoobrowse-api/service/xmlUtils.h20
7 files changed, 168 insertions, 0 deletions
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 :
<library>..//boost_filesystem
<library>..//boost_date_time
<library>../..//glibmm
+ <library>../..//libxmlpp
<icetray.sql.namespace>Gentoo::Service
<include>.
: :
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<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/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 <command.h>
+
+namespace Gentoo {
+ namespace Utils {
+ namespace Database {
+ bool bindOptionalsS(const DB::CommandPtr & db, unsigned int c, const std::vector<boost::optional<Glib::ustring> > & 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 <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+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 <boost/filesystem/path.hpp>
+#include <sys/stat.h>
+
+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<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/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 <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
+