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