NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
io.cpp
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2013-2022 Regents of the University of California.
4
*
5
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6
*
7
* 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.
20
*/
21
22
#include "
ndn-cxx/util/io.hpp
"
23
#include "
ndn-cxx/encoding/buffer-stream.hpp
"
24
#include "
ndn-cxx/security/transform/base64-decode.hpp
"
25
#include "
ndn-cxx/security/transform/base64-encode.hpp
"
26
#include "
ndn-cxx/security/transform/buffer-source.hpp
"
27
#include "
ndn-cxx/security/transform/hex-decode.hpp
"
28
#include "
ndn-cxx/security/transform/hex-encode.hpp
"
29
#include "
ndn-cxx/security/transform/stream-sink.hpp
"
30
#include "
ndn-cxx/security/transform/stream-source.hpp
"
31
#include "
ndn-cxx/security/transform/strip-space.hpp
"
32
33
namespace
ndn
{
34
namespace
io {
35
36
shared_ptr<Buffer>
37
loadBuffer
(std::istream& is,
IoEncoding
encoding)
38
{
39
namespace
t =
ndn::security::transform
;
40
41
OBufferStream
os;
42
try
{
43
switch
(encoding) {
44
case
NO_ENCODING
:
45
t::streamSource
(is) >>
t::streamSink
(os);
46
return
os.
buf
();
47
case
BASE64
:
48
t::streamSource
(is) >>
t::stripSpace
(
"\n"
) >>
t::base64Decode
(
false
) >>
t::streamSink
(os);
49
return
os.
buf
();
50
case
HEX
:
51
t::streamSource
(is) >>
t::hexDecode
() >>
t::streamSink
(os);
52
return
os.
buf
();
53
}
54
}
55
catch
(
const
std::runtime_error& e) {
56
NDN_THROW_NESTED
(
Error
(e.what()));
57
}
58
59
NDN_THROW
(std::invalid_argument(
"Unknown IoEncoding "
+
to_string
(encoding)));
60
}
61
62
void
63
saveBuffer
(span<const uint8_t> buf, std::ostream& os,
IoEncoding
encoding)
64
{
65
namespace
t =
ndn::security::transform
;
66
67
try
{
68
switch
(encoding) {
69
case
NO_ENCODING
:
70
t::bufferSource
(buf) >>
t::streamSink
(os);
71
return
;
72
case
BASE64
:
73
t::bufferSource
(buf) >>
t::base64Encode
() >>
t::streamSink
(os);
74
return
;
75
case
HEX
:
76
t::bufferSource
(buf) >>
t::hexEncode
(
true
) >>
t::streamSink
(os);
77
return
;
78
}
79
}
80
catch
(
const
std::runtime_error& e) {
81
NDN_THROW_NESTED
(
Error
(e.what()));
82
}
83
84
NDN_THROW
(std::invalid_argument(
"Unknown IoEncoding "
+
to_string
(encoding)));
85
}
86
87
}
// namespace io
88
}
// namespace ndn
NDN_THROW_NESTED
#define NDN_THROW_NESTED(e)
Definition:
exception.hpp:71
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition:
ndn-strategy-choice-helper.hpp:34
ndn::to_string
std::string to_string(const T &val)
Definition:
backports.hpp:86
stream-source.hpp
io.hpp
ndn::security::transform::base64Decode
unique_ptr< Transform > base64Decode(bool expectNewlineEvery64Bytes)
Definition:
base64-decode.cpp:124
ndn::security::transform::hexEncode
unique_ptr< Transform > hexEncode(bool useUpperCase)
Definition:
hex-encode.cpp:70
strip-space.hpp
ndn::io::Error
Definition:
io.hpp:34
ndn::security::transform::stripSpace
unique_ptr< Transform > stripSpace(const char *whitespaces)
constructs a StripSpace transform
Definition:
strip-space.cpp:54
ndn::security::transform::bufferSource
BufferSource bufferSource
Definition:
buffer-source.hpp:74
NDN_THROW
#define NDN_THROW(e)
Definition:
exception.hpp:61
hex-encode.hpp
ndn::security::transform
Definition:
back-end-file.hpp:30
stream-sink.hpp
buffer-source.hpp
ndn::io::HEX
Hexadecimal encoding.
Definition:
io.hpp:58
ndn::io::NO_ENCODING
Raw binary, without encoding.
Definition:
io.hpp:45
ndn::security::transform::streamSink
unique_ptr< Sink > streamSink(std::ostream &os)
Definition:
stream-sink.cpp:53
base64-encode.hpp
ndn::security::transform::hexDecode
unique_ptr< Transform > hexDecode()
Definition:
hex-decode.cpp:115
ndn::OBufferStream::buf
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Definition:
buffer-stream.cpp:54
ndn::OBufferStream
implements an output stream that constructs ndn::Buffer
Definition:
buffer-stream.hpp:70
ndn::io::BASE64
Base64 encoding.
Definition:
io.hpp:52
ndn::io::saveBuffer
void saveBuffer(span< const uint8_t > buf, std::ostream &os, IoEncoding encoding)
Writes a sequence of bytes to a stream.
Definition:
io.cpp:63
buffer-stream.hpp
hex-decode.hpp
ndn::security::transform::streamSource
StreamSource streamSource
Definition:
stream-source.hpp:62
base64-decode.hpp
ndn::security::transform::base64Encode
unique_ptr< Transform > base64Encode(bool needBreak)
Definition:
base64-encode.cpp:127
ndn::io::loadBuffer
shared_ptr< Buffer > loadBuffer(std::istream &is, IoEncoding encoding)
Reads bytes from a stream until EOF.
Definition:
io.cpp:37
ndn::io::IoEncoding
IoEncoding
Indicates how a file or stream of bytes is encoded.
Definition:
io.hpp:42
ndnSIM
ndn-cxx
ndn-cxx
util
io.cpp
Generated on Fri May 6 2022 12:34:13 for ndnSIM by
1.8.13