blob: ff6852b31ae0d579f7dd1e415ad7474246bf602f [file] [log] [blame]
Yingdi Yuf50098d2014-02-26 14:26:29 -08001/**
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 Shi482ccc52014-03-31 13:05:24 -070016#include "../security/cryptopp.hpp"
Yingdi Yuf50098d2014-02-26 14:26:29 -080017
18
19namespace ndn {
20namespace io {
21
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070022class Error : public std::runtime_error
23{
24public:
25 explicit
26 Error(const std::string& what)
27 : std::runtime_error(what)
28 {
29 }
30};
Yingdi Yuf50098d2014-02-26 14:26:29 -080031
32enum IoEncoding {
33 NO_ENCODING,
34 BASE_64,
35 HEX
36};
37
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038template<typename T>
39shared_ptr<T>
Yingdi Yuf50098d2014-02-26 14:26:29 -080040load(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070050
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 Yuf50098d2014-02-26 14:26:29 -080071
72 object->wireDecode(Block(os.buf()));
73 return object;
74 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075 catch (CryptoPP::Exception& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -080076 {
77 return shared_ptr<T>();
78 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070079 catch (Block::Error& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -080080 {
81 return shared_ptr<T>();
82 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083 catch (TypeError& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -080084 {
85 return shared_ptr<T>();
86 }
87}
88
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089template<typename T>
90shared_ptr<T>
Yingdi Yuf50098d2014-02-26 14:26:29 -080091load(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070097template<typename T>
Yingdi Yuf50098d2014-02-26 14:26:29 -080098void
99save(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107
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 Yuf50098d2014-02-26 14:26:29 -0800131 return;
132 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700133 catch (CryptoPP::Exception& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800134 {
135 throw Error(e.what());
136 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700137 catch (Block::Error& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800138 {
139 throw Error(e.what());
140 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700141 catch (TypeError& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800142 {
143 throw Error(e.what());
144 }
145}
146
147template<typename T>
148void
149save(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