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 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 25 | #include "../encoding/block.hpp" |
| 26 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 27 | #include <iostream> |
| 28 | #include <fstream> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 29 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace io { |
| 32 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 33 | class Error : public std::runtime_error |
| 34 | { |
| 35 | public: |
| 36 | explicit |
| 37 | Error(const std::string& what) |
| 38 | : std::runtime_error(what) |
| 39 | { |
| 40 | } |
| 41 | }; |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 42 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 43 | /** \brief indicates how a file or stream is encoded |
| 44 | */ |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 45 | enum IoEncoding { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 46 | NO_ENCODING, ///< binary without encoding |
| 47 | BASE_64, ///< base64 encoding |
| 48 | HEX ///< uppercase hexadecimal encoding |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame^] | 51 | /** \brief loads a TLV block from a stream |
| 52 | * \return if success, the Block and true |
| 53 | * otherwise, a default-constructed Block and false |
| 54 | */ |
| 55 | optional<Block> |
| 56 | loadBlock(std::istream& is, IoEncoding encoding = BASE_64); |
| 57 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 58 | /** \brief loads a TLV element from a stream |
| 59 | * \tparam T type of TLV element; T must be WireDecodable, |
| 60 | * and must have a Error nested type |
| 61 | * \return the TLV element, or nullptr if an error occurs |
| 62 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 63 | template<typename T> |
| 64 | shared_ptr<T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 65 | load(std::istream& is, IoEncoding encoding = BASE_64) |
| 66 | { |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame^] | 67 | optional<Block> block = loadBlock(is, encoding); |
| 68 | if (!block) { |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 69 | return nullptr; |
| 70 | } |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 71 | |
| 72 | try { |
| 73 | auto obj = make_shared<T>(); |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame^] | 74 | obj->wireDecode(*block); |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 75 | return obj; |
| 76 | } |
| 77 | catch (const typename T::Error& e) { |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 78 | return nullptr; |
| 79 | } |
| 80 | catch (const tlv::Error& e) { |
| 81 | return nullptr; |
| 82 | } |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 85 | /** \brief loads a TLV element from a file |
| 86 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 87 | template<typename T> |
| 88 | shared_ptr<T> |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 89 | load(const std::string& filename, IoEncoding encoding = BASE_64) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 90 | { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 91 | std::ifstream is(filename); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 92 | return load<T>(is, encoding); |
| 93 | } |
| 94 | |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame^] | 95 | /** \brief saves a TLV block to a stream |
| 96 | * \throw Error error during saving |
| 97 | */ |
| 98 | void |
| 99 | saveBlock(const Block& block, std::ostream& os, IoEncoding encoding = BASE_64); |
| 100 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 101 | /** \brief saves a TLV element to a stream |
| 102 | * \tparam T type of TLV element; T must be WireEncodable, |
| 103 | * and must have a Error nested type |
| 104 | * \throw Error error during encoding or saving |
| 105 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 106 | template<typename T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 107 | void |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 108 | save(const T& obj, std::ostream& os, IoEncoding encoding = BASE_64) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 109 | { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 110 | Block block; |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 111 | try { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 112 | block = obj.wireEncode(); |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 113 | } |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 114 | catch (const typename T::Error& e) { |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 115 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 116 | } |
| 117 | catch (const tlv::Error& e) { |
| 118 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 119 | } |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 120 | |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame^] | 121 | saveBlock(block, os, encoding); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 124 | /** \brief saves a TLV element to a file |
| 125 | */ |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 126 | template<typename T> |
| 127 | void |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 128 | save(const T& obj, const std::string& filename, IoEncoding encoding = BASE_64) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 129 | { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame] | 130 | std::ofstream os(filename); |
| 131 | save(obj, os, encoding); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | } // namespace io |
| 135 | } // namespace ndn |
| 136 | |
| 137 | #endif // NDN_UTIL_IO_HPP |