blob: 6ca861f1f48919f93203b569a60aa3b12130ba65 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Yingdi Yuf50098d2014-02-26 14:26:29 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Yuf50098d2014-02-26 14:26:29 -080011 */
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 Shi482ccc52014-03-31 13:05:24 -070023#include "../security/cryptopp.hpp"
Yingdi Yuf50098d2014-02-26 14:26:29 -080024
25
26namespace ndn {
27namespace io {
28
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070029class Error : public std::runtime_error
30{
31public:
32 explicit
33 Error(const std::string& what)
34 : std::runtime_error(what)
35 {
36 }
37};
Yingdi Yuf50098d2014-02-26 14:26:29 -080038
39enum IoEncoding {
40 NO_ENCODING,
41 BASE_64,
42 HEX
43};
44
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045template<typename T>
46shared_ptr<T>
Yingdi Yuf50098d2014-02-26 14:26:29 -080047load(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070057
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 Yuf50098d2014-02-26 14:26:29 -080078
79 object->wireDecode(Block(os.buf()));
80 return object;
81 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070082 catch (CryptoPP::Exception& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -080083 {
84 return shared_ptr<T>();
85 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070086 catch (Tlv::Error& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -080087 {
88 return shared_ptr<T>();
89 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070090 catch (TypeError& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -080091 {
92 return shared_ptr<T>();
93 }
94}
95
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070096template<typename T>
97shared_ptr<T>
Yingdi Yuf50098d2014-02-26 14:26:29 -080098load(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104template<typename T>
Yingdi Yuf50098d2014-02-26 14:26:29 -0800105void
106save(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114
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 Yuf50098d2014-02-26 14:26:29 -0800138 return;
139 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700140 catch (CryptoPP::Exception& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800141 {
142 throw Error(e.what());
143 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700144 catch (Tlv::Error& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800145 {
146 throw Error(e.what());
147 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700148 catch (TypeError& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800149 {
150 throw Error(e.what());
151 }
152}
153
154template<typename T>
155void
156save(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