You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libpEpDatatypes/src/types.cc

183 lines
4.4 KiB

#include "types.hh"
#include <pEp/status_to_string.hh> // from libpEpAdapter
#include <pEp/mime.h>
/*
#include <pEp/pEpEngine.h>
#include <pEp/stringpair.h>
#include <pEp/message.h>
*/
#include <string>
#include <cstring>
namespace pEp
{
EngineError::EngineError(PEP_STATUS status, const char* message)
: std::runtime_error(
std::string{"EngineError: "}
+ (message ? '"' + std::string{message} + "\" " : std::string{} )
+ status_to_string(status)
)
{}
////////////////
template<>
template<>
message* Wrapper<::message*>::_new<PEP_msg_direction, const char*>(PEP_msg_direction dir, const char* src)
{
message* m = nullptr;
bool pep_msg = false;
PEP_STATUS status = mime_decode_message(src, strlen(src), &m, &pep_msg);
if(status != PEP_STATUS_OK)
{
throw EngineError(status, "mime_decode_message()");
}
m->dir = dir;
return m;
}
template<>
void Wrapper<::message*>::_free(::message* m)
{
free_message(m);
}
template<>
::message* Wrapper<::message*>::copy_out() const
{
return ::message_dup(value);
}
////////////////
template<>
template<>
::stringpair_t* Wrapper<::stringpair_t*>::_new(const char* key, const char* value)
{
stringpair_t* sp = new_stringpair(key, value);
if(!sp)
{
throw EngineError(PEP_OUT_OF_MEMORY, "new_stringpair()");
}
return sp;
}
template<>
template<>
::stringpair_t* Wrapper<::stringpair_t*>::_new(char* key, char* value)
{
return _new<const char*, const char*>(
const_cast<const char*>(key),
const_cast<const char*>(value)
);
}
template<>
template<>
::stringpair_t* Wrapper<::stringpair_t*>::_new(const std::string& key, const std::string& value)
{
return Wrapper<::stringpair_t*>::_new(key.c_str(), value.c_str());
}
template<>
void Wrapper<::stringpair_t*>::_free(::stringpair_t* sp)
{
free_stringpair(sp);
}
template<>
::stringpair_t* Wrapper<::stringpair_t*>::copy_out() const
{
return ::stringpair_dup(value);
}
template<>
void Wrapper<::stringpair_list_t*>::_free(::stringpair_list_t* spl)
{
free_stringpair_list(spl);
}
template<>
stringpair_t* stringpair_list_t::* const ListWrapper<stringpair_list_t*, stringpair_t*>::Value = &stringpair_list_t::value;
template<>
int StringPairList::size() const
{
return stringpair_list_length(value);
}
// faster than .size()==0 because it's not necessary to iterate throgh the whole list
template<>
bool StringPairList::empty() const
{
return !(value && value->value);
}
template<>
void StringPairList::erase( const StringPairList::iterator& it)
{
if(it.value && it.value->value && it.value->value->key)
{
value = stringpair_list_delete_by_key(value, it.value->value->key);
}
}
template<>
void StringPairList::clear()
{
free_stringpair_list(value);
value = nullptr;
}
template<>
void StringPairList::push_back(::stringpair_t*&& sp)
{
auto last = stringpair_list_add(value, sp);
if(value==nullptr)
value = last;
sp = nullptr;
}
template<>
void StringPairList::push_back(StringPair&& sp)
{
auto last = stringpair_list_add(value, sp.move_out());
if(value==nullptr)
value = last;
}
template<>
ListWrapper<::stringpair_list_t*, stringpair_t*>::ListWrapper(const std::initializer_list<StringPair>& il)
: StringPairList{}
{
::stringpair_list_t* last = nullptr;
for(const StringPair& sp : il)
{
last = stringpair_list_add(last, stringpair_dup(sp.get()));
if(last==nullptr)
{
throw std::runtime_error("Cannot create StringPairList from {}: Out Of Memory.");
}
if(value==nullptr)
value = last; // save the head of linked list.
}
}
////////////////
template class Wrapper<::pEp_identity*>;
template class Wrapper<::stringpair_t*>;
template class Wrapper<::message*>;
template class ListWrapper<::stringpair_list_t*, ::stringpair_t*>;
} // end of namespace pEp