mirror of
https://github.com/libretro/devilutionX.git
synced 2026-11-10 10:51:25 -10:00
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]>
52 lines
1.4 KiB
C++
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
|