// -*- C++ -*- /*****************************************************************************\ * Copyright (c) 2004 Mark Aylett * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, sublicense, and/or sell copies of the Software, and to permit * * persons to whom the Software is furnished to do so, subject to the * * following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * * USE OR OTHER DEALINGS IN THE SOFTWARE. * \*****************************************************************************/ /** * \file mar_util_cpp.h * \brief TODO */ #ifndef INCLUDED_MAR_UTIL_CPP #define INCLUDED_MAR_UTIL_CPP #ifndef INCLUDED_MAR_ARCHIVE_CPP #include "mar_archive_cpp.h" #endif // INCLUDED_MAR_ARCHIVE_CPP #ifndef INCLUDED_MAR_EXCEPTION_CPP #include "mar_exception_cpp.h" #endif // INCLUDED_MAR_EXCEPTION_CPP namespace mar { inline bool result(int ret) { if (-1 == ret) throw error(); return MAR_NOMATCH != ret; } // Creational. inline archive create() { archive ar(archive::create()); if (!ar) throw error(); return ar; } inline archive open(const char* path, int flags) { archive ar(archive::open(path, flags)); if (!ar) throw error(); return ar; } inline archive open(const char* path, int flags, mode_t mode) { archive ar(archive::open(path, flags, mode)); if (!ar) throw error(); return ar; } inline archive opencopy(const char* path, const char* master, int flags = rdwr | creat | excl, mode_t mode = 0664) { archive ar(archive::opencopy(path, master, flags, mode)); if (!ar) throw error(); return ar; } inline void copy(archive& dst, const archive& src) { if (-1 == dst.copy(src)) throw error(); } inline void release(archive& ar) { if (-1 == ar.release()) throw error(); } // Meta Modifiers. inline void removepairs(archive& ar) { if (-1 == ar.removepairs()) throw error(); } inline void setmeta(archive& ar, size_t ord, const void* data, size_t size) { if (-1 == ar.setmeta(ord, data, size)) throw error(); } inline void setmeta(archive& ar, size_t ord, const char* data) { setmeta(ar, ord, data, strlen(data)); } inline size_t setmeta(archive& ar, const pair& p) { size_t ord; if (-1 == ar.setmeta(p, ord)) throw error(); return ord; } inline std::pair unsetmeta(archive& ar, const char* key) { size_t ord; bool match(result(ar.unsetmeta(key, ord))); return std::make_pair(ord, match); } inline bool unsetmeta(archive& ar, size_t ord) { return result(ar.unsetmeta(ord)); } // User Modifiers. inline void insert(archive& ar, const char* path) { if (-1 == ar.insert(path)) throw error(); } inline size_t read(archive& ar, void* buf, size_t size) { ssize_t ret(ar.read(buf, size)); if (-1 == ret) throw error(); return ret; } inline off_t seek(archive& ar, off_t offset, int whence) { off_t ret(ar.seek(offset, whence)); if (-1 == ret) throw error(); return ret; } inline void setuser(archive& ar, const void* data, size_t size) { if (-1 == ar.setuser(data, size)) throw error(); } inline void setuser(archive& ar, const char* data) { setuser(ar, data, strlen(data)); } inline void sync(archive& ar) { if (-1 == ar.sync()) throw error(); } inline void truncate(archive& ar, size_t size) { if (-1 == ar.truncate(size)) throw error(); } inline size_t write(archive& ar, const void* buf, size_t size) { ssize_t ret(ar.write(buf, size)); if (-1 == ret) throw error(); return ret; } // Meta Accessors. inline const void* meta(const archive& ar, const char* key, size_t& size) { const void* ret = ar.meta(key, size); if (!ret) throw error(); return ret; } inline const void* meta(const archive& ar, const char* key) { const void* ret = ar.meta(key); if (!ret) throw error(); return ret; } inline const void* meta(const archive& ar, size_t ord, size_t& size) { const void* ret = ar.meta(ord, size); if (!ret) throw error(); return ret; } inline const void* meta(const archive& ar, size_t ord) { const void* ret = ar.meta(ord); if (!ret) throw error(); return ret; } inline bool meta(const archive& ar, pair& p, size_t ord) { return result(ar.meta(p, ord)); } inline size_t metapairs(const archive& ar) { size_t size; if (-1 == ar.metapairs(size)) throw error(); return size; } inline bool tokey(const archive& ar, key& k, size_t ord) { return result(ar.tokey(k, ord)); } inline std::pair toord(const archive& ar, const char* key) { size_t ord; bool match(result(ar.toord(ord, key))); return std::make_pair(ord, match); } // User Accessors. inline void extract(const archive& ar, const char* path) { if (-1 == ar.extract(path)) throw error(); } inline const void* user(const archive& ar, size_t& size) { const void* ret = ar.user(size); if (!ret) throw error(); return ret; } inline const void* user(const archive& ar) { const void* ret = ar.user(); if (!ret) throw error(); return ret; } inline size_t usersize(const archive& ar) { size_t size; if (-1 == ar.usersize(size)) throw error(); return size; } } #endif // INCLUDED_MAR_UTIL_CPP