All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
io.hpp
Go to the documentation of this file.
1 
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>
16 #include "../security/cryptopp.hpp"
17 
18 
19 namespace ndn {
20 namespace io {
21 
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 };
31 
32 enum IoEncoding {
36 };
37 
38 template<typename T>
39 shared_ptr<T>
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;
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  }
71 
72  object->wireDecode(Block(os.buf()));
73  return object;
74  }
75  catch (CryptoPP::Exception& e)
76  {
77  return shared_ptr<T>();
78  }
79  catch (Tlv::Error& e)
80  {
81  return shared_ptr<T>();
82  }
83  catch (TypeError& e)
84  {
85  return shared_ptr<T>();
86  }
87 }
88 
89 template<typename T>
90 shared_ptr<T>
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 
97 template<typename T>
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();
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  }
131  return;
132  }
133  catch (CryptoPP::Exception& e)
134  {
135  throw Error(e.what());
136  }
137  catch (Tlv::Error& e)
138  {
139  throw Error(e.what());
140  }
141  catch (TypeError& e)
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
Class implementing interface similar to ostringstream, but to construct ndn::Buffer.
Definition: buffer.hpp:176
Class representing wire element of the NDN packet.
Definition: block.hpp:26
shared_ptr< T > load(std::istream &is, IoEncoding encoding=BASE_64)
Definition: io.hpp:40
Error(const std::string &what)
Definition: io.hpp:26
void save(const T &object, std::ostream &os, IoEncoding encoding=BASE_64)
Definition: io.hpp:99
IoEncoding
Definition: io.hpp:32