blob: a1691131dd3c2a6fa1b9691d06e549b5bc5b6fdf [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yuf50098d2014-02-26 14:26:29 -08002/**
Junxiao Shi2d4be252016-08-17 04:29:00 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Yingdi Yuf50098d2014-02-26 14:26:29 -080020 */
21
22#ifndef NDN_UTIL_IO_HPP
23#define NDN_UTIL_IO_HPP
24
Yingdi Yuf50098d2014-02-26 14:26:29 -080025#include "../encoding/block.hpp"
26
Yingdi Yuf50098d2014-02-26 14:26:29 -080027#include <iostream>
28#include <fstream>
Yingdi Yuf50098d2014-02-26 14:26:29 -080029
Yingdi Yuf50098d2014-02-26 14:26:29 -080030namespace ndn {
31namespace io {
32
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070033class Error : public std::runtime_error
34{
35public:
36 explicit
37 Error(const std::string& what)
38 : std::runtime_error(what)
39 {
40 }
41};
Yingdi Yuf50098d2014-02-26 14:26:29 -080042
Junxiao Shif2515a72016-08-19 02:21:59 +000043/** \brief indicates how a file or stream is encoded
44 */
Yingdi Yuf50098d2014-02-26 14:26:29 -080045enum IoEncoding {
Junxiao Shif2515a72016-08-19 02:21:59 +000046 NO_ENCODING, ///< binary without encoding
47 BASE_64, ///< base64 encoding
48 HEX ///< uppercase hexadecimal encoding
Yingdi Yuf50098d2014-02-26 14:26:29 -080049};
50
Junxiao Shi160701a2016-08-30 11:35:25 +000051/** \brief loads a TLV block from a stream
52 * \return if success, the Block and true
53 * otherwise, a default-constructed Block and false
54 */
55optional<Block>
56loadBlock(std::istream& is, IoEncoding encoding = BASE_64);
57
Junxiao Shif2515a72016-08-19 02:21:59 +000058/** \brief loads a TLV element from a stream
59 * \tparam T type of TLV element; T must be WireDecodable,
60 * and must have a Error nested type
61 * \return the TLV element, or nullptr if an error occurs
62 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063template<typename T>
64shared_ptr<T>
Yingdi Yuf50098d2014-02-26 14:26:29 -080065load(std::istream& is, IoEncoding encoding = BASE_64)
66{
Junxiao Shi160701a2016-08-30 11:35:25 +000067 optional<Block> block = loadBlock(is, encoding);
68 if (!block) {
Junxiao Shi2d4be252016-08-17 04:29:00 +000069 return nullptr;
70 }
Junxiao Shif2515a72016-08-19 02:21:59 +000071
72 try {
73 auto obj = make_shared<T>();
Junxiao Shi160701a2016-08-30 11:35:25 +000074 obj->wireDecode(*block);
Junxiao Shif2515a72016-08-19 02:21:59 +000075 return obj;
76 }
77 catch (const typename T::Error& e) {
Junxiao Shi2d4be252016-08-17 04:29:00 +000078 return nullptr;
79 }
80 catch (const tlv::Error& e) {
81 return nullptr;
82 }
Yingdi Yuf50098d2014-02-26 14:26:29 -080083}
84
Junxiao Shif2515a72016-08-19 02:21:59 +000085/** \brief loads a TLV element from a file
86 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087template<typename T>
88shared_ptr<T>
Junxiao Shif2515a72016-08-19 02:21:59 +000089load(const std::string& filename, IoEncoding encoding = BASE_64)
Yingdi Yuf50098d2014-02-26 14:26:29 -080090{
Junxiao Shif2515a72016-08-19 02:21:59 +000091 std::ifstream is(filename);
Yingdi Yuf50098d2014-02-26 14:26:29 -080092 return load<T>(is, encoding);
93}
94
Junxiao Shi160701a2016-08-30 11:35:25 +000095/** \brief saves a TLV block to a stream
96 * \throw Error error during saving
97 */
98void
99saveBlock(const Block& block, std::ostream& os, IoEncoding encoding = BASE_64);
100
Junxiao Shif2515a72016-08-19 02:21:59 +0000101/** \brief saves a TLV element to a stream
102 * \tparam T type of TLV element; T must be WireEncodable,
103 * and must have a Error nested type
104 * \throw Error error during encoding or saving
105 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106template<typename T>
Yingdi Yuf50098d2014-02-26 14:26:29 -0800107void
Junxiao Shif2515a72016-08-19 02:21:59 +0000108save(const T& obj, std::ostream& os, IoEncoding encoding = BASE_64)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800109{
Junxiao Shif2515a72016-08-19 02:21:59 +0000110 Block block;
Junxiao Shi2d4be252016-08-17 04:29:00 +0000111 try {
Junxiao Shif2515a72016-08-19 02:21:59 +0000112 block = obj.wireEncode();
Junxiao Shi2d4be252016-08-17 04:29:00 +0000113 }
Junxiao Shif2515a72016-08-19 02:21:59 +0000114 catch (const typename T::Error& e) {
Junxiao Shi2d4be252016-08-17 04:29:00 +0000115 BOOST_THROW_EXCEPTION(Error(e.what()));
116 }
117 catch (const tlv::Error& e) {
118 BOOST_THROW_EXCEPTION(Error(e.what()));
119 }
Junxiao Shif2515a72016-08-19 02:21:59 +0000120
Junxiao Shi160701a2016-08-30 11:35:25 +0000121 saveBlock(block, os, encoding);
Yingdi Yuf50098d2014-02-26 14:26:29 -0800122}
123
Junxiao Shif2515a72016-08-19 02:21:59 +0000124/** \brief saves a TLV element to a file
125 */
Yingdi Yuf50098d2014-02-26 14:26:29 -0800126template<typename T>
127void
Junxiao Shif2515a72016-08-19 02:21:59 +0000128save(const T& obj, const std::string& filename, IoEncoding encoding = BASE_64)
Yingdi Yuf50098d2014-02-26 14:26:29 -0800129{
Junxiao Shif2515a72016-08-19 02:21:59 +0000130 std::ofstream os(filename);
131 save(obj, os, encoding);
Yingdi Yuf50098d2014-02-26 14:26:29 -0800132}
133
134} // namespace io
135} // namespace ndn
136
137#endif // NDN_UTIL_IO_HPP