Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef NDN_UTIL_IO_HPP |
| 14 | #define NDN_UTIL_IO_HPP |
| 15 | |
| 16 | #include "../common.hpp" |
| 17 | |
| 18 | #include "../encoding/block.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 19 | #include "../encoding/buffer-stream.hpp" |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 20 | |
| 21 | #include <string> |
| 22 | #include <iostream> |
| 23 | #include <fstream> |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 24 | #include "../security/cryptopp.hpp" |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 25 | |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace io { |
| 29 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 30 | class Error : public std::runtime_error |
| 31 | { |
| 32 | public: |
| 33 | explicit |
| 34 | Error(const std::string& what) |
| 35 | : std::runtime_error(what) |
| 36 | { |
| 37 | } |
| 38 | }; |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 39 | |
| 40 | enum IoEncoding { |
| 41 | NO_ENCODING, |
| 42 | BASE_64, |
| 43 | HEX |
| 44 | }; |
| 45 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 46 | template<typename T> |
| 47 | shared_ptr<T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 48 | load(std::istream& is, IoEncoding encoding = BASE_64) |
| 49 | { |
| 50 | typedef typename T::Error TypeError; |
| 51 | try |
| 52 | { |
| 53 | using namespace CryptoPP; |
| 54 | |
| 55 | shared_ptr<T> object = make_shared<T>(); |
| 56 | |
| 57 | OBufferStream os; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 58 | |
| 59 | switch (encoding) |
| 60 | { |
| 61 | case NO_ENCODING: |
| 62 | { |
| 63 | FileSource ss(is, true, new FileSink(os)); |
| 64 | break; |
| 65 | } |
| 66 | case BASE_64: |
| 67 | { |
| 68 | FileSource ss(is, true, new Base64Decoder(new FileSink(os))); |
| 69 | break; |
| 70 | } |
| 71 | case HEX: |
| 72 | { |
| 73 | FileSource ss(is, true, new HexDecoder(new FileSink(os))); |
| 74 | break; |
| 75 | } |
| 76 | default: |
| 77 | return shared_ptr<T>(); |
| 78 | } |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 79 | |
| 80 | object->wireDecode(Block(os.buf())); |
| 81 | return object; |
| 82 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 83 | catch (CryptoPP::Exception& e) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 84 | { |
| 85 | return shared_ptr<T>(); |
| 86 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 87 | catch (Tlv::Error& e) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 88 | { |
| 89 | return shared_ptr<T>(); |
| 90 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | catch (TypeError& e) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 92 | { |
| 93 | return shared_ptr<T>(); |
| 94 | } |
| 95 | } |
| 96 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 97 | template<typename T> |
| 98 | shared_ptr<T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 99 | load(const std::string& file, IoEncoding encoding = BASE_64) |
| 100 | { |
| 101 | std::ifstream is(file.c_str()); |
| 102 | return load<T>(is, encoding); |
| 103 | } |
| 104 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 105 | template<typename T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 106 | void |
| 107 | save(const T& object, std::ostream& os, IoEncoding encoding = BASE_64) |
| 108 | { |
| 109 | typedef typename T::Error TypeError; |
| 110 | try |
| 111 | { |
| 112 | using namespace CryptoPP; |
| 113 | |
| 114 | Block block = object.wireEncode(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 115 | |
| 116 | switch (encoding) |
| 117 | { |
| 118 | case NO_ENCODING: |
| 119 | { |
| 120 | StringSource ss(block.wire(), block.size(), true, |
| 121 | new FileSink(os)); |
| 122 | break; |
| 123 | } |
| 124 | case BASE_64: |
| 125 | { |
| 126 | StringSource ss(block.wire(), block.size(), true, |
| 127 | new Base64Encoder(new FileSink(os), true, 64)); |
| 128 | break; |
| 129 | } |
| 130 | case HEX: |
| 131 | { |
| 132 | StringSource ss(block.wire(), block.size(), true, |
| 133 | new HexEncoder(new FileSink(os))); |
| 134 | break; |
| 135 | } |
| 136 | default: |
| 137 | return; |
| 138 | } |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 139 | return; |
| 140 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 141 | catch (CryptoPP::Exception& e) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 142 | { |
| 143 | throw Error(e.what()); |
| 144 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 145 | catch (Tlv::Error& e) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 146 | { |
| 147 | throw Error(e.what()); |
| 148 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 149 | catch (TypeError& e) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 150 | { |
| 151 | throw Error(e.what()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | template<typename T> |
| 156 | void |
| 157 | save(const T& object, const std::string& file, IoEncoding encoding = BASE_64) |
| 158 | { |
| 159 | std::ofstream os(file.c_str()); |
| 160 | save(object, os, encoding); |
| 161 | } |
| 162 | |
| 163 | } // namespace io |
| 164 | } // namespace ndn |
| 165 | |
| 166 | #endif // NDN_UTIL_IO_HPP |