// Provenance: AVA2 EterLib/parser.cpp (script::Group). CharNextExA/CharPrevExA walk the default // code page (a DBCS lead byte and its trail are one character); NUL ends the source like it does // for CharNextExA. Kept quirks: LocaleString_Skip never advances past a single-byte space, and a // "name|" argument without a value reuses the previous argument's value buffer. #include "parser.h" #include "python_app.h" #include "shared/codepage/codepage.h" #include #include #include using namespace script; namespace { // CharNextExA: one character, two bytes for a lead byte with a trail; NUL does not advance. const char* CharNext(unsigned codePage, const char* cur) { if (!*cur) return cur; if (mt_codepage::is_lead_byte(codePage, static_cast(*cur)) && cur[1]) return cur + 2; return cur + 1; } // CharPrevExA: the start of the character before cur, scanning from base. char* CharPrev(unsigned codePage, char* base, char* cur) { char* prev = base; for (char* p = base; p < cur;) { prev = p; const char* next = CharNext(codePage, p); if (next == p) break; p = const_cast(next); } return prev; } const char* LocaleString_FindChar(const char* base, int len, char test) { if (!base) return nullptr; const unsigned codePage = ava2::GetDefaultCodePage(); int pos = 0; while (pos < len) { const char* cur = base + pos; const int cur_len = int(CharNext(codePage, cur) - cur); if (cur_len > 1) pos += cur_len; else if (1 == cur_len) { if (*cur == test) return cur; ++pos; } else break; } return nullptr; } int LocaleString_RightTrim(char* base, int len) { const unsigned codePage = ava2::GetDefaultCodePage(); int pos = len; while (pos > 0) { char* cur = base + pos; char* prev = CharPrev(codePage, base, cur); const int prev_len = int(cur - prev); if (prev_len != 1) break; if (!isspace(static_cast(*prev)) && *prev != '\n' && *prev != '\r') break; *prev = '\0'; pos -= prev_len; } return pos > 0 ? pos : 0; } // The Windows loop never advances on a single-byte whitespace, so it spins 9216 times, traces // "Infinite loop" and returns that space; the result is the same without the spin. const char* LocaleString_Skip(unsigned codePage, const char* cur) { while (*cur) { const char* next = CharNext(codePage, cur); const int cur_len = int(next - cur); if (cur_len > 1) cur = next; else { if (isspace(static_cast(*cur)) || *cur == '\n' || *cur == '\r') SDL_Log("Infinite loop in LocaleString_Skip [%s]", cur); return cur; } } return cur; } } // namespace bool Group::GetArg(const char* c_arg_base, int arg_len, TArgList& argList) { // Windows: char szName[32 + 1], szValue[64 + 1] on the stack; one spare byte here keeps a // two-byte character written at length 31 / 63 inside the buffer. char szName[32 + 2] = {}; char szValue[64 + 2] = {}; int iNameLen = 0; int iValueLen = 0; int pos = 0; bool isValue = false; const unsigned codePage = ava2::GetDefaultCodePage(); while (pos < arg_len) { const char* cur = c_arg_base + pos; const int iCharLen = int(CharNext(codePage, cur) - cur); if (iCharLen > 1) { if (isValue) { if (iValueLen >= 64) { SDL_Log("argument value overflow: must be shorter than 64 letters"); return false; } memcpy(szValue + iValueLen, cur, iCharLen); iValueLen += iCharLen; szValue[iValueLen] = '\0'; } else { if (iNameLen >= 32) { SDL_Log("argument name overflow: must be shorter than 32 letters"); return false; } memcpy(szName + iNameLen, cur, iCharLen); iNameLen += iCharLen; szName[iNameLen] = '\0'; } } else if (iCharLen == 1) { const char c = *cur; if (c == '|') { if (iNameLen == 0) { SDL_Log("no argument name"); return false; } isValue = false; iNameLen = LocaleString_RightTrim(szName, iNameLen); iValueLen = LocaleString_RightTrim(szValue, iValueLen); argList.push_back(TArg(szName, szValue)); iNameLen = 0; iValueLen = 0; } else if (c == ';') { isValue = true; } else if (!isValue && iNameLen == 0 && isspace(static_cast(c))) { // leading spaces of a name are skipped } else if (c == '\r' || c == '\n') { } else if (isValue) { if (iValueLen >= 64) { SDL_Log("argument value overflow: must be shorter than 64 letters"); return false; } szValue[iValueLen++] = c; szValue[iValueLen] = '\0'; } else { if (iNameLen >= 32) { SDL_Log("argument name overflow: must be shorter than 32 letters"); return false; } szName[iNameLen++] = c; szName[iNameLen] = '\0'; } } else { break; } pos += iCharLen; } if (iNameLen != 0 && iValueLen != 0) { iNameLen = LocaleString_RightTrim(szName, iNameLen); iValueLen = LocaleString_RightTrim(szValue, iValueLen); argList.push_back(TArg(szName, szValue)); } return true; } int Group::Create(const std::string& stSource) { m_cmdList.clear(); if (stSource.empty()) return -1; const char* str_base = stSource.c_str(); if (!*str_base) { SDL_Log("Source file has no content"); return -1; } const int str_len = int(stSource.length()); int str_pos = 0; int len_line = 0; const unsigned codePage = ava2::GetDefaultCodePage(); char box_data[1024 + 1]; while (str_pos < str_len) { TCmd cmd; const char* word = str_base + str_pos; const char* word_next = CharNext(codePage, word); const int word_len = int(word_next - word); if (word_len > 1) { str_pos += word_len; cmd.name.assign("LETTER"); cmd.argList.push_back(TArg("value", std::string(word, word_next))); m_cmdList.push_back(cmd); } else if (word_len == 1) { const char cur = *word; if ('[' == cur) { ++str_pos; const char* box_begin = str_base + str_pos; const char* box_end = LocaleString_FindChar(box_begin, str_len - str_pos, ']'); if (!box_end) { SDL_Log(" !! PARSING ERROR - Syntax Error : %s", box_begin); return -1; } str_pos += int(box_end - box_begin) + 1; const char* data_begin = LocaleString_Skip(codePage, box_begin); int data_len = int(box_end - data_begin); if (data_len >= 1024) { SDL_Log(" !! PARSING ERROR - Buffer Overflow : %d, %s", data_len, str_base); return -1; } memcpy(box_data, data_begin, data_len); box_data[data_len] = '\0'; data_len = LocaleString_RightTrim(box_data, data_len); const char* space = LocaleString_FindChar(box_data, data_len, ' '); if (space) { cmd.name.assign(box_data, space - box_data); const char* arg = LocaleString_Skip(codePage, CharNext(codePage, space)); const int arg_len = data_len - int(arg - box_data); if (!GetArg(arg, arg_len, cmd.argList)) { SDL_Log(" !! PARSING ERROR - Unknown Arguments : %d, %s", arg_len, arg); return -1; } } else { cmd.name.assign(box_data); cmd.argList.clear(); } m_cmdList.push_back(cmd); } else if (cur == '\r' || cur == '\n') { if (cur == '\n') len_line += 1; ++str_pos; } else { ++str_pos; cmd.name.assign("LETTER"); cmd.argList.push_back(TArg("value", std::string(1, cur))); m_cmdList.push_back(cmd); } } else break; } return len_line; } bool Group::GetCmd(TCmd& cmd) { if (m_cmdList.empty()) return false; cmd = m_cmdList.front(); m_cmdList.pop_front(); return true; } bool Group::ReadCmd(TCmd& cmd) { if (m_cmdList.empty()) return false; cmd = m_cmdList.front(); return true; }