Path: blob/main/RSDKv4/Ini.cpp
817 views
#include "RetroEngine.hpp"1#if !RETRO_USE_ORIGINAL_CODE2#include <stdlib.h>3#include <algorithm>4#include <string>56int strncmp(char const *a, char const *b)7{8for (;; a++, b++) {9int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);10if (d != 0 || !*a)11return d;12}13}1415IniParser::IniParser(const char *filename, bool addPath)16{17items.clear();18char buf[0x100];19char section[0x40];20bool hasSection = false;21char key[0x100];22char value[0x100];2324char pathBuffer[0x80];2526if (addPath) {27#if RETRO_PLATFORM == RETRO_UWP28if (!usingCWD)29sprintf(pathBuffer, "%s/%s", getResourcesPath(), filename);30else31sprintf(pathBuffer, "%s", filename);32#elif RETRO_PLATFORM == RETRO_OSX33sprintf(pathBuffer, "%s/%s", gamePath, filename);34#else35sprintf(pathBuffer, "%s", filename);36#endif37}38else {39sprintf(pathBuffer, "%s", filename);40}4142FileIO *f;43if ((f = fOpen(pathBuffer, "r")) == NULL) {44PrintLog("ERROR: Couldn't open file '%s'!", filename);45return;46}4748while (true) {49bool flag = false;50int ret = 0;51int strLen = 0;52while (true) {53ret = (int)fRead(&buf[strLen++], sizeof(byte), 1, f);54flag = ret == 0;55if (ret == 0) {56strLen--;57break;58}59if (buf[strLen - 1] == '\n')60break;61}62buf[strLen] = 0;63if (buf[0] == '#')64continue;6566if (sscanf(buf, "[%[^][]]", section) == 1) {67hasSection = true;68}69else if (sscanf(buf, "%[^;=]= %[^\t\r\n]", key, value) == 2 || sscanf(buf, "%[^;=]=%[^\t\r\n]", key, value) == 270|| sscanf(buf, "%[^;=] = %[^\t\r\n]", key, value) == 2 || sscanf(buf, "%[^;=] =%[^\t\r\n]", key, value) == 2) {71ConfigItem item;72if (hasSection)73sprintf(item.section, "%s", section);74else75sprintf(item.section, "");7677sprintf(item.key, "%s", key);78sprintf(item.value, "%s", value);79item.hasSection = hasSection;80items.push_back(item);81}82if (flag)83break;84}8586fClose(f);87}8889int IniParser::GetString(const char *section, const char *key, char *dest)90{91if (items.size() == 0)92return 0;9394for (int x = 0; x < items.size(); x++) {95if (!strcmp(section, items[x].section)) {96if (!strcmp(key, items[x].key)) {97strcpy(dest, items[x].value);98return 1;99}100}101}102103return 0;104}105int IniParser::GetInteger(const char *section, const char *key, int *dest)106{107if (items.size() == 0)108return 0;109110for (int x = 0; x < items.size(); x++) {111if (!strcmp(section, items[x].section)) {112if (!strcmp(key, items[x].key)) {113*dest = atoi(items[x].value);114return 1;115}116}117}118119return 0;120}121int IniParser::GetFloat(const char *section, const char *key, float *dest)122{123if (items.size() == 0)124return 0;125126for (int x = 0; x < items.size(); x++) {127if (!strcmp(section, items[x].section)) {128if (!strcmp(key, items[x].key)) {129*dest = atof(items[x].value);130return 1;131}132}133}134135return 0;136}137int IniParser::GetBool(const char *section, const char *key, bool *dest)138{139if (items.size() == 0)140return 0;141142for (int x = 0; x < items.size(); x++) {143if (!strcmp(section, items[x].section)) {144if (!strcmp(key, items[x].key)) {145*dest = !strncmp(items[x].value, "true") || !strcmp(items[x].value, "1");146return 1;147}148}149}150151return 0;152}153154int IniParser::SetString(const char *section, const char *key, char *value)155{156int where = -1;157for (int x = 0; x < items.size(); x++) {158if (strcmp(section, items[x].section) == 0) {159if (strcmp(key, items[x].key) == 0) {160where = x;161break;162}163}164}165if (where < 0) {166where = (int)items.size();167items.push_back(ConfigItem());168}169170strcpy(items[where].section, section);171strcpy(items[where].key, key);172strcpy(items[where].value, value);173items[where].type = INI_ITEM_STRING;174return 1;175}176int IniParser::SetInteger(const char *section, const char *key, int value)177{178int where = -1;179for (int x = 0; x < items.size(); x++) {180if (strcmp(section, items[x].section) == 0) {181if (strcmp(key, items[x].key) == 0) {182where = x;183break;184}185}186}187if (where < 0) {188where = (int)items.size();189items.push_back(ConfigItem());190}191192strcpy(items[where].section, section);193strcpy(items[where].key, key);194sprintf(items[where].value, "%d", value);195items[where].type = INI_ITEM_INT;196return 1;197}198int IniParser::SetFloat(const char *section, const char *key, float value)199{200int where = -1;201for (int x = 0; x < items.size(); x++) {202if (strcmp(section, items[x].section) == 0) {203if (strcmp(key, items[x].key) == 0) {204where = x;205break;206}207}208}209if (where < 0) {210where = (int)items.size();211items.push_back(ConfigItem());212}213214strcpy(items[where].section, section);215strcpy(items[where].key, key);216sprintf(items[where].value, "%f", value);217items[where].type = INI_ITEM_FLOAT;218return 1;219}220int IniParser::SetBool(const char *section, const char *key, bool value)221{222int where = -1;223for (int x = 0; x < items.size(); x++) {224if (strcmp(section, items[x].section) == 0) {225if (strcmp(key, items[x].key) == 0) {226where = x;227break;228}229}230}231if (where < 0) {232where = (int)items.size();233items.push_back(ConfigItem());234}235236strcpy(items[where].section, section);237strcpy(items[where].key, key);238sprintf(items[where].value, "%s", value ? "true" : "false");239items[where].type = INI_ITEM_BOOL;240return 1;241}242int IniParser::SetComment(const char *section, const char *key, const char *comment)243{244int where = -1;245for (int x = 0; x < items.size(); x++) {246if (strcmp(section, items[x].section) == 0) {247if (strcmp(key, items[x].key) == 0) {248where = x;249break;250}251}252}253if (where < 0) {254where = (int)items.size();255items.push_back(ConfigItem());256}257258strcpy(items[where].section, section);259strcpy(items[where].key, key);260sprintf(items[where].value, "%s", comment);261items[where].type = INI_ITEM_COMMENT;262return 1;263}264265void IniParser::Write(const char *filename, bool addPath)266{267char pathBuffer[0x80];268269if (addPath) {270#if RETRO_PLATFORM == RETRO_UWP271if (!usingCWD)272sprintf(pathBuffer, "%s/%s", getResourcesPath(), filename);273else274sprintf(pathBuffer, "%s", filename);275#elif RETRO_PLATFORM == RETRO_OSX276sprintf(pathBuffer, "%s/%s", gamePath, filename);277#else278sprintf(pathBuffer, "%s", filename);279#endif280}281else {282sprintf(pathBuffer, "%s", filename);283}284285FileIO *f;286if ((f = fOpen(pathBuffer, "w")) == NULL) {287PrintLog("ERROR: Couldn't open file '%s' for writing!", filename);288return;289}290291char sections[10][60];292char past[60];293int c = 0;294sprintf(past, "");295for (int i = 0; i < items.size(); ++i) {296if (std::find(std::begin(sections), std::end(sections), items[i].section) == std::end(sections) && strcmp(past, items[i].section) != 0) {297sprintf(past, "%s", items[i].section);298sprintf(sections[c], "%s", items[i].section);299c++;300}301}302303if (c > 1) {304if (strcmp(sections[0], sections[c - 1]) == 0)305c--;306}307308char buffer[0x100];309310// Sectionless items311for (int i = 0; i < items.size(); ++i) {312if (strcmp("", items[i].section) == 0) {313switch (items[i].type) {314default:315case INI_ITEM_STRING:316case INI_ITEM_INT:317case INI_ITEM_FLOAT:318case INI_ITEM_BOOL:319sprintf(buffer, "%s=%s\n", items[i].key, items[i].value);320fWrite(&buffer, 1, StrLength(buffer), f);321break;322case INI_ITEM_COMMENT:323sprintf(buffer, "; %s\n", items[i].value);324fWrite(&buffer, 1, StrLength(buffer), f);325break;326}327}328}329sprintf(buffer, "\n");330fWrite(&buffer, StrLength(buffer), 1, f);331332// Sections333for (int s = 0; s < c; ++s) {334sprintf(buffer, "[%s]\n", sections[s]);335fWrite(&buffer, 1, StrLength(buffer), f);336for (int i = 0; i < items.size(); ++i) {337if (strcmp(sections[s], items[i].section) == 0) {338switch (items[i].type) {339default:340case INI_ITEM_STRING:341case INI_ITEM_INT:342case INI_ITEM_FLOAT:343case INI_ITEM_BOOL:344sprintf(buffer, "%s=%s\n", items[i].key, items[i].value);345fWrite(&buffer, 1, StrLength(buffer), f);346break;347case INI_ITEM_COMMENT:348sprintf(buffer, "; %s\n", items[i].value);349fWrite(&buffer, 1, StrLength(buffer), f);350break;351}352}353}354355if (s + 1 < c) {356sprintf(buffer, "\n");357fWrite(&buffer, StrLength(buffer), 1, f);358}359}360361fClose(f);362}363#endif364365366