ANNA Suite  2020b
Multipurpose development suite for Telco applications
AvpData.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_diameter_codec_basetypes_AvpData_hpp
10 #define anna_diameter_codec_basetypes_AvpData_hpp
11 
12 
14 #include <anna/core/DataBlock.hpp>
15 #include <anna/core/functions.hpp>
16 
17 #include <anna/core/functions.hpp>
18 
19 // STL
20 #include <string>
21 
22 
23 namespace anna {
24 
25 namespace diameter {
26 
27 namespace codec {
28 
29 namespace basetypes {
30 
56 class AvpData {
57 
65  virtual void codeBasic(char* buffer, int &size) noexcept(false) = 0;
66 
74  virtual void updateBasic() noexcept(false) {;}
75 
82  virtual void setPrintableString(const char * printableString) noexcept(false) = 0;
83 
84 
85 protected:
86 
96  std::string assertPrintable(const char* buffer, const int size) const noexcept(false) {
97  std::string result;
98 
99  if(size == 0) return result;
100 
101  bool printable;
102  result = anna::functions::asAsciiString(buffer, size, printable);
103 
104  if(!printable) {
105  std::string ex = getFormatName();
106  ex += "::assertPrintable | Non-printable data provided";
108  }
109 
110  return result;
111  }
112 
113 public:
114 
118  AvpData() {};
119 
120 
121  // gets
122 
128  virtual std::string getFormatName() const = 0;
129 
136  virtual int getSize() const = 0;
137 
138 
139  // helpers
140 
147  void code(char* buffer, int &size) noexcept(false) {
148  codeBasic(buffer, size);
149  }
150 
151 
159  virtual std::string asPrintableString() noexcept(false) {
160  int size = getSize();
161  char buffer[size];
162  code(buffer, size);
163  return (assertPrintable(buffer, size));
164  }
165 
171  std::string asDataBlockString() noexcept(false) {
172  int size = getSize();
173  char buffer[size];
174  code(buffer, size);
175  anna::DataBlock db(buffer, size);
176  return(db.asString());
177  }
178 
186  virtual std::string asString() noexcept(false) {
187  return(asDataBlockString());
188  }
189 
196  std::string asHexString() noexcept(false) {
197  int size = getSize();
198  char buffer[size];
199  code(buffer, size);
200  anna::DataBlock db(buffer, size);
201  return anna::functions::asHexString(db);
202  }
203 
204 
205  // sets
206 
217  virtual void decode(const char* buffer, const int size) noexcept(false) = 0;
218 
224  void fromPrintableString(const char * printableString) noexcept(false) {
225  if(!printableString) {
226  std::string ex = getFormatName();
227  ex += "::fromPrintableString | Null printableString provided";
229  }
230 
231  /*std::string dummy =*/assertPrintable(printableString, strlen(printableString));
232  setPrintableString(printableString);
233  }
234 
240  void fromHexString(const std::string& hexString) noexcept(false) {
241  anna::DataBlock db(true);
242  anna::functions::fromHexString(hexString, db);
243  decode(db.getData(), db.getSize());
244  }
245 };
246 
247 }
248 }
249 }
250 }
251 
252 
253 #endif
static DataBlock & fromHexString(const std::string &hexString, DataBlock &target) noexcept(false)
const char * getData() const
Definition: DataBlock.hpp:86
static std::string asAsciiString(const char *buffer, int size, bool &isFullyPrintable)
virtual void decode(const char *buffer, const int size) noexcept(false)=0
void fromPrintableString(const char *printableString) noexcept(false)
Definition: AvpData.hpp:224
int getSize() const
Definition: DataBlock.hpp:78
std::string asDataBlockString() noexcept(false)
Definition: AvpData.hpp:171
std::string asHexString() noexcept(false)
Definition: AvpData.hpp:196
static std::string asHexString(const int number)
std::string assertPrintable(const char *buffer, const int size) const noexcept(false)
Definition: AvpData.hpp:96
Definition: app.hpp:12
virtual std::string getFormatName() const =0
virtual std::string asString() noexcept(false)
Definition: AvpData.hpp:186
#define ANNA_FILE_LOCATION
Definition: defines.hpp:23
void code(char *buffer, int &size) noexcept(false)
Definition: AvpData.hpp:147
virtual std::string asPrintableString() noexcept(false)
Definition: AvpData.hpp:159
AvpData()
Definition: AvpData.hpp:118
Definition: RuntimeException.hpp:23
std::string asString(const int characterByLine=24) const
void fromHexString(const std::string &hexString) noexcept(false)
Definition: AvpData.hpp:240
Definition: DataBlock.hpp:24