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