| 1 |
#ifndef SOURCETOOLS_READ_MEMORY_MAPPED_READER_H |
| 2 |
#define SOURCETOOLS_READ_MEMORY_MAPPED_READER_H |
| 3 |
|
| 4 |
#include <vector> |
| 5 |
#include <string> |
| 6 |
#include <algorithm> |
| 7 |
|
| 8 |
#include <sourcetools/core/macros.h> |
| 9 |
|
| 10 |
#include <sourcetools/r/RHeaders.h> |
| 11 |
#include <sourcetools/r/RUtils.h> |
| 12 |
|
| 13 |
#ifndef _WIN32 |
| 14 |
# include <sourcetools/read/posix/FileConnection.h> |
| 15 |
# include <sourcetools/read/posix/MemoryMappedConnection.h> |
| 16 |
#else |
| 17 |
# include <sourcetools/read/windows/FileConnection.h> |
| 18 |
# include <sourcetools/read/windows/MemoryMappedConnection.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
namespace sourcetools { |
| 22 |
namespace detail { |
| 23 |
|
| 24 |
class MemoryMappedReader |
| 25 |
{ |
| 26 |
public: |
| 27 |
|
| 28 |
class VectorReader |
| 29 |
{ |
| 30 |
public: |
| 31 |
|
| 32 |
explicit VectorReader(std::vector<std::string>* pData) |
| 33 |
: pData_(pData) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
template <typename T> |
| 38 |
void operator()(const T& lhs, const T& rhs) |
| 39 |
{ |
| 40 |
pData_->push_back(std::string(lhs, rhs)); |
| 41 |
} |
| 42 |
|
| 43 |
private: |
| 44 |
std::vector<std::string>* pData_; |
| 45 |
}; |
| 46 |
|
| 47 |
static bool read(const char* path, std::string* pContent) |
| 48 |
{ |
| 49 |
// Open file connection |
| 50 |
FileConnection conn(path); |
| 51 |
if (!conn.open()) |
| 52 |
return false; |
| 53 |
|
| 54 |
// Get size of file |
| 55 |
std::size_t size; |
| 56 |
if (!conn.size(&size)) |
| 57 |
return false; |
| 58 |
|
| 59 |
// Early return for empty files |
| 60 |
if (UNLIKELY(size == 0)) |
| 61 |
return true; |
| 62 |
|
| 63 |
// mmap the file |
| 64 |
MemoryMappedConnection map(conn, size); |
| 65 |
if (!map.open()) |
| 66 |
return false; |
| 67 |
|
| 68 |
pContent->assign(map, size); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
template <typename F> |
| 73 |
static bool read_lines(const char* path, F f) |
| 74 |
{ |
| 75 |
FileConnection conn(path); |
| 76 |
if (!conn.open()) |
| 77 |
return false; |
| 78 |
|
| 79 |
// Get size of file |
| 80 |
std::size_t size; |
| 81 |
if (!conn.size(&size)) |
| 82 |
return false; |
| 83 |
|
| 84 |
// Early return for empty files |
| 85 |
if (UNLIKELY(size == 0)) |
| 86 |
return true; |
| 87 |
|
| 88 |
// mmap the file |
| 89 |
MemoryMappedConnection map(conn, size); |
| 90 |
if (!map.open()) |
| 91 |
return false; |
| 92 |
|
| 93 |
// special case: just a '\n' |
| 94 |
bool endsWithNewline = map[size - 1] == '\n'; |
| 95 |
if (size == 1 && endsWithNewline) |
| 96 |
return true; |
| 97 |
|
| 98 |
// Search for newlines |
| 99 |
const char* lower = map; |
| 100 |
const char* upper = map; |
| 101 |
const char* end = map + size; |
| 102 |
while (true) |
| 103 |
{ |
| 104 |
upper = std::find(lower, end, '\n'); |
| 105 |
if (upper == end) |
| 106 |
break; |
| 107 |
|
| 108 |
// Handle '\r\n' |
| 109 |
int CR = *(upper - 1) == '\r'; |
| 110 |
upper -= CR; |
| 111 |
|
| 112 |
// Pass to functor |
| 113 |
f(lower, upper); |
| 114 |
|
| 115 |
// Update |
| 116 |
lower = upper + 1 + CR; |
| 117 |
} |
| 118 |
|
| 119 |
// If this file ended with a newline, we're done |
| 120 |
if (endsWithNewline) |
| 121 |
return true; |
| 122 |
|
| 123 |
// Otherwise, consume one more string, then we're done |
| 124 |
f(lower, end); |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
static bool read_lines(const char* path, std::vector<std::string>* pContent) |
| 129 |
{ |
| 130 |
VectorReader reader(pContent); |
| 131 |
return read_lines(path, reader); |
| 132 |
} |
| 133 |
|
| 134 |
}; |
| 135 |
|
| 136 |
} // namespace detail |
| 137 |
} // namespace sourcetools |
| 138 |
|
| 139 |
#endif /* SOURCETOOLS_READ_MEMORY_MAPPED_READER_H */ |