Telnet++  3.1.0.2
A C++ library for interacting with Telnet streams
core.hpp
1 #pragma once
2 
3 #include "telnetpp/detail/export.hpp" // IWYU pragma: export
4 
5 #include <gsl/gsl-lite.hpp>
6 
7 #include <string>
8 #include <cstdint>
9 
10 namespace telnetpp {
11 
12 using byte = std::uint8_t;
13 using option_type = std::uint8_t;
14 using command_type = std::uint8_t;
15 using negotiation_type = std::uint8_t;
16 using subnegotiation_content_type = byte;
17 
18 // TELNET Commands
19 static constexpr command_type const se = 240; // Subnegotiation End
20 static constexpr command_type const nop = 241; // No Operation
21 static constexpr command_type const dm = 242; // Data Mark
22 static constexpr command_type const brk = 243; // Break
23 static constexpr command_type const ip = 244; // Interrupt Process
24 static constexpr command_type const ao = 245; // Abort Output
25 static constexpr command_type const ayt = 246; // Are You There?
26 static constexpr command_type const ec = 247; // Erase Character
27 static constexpr command_type const el = 248; // Erase Line
28 static constexpr command_type const ga = 249; // Go Ahead
29 static constexpr command_type const sb = 250; // Subnegotiation Begin
30 static constexpr command_type const will = 251;
31 static constexpr command_type const wont = 252;
32 static constexpr command_type const do_ = 253; // NOLINT
33 static constexpr command_type const dont = 254;
34 static constexpr command_type const iac = 255; // Interpret As Command
35 
36 // A stream of bytes in Telnet++ is exposed as a non-owning span. It is
37 // expected that the data will live for no longer than any function in
38 // which it is found. For that reason, these spans should never be stored.
39 // if this is necessary, it must be converted into a longer-term data
40 // structure, e.g. a std::vector, or std::basic_string<byte>.
41 using bytes = gsl::span<byte const>;
42 
43 // Where necessary, bytes are stored in this type, which has the small
44 // string optimization, meaning that most cases will not cause an allocation.
45 using byte_storage = std::basic_string<byte>;
46 
47 namespace literals {
48 
49 // A simple function to convert from string literals to stored bytes.
50 inline byte_storage operator""_tb(char const *text, size_t length)
51 {
52  byte_storage result;
53  result.reserve(length);
54 
55  for (auto ch : gsl::span<char const>{text, length})
56  {
57  result.push_back(static_cast<telnetpp::byte>(ch));
58  }
59 
60  return result;
61 }
62 
63 } // namespace literals
64 } // namespace telnetpp