blob: ea093d50f57a18b2b3e060a50c90b07f9bc6748e [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2016 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
#ifndef NDN_UTIL_IO_HPP
#define NDN_UTIL_IO_HPP
#include "../encoding/block.hpp"
#include <iostream>
#include <fstream>
namespace ndn {
namespace io {
class Error : public std::runtime_error
{
public:
explicit
Error(const std::string& what)
: std::runtime_error(what)
{
}
};
/** \brief indicates how a file or stream is encoded
*/
enum IoEncoding {
/** \brief binary without encoding
*/
NO_ENCODING,
/** \brief base64 encoding
*
* \p save() inserts a newline after every 64 characters,
* \p load() can accept base64 text with or without newlines
*/
BASE64,
/** \brief hexadecimal encoding
*
* \p save() uses uppercase letters A-F, \p load() can accept mixed-case
*/
HEX
};
constexpr IoEncoding DEPRECATED(BASE_64) = BASE64;
/** \brief loads a TLV block from a stream
* \return if success, the Block and true
* otherwise, a default-constructed Block and false
*/
optional<Block>
loadBlock(std::istream& is, IoEncoding encoding = BASE64);
/** \brief loads a TLV element from a stream
* \tparam T type of TLV element; T must be WireDecodable,
* and must have a Error nested type
* \return the TLV element, or nullptr if an error occurs
*/
template<typename T>
shared_ptr<T>
load(std::istream& is, IoEncoding encoding = BASE64)
{
optional<Block> block = loadBlock(is, encoding);
if (!block) {
return nullptr;
}
try {
auto obj = make_shared<T>();
obj->wireDecode(*block);
return obj;
}
catch (const typename T::Error& e) {
return nullptr;
}
catch (const tlv::Error& e) {
return nullptr;
}
}
/** \brief loads a TLV element from a file
*/
template<typename T>
shared_ptr<T>
load(const std::string& filename, IoEncoding encoding = BASE64)
{
std::ifstream is(filename);
return load<T>(is, encoding);
}
/** \brief saves a TLV block to a stream
* \throw Error error during saving
*/
void
saveBlock(const Block& block, std::ostream& os, IoEncoding encoding = BASE64);
/** \brief saves a TLV element to a stream
* \tparam T type of TLV element; T must be WireEncodable,
* and must have a Error nested type
* \throw Error error during encoding or saving
*/
template<typename T>
void
save(const T& obj, std::ostream& os, IoEncoding encoding = BASE64)
{
Block block;
try {
block = obj.wireEncode();
}
catch (const typename T::Error& e) {
BOOST_THROW_EXCEPTION(Error(e.what()));
}
catch (const tlv::Error& e) {
BOOST_THROW_EXCEPTION(Error(e.what()));
}
saveBlock(block, os, encoding);
}
/** \brief saves a TLV element to a file
*/
template<typename T>
void
save(const T& obj, const std::string& filename, IoEncoding encoding = BASE64)
{
std::ofstream os(filename);
save(obj, os, encoding);
}
} // namespace io
} // namespace ndn
#endif // NDN_UTIL_IO_HPP