mirror of
https://github.com/sfall-team/sfall.git
synced 2026-07-27 16:52:34 -07:00
get_ini_section(s): switched to using Config class
- Load configs via IniReader to take advantage of it's cache
This commit is contained in:
@@ -1855,11 +1855,16 @@
|
|||||||
opcode: 0x81eb
|
opcode: 0x81eb
|
||||||
- name: get_ini_section
|
- name: get_ini_section
|
||||||
detail: array get_ini_section(string file, string sect)
|
detail: array get_ini_section(string file, string sect)
|
||||||
doc: 'Returns an associative array of keys and values for a given INI file and section. NOTE: all keys and their values will be of String type.'
|
doc: |
|
||||||
|
Returns an associative array of keys and values for a given INI file and section.
|
||||||
|
- If INI file is not found, will return an empty array.
|
||||||
|
- All keys and their values will be of String type.
|
||||||
macro: sfall.h
|
macro: sfall.h
|
||||||
- name: get_ini_sections
|
- name: get_ini_sections
|
||||||
detail: array get_ini_sections(string file)
|
detail: array get_ini_sections(string file)
|
||||||
doc: Returns an array of names of all sections in a given INI file.
|
doc: |
|
||||||
|
Returns an array of names of all sections in a given INI file.
|
||||||
|
- If INI file is not found, will return an empty array.
|
||||||
macro: sfall.h
|
macro: sfall.h
|
||||||
- name: set_ini_setting
|
- name: set_ini_setting
|
||||||
detail: void set_ini_setting(string setting, int/string value)
|
detail: void set_ini_setting(string setting, int/string value)
|
||||||
|
|||||||
@@ -156,52 +156,41 @@ static std::string GetIniFilePathFromArg(const ScriptValue& arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void mf_get_ini_sections(OpcodeContext& ctx) {
|
void mf_get_ini_sections(OpcodeContext& ctx) {
|
||||||
// TODO: use Config
|
Config* config = IniReader::GetIniConfig(GetIniFilePathFromArg(ctx.arg(0)).c_str());
|
||||||
if (!GetPrivateProfileSectionNamesA(ScriptExtender::gTextBuffer, ScriptExtender::TextBufferSize(), GetIniFilePathFromArg(ctx.arg(0)).c_str())) {
|
if (config == nullptr) {
|
||||||
ctx.setReturn(CreateTempArray(0, 0));
|
ctx.setReturn(CreateTempArray(0, 0));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::vector<char*> sections;
|
const auto& data = config->data();
|
||||||
char* section = ScriptExtender::gTextBuffer;
|
size_t numSections = config->data().size();
|
||||||
while (*section != 0) {
|
int arrayId = CreateTempArray(numSections, 0);
|
||||||
sections.push_back(section); // position
|
size_t i = 0;
|
||||||
section += std::strlen(section) + 1;
|
for (auto sectIt = data.cbegin(); sectIt != data.cend(); ++sectIt) {
|
||||||
}
|
arrays[arrayId].val[i].set(sectIt->first.c_str(), sectIt->first.size());
|
||||||
size_t sz = sections.size();
|
++i;
|
||||||
int arrayId = CreateTempArray(sz, 0);
|
|
||||||
auto& arr = arrays[arrayId];
|
|
||||||
|
|
||||||
for (size_t i = 0; i < sz; ++i) {
|
|
||||||
size_t j = i + 1;
|
|
||||||
int len = (j < sz) ? sections[j] - sections[i] - 1 : -1;
|
|
||||||
arr.val[i].set(sections[i], len); // copy string from buffer
|
|
||||||
}
|
}
|
||||||
ctx.setReturn(arrayId);
|
ctx.setReturn(arrayId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mf_get_ini_section(OpcodeContext& ctx) {
|
static void CopyConfigSectionToArray(DWORD arrayId, const Config::Section& section) {
|
||||||
auto section = ctx.arg(1).strValue();
|
for (auto valueIt = section.cbegin(); valueIt != section.cend(); ++valueIt) {
|
||||||
int arrayId = CreateTempArray(-1, 0); // associative
|
SetArray(arrayId, valueIt->first.c_str(), valueIt->second.c_str(), false);
|
||||||
|
|
||||||
// TODO: use config
|
|
||||||
if (GetPrivateProfileSectionA(section, ScriptExtender::gTextBuffer, ScriptExtender::TextBufferSize(), GetIniFilePathFromArg(ctx.arg(0)).c_str())) {
|
|
||||||
auto& arr = arrays[arrayId];
|
|
||||||
char *key = ScriptExtender::gTextBuffer, *val = nullptr;
|
|
||||||
while (*key != 0) {
|
|
||||||
char* val = std::strpbrk(key, "=");
|
|
||||||
if (val != nullptr) {
|
|
||||||
*val = '\0';
|
|
||||||
val += 1;
|
|
||||||
|
|
||||||
SetArray(arrayId, ScriptValue(key), ScriptValue(val), false);
|
|
||||||
|
|
||||||
key = val + std::strlen(val) + 1;
|
|
||||||
} else {
|
|
||||||
key += std::strlen(key) + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void mf_get_ini_section(OpcodeContext& ctx) {
|
||||||
|
auto sectionName = ctx.arg(1).strValue();
|
||||||
|
int arrayId = CreateTempArray(-1, 0); // associative
|
||||||
ctx.setReturn(arrayId);
|
ctx.setReturn(arrayId);
|
||||||
|
|
||||||
|
Config* config = IniReader::GetIniConfig(GetIniFilePathFromArg(ctx.arg(0)).c_str());
|
||||||
|
if (config == nullptr) return; // ini file not found
|
||||||
|
|
||||||
|
const auto& data = config->data();
|
||||||
|
auto sectIt = data.find(sectionName);
|
||||||
|
if (sectIt == data.end()) return; // ini section not found
|
||||||
|
|
||||||
|
CopyConfigSectionToArray(arrayId, sectIt->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mf_config_load(OpcodeContext& ctx) {
|
void mf_config_load(OpcodeContext& ctx) {
|
||||||
@@ -247,10 +236,7 @@ void mf_config_load(OpcodeContext& ctx) {
|
|||||||
const auto& data = config->data();
|
const auto& data = config->data();
|
||||||
for (auto sectIt = data.cbegin(); sectIt != data.cend(); ++sectIt) {
|
for (auto sectIt = data.cbegin(); sectIt != data.cend(); ++sectIt) {
|
||||||
DWORD subArrayId = CreateArray(-1, 0);
|
DWORD subArrayId = CreateArray(-1, 0);
|
||||||
const auto& section = sectIt->second;
|
CopyConfigSectionToArray(subArrayId, sectIt->second);
|
||||||
for (auto valueIt = section.cbegin(); valueIt != section.cend(); ++valueIt) {
|
|
||||||
SetArray(subArrayId, valueIt->first.c_str(), valueIt->second.c_str(), false);
|
|
||||||
}
|
|
||||||
SetArray(arrayId, sectIt->first.c_str(), subArrayId, false);
|
SetArray(arrayId, sectIt->first.c_str(), subArrayId, false);
|
||||||
}
|
}
|
||||||
// Save new array ID to cache and return it.
|
// Save new array ID to cache and return it.
|
||||||
|
|||||||
Reference in New Issue
Block a user