blob: 6026f0607b377be4b7f4ee30b821faf196df9611 [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
22struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
23
24enum IoEncoding {
25 NO_ENCODING,
26 BASE_64,
27 HEX
28};
29
30template<typename T>
31shared_ptr<T>
32load(std::istream& is, IoEncoding encoding = BASE_64)
33{
34 typedef typename T::Error TypeError;
35 try
36 {
37 using namespace CryptoPP;
38
39 shared_ptr<T> object = make_shared<T>();
40
41 OBufferStream os;
42
43 switch(encoding)
44 {
45 case NO_ENCODING:
46 {
47 FileSource ss(is, true, new FileSink(os));
48 break;
49 }
50 case BASE_64:
51 {
52 FileSource ss(is, true, new Base64Decoder(new FileSink(os)));
53 break;
54 }
55 case HEX:
56 {
57 FileSource ss(is, true, new HexDecoder(new FileSink(os)));
58 break;
59 }
60 default:
61 return shared_ptr<T>();
62 }
63
64 object->wireDecode(Block(os.buf()));
65 return object;
66 }
67 catch(CryptoPP::Exception& e)
68 {
69 return shared_ptr<T>();
70 }
71 catch(Block::Error& e)
72 {
73 return shared_ptr<T>();
74 }
75 catch(TypeError& e)
76 {
77 return shared_ptr<T>();
78 }
79}
80
81template<typename T>
82shared_ptr<T>
83load(const std::string& file, IoEncoding encoding = BASE_64)
84{
85 std::ifstream is(file.c_str());
86 return load<T>(is, encoding);
87}
88
89template<typename T>
90void
91save(const T& object, std::ostream& os, IoEncoding encoding = BASE_64)
92{
93 typedef typename T::Error TypeError;
94 try
95 {
96 using namespace CryptoPP;
97
98 Block block = object.wireEncode();
99
100 switch(encoding)
101 {
102 case NO_ENCODING:
103 {
104 StringSource ss(block.wire(), block.size(), true, new FileSink(os));
105 break;
106 }
107 case BASE_64:
108 {
109 StringSource ss(block.wire(), block.size(), true, new Base64Encoder(new FileSink(os), true, 64));
110 break;
111 }
112 case HEX:
113 {
114 StringSource ss(block.wire(), block.size(), true, new HexEncoder(new FileSink(os)));
115 break;
116 }
117 default:
118 return;
119 }
120 return;
121 }
122 catch(CryptoPP::Exception& e)
123 {
124 throw Error(e.what());
125 }
126 catch(Block::Error& e)
127 {
128 throw Error(e.what());
129 }
130 catch(TypeError& e)
131 {
132 throw Error(e.what());
133 }
134}
135
136template<typename T>
137void
138save(const T& object, const std::string& file, IoEncoding encoding = BASE_64)
139{
140 std::ofstream os(file.c_str());
141 save(object, os, encoding);
142}
143
144} // namespace io
145} // namespace ndn
146
147#endif // NDN_UTIL_IO_HPP
148