blob: 5a8034a2e02f24a775818bdad76c40e970e1b101 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa84f4642017-08-23 16:14:51 -04002/*
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Yingdi Yuf50098d2014-02-26 14:26:29 -080020 */
21
22#ifndef NDN_UTIL_IO_HPP
23#define NDN_UTIL_IO_HPP
24
Junxiao Shi435bb552016-09-04 03:14:47 +000025#include "concepts.hpp"
Yingdi Yuf50098d2014-02-26 14:26:29 -080026#include "../encoding/block.hpp"
27
Yingdi Yuf50098d2014-02-26 14:26:29 -080028#include <fstream>
Yingdi Yuf50098d2014-02-26 14:26:29 -080029
Yingdi Yuf50098d2014-02-26 14:26:29 -080030namespace ndn {
31namespace io {
32
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070033class Error : public std::runtime_error
34{
35public:
36 explicit
37 Error(const std::string& what)
38 : std::runtime_error(what)
39 {
40 }
41};
Yingdi Yuf50098d2014-02-26 14:26:29 -080042
Junxiao Shif2515a72016-08-19 02:21:59 +000043/** \brief indicates how a file or stream is encoded
44 */
Yingdi Yuf50098d2014-02-26 14:26:29 -080045enum IoEncoding {
Junxiao Shi4ce0bcf2016-09-03 07:09:03 +000046 /** \brief binary without encoding
47 */
48 NO_ENCODING,
49
50 /** \brief base64 encoding
51 *
52 * \p save() inserts a newline after every 64 characters,
53 * \p load() can accept base64 text with or without newlines
54 */
55 BASE64,
56
57 /** \brief hexadecimal encoding
58 *
59 * \p save() uses uppercase letters A-F, \p load() can accept mixed-case
60 */
61 HEX
Yingdi Yuf50098d2014-02-26 14:26:29 -080062};
63
Junxiao Shi435bb552016-09-04 03:14:47 +000064namespace detail {
65
66template<typename T>
67static void
68checkInnerError(typename T::Error*)
69{
70 static_assert(std::is_base_of<tlv::Error, typename T::Error>::value,
71 "T::Error, if declared, must inherit from ndn::tlv::Error");
72}
73
74template<typename T>
75static void
76checkInnerError(...)
77{
78 // T::Error is not declared
79}
80
81} // namespace detail
82
Junxiao Shi160701a2016-08-30 11:35:25 +000083/** \brief loads a TLV block from a stream
84 * \return if success, the Block and true
85 * otherwise, a default-constructed Block and false
86 */
87optional<Block>
Junxiao Shi4ce0bcf2016-09-03 07:09:03 +000088loadBlock(std::istream& is, IoEncoding encoding = BASE64);
Junxiao Shi160701a2016-08-30 11:35:25 +000089
Junxiao Shif2515a72016-08-19 02:21:59 +000090/** \brief loads a TLV element from a stream
91 * \tparam T type of TLV element; T must be WireDecodable,
92 * and must have a Error nested type
93 * \return the TLV element, or nullptr if an error occurs
94 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095template<typename T>
96shared_ptr<T>
Junxiao Shi4ce0bcf2016-09-03 07:09:03 +000097load(std::istream& is, IoEncoding encoding = BASE64)
Yingdi Yuf50098d2014-02-26 14:26:29 -080098{
Junxiao Shi435bb552016-09-04 03:14:47 +000099 BOOST_CONCEPT_ASSERT((WireDecodable<T>));
100 detail::checkInnerError<T>(nullptr);
101
Junxiao Shi160701a2016-08-30 11:35:25 +0000102 optional<Block> block = loadBlock(is, encoding);
103 if (!block) {
Junxiao Shi2d4be252016-08-17 04:29:00 +0000104 return nullptr;
105 }
Junxiao Shif2515a72016-08-19 02:21:59 +0000106
107 try {
Junxiao Shi435bb552016-09-04 03:14:47 +0000108 return make_shared<T>(*block);
Junxiao Shif2515a72016-08-19 02:21:59 +0000109 }
Junxiao Shi435bb552016-09-04 03:14:47 +0000110 catch (const tlv::Error&) {
Junxiao Shi2d4be252016-08-17 04:29:00 +0000111 return nullptr;
112 }
Yingdi Yuf50098d2014-02-26 14:26:29 -0800113}
114
Junxiao Shif2515a72016-08-19 02:21:59 +0000115/** \brief loads a TLV element from a file
116 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700117template<typename T>
118shared_ptr<T>
Junxiao Shi4ce0bcf2016-09-03 07:09:03 +0000119load(const std::string& filename, IoEncoding encoding = BASE64)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800120{
Junxiao Shif2515a72016-08-19 02:21:59 +0000121 std::ifstream is(filename);
Yingdi Yuf50098d2014-02-26 14:26:29 -0800122 return load<T>(is, encoding);
123}
124
Junxiao Shi160701a2016-08-30 11:35:25 +0000125/** \brief saves a TLV block to a stream
126 * \throw Error error during saving
127 */
128void
Junxiao Shi4ce0bcf2016-09-03 07:09:03 +0000129saveBlock(const Block& block, std::ostream& os, IoEncoding encoding = BASE64);
Junxiao Shi160701a2016-08-30 11:35:25 +0000130
Junxiao Shif2515a72016-08-19 02:21:59 +0000131/** \brief saves a TLV element to a stream
132 * \tparam T type of TLV element; T must be WireEncodable,
133 * and must have a Error nested type
134 * \throw Error error during encoding or saving
135 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136template<typename T>
Yingdi Yuf50098d2014-02-26 14:26:29 -0800137void
Junxiao Shi4ce0bcf2016-09-03 07:09:03 +0000138save(const T& obj, std::ostream& os, IoEncoding encoding = BASE64)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800139{
Junxiao Shi435bb552016-09-04 03:14:47 +0000140 BOOST_CONCEPT_ASSERT((WireEncodable<T>));
141 detail::checkInnerError<T>(nullptr);
142
Junxiao Shif2515a72016-08-19 02:21:59 +0000143 Block block;
Junxiao Shi2d4be252016-08-17 04:29:00 +0000144 try {
Junxiao Shif2515a72016-08-19 02:21:59 +0000145 block = obj.wireEncode();
Junxiao Shi2d4be252016-08-17 04:29:00 +0000146 }
Junxiao Shi2d4be252016-08-17 04:29:00 +0000147 catch (const tlv::Error& e) {
148 BOOST_THROW_EXCEPTION(Error(e.what()));
149 }
Junxiao Shif2515a72016-08-19 02:21:59 +0000150
Junxiao Shi160701a2016-08-30 11:35:25 +0000151 saveBlock(block, os, encoding);
Yingdi Yuf50098d2014-02-26 14:26:29 -0800152}
153
Junxiao Shif2515a72016-08-19 02:21:59 +0000154/** \brief saves a TLV element to a file
155 */
Yingdi Yuf50098d2014-02-26 14:26:29 -0800156template<typename T>
157void
Junxiao Shi4ce0bcf2016-09-03 07:09:03 +0000158save(const T& obj, const std::string& filename, IoEncoding encoding = BASE64)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800159{
Junxiao Shif2515a72016-08-19 02:21:59 +0000160 std::ofstream os(filename);
161 save(obj, os, encoding);
Yingdi Yuf50098d2014-02-26 14:26:29 -0800162}
163
164} // namespace io
165} // namespace ndn
166
167#endif // NDN_UTIL_IO_HPP