CppCommon  1.0.4.1
C++ Common Library
reader.cpp
Go to the documentation of this file.
1 
9 #include "common/reader.h"
10 
11 #include "utility/countof.h"
12 
13 namespace CppCommon {
14 
15 std::vector<uint8_t> Reader::ReadAllBytes()
16 {
17  const size_t PAGE = 8192;
18 
19  uint8_t buffer[PAGE];
20  std::vector<uint8_t> result;
21  size_t size = 0;
22 
23  do
24  {
25  size = Read(buffer, countof(buffer));
26  result.insert(result.end(), buffer, buffer + size);
27  } while (size == countof(buffer));
28 
29  return result;
30 }
31 
32 std::string Reader::ReadAllText()
33 {
34  std::vector<uint8_t> bytes = ReadAllBytes();
35  return std::string(bytes.begin(), bytes.end());
36 }
37 
38 std::vector<std::string> Reader::ReadAllLines()
39 {
40  std::string temp;
41  std::vector<std::string> result;
42  std::vector<uint8_t> bytes = ReadAllBytes();
43 
44  for (auto ch : bytes)
45  {
46  if ((ch == '\r') || (ch == '\n'))
47  {
48  if (!temp.empty())
49  {
50  result.push_back(temp);
51  temp.clear();
52  }
53  }
54  else
55  temp += ch;
56  }
57 
58  return result;
59 }
60 
61 } // namespace CppCommon
std::vector< std::string > ReadAllLines()
Read all text lines.
Definition: reader.cpp:38
std::vector< uint8_t > ReadAllBytes()
Read all bytes.
Definition: reader.cpp:15
std::string ReadAllText()
Read all text.
Definition: reader.cpp:32
virtual size_t Read(void *buffer, size_t size)=0
Read a bytes buffer base method.
Static array countof definition.
C++ Common project definitions.
Definition: token_bucket.h:15
constexpr size_t countof(const T(&)[N]) noexcept
Count of elements in static array.
Definition: countof.h:16
Reader interface definition.