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" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 26 | #include "../encoding/buffer-stream.hpp" |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame^] | 27 | #include "../security/cryptopp.hpp" |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 28 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 29 | #include <iostream> |
| 30 | #include <fstream> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 31 | |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace io { |
| 35 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 36 | class Error : public std::runtime_error |
| 37 | { |
| 38 | public: |
| 39 | explicit |
| 40 | Error(const std::string& what) |
| 41 | : std::runtime_error(what) |
| 42 | { |
| 43 | } |
| 44 | }; |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 45 | |
| 46 | enum IoEncoding { |
| 47 | NO_ENCODING, |
| 48 | BASE_64, |
| 49 | HEX |
| 50 | }; |
| 51 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 52 | template<typename T> |
| 53 | shared_ptr<T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 54 | load(std::istream& is, IoEncoding encoding = BASE_64) |
| 55 | { |
| 56 | typedef typename T::Error TypeError; |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame^] | 57 | try { |
| 58 | using namespace CryptoPP; |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 59 | |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame^] | 60 | shared_ptr<T> object = make_shared<T>(); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 61 | |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame^] | 62 | OBufferStream os; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 63 | |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame^] | 64 | switch (encoding) { |
| 65 | case NO_ENCODING: { |
| 66 | FileSource ss(is, true, new FileSink(os)); |
| 67 | break; |
| 68 | } |
| 69 | case BASE_64: { |
| 70 | FileSource ss(is, true, new Base64Decoder(new FileSink(os))); |
| 71 | break; |
| 72 | } |
| 73 | case HEX: { |
| 74 | FileSource ss(is, true, new HexDecoder(new FileSink(os))); |
| 75 | break; |
| 76 | } |
| 77 | default: |
| 78 | return nullptr; |
| 79 | } |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 80 | |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame^] | 81 | object->wireDecode(Block(os.buf())); |
| 82 | return object; |
| 83 | } |
| 84 | catch (const TypeError& e) { |
| 85 | return nullptr; |
| 86 | } |
| 87 | catch (const CryptoPP::Exception& e) { |
| 88 | return nullptr; |
| 89 | } |
| 90 | catch (const tlv::Error& e) { |
| 91 | return nullptr; |
| 92 | } |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 95 | template<typename T> |
| 96 | shared_ptr<T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 97 | load(const std::string& file, IoEncoding encoding = BASE_64) |
| 98 | { |
| 99 | std::ifstream is(file.c_str()); |
| 100 | return load<T>(is, encoding); |
| 101 | } |
| 102 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 103 | template<typename T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 104 | void |
| 105 | save(const T& object, std::ostream& os, IoEncoding encoding = BASE_64) |
| 106 | { |
| 107 | typedef typename T::Error TypeError; |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame^] | 108 | try { |
| 109 | using namespace CryptoPP; |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 110 | |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame^] | 111 | Block block = object.wireEncode(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 112 | |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame^] | 113 | switch (encoding) { |
| 114 | case NO_ENCODING: { |
| 115 | StringSource ss(block.wire(), block.size(), true, new FileSink(os)); |
| 116 | break; |
| 117 | } |
| 118 | case BASE_64: { |
| 119 | StringSource ss(block.wire(), block.size(), true, |
| 120 | new Base64Encoder(new FileSink(os), true, 64)); |
| 121 | break; |
| 122 | } |
| 123 | case HEX: { |
| 124 | StringSource ss(block.wire(), block.size(), true, |
| 125 | new HexEncoder(new FileSink(os))); |
| 126 | break; |
| 127 | } |
| 128 | default: |
| 129 | return; |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 130 | } |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame^] | 131 | return; |
| 132 | } |
| 133 | catch (const TypeError& e) { |
| 134 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 135 | } |
| 136 | catch (const CryptoPP::Exception& e) { |
| 137 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 138 | } |
| 139 | catch (const tlv::Error& e) { |
| 140 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 141 | } |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | template<typename T> |
| 145 | void |
| 146 | save(const T& object, const std::string& file, IoEncoding encoding = BASE_64) |
| 147 | { |
| 148 | std::ofstream os(file.c_str()); |
| 149 | save(object, os, encoding); |
| 150 | } |
| 151 | |
| 152 | } // namespace io |
| 153 | } // namespace ndn |
| 154 | |
| 155 | #endif // NDN_UTIL_IO_HPP |