StringParser.cti
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief StringParser inline template implementation */
16 
17 //#include "StringParser.ih"
18 
19 // Custom includes
20 
21 #define prefix_ inline
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 // senf::StringParser<LengthParser>
26 
27 template <class LengthParser>
28 prefix_ senf::StringParser<LengthParser>::StringParser(data_iterator i, state_type s)
29  : PacketParserBase(i, s, init_bytes)
30  {}
31 
32 template <class LengthParser>
33 prefix_ typename senf::StringParser<LengthParser>::size_type senf::StringParser<LengthParser>::bytes()
34  const
35 {
36  return length_().value() + senf::bytes(length_());
37 }
38 
39 template <class LengthParser>
40 prefix_ typename senf::StringParser<LengthParser>::value_type senf::StringParser<LengthParser>::value()
41  const
42 {
43  validate(bytes());
44  return std::string(i()+senf::bytes(length_()), i()+bytes());
45 }
46 
47 template <class LengthParser>
48 prefix_ void senf::StringParser<LengthParser>::value(value_type v)
49 {
50  validate(bytes());
51  typename LengthParser::value_type sz (std::min<size_type>(v.size(), LengthParser::max_value+0));
52  resize(bytes(), sz+senf::bytes(length_()));
53  length_() << sz;
54  std::copy(v.begin(), boost::next(v.begin(),sz), i()+senf::bytes(length_()));
55 }
56 
57 template <class LengthParser>
58 prefix_ senf::StringParser<LengthParser>::operator value_type()
59  const
60 {
61  return value();
62 }
63 
64 template <class LengthParser>
65 prefix_ typename senf::StringParser<LengthParser> const & senf::StringParser<LengthParser>::operator=(value_type other)
66 {
67  value(other);
68  return *this;
69 }
70 
71 template <class LengthParser>
72 prefix_ bool senf::StringParser<LengthParser>::empty()
73  const
74 {
75  return length() == 0;
76 }
77 
78 template <class LengthParser>
79 prefix_ void senf::StringParser<LengthParser>::clear()
80 {
81  validate(bytes());
82  resize(bytes(), senf::bytes(length_()));
83  length_() << 0;
84 }
85 
86 template <class LengthParser>
87 prefix_ bool senf::StringParser<LengthParser>::operator==(value_type const & other)
88  const
89 {
90  if (other.size() > length())
91  return false;
92  return std::equal(other.begin(), other.end(), i()+senf::bytes(length_()));
93 }
94 
95 template <class LengthParser>
96 prefix_ std::ostream & senf::operator<<(std::ostream & os, StringParser<LengthParser> const & value)
97 {
98  os << value.value();
99  return os;
100 }
101 
102 //-/////////////////////////////////////////////////////////////////////////////////////////////////
103 // senf::FixedSizeStringParser<N>
104 
105 template <std::string::size_type N>
106 prefix_ typename senf::FixedSizeStringParser<N>::value_type senf::FixedSizeStringParser<N>::value()
107  const
108 {
109  return std::string(i(), i()+fixed_bytes);
110 }
111 
112 template <std::string::size_type N>
113 prefix_ void senf::FixedSizeStringParser<N>::value(value_type v)
114 {
115  std::copy(v.begin(), v.end(), i());
116  std::fill(i()+v.size(), i()+fixed_bytes, '\0');
117 }
118 
119 template <std::string::size_type N>
120 prefix_ senf::FixedSizeStringParser<N>::operator value_type()
121  const
122 {
123  return value();
124 }
125 
126 template <std::string::size_type N>
127 prefix_ typename senf::FixedSizeStringParser<N> const & senf::FixedSizeStringParser<N>::operator=(value_type other)
128 {
129  value(other);
130  return *this;
131 }
132 
133 template <std::string::size_type N>
134 prefix_ bool senf::FixedSizeStringParser<N>::operator==(value_type const & other)
135  const
136 {
137  if (other.size() > N)
138  return false;
139  return std::equal(other.begin(), other.end(), i());
140 }
141 
142 template <std::string::size_type N>
143 prefix_ std::ostream & senf::operator<<(std::ostream & os, FixedSizeStringParser<N> const & value)
144 {
145  os << value.value();
146  return os;
147 }
148 
149 //-/////////////////////////////////////////////////////////////////////////////////////////////////
150 #undef prefix_
151 
152 
153 // Local Variables:
154 // mode: c++
155 // fill-column: 100
156 // comment-column: 40
157 // c-file-style: "senf"
158 // indent-tabs-mode: nil
159 // ispell-local-dictionary: "american"
160 // compile-command: "scons -u test"
161 // End: