cpp-bencoding
Utils.h
Go to the documentation of this file.
1 
8 #ifndef BENCODING_UTILS_H
9 #define BENCODING_UTILS_H
10 
11 #include <ios>
12 #include <istream>
13 #include <queue>
14 #include <sstream>
15 #include <stack>
16 #include <string>
17 
18 namespace bencoding {
19 
22 
34 template<typename N>
35 bool strToNum(const std::string &str, N &num,
36  std::ios_base &(* format)(std::ios_base &) = std::dec) {
37  std::istringstream stream(str);
38  N convNum = 0;
39  stream >> format >> convNum;
40  if (!stream.fail() && stream.eof()) {
41  num = convNum;
42  return true;
43  }
44  return false;
45 }
46 
48 
51 
52 bool readUpTo(std::istream &stream, std::string &readData, char sentinel);
53 bool readUntil(std::istream &stream, std::string &readData, char last);
54 
56 
59 
60 std::string replace(const std::string &str, char what,
61  const std::string &withWhat);
62 
64 
65 } // namespace bencoding
66 
67 #endif
bool readUpTo(std::istream &stream, std::string &readData, char sentinel)
Reads data from the given stream up to sentinel, which is left in stream.
Definition: Utils.cpp:27
std::string replace(const std::string &str, char what, const std::string &withWhat)
Replaces all occurrences of what with withWhat in str and returns the resulting string.
Definition: Utils.cpp:65
Main namespace of the bencoding library.
bool strToNum(const std::string &str, N &num, std::ios_base &(*format)(std::ios_base &)=std::dec)
Converts the given string into a number.
Definition: Utils.h:35
bool readUntil(std::istream &stream, std::string &readData, char last)
Reads data from the given stream until last, which is also read.
Definition: Utils.cpp:50