Files
devilutionX/Source/data/parser.cpp
T
ephphathaandGleb Mazovetskiy 32c3316743 Load experience data from file
Also added an iterator based API, though it's not useful for this use-case. Might be nice in the future?

The field/record iterators is single-pass input iterators with shared state.
To avoid rescanning fields unnecessarily parseInt currently can only be called once, it would be possible to make these iterators bidirectional with a bit of extra state (holding onto the start pointer)

Co-authored-by: Gleb Mazovetskiy <[email protected]>
2023-08-24 05:46:44 +02:00

52 lines
1.4 KiB
C++

#include "parser.hpp"
namespace devilution {
GetFieldResult HandleRecordSeparator(const char *begin, const char *end)
{
if (begin == end) {
return { end, GetFieldResult::Status::EndOfFile };
}
if (*begin == '\r') {
++begin;
if (begin == end) {
return { end, GetFieldResult::Status::FileTruncated };
}
// carriage returns should be followed by a newline, so let's let the following checks handle it
}
if (*begin == '\n') {
return { begin + 1, GetFieldResult::Status::EndOfRecord };
}
return { begin, GetFieldResult::Status::BadRecordSeparator };
}
GetFieldResult DiscardMultipleFields(const char *begin, const char *end, unsigned skipLength)
{
GetFieldResult result { begin };
while (skipLength > 0) {
result = DiscardField(result.next, end);
if (result.endOfRecord()) {
// Found the end of record early, we can reuse the error code so just return it
return result;
}
--skipLength;
}
return result;
}
GetFieldResult DiscardMultipleRecords(const char *begin, const char *end, unsigned skipLength)
{
GetFieldResult result { begin };
while (skipLength > 0) {
result = DiscardRemainingFields(result.next, end);
if (result.endOfFile()) {
// Found the end of file early, we can reuse the error code so just return it
return result;
}
--skipLength;
}
return result;
}
} // namespace devilution