Add tests for SaveMap

This commit is contained in:
Jiga228
2025-10-13 21:06:23 +07:00
parent 8819114b63
commit 87c72e4a48
7 changed files with 255 additions and 41 deletions
+55 -35
View File
@@ -32,63 +32,64 @@ SaveMap::SaveMap(const char* class_name) : class_name(class_name)
SaveMap::SaveMap(const std::string& json_data)
{
for (size_t i = 0; i < json_data.length() - 1;)
std::string json_data_blank = CleaningJSON(json_data);
for (size_t i = 0; i < json_data_blank.length() - 1;)
{
size_t key_begin = i = json_data.find_first_of('\"', i) + 1;
size_t key_end = i = json_data.find_first_of('\"', i);
size_t key_begin = i = json_data_blank.find_first_of('\"', i) + 1;
size_t key_end = i = json_data_blank.find_first_of('\"', i);
std::string name = json_data.substr(key_begin, key_end - key_begin);
std::string name = json_data_blank.substr(key_begin, key_end - key_begin);
if (class_name.empty())
{
size_t begin = json_data.find(':', i) + 2;
size_t end = i = json_data.find('\"', begin);
size_t begin = json_data_blank.find(':', i) + 2;
size_t end = i = json_data_blank.find('\"', begin);
i++;
class_name = json_data.substr(begin, end - begin);
class_name = json_data_blank.substr(begin, end - begin);
continue;
}
if (name[0] == 'i') {
size_t begin = json_data.find(':', i) + 2;
size_t end = i = json_data.find_first_of(",}", i);
size_t begin = json_data_blank.find(':', i) + 1;
size_t end = i = json_data_blank.find_first_of(",}", i);
i++;
std::string value = json_data.substr(begin, end - begin - 1);
std::string value = json_data_blank.substr(begin, end - begin);
save_long[name.c_str() + 1] = std::stoll(value);
} else if (name[0] == 'd')
{
size_t begin = json_data.find(':', i) + 2;
size_t end = i = json_data.find_first_of(",}", i);
size_t begin = json_data_blank.find(':', i) + 1;
size_t end = i = json_data_blank.find_first_of(",}", i);
i++;
std::string value = json_data.substr(begin, end - begin - 1);
std::string value = json_data_blank.substr(begin, end - begin);
save_double[name.c_str() + 1] = std::stod(value);
} else if (name[0] == 's')
{
size_t begin = json_data.find(':', i) + 2;
size_t end = i = json_data.find('\"', begin);
size_t begin = json_data_blank.find(':', i) + 2;
size_t end = i = json_data_blank.find('\"', begin);
i++;
std::string value;
if (begin != end)
value = json_data.substr(begin, end - begin);
value = json_data_blank.substr(begin, end - begin);
else
value = "";
save_string[name.c_str() + 1] = value;
} else if (name[0] == 'o')
{
size_t begin = i = json_data.find('{', i);
size_t begin = i = json_data_blank.find('{', i);
int open = 1, close = 0;
while (close < open)
{
i++;
if (json_data[i] == '{')
if (json_data_blank[i] == '{')
open++;
else if (json_data[i] == '}')
else if (json_data_blank[i] == '}')
close++;
}
size_t end = i;
i++;
std::string object_json = json_data.substr(begin, end - begin + 1);
std::string object_json = json_data_blank.substr(begin, end - begin + 1);
begin = object_json.find(':') + 2;
end = object_json.find('\"', begin);
@@ -102,20 +103,20 @@ SaveMap::SaveMap(const std::string& json_data)
{
std::string key = name.substr(2, name.length() - 2);
size_t begin_arr = i = json_data.find('[', i);
size_t begin_arr = i = json_data_blank.find('[', i);
int open = 1, close = 0;
while (close < open)
{
i++;
if (json_data[i] == '[')
if (json_data_blank[i] == '[')
open++;
else if (json_data[i] == ']')
else if (json_data_blank[i] == ']')
close++;
}
size_t end_arr = i;
i++;
std::string arr_data = json_data.substr(begin_arr, end_arr - begin_arr + 1);
std::string arr_data = json_data_blank.substr(begin_arr, end_arr - begin_arr + 1);
if (name[1] == 'i')
{
save_vector_integer[key] = std::vector<long long>();
@@ -155,6 +156,8 @@ SaveMap::SaveMap(const std::string& json_data)
while (j < arr_data.length())
{
size_t begin = arr_data.find('\"', j) + 1;
if (begin == std::string::npos + 1)
break;
size_t end = j = arr_data.find('\"', begin);
std::string value;
if (begin != end)
@@ -162,7 +165,7 @@ SaveMap::SaveMap(const std::string& json_data)
else
value = "";
vector_strings.push_back(value);
j += 3;
++j;
}
} else if (name[1] == 'o')
{
@@ -206,20 +209,20 @@ SaveMap::SaveMap(const std::string& json_data)
{
std::string key = name.substr(2, name.length() - 2);
size_t begin_arr = i = json_data.find('[', i);
size_t begin_arr = i = json_data_blank.find('[', i);
int open = 1, close = 0;
while (close < open)
{
i++;
if (json_data[i] == '[')
if (json_data_blank[i] == '[')
open++;
else if (json_data[i] == ']')
else if (json_data_blank[i] == ']')
close++;
}
size_t end_arr = i;
i++;
std::string arr_data = json_data.substr(begin_arr, end_arr - begin_arr + 1);
std::string arr_data = json_data_blank.substr(begin_arr, end_arr - begin_arr + 1);
if (name[1] == 'i')
{
save_list_integer[key] = std::list<long long>();
@@ -304,21 +307,21 @@ SaveMap::SaveMap(const std::string& json_data)
}
}
else if (name == "Parent parameters") {
size_t parent_begin = i = json_data.find('{', i);
size_t parent_begin = i = json_data_blank.find('{', i);
int open = 1, close = 0;
while (close < open)
{
i++;
if (json_data[i] == '{')
if (json_data_blank[i] == '{')
open++;
else if (json_data[i] == '}')
else if (json_data_blank[i] == '}')
close++;
}
size_t parent_end = i;
i++;
std::string str = json_data.substr(parent_begin, parent_end - parent_begin + 1);
std::string str = json_data_blank.substr(parent_begin, parent_end - parent_begin + 1);
parent_ = std::make_shared<SaveMap>(str);
}
}
@@ -448,9 +451,9 @@ double SaveMap::GetDouble(const char* name)
return save_double[name];
}
const char* SaveMap::GetString(const char* name)
std::string SaveMap::GetString(const char* name)
{
return save_string[name].c_str();
return save_string[name];
}
ISave* SaveMap::GetObject(const char* name)
@@ -634,3 +637,20 @@ std::shared_ptr<SaveMap> SaveMap::connect_to(const std::shared_ptr<SaveMap>& par
this->parent_ = parent;
return std::shared_ptr<SaveMap>(this);
}
std::string SaveMap::CleaningJSON(const std::string& json_data)
{
std::string blank_str;
blank_str.reserve(json_data.length());
bool open_str = false;
for (auto i : json_data)
{
if (i == '\"')
open_str = !open_str;
if (open_str || (i != ' ' && i != '\t' && i != '\n' && i != '\r'))
blank_str += i;
}
return blank_str;
}
+3 -1
View File
@@ -60,7 +60,7 @@ public:
// Методы получения значенй
long long GetInteger(const char* name);
double GetDouble(const char* name);
const char* GetString(const char* name);
std::string GetString(const char* name);
ISave* GetObject(const char* name);
std::vector<long long>& GetVectorInteger(const char* name);
std::vector<double>& GetVectorDouble(const char* name);
@@ -78,4 +78,6 @@ public:
std::string serialize() noexcept;
std::shared_ptr<SaveMap> connect_to(const std::shared_ptr<SaveMap>& parent);
static std::string CleaningJSON(const std::string& json_data);
};