DarkflameServer/dCommon/LDFFormat.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
2.9 KiB
C++
Raw Normal View History

#include "LDFFormat.h"
// Custom Classes
#include "GeneralUtils.h"
// C++
#include <sstream>
#include <vector>
//! Returns a pointer to a LDFData value based on string format
LDFBaseData* LDFBaseData::DataFromString(const std::string& format) {
2022-07-28 13:39:57 +00:00
// First, check the format
std::istringstream ssFormat(format);
std::string token;
2022-07-28 13:39:57 +00:00
std::vector<std::string> keyValueArray;
while (std::getline(ssFormat, token, '=')) {
keyValueArray.push_back(token);
}
2022-07-28 13:39:57 +00:00
if (keyValueArray.size() == 2) {
std::u16string key = GeneralUtils::ASCIIToUTF16(keyValueArray[0]);
2022-07-28 13:39:57 +00:00
std::vector<std::string> dataArray;
std::istringstream ssData(keyValueArray[1]);
while (std::getline(ssData, token, ':')) {
dataArray.push_back(token);
}
if (dataArray.size() > 2) { // hacky fix for strings with colons in them
std::vector<std::string> newDataArray;
newDataArray.push_back(dataArray[0]);
std::string value = "";
for (size_t i = 1; i < dataArray.size(); ++i) {
value += dataArray[i] + ':';
}
value.pop_back(); // remove last colon
newDataArray.push_back(value);
dataArray = newDataArray;
}
2022-07-28 13:39:57 +00:00
if ((dataArray[0] == "0" || dataArray[0] == "13") && dataArray.size() == 1) {
dataArray.push_back("");
}
2022-07-28 13:39:57 +00:00
if (dataArray.size() == 2) {
eLDFType type = static_cast<eLDFType>(stoi(dataArray[0]));
2022-07-28 13:39:57 +00:00
switch (type) {
case LDF_TYPE_UTF_16: {
2022-08-02 13:56:20 +00:00
std::u16string data = GeneralUtils::UTF8ToUTF16(dataArray[1]);
return new LDFData<std::u16string>(key, data);
}
2022-07-28 13:39:57 +00:00
case LDF_TYPE_S32: {
int32_t data = static_cast<int32_t>(stoull(dataArray[1]));
return new LDFData<int32_t>(key, data);
}
2022-07-28 13:39:57 +00:00
case LDF_TYPE_FLOAT: {
float data = static_cast<float>(stof(dataArray[1]));
return new LDFData<float>(key, data);
}
2022-07-28 13:39:57 +00:00
case LDF_TYPE_DOUBLE: {
double data = static_cast<float>(stod(dataArray[1]));
return new LDFData<double>(key, data);
}
2022-07-28 13:39:57 +00:00
case LDF_TYPE_U32:
{
uint32_t data;
2022-07-28 13:39:57 +00:00
if (dataArray[1] == "true") {
data = 1;
} else if (dataArray[1] == "false") {
data = 0;
} else {
data = static_cast<uint32_t>(stoul(dataArray[1]));
}
2022-07-28 13:39:57 +00:00
return new LDFData<uint32_t>(key, data);
}
2022-07-28 13:39:57 +00:00
case LDF_TYPE_BOOLEAN: {
bool data;
2022-07-28 13:39:57 +00:00
if (dataArray[1] == "true") {
data = true;
} else if (dataArray[1] == "false") {
data = false;
} else {
data = static_cast<bool>(stoi(dataArray[1]));
}
2022-07-28 13:39:57 +00:00
return new LDFData<bool>(key, data);
}
2022-07-28 13:39:57 +00:00
case LDF_TYPE_U64: {
uint64_t data = static_cast<uint64_t>(stoull(dataArray[1]));
return new LDFData<uint64_t>(key, data);
}
2022-07-28 13:39:57 +00:00
case LDF_TYPE_OBJID: {
LWOOBJID data = static_cast<LWOOBJID>(stoll(dataArray[1]));
return new LDFData<LWOOBJID>(key, data);
}
2022-07-28 13:39:57 +00:00
case LDF_TYPE_UTF_8: {
std::string data = dataArray[1];
return new LDFData<std::string>(key, data);
}
2022-07-28 13:39:57 +00:00
case LDF_TYPE_UNKNOWN: {
return nullptr;
2022-07-28 13:39:57 +00:00
}
}
}
}
2022-07-28 13:39:57 +00:00
return nullptr;
2022-07-28 13:39:57 +00:00
}