esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
io.hpp
Go to the documentation of this file.
1#ifndef NDNPH_CLI_IO_HPP
2#define NDNPH_CLI_IO_HPP
3
4#include "../tlv/decoder.hpp"
5#include "../tlv/encoder.hpp"
6#include <fstream>
7#include <iostream>
8
9namespace ndnph {
10namespace cli {
11
13template<typename T, int bufferSize = 4096>
14inline bool
15input(Region& region, T& target, std::istream& is = std::cin) {
16 uint8_t* buffer = region.alloc(bufferSize);
17 if (buffer == nullptr) {
18 fprintf(stderr, "ndnph::cli::input alloc error\n");
19 exit(1);
20 }
21 is.read(reinterpret_cast<char*>(buffer), bufferSize);
22
23 if (!Decoder(buffer, is.gcount()).decode(target)) {
24 fprintf(stderr, "ndnph::cli::input decode error\n");
25 exit(1);
26 }
27 return true;
28}
29
31template<typename Encodable, int bufferSize = 65536>
32inline void
33output(const Encodable& packet, std::ostream& os = std::cout) {
35 Encoder encoder(temp);
36 if (!encoder.prepend(packet)) {
37 fprintf(stderr, "ndnph::cli::output encode error\n");
38 exit(1);
39 }
40 os.write(reinterpret_cast<const char*>(encoder.begin()), encoder.size());
41}
42
43} // namespace cli
44} // namespace ndnph
45
46#endif // NDNPH_CLI_IO_HPP
TLV decoder.
Definition decoder.hpp:10
bool decode(T &target) const
Decode first TLV into target object.
Definition decoder.hpp:158
TLV encoder that accepts items in reverse order.
Definition encoder.hpp:10
bool prepend(const First &first, const Arg &... arg)
Prepend a sequence of values.
Definition encoder.hpp:123
const uint8_t * begin() const
Get output begin.
Definition encoder.hpp:34
size_t size() const
Get output size.
Definition encoder.hpp:44
Region-based memory allocator thats owns memory of NDNph objects.
Definition region.hpp:9
uint8_t * alloc(size_t size)
Allocate a buffer with no alignment requirement.
Definition region.hpp:27
Region with statically allocated memory.
Definition region.hpp:143
bool input(Region &region, T &target, std::istream &is=std::cin)
Read and decode from input stream.
Definition io.hpp:15
void output(const Encodable &packet, std::ostream &os=std::cout)
Write an Encodable to output stream.
Definition io.hpp:33
Definition fs.hpp:33