Commit f1b62cc7 authored by nanahira's avatar nanahira

move lflist reader to deckmanager cpp

parent 3214865a
......@@ -8,10 +8,51 @@ namespace ygo {
DeckManager deckManager;
void DeckManager::LoadLFListFromLineProvider(const std::function<bool(char*, size_t)>& getLine, bool insert) {
std::vector<LFList> loadedLists;
auto cur = loadedLists.rend();
char linebuf[256]{};
wchar_t strBuffer[256]{};
while (getLine(linebuf, sizeof(linebuf))) {
if (linebuf[0] == '#') continue;
if (linebuf[0] == '!') {
auto len = std::strcspn(linebuf, "\r\n");
linebuf[len] = 0;
BufferIO::DecodeUTF8(&linebuf[1], strBuffer);
LFList newlist;
newlist.listName = strBuffer;
newlist.hash = 0x7dfcee6a;
loadedLists.push_back(newlist);
cur = loadedLists.rbegin();
continue;
}
if (cur == loadedLists.rend()) continue;
char* pos = linebuf;
errno = 0;
auto result = std::strtoul(pos, &pos, 10);
if (errno || result > UINT32_MAX) continue;
if (pos == linebuf || *pos != ' ') continue;
uint32_t code = static_cast<uint32_t>(result);
errno = 0;
int count = std::strtol(pos, &pos, 10);
if (errno) continue;
if (count < 0 || count > 2) continue;
cur->content[code] = count;
cur->hash = cur->hash ^ ((code << 18) | (code >> 14))
^ ((code << (27 + count)) | (code >> (5 - count)));
}
if (insert) {
_lfList.insert(_lfList.begin(), loadedLists.begin(), loadedLists.end());
} else {
_lfList.insert(_lfList.end(), loadedLists.begin(), loadedLists.end());
}
}
void DeckManager::LoadLFListSingle(const char* path, bool insert) {
FILE* fp = myfopen(path, "r");
if (!fp) return;
_LoadLFListFromLineProvider([&](char* buf, size_t sz) {
LoadLFListFromLineProvider([&](char* buf, size_t sz) {
return std::fgets(buf, sz, fp) != nullptr;
}, insert);
std::fclose(fp);
......@@ -19,7 +60,7 @@ void DeckManager::LoadLFListSingle(const char* path, bool insert) {
void DeckManager::LoadLFListSingle(const wchar_t* path, bool insert) {
FILE* fp = mywfopen(path, "r");
if (!fp) return;
_LoadLFListFromLineProvider([&](char* buf, size_t sz) {
LoadLFListFromLineProvider([&](char* buf, size_t sz) {
return std::fgets(buf, sz, fp) != nullptr;
}, insert);
std::fclose(fp);
......@@ -27,7 +68,7 @@ void DeckManager::LoadLFListSingle(const wchar_t* path, bool insert) {
void DeckManager::LoadLFListSingle(irr::io::IReadFile* reader, bool insert) {
std::string linebuf;
char ch{};
_LoadLFListFromLineProvider([&](char* buf, size_t sz) {
LoadLFListFromLineProvider([&](char* buf, size_t sz) {
while (reader->read(&ch, 1)) {
if (ch == '\0') break;
linebuf.push_back(ch);
......
......@@ -4,6 +4,7 @@
#include <unordered_map>
#include <vector>
#include <sstream>
#include <functional>
#include "data_manager.h"
#include "bufferio.h"
......@@ -105,53 +106,7 @@ public:
static bool SaveDeckArray(const DeckArray& deck, const wchar_t* name);
private:
template<typename LineProvider>
void _LoadLFListFromLineProvider(LineProvider getLine, bool insert = false) {
std::vector<LFList> loadedLists;
auto cur = loadedLists.rend(); // 注意:在临时 list 上操作
char linebuf[256]{};
wchar_t strBuffer[256]{};
while (getLine(linebuf, sizeof(linebuf))) {
if (linebuf[0] == '#')
continue;
if (linebuf[0] == '!') {
auto len = std::strcspn(linebuf, "\r\n");
linebuf[len] = 0;
BufferIO::DecodeUTF8(&linebuf[1], strBuffer);
LFList newlist;
newlist.listName = strBuffer;
newlist.hash = 0x7dfcee6a;
loadedLists.push_back(newlist);
cur = loadedLists.rbegin();
continue;
}
if (cur == loadedLists.rend())
continue;
char* pos = linebuf;
errno = 0;
auto result = std::strtoul(pos, &pos, 10);
if (errno || result > UINT32_MAX)
continue;
if (pos == linebuf || *pos != ' ')
continue;
uint32_t code = static_cast<uint32_t>(result);
errno = 0;
int count = std::strtol(pos, &pos, 10);
if (errno)
continue;
if (count < 0 || count > 2)
continue;
cur->content[code] = count;
cur->hash = cur->hash ^ ((code << 18) | (code >> 14)) ^ ((code << (27 + count)) | (code >> (5 - count)));
}
if (insert) {
_lfList.insert(_lfList.begin(), loadedLists.begin(), loadedLists.end());
} else {
_lfList.insert(_lfList.end(), loadedLists.begin(), loadedLists.end());
}
}
void LoadLFListFromLineProvider(const std::function<bool(char*, size_t)>& getLine, bool insert = false);
};
extern DeckManager deckManager;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment