-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify txtdata parsing with a helper class
- Loading branch information
Showing
10 changed files
with
270 additions
and
645 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "data/record_reader.hpp" | ||
|
||
namespace devilution { | ||
|
||
void RecordReader::advance() | ||
{ | ||
if (needsIncrement_) { | ||
++it_; | ||
} else { | ||
needsIncrement_ = true; | ||
} | ||
if (it_ == end_) { | ||
DataFile::reportFatalError(DataFile::Error::NotEnoughColumns, filename_); | ||
} | ||
} | ||
|
||
} // namespace devilution |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#pragma once | ||
|
||
#include <string_view> | ||
#include <type_traits> | ||
|
||
#include <expected.hpp> | ||
#include <function_ref.hpp> | ||
|
||
#include "data/file.hpp" | ||
#include "data/iterators.hpp" | ||
|
||
namespace devilution { | ||
|
||
/** | ||
* @brief A record reader that treats every error as fatal. | ||
*/ | ||
class RecordReader { | ||
public: | ||
RecordReader(DataFileRecord &record, std::string_view filename) | ||
: it_(record.begin()) | ||
, end_(record.end()) | ||
, filename_(filename) | ||
{ | ||
} | ||
|
||
template <typename T> | ||
typename std::enable_if_t<std::is_integral_v<T>, void> | ||
readInt(std::string_view name, T &out) | ||
{ | ||
advance(); | ||
DataFileField field = *it_; | ||
if (tl::expected<void, DataFileField::Error> result = field.parseInt(out); !result.has_value()) { | ||
DataFile::reportFatalFieldError(result.error(), filename_, name, field); | ||
} | ||
} | ||
|
||
template <typename T> | ||
typename std::enable_if_t<std::is_integral_v<T>, void> | ||
readOptionalInt(std::string_view name, T &out) | ||
{ | ||
advance(); | ||
DataFileField field = *it_; | ||
if (field.value().empty()) return; | ||
if (tl::expected<void, DataFileField::Error> result = field.parseInt(out); !result.has_value()) { | ||
DataFile::reportFatalFieldError(result.error(), filename_, name, field); | ||
} | ||
} | ||
|
||
template <typename T, size_t N> | ||
void readIntArray(std::string_view name, T (&out)[N]) | ||
{ | ||
advance(); | ||
DataFileField field = *it_; | ||
if (tl::expected<void, DataFileField::Error> result = field.parseIntArray(out); !result.has_value()) { | ||
DataFile::reportFatalFieldError(result.error(), filename_, name, field); | ||
} | ||
} | ||
|
||
template <typename T> | ||
typename std::enable_if_t<std::is_integral_v<T>, void> | ||
readFixed6(std::string_view name, T &out) | ||
{ | ||
advance(); | ||
DataFileField field = *it_; | ||
if (tl::expected<void, DataFileField::Error> result = field.parseFixed6(out); !result.has_value()) { | ||
DataFile::reportFatalFieldError(result.error(), filename_, name, field); | ||
} | ||
} | ||
|
||
void readBool(std::string_view name, bool &out) | ||
{ | ||
advance(); | ||
DataFileField field = *it_; | ||
if (tl::expected<void, DataFileField::Error> result = field.parseBool(out); !result.has_value()) { | ||
DataFile::reportFatalFieldError(result.error(), filename_, name, field); | ||
} | ||
} | ||
|
||
void readString(std::string_view name, std::string &out) | ||
{ | ||
advance(); | ||
out = (*it_).value(); | ||
} | ||
|
||
template <typename T, typename F> | ||
void read(std::string_view name, T &out, F &&parseFn) | ||
{ | ||
advance(); | ||
DataFileField field = *it_; | ||
if (tl::expected<T, std::string> result = parseFn(field.value()); result.has_value()) { | ||
out = *std::move(result); | ||
} else { | ||
DataFile::reportFatalFieldError(DataFileField::Error::InvalidValue, filename_, name, field, result.error()); | ||
} | ||
} | ||
|
||
template <typename T, typename F> | ||
void readEnumList(std::string_view name, T &out, F &&parseFn) | ||
{ | ||
advance(); | ||
DataFileField field = *it_; | ||
if (tl::expected<void, std::string> result = field.parseEnumList(out, std::forward<F>(parseFn)); !result.has_value()) { | ||
DataFile::reportFatalFieldError(DataFileField::Error::InvalidValue, filename_, name, field, result.error()); | ||
} | ||
} | ||
|
||
std::string_view value() | ||
{ | ||
advance(); | ||
needsIncrement_ = false; | ||
return (*it_).value(); | ||
} | ||
|
||
void advance(); | ||
|
||
private: | ||
FieldIterator it_; | ||
const FieldIterator end_; | ||
std::string_view filename_; | ||
bool needsIncrement_ = false; | ||
}; | ||
|
||
} // namespace devilution |
Oops, something went wrong.