Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 2 | /** |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_UTIL_IO_HPP |
| 23 | #define NDN_UTIL_IO_HPP |
| 24 | |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 25 | #include "concepts.hpp" |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 26 | #include "../encoding/block.hpp" |
| 27 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 28 | #include <iostream> |
| 29 | #include <fstream> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 30 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 31 | namespace ndn { |
| 32 | namespace io { |
| 33 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 34 | class Error : public std::runtime_error |
| 35 | { |
| 36 | public: |
| 37 | explicit |
| 38 | Error(const std::string& what) |
| 39 | : std::runtime_error(what) |
| 40 | { |
| 41 | } |
| 42 | }; |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 43 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 44 | /** \brief indicates how a file or stream is encoded |
| 45 | */ |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 46 | enum IoEncoding { |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 47 | /** \brief binary without encoding |
| 48 | */ |
| 49 | NO_ENCODING, |
| 50 | |
| 51 | /** \brief base64 encoding |
| 52 | * |
| 53 | * \p save() inserts a newline after every 64 characters, |
| 54 | * \p load() can accept base64 text with or without newlines |
| 55 | */ |
| 56 | BASE64, |
| 57 | |
| 58 | /** \brief hexadecimal encoding |
| 59 | * |
| 60 | * \p save() uses uppercase letters A-F, \p load() can accept mixed-case |
| 61 | */ |
| 62 | HEX |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 65 | constexpr IoEncoding DEPRECATED(BASE_64) = BASE64; |
| 66 | |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 67 | namespace detail { |
| 68 | |
| 69 | template<typename T> |
| 70 | static void |
| 71 | checkInnerError(typename T::Error*) |
| 72 | { |
| 73 | static_assert(std::is_base_of<tlv::Error, typename T::Error>::value, |
| 74 | "T::Error, if declared, must inherit from ndn::tlv::Error"); |
| 75 | } |
| 76 | |
| 77 | template<typename T> |
| 78 | static void |
| 79 | checkInnerError(...) |
| 80 | { |
| 81 | // T::Error is not declared |
| 82 | } |
| 83 | |
| 84 | } // namespace detail |
| 85 | |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 86 | /** \brief loads a TLV block from a stream |
| 87 | * \return if success, the Block and true |
| 88 | * otherwise, a default-constructed Block and false |
| 89 | */ |
| 90 | optional<Block> |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 91 | loadBlock(std::istream& is, IoEncoding encoding = BASE64); |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 92 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 93 | /** \brief loads a TLV element from a stream |
| 94 | * \tparam T type of TLV element; T must be WireDecodable, |
| 95 | * and must have a Error nested type |
| 96 | * \return the TLV element, or nullptr if an error occurs |
| 97 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 98 | template<typename T> |
| 99 | shared_ptr<T> |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 100 | load(std::istream& is, IoEncoding encoding = BASE64) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 101 | { |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 102 | BOOST_CONCEPT_ASSERT((WireDecodable<T>)); |
| 103 | detail::checkInnerError<T>(nullptr); |
| 104 | |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 105 | optional<Block> block = loadBlock(is, encoding); |
| 106 | if (!block) { |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 107 | return nullptr; |
| 108 | } |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 109 | |
| 110 | try { |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 111 | return make_shared<T>(*block); |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 112 | } |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 113 | catch (const tlv::Error&) { |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 114 | return nullptr; |
| 115 | } |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 118 | /** \brief loads a TLV element from a file |
| 119 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 120 | template<typename T> |
| 121 | shared_ptr<T> |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 122 | load(const std::string& filename, IoEncoding encoding = BASE64) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 123 | { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 124 | std::ifstream is(filename); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 125 | return load<T>(is, encoding); |
| 126 | } |
| 127 | |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 128 | /** \brief saves a TLV block to a stream |
| 129 | * \throw Error error during saving |
| 130 | */ |
| 131 | void |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 132 | saveBlock(const Block& block, std::ostream& os, IoEncoding encoding = BASE64); |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 133 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 134 | /** \brief saves a TLV element to a stream |
| 135 | * \tparam T type of TLV element; T must be WireEncodable, |
| 136 | * and must have a Error nested type |
| 137 | * \throw Error error during encoding or saving |
| 138 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 139 | template<typename T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 140 | void |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 141 | save(const T& obj, std::ostream& os, IoEncoding encoding = BASE64) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 142 | { |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 143 | BOOST_CONCEPT_ASSERT((WireEncodable<T>)); |
| 144 | detail::checkInnerError<T>(nullptr); |
| 145 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 146 | Block block; |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 147 | try { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 148 | block = obj.wireEncode(); |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 149 | } |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 150 | catch (const tlv::Error& e) { |
| 151 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 152 | } |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 153 | |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 154 | saveBlock(block, os, encoding); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 157 | /** \brief saves a TLV element to a file |
| 158 | */ |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 159 | template<typename T> |
| 160 | void |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 161 | save(const T& obj, const std::string& filename, IoEncoding encoding = BASE64) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 162 | { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 163 | std::ofstream os(filename); |
| 164 | save(obj, os, encoding); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | } // namespace io |
| 168 | } // namespace ndn |
| 169 | |
| 170 | #endif // NDN_UTIL_IO_HPP |