Babel
Epitech's C++ VoIP project
Utilities.hpp
Go to the documentation of this file.
1 //
2 // Created by cbihan on 15/10/2021.
3 //
4 
5 #pragma once
6 
7 #include <string>
8 #include <sstream>
9 #include "Network/Message.hpp"
10 #include "Network/RFCCodes.hpp"
11 
12 namespace Babel::Utils
13 {
15  inline bool getString(Message<RFCCodes> &m, std::string &str, std::pair<uint8_t, uint8_t> minMaxSize)
16  {
17  uint8_t stringLength;
18  m >> stringLength;
19  if (stringLength < minMaxSize.first || stringLength > minMaxSize.second) {
20  return false;
21  }
22  Message<RFCCodes>::GetBytes(m, str, stringLength);
23  return true;
24  };
25 
27  inline bool getString(Message<RFCCodes> &m, std::string &str)
28  {
29  uint8_t stringLength;
30  m >> stringLength;
31 
32  Message<RFCCodes>::GetBytes(m, str, stringLength);
33  return true;
34  };
35 
37  template<typename T>
38  static bool tryParse(const std::string &s, T &f)
39  {
40  std::istringstream iss(s);
41 
42  iss >> std::noskipws >> f;
43  return iss.eof() && !iss.fail();
44  }
45 }
Message.hpp
Babel::Message< RFCCodes >
RFCCodes.hpp
Babel::Utils
Definition: Utilities.hpp:12
Babel::Utils::tryParse
static bool tryParse(const std::string &s, T &f)
return true if parsing has been successful result ill be in i
Definition: Utilities.hpp:38
Babel::Utils::getString
bool getString(Message< RFCCodes > &m, std::string &str, std::pair< uint8_t, uint8_t > minMaxSize)
get a string from a message the data layout of the message must be uint8 string_length and the string
Definition: Utilities.hpp:15
Babel::Message::GetBytes
static Message< T > & GetBytes(Message< T > &msg, DataType &data, uint64_t size)
Get size bytes from the message and put it in data, useful for strings (no compile time size)
Definition: Message.hpp:88