NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
io.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_UTIL_IO_HPP
23 #define NDN_UTIL_IO_HPP
24 
25 #include "../common.hpp"
26 
27 #include "../encoding/block.hpp"
28 #include "../encoding/buffer-stream.hpp"
29 
30 #include <iostream>
31 #include <fstream>
32 #include "../security/cryptopp.hpp"
33 
34 
35 namespace ndn {
36 namespace io {
37 
38 class Error : public std::runtime_error
39 {
40 public:
41  explicit
42  Error(const std::string& what)
43  : std::runtime_error(what)
44  {
45  }
46 };
47 
48 enum IoEncoding {
52 };
53 
54 template<typename T>
55 shared_ptr<T>
56 load(std::istream& is, IoEncoding encoding = BASE_64)
57 {
58  typedef typename T::Error TypeError;
59  try
60  {
61  using namespace CryptoPP;
62 
63  shared_ptr<T> object = make_shared<T>();
64 
65  OBufferStream os;
66 
67  switch (encoding)
68  {
69  case NO_ENCODING:
70  {
71  FileSource ss(is, true, new FileSink(os));
72  break;
73  }
74  case BASE_64:
75  {
76  FileSource ss(is, true, new Base64Decoder(new FileSink(os)));
77  break;
78  }
79  case HEX:
80  {
81  FileSource ss(is, true, new HexDecoder(new FileSink(os)));
82  break;
83  }
84  default:
85  return shared_ptr<T>();
86  }
87 
88  object->wireDecode(Block(os.buf()));
89  return object;
90  }
91  catch (TypeError& e)
92  {
93  return shared_ptr<T>();
94  }
95  catch (CryptoPP::Exception& e)
96  {
97  return shared_ptr<T>();
98  }
99  catch (tlv::Error& e)
100  {
101  return shared_ptr<T>();
102  }
103 }
104 
105 template<typename T>
106 shared_ptr<T>
107 load(const std::string& file, IoEncoding encoding = BASE_64)
108 {
109  std::ifstream is(file.c_str());
110  return load<T>(is, encoding);
111 }
112 
113 template<typename T>
114 void
115 save(const T& object, std::ostream& os, IoEncoding encoding = BASE_64)
116 {
117  typedef typename T::Error TypeError;
118  try
119  {
120  using namespace CryptoPP;
121 
122  Block block = object.wireEncode();
123 
124  switch (encoding)
125  {
126  case NO_ENCODING:
127  {
128  StringSource ss(block.wire(), block.size(), true,
129  new FileSink(os));
130  break;
131  }
132  case BASE_64:
133  {
134  StringSource ss(block.wire(), block.size(), true,
135  new Base64Encoder(new FileSink(os), true, 64));
136  break;
137  }
138  case HEX:
139  {
140  StringSource ss(block.wire(), block.size(), true,
141  new HexEncoder(new FileSink(os)));
142  break;
143  }
144  default:
145  return;
146  }
147  return;
148  }
149  catch (TypeError& e)
150  {
151  BOOST_THROW_EXCEPTION(Error(e.what()));
152  }
153  catch (CryptoPP::Exception& e)
154  {
155  BOOST_THROW_EXCEPTION(Error(e.what()));
156  }
157  catch (tlv::Error& e)
158  {
159  BOOST_THROW_EXCEPTION(Error(e.what()));
160  }
161 }
162 
163 template<typename T>
164 void
165 save(const T& object, const std::string& file, IoEncoding encoding = BASE_64)
166 {
167  std::ofstream os(file.c_str());
168  save(object, os, encoding);
169 }
170 
171 } // namespace io
172 } // namespace ndn
173 
174 #endif // NDN_UTIL_IO_HPP
Copyright (c) 2011-2015 Regents of the University of California.
Copyright (c) 2013-2014 Regents of the University of California.
Definition: oid.hpp:29
const uint8_t * wire() const
Definition: block.cpp:495
STL namespace.
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
shared_ptr< T > load(std::istream &is, IoEncoding encoding=BASE_64)
Definition: io.hpp:56
size_t size() const
Definition: block.cpp:504
Error(const std::string &what)
Definition: io.hpp:42
Class implementing interface similar to ostringstream, but to construct ndn::Buffer.
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
void save(const T &object, std::ostream &os, IoEncoding encoding=BASE_64)
Definition: io.hpp:115
IoEncoding
Definition: io.hpp:48