blob: c61dccf39725be7929dfdfc31f304b419388c4c2 [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"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070019#include "../encoding/buffer-stream.hpp"
Yingdi Yuf50098d2014-02-26 14:26:29 -080020
21#include <string>
22#include <iostream>
23#include <fstream>
Junxiao Shi482ccc52014-03-31 13:05:24 -070024#include "../security/cryptopp.hpp"
Yingdi Yuf50098d2014-02-26 14:26:29 -080025
26
27namespace ndn {
28namespace io {
29
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070030class Error : public std::runtime_error
31{
32public:
33 explicit
34 Error(const std::string& what)
35 : std::runtime_error(what)
36 {
37 }
38};
Yingdi Yuf50098d2014-02-26 14:26:29 -080039
40enum IoEncoding {
41 NO_ENCODING,
42 BASE_64,
43 HEX
44};
45
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070046template<typename T>
47shared_ptr<T>
Yingdi Yuf50098d2014-02-26 14:26:29 -080048load(std::istream& is, IoEncoding encoding = BASE_64)
49{
50 typedef typename T::Error TypeError;
51 try
52 {
53 using namespace CryptoPP;
54
55 shared_ptr<T> object = make_shared<T>();
56
57 OBufferStream os;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058
59 switch (encoding)
60 {
61 case NO_ENCODING:
62 {
63 FileSource ss(is, true, new FileSink(os));
64 break;
65 }
66 case BASE_64:
67 {
68 FileSource ss(is, true, new Base64Decoder(new FileSink(os)));
69 break;
70 }
71 case HEX:
72 {
73 FileSource ss(is, true, new HexDecoder(new FileSink(os)));
74 break;
75 }
76 default:
77 return shared_ptr<T>();
78 }
Yingdi Yuf50098d2014-02-26 14:26:29 -080079
80 object->wireDecode(Block(os.buf()));
81 return object;
82 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083 catch (CryptoPP::Exception& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -080084 {
85 return shared_ptr<T>();
86 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070087 catch (Tlv::Error& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -080088 {
89 return shared_ptr<T>();
90 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091 catch (TypeError& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -080092 {
93 return shared_ptr<T>();
94 }
95}
96
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097template<typename T>
98shared_ptr<T>
Yingdi Yuf50098d2014-02-26 14:26:29 -080099load(const std::string& file, IoEncoding encoding = BASE_64)
100{
101 std::ifstream is(file.c_str());
102 return load<T>(is, encoding);
103}
104
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700105template<typename T>
Yingdi Yuf50098d2014-02-26 14:26:29 -0800106void
107save(const T& object, std::ostream& os, IoEncoding encoding = BASE_64)
108{
109 typedef typename T::Error TypeError;
110 try
111 {
112 using namespace CryptoPP;
113
114 Block block = object.wireEncode();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700115
116 switch (encoding)
117 {
118 case NO_ENCODING:
119 {
120 StringSource ss(block.wire(), block.size(), true,
121 new FileSink(os));
122 break;
123 }
124 case BASE_64:
125 {
126 StringSource ss(block.wire(), block.size(), true,
127 new Base64Encoder(new FileSink(os), true, 64));
128 break;
129 }
130 case HEX:
131 {
132 StringSource ss(block.wire(), block.size(), true,
133 new HexEncoder(new FileSink(os)));
134 break;
135 }
136 default:
137 return;
138 }
Yingdi Yuf50098d2014-02-26 14:26:29 -0800139 return;
140 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700141 catch (CryptoPP::Exception& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800142 {
143 throw Error(e.what());
144 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700145 catch (Tlv::Error& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800146 {
147 throw Error(e.what());
148 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700149 catch (TypeError& e)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800150 {
151 throw Error(e.what());
152 }
153}
154
155template<typename T>
156void
157save(const T& object, const std::string& file, IoEncoding encoding = BASE_64)
158{
159 std::ofstream os(file.c_str());
160 save(object, os, encoding);
161}
162
163} // namespace io
164} // namespace ndn
165
166#endif // NDN_UTIL_IO_HPP