commit 88315be53a4fbcd5f9d617f54dc815d6ed9d543e Author: Volker Birk Date: Tue Jan 16 22:15:00 2018 +0100 initial version diff --git a/.hgignore b/.hgignore new file mode 100644 index 0000000..cbab4c3 --- /dev/null +++ b/.hgignore @@ -0,0 +1,4 @@ +syntax: glob +*.o +*.a +*.swp diff --git a/BlobList.cc b/BlobList.cc new file mode 100644 index 0000000..90dd00d --- /dev/null +++ b/BlobList.cc @@ -0,0 +1,87 @@ +#include "BlobList.hh" + +namespace pEp { + namespace Common { + BlobList::Iterator& BlobList::Iterator::operator= (const BlobList::Iterator& second) + { + p = second.p; + return *this; + } + + const BlobList::Blob& BlobList::Iterator::operator* () + { + if (!p) + throw out_of_range("BlobList::Iterator points to nowhere"); + + if (_p != p) { + _b = { p->filename, p->mime_type, p->value, p->size, p->disposition }; + _p = p; + } + return _b; + } + + BlobList::Iterator& BlobList::Iterator::operator++ () + { + if (!p) + throw out_of_range("BlobList::Iterator points to nowhere"); + + p = p->next; + return *this; + } + + BlobList::Iterator BlobList::Iterator::operator++ (int) + { + Iterator second = *this; + (*this)++; + return second; + } + + BlobList::Iterator BlobList::end() + { + return BlobList::Iterator(); + } + + BlobList::Iterator BlobList::begin() + { + BlobList::Iterator _begin; + _begin.iterating = this; + _begin.p = this->bl.get(); + return _begin; + } + + BlobList::BlobList() + : bl(::new_bloblist(nullptr, 0, nullptr, nullptr), &::free_bloblist), + _size(0) + { + if (!bl.get()) + throw bad_alloc(); + bl->release_value = release_value; + } + + BlobList::BlobList(const BlobList& second) + : bl(::bloblist_dup(second.bl.get()), &::free_bloblist), + _size(second._size) + { + if (!bl.get()) + throw bad_alloc(); + } + + BlobList& BlobList::operator= (const BlobList& second) + { + bl = blobdata(::bloblist_dup(second.bl.get()), &::free_bloblist); + return *this; + } + + void BlobList::release_value(char *v) + { + delete[] v; + } + + void BlobList::push_back(const Blob& val) + { + ::bloblist_add(bl.get(), val.blob, val.size, val.mime_type.c_str(), val.filename.c_str()); + ++_size; + } + }; +}; + diff --git a/BlobList.hh b/BlobList.hh new file mode 100644 index 0000000..0e1e6a9 --- /dev/null +++ b/BlobList.hh @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include + +#include + +namespace pEp { + namespace Common { + using namespace std; + + class BlobList { + public: + struct Blob { + string filename; + string mime_type; + char *blob = nullptr; + size_t size = 0; + content_disposition_type cd = PEP_CONTENT_DISP_ATTACHMENT; + }; + + class Iterator : public iterator< forward_iterator_tag, Blob > { + friend class BlobList; + + private: + ::bloblist_t *p = nullptr; + ::bloblist_t *_p = nullptr; + Blob _b; + BlobList *iterating = nullptr; + + public: + Iterator() { } + Iterator(const Iterator& second) : p(second.p), iterating(second.iterating) { } + Iterator& operator= (const Iterator& second); + ~Iterator() { } + bool operator== (const Iterator& second) const { return p == second.p; } + bool operator!= (const Iterator& second) const { return p != second.p; } + const Blob& operator* (); + operator bloblist_t *() { return p; } + Iterator& operator++ (); + Iterator operator++ (int); + }; + + Iterator end(); + Iterator begin(); + + BlobList(); + BlobList(const BlobList& second); + BlobList& operator= (const BlobList& second); + ~BlobList(); + + void push_back(const Blob& val); + size_t size() const { return _size; } + bool empty() const { return _size == 0; } + + private: + static void release_value(char *v); + typedef unique_ptr< ::bloblist_t, decltype(&::free_bloblist) > blobdata; + blobdata bl; + size_t _size; + }; + }; +}; + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9b6152c --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +include Makefile.conf +-include local.conf + +TARGET?=libpEpAdapter.a +PEPENGINE_IN?=$(HOME) + +CXXFLAGS += -I$(HOME)/include -std=c++14 + +SOURCE=$(wildcard *.cc) +OBJECTS=$(subst .cc,.o,$(SOURCE)) + +all: $(TARGET) + +%.o: %.cc %.hh + $(CXX) $(CXXFLAGS) -c $< + +$(TARGET): $(OBJECTS) + ar -rc $@ $^ + +.PHONY: clean + +clean: + rm -f $(TARGET) $(OBJECTS) diff --git a/Makefile.conf b/Makefile.conf new file mode 100644 index 0000000..be29840 --- /dev/null +++ b/Makefile.conf @@ -0,0 +1,2 @@ +# TARGET=libpEpAdapter.a +# PEPENGINE_IN=$(HOME)