mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2024-11-21 21:17:25 +00:00
chore: Use TryParse for LDF parsing (#1206)
* LDF: Simplify parsing * Update GeneralUtils.h
This commit is contained in:
parent
570c597148
commit
3dd2791066
@ -126,6 +126,11 @@ namespace GeneralUtils {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
T Parse(const char* value);
|
T Parse(const char* value);
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline bool Parse(const char* value) {
|
||||||
|
return std::stoi(value);
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
inline int32_t Parse(const char* value) {
|
inline int32_t Parse(const char* value) {
|
||||||
return std::stoi(value);
|
return std::stoi(value);
|
||||||
|
@ -61,35 +61,33 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case LDF_TYPE_S32: {
|
case LDF_TYPE_S32: {
|
||||||
try {
|
int32_t data;
|
||||||
int32_t data = static_cast<int32_t>(strtoul(ldfTypeAndValue.second.data(), &storage, 10));
|
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||||
returnValue = new LDFData<int32_t>(key, data);
|
|
||||||
} catch (std::exception) {
|
|
||||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid int32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid int32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
returnValue = new LDFData<int32_t>(key, data);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case LDF_TYPE_FLOAT: {
|
case LDF_TYPE_FLOAT: {
|
||||||
try {
|
float data;
|
||||||
float data = strtof(ldfTypeAndValue.second.data(), &storage);
|
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||||
returnValue = new LDFData<float>(key, data);
|
|
||||||
} catch (std::exception) {
|
|
||||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid float value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid float value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
returnValue = new LDFData<float>(key, data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case LDF_TYPE_DOUBLE: {
|
case LDF_TYPE_DOUBLE: {
|
||||||
try {
|
double data;
|
||||||
double data = strtod(ldfTypeAndValue.second.data(), &storage);
|
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||||
returnValue = new LDFData<double>(key, data);
|
|
||||||
} catch (std::exception) {
|
|
||||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid double value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid double value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
returnValue = new LDFData<double>(key, data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,9 +100,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
|||||||
} else if (ldfTypeAndValue.second == "false") {
|
} else if (ldfTypeAndValue.second == "false") {
|
||||||
data = 0;
|
data = 0;
|
||||||
} else {
|
} else {
|
||||||
try {
|
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||||
data = static_cast<uint32_t>(strtoul(ldfTypeAndValue.second.data(), &storage, 10));
|
|
||||||
} catch (std::exception) {
|
|
||||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid uint32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid uint32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@ -122,9 +118,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
|||||||
} else if (ldfTypeAndValue.second == "false") {
|
} else if (ldfTypeAndValue.second == "false") {
|
||||||
data = false;
|
data = false;
|
||||||
} else {
|
} else {
|
||||||
try {
|
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||||
data = static_cast<bool>(strtol(ldfTypeAndValue.second.data(), &storage, 10));
|
|
||||||
} catch (std::exception) {
|
|
||||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid bool value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid bool value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@ -135,24 +129,22 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case LDF_TYPE_U64: {
|
case LDF_TYPE_U64: {
|
||||||
try {
|
uint64_t data;
|
||||||
uint64_t data = static_cast<uint64_t>(strtoull(ldfTypeAndValue.second.data(), &storage, 10));
|
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||||
returnValue = new LDFData<uint64_t>(key, data);
|
|
||||||
} catch (std::exception) {
|
|
||||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid uint64 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid uint64 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
returnValue = new LDFData<uint64_t>(key, data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case LDF_TYPE_OBJID: {
|
case LDF_TYPE_OBJID: {
|
||||||
try {
|
LWOOBJID data;
|
||||||
LWOOBJID data = static_cast<LWOOBJID>(strtoll(ldfTypeAndValue.second.data(), &storage, 10));
|
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||||
returnValue = new LDFData<LWOOBJID>(key, data);
|
|
||||||
} catch (std::exception) {
|
|
||||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid LWOOBJID value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid LWOOBJID value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
returnValue = new LDFData<LWOOBJID>(key, data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ protected:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#define LdfUniquePtr std::unique_ptr<LDFBaseData>
|
typedef std::unique_ptr<LDFBaseData> LdfUniquePtr;
|
||||||
|
|
||||||
// Suite of tests for parsing LDF values
|
// Suite of tests for parsing LDF values
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user