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