Path: blob/main/misc/emulator/xnes/snes9x/conffile.cpp
28515 views
/***********************************************************************************1Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.23(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),4Jerremy Koot ([email protected])56(c) Copyright 2002 - 2004 Matthew Kendora78(c) Copyright 2002 - 2005 Peter Bortas ([email protected])910(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)1112(c) Copyright 2001 - 2006 John Weidman ([email protected])1314(c) Copyright 2002 - 2006 funkyass ([email protected]),15Kris Bleakley ([email protected])1617(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),18Nach ([email protected]),1920(c) Copyright 2002 - 2011 zones ([email protected])2122(c) Copyright 2006 - 2007 nitsuja2324(c) Copyright 2009 - 2011 BearOso,25OV2262728BS-X C emulator code29(c) Copyright 2005 - 2006 Dreamer Nom,30zones3132C4 x86 assembler and some C emulation code33(c) Copyright 2000 - 2003 _Demo_ ([email protected]),34Nach,35zsKnight ([email protected])3637C4 C++ code38(c) Copyright 2003 - 2006 Brad Jorsch,39Nach4041DSP-1 emulator code42(c) Copyright 1998 - 2006 _Demo_,43Andreas Naive ([email protected]),44Gary Henderson,45Ivar ([email protected]),46John Weidman,47Kris Bleakley,48Matthew Kendora,49Nach,50neviksti ([email protected])5152DSP-2 emulator code53(c) Copyright 2003 John Weidman,54Kris Bleakley,55Lord Nightmare ([email protected]),56Matthew Kendora,57neviksti5859DSP-3 emulator code60(c) Copyright 2003 - 2006 John Weidman,61Kris Bleakley,62Lancer,63z80 gaiden6465DSP-4 emulator code66(c) Copyright 2004 - 2006 Dreamer Nom,67John Weidman,68Kris Bleakley,69Nach,70z80 gaiden7172OBC1 emulator code73(c) Copyright 2001 - 2004 zsKnight,74pagefault ([email protected]),75Kris Bleakley76Ported from x86 assembler to C by sanmaiwashi7778SPC7110 and RTC C++ emulator code used in 1.39-1.5179(c) Copyright 2002 Matthew Kendora with research by80zsKnight,81John Weidman,82Dark Force8384SPC7110 and RTC C++ emulator code used in 1.52+85(c) Copyright 2009 byuu,86neviksti8788S-DD1 C emulator code89(c) Copyright 2003 Brad Jorsch with research by90Andreas Naive,91John Weidman9293S-RTC C emulator code94(c) Copyright 2001 - 2006 byuu,95John Weidman9697ST010 C++ emulator code98(c) Copyright 2003 Feather,99John Weidman,100Kris Bleakley,101Matthew Kendora102103Super FX x86 assembler emulator code104(c) Copyright 1998 - 2003 _Demo_,105pagefault,106zsKnight107108Super FX C emulator code109(c) Copyright 1997 - 1999 Ivar,110Gary Henderson,111John Weidman112113Sound emulator code used in 1.5-1.51114(c) Copyright 1998 - 2003 Brad Martin115(c) Copyright 1998 - 2006 Charles Bilyue'116117Sound emulator code used in 1.52+118(c) Copyright 2004 - 2007 Shay Green ([email protected])119120SH assembler code partly based on x86 assembler code121(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])1221232xSaI filter124(c) Copyright 1999 - 2001 Derek Liauw Kie Fa125126HQ2x, HQ3x, HQ4x filters127(c) Copyright 2003 Maxim Stepin ([email protected])128129NTSC filter130(c) Copyright 2006 - 2007 Shay Green131132GTK+ GUI code133(c) Copyright 2004 - 2011 BearOso134135Win32 GUI code136(c) Copyright 2003 - 2006 blip,137funkyass,138Matthew Kendora,139Nach,140nitsuja141(c) Copyright 2009 - 2011 OV2142143Mac OS GUI code144(c) Copyright 1998 - 2001 John Stiles145(c) Copyright 2001 - 2011 zones146147148Specific ports contains the works of other authors. See headers in149individual files.150151152Snes9x homepage: http://www.snes9x.com/153154Permission to use, copy, modify and/or distribute Snes9x in both binary155and source form, for non-commercial purposes, is hereby granted without156fee, providing that this license information and copyright notice appear157with all copies and any derived work.158159This software is provided 'as-is', without any express or implied160warranty. In no event shall the authors be held liable for any damages161arising from the use of this software or it's derivatives.162163Snes9x is freeware for PERSONAL USE only. Commercial users should164seek permission of the copyright holders first. Commercial use includes,165but is not limited to, charging money for Snes9x or software derived from166Snes9x, including Snes9x or derivatives in commercial game bundles, and/or167using Snes9x as a promotion for your commercial product.168169The copyright holders request that bug fixes and improvements to the code170should be forwarded to them so everyone can benefit from the modifications171in future versions.172173Super NES and Super Nintendo Entertainment System are trademarks of174Nintendo Co., Limited and its subsidiary companies.175***********************************************************************************/176177178#include <stdio.h>179#include <time.h>180#include <string.h>181#include <string>182183#include "conffile.h"184185#ifdef __WIN32__186#define snprintf _snprintf // needs ANSI compliant name187#endif188189#define SORT_SECTIONS_BY_SIZE // output190191using namespace std;192193bool ConfigFile::defaultAutoAdd = false;194bool ConfigFile::niceAlignment = false;195bool ConfigFile::showComments = true;196bool ConfigFile::alphaSort = true;197bool ConfigFile::timeSort = false;198static ConfigFile* curConfigFile = NULL; // for section_then_key_less199200ConfigFile::ConfigFile(void) {201Clear();202}203204void ConfigFile::Clear(void){205data.clear();206sectionSizes.ClearSections();207linectr = 0;208}209210bool ConfigFile::LoadFile(const char *filename){211STREAM s;212bool ret=false;213const char *n, *n2;214215if((s=OPEN_STREAM(filename, "r"))){216n=filename;217n2=strrchr(n, '/'); if(n2!=NULL) n=n2+1;218n2=strrchr(n, '\\'); if(n2!=NULL) n=n2+1;219LoadFile(new fReader(s), n);220CLOSE_STREAM(s);221ret = true;222} else {223fprintf(stderr, "Couldn't open conffile ");224perror(filename);225}226return ret;227}228229230void ConfigFile::LoadFile(Reader *r, const char *name){231curConfigFile = this;232string l, key, val;233string section;234string comment;235int i, line, line2;236bool eof;237238line=line2=0;239section.clear();240do {241line=line2++;242l=r->getline(eof);243ConfigEntry::trim(l);244if(l.size()==0) continue;245246if(l[0]=='#' || l[0]==';'){247// comment248continue;249}250251if(l[0]=='['){252if(*l.rbegin()!=']'){253if(name) fprintf(stderr, "%s:", name);254fprintf(stderr, "[%d]: Ignoring invalid section header\n", line);255continue;256}257section.assign(l, 1, l.size()-2);258continue;259}260261while(*l.rbegin()=='\\'){262l.erase(l.size()-1);263line2++;264val=r->getline(eof);265if(eof){266fprintf(stderr, "Unexpected EOF reading config file");267if(name) fprintf(stderr, " '%s'", name);268fprintf(stderr, "\n");269return;270}271ConfigEntry::trim(val);272l+=val;273}274i=l.find('=');275if(i<0){276if(name) fprintf(stderr, "%s:", name);277fprintf(stderr, "[%d]: Ignoring invalid entry\n", line);278continue;279}280key=l.substr(0,i); ConfigEntry::trim(key);281val=l.substr(i+1); comment = ConfigEntry::trimCommented(val);282if(val.size() > 0 && val[0]=='"' && *val.rbegin()=='"') val=val.substr(1, val.size()-2);283284ConfigEntry e(line, section, key, val);285e.comment = comment;286if(data.erase(e))287sectionSizes.DecreaseSectionSize(e.section);288data.insert(e);289sectionSizes.IncreaseSectionSize(e.section);290} while(!eof);291curConfigFile = NULL;292}293294bool ConfigFile::SaveTo(const char *filename){295string section;296FILE *fp;297298if((fp=fopen(filename, "w"))==NULL){299fprintf(stderr, "Couldn't write conffile ");300perror(filename);301return false;302}303304curConfigFile = this;305section.clear();306set<ConfigEntry, ConfigEntry::line_less> tmp;307fprintf(fp, "# Config file output by snes9x\n");308time_t t=time(NULL);309fprintf(fp, "# %s", ctime(&t));310311#ifdef SORT_SECTIONS_BY_SIZE312std::set<ConfigEntry, ConfigEntry::section_then_key_less> data2;313for(set<ConfigEntry, ConfigEntry::key_less>::iterator k=data.begin(); k!=data.end(); k++){314ConfigEntry e (k->line, k->section, k->key, k->val); e.comment = k->comment;315data2.insert(e);316}317#else318#define data2 data319#define section_then_key_less key_less320#endif321322for(set<ConfigEntry, ConfigEntry::section_then_key_less>::iterator j=data2.begin(); ; j++){323if(j==data2.end() || j->section!=section){324if(!tmp.empty()){325fprintf(fp, "\n[%s]\n", section.c_str());326unsigned int maxKeyLen=0, maxValLen=0; int maxLeftDiv=0; int maxRightDiv=-1;327if(niceAlignment){328for(set<ConfigEntry, ConfigEntry::line_less>::iterator i=tmp.begin(); i!=tmp.end(); i++){329int len3 = i->key.find_last_of(':');330maxRightDiv = MAX(maxRightDiv, len3);331len3 = i->key.length() - len3;332maxLeftDiv = MAX(maxLeftDiv, len3);333maxKeyLen = MAX(maxKeyLen, i->key.length()+3);334if(showComments){335string o=i->val; ConfigEntry::trim(o);336unsigned int len = o.length();337for(signed int j=len-1;j>=0;j--) if(o.at(j)=='#') len++;338if(o!=i->val) len+=2;339maxValLen = MAX(maxValLen, len);340}341}342if(maxValLen>48) maxValLen=48; // limit spacing343}344345for(set<ConfigEntry, ConfigEntry::line_less>::iterator i=tmp.begin(); i!=tmp.end(); i++){346string o=i->val; ConfigEntry::trim(o);347if(o!=i->val) o="\""+i->val+"\"";348int off=0, len3=0;349for(;;){350int k=o.find('#',off);351if(k>=0){352o.insert(k,1,'#'); // re-double any comment characters353off=k+2;354if(off<(int)o.length()) continue;355}356break;357}358if(niceAlignment){359len3=i->key.find_last_of(':');360int len3sub=0;361if(len3 < maxRightDiv){362for(int j=len3;j<maxRightDiv;j++) fputc(' ',fp);363len3sub=maxRightDiv-len3;364len3 = maxRightDiv;365}366len3+=maxLeftDiv-i->key.length();367for(unsigned int j=i->key.length()+len3+3;j<maxKeyLen;j++) fputc(' ',fp);368fprintf(fp, "%s", i->key.c_str());369for(int j=0;j<len3-len3sub;j++) fputc(' ',fp);370fprintf(fp, " = %s", o.c_str());371} else372fprintf(fp, "%s = %s", i->key.c_str(), o.c_str());373374if(showComments && !i->comment.empty()){375if(niceAlignment) for(unsigned int j=o.length();j<maxValLen;j++) fputc(' ',fp);376fprintf(fp, " # %s", i->comment.c_str());377}378fprintf(fp, "\n");379}380}381if(j==data2.end()) break;382section=j->section;383tmp.clear();384}385tmp.insert(*j);386}387curConfigFile = NULL;388389#undef data2390#undef section_then_key_less391392if(ferror(fp))393{394fp = fp;395}396397fclose(fp);398return true;399}400401402/***********************************************/403404string ConfigFile::Get(const char *key){405set<ConfigEntry, ConfigEntry::key_less>::iterator i;406i=data.find(ConfigEntry(key));407i->used=true;408return i->val;409}410bool ConfigFile::Has(const char *key){411return data.find(ConfigEntry(key))!=data.end();412}413414// exists and isn't completely empty (any side-effects are intentional)415bool ConfigFile::Exists(const char *key){416const char* val = GetString(key, NULL);417return val && *val;418}419420421string ConfigFile::GetString(const char *key, string def){422if(!Exists(key))423return def;424return Get(key);425}426427char *ConfigFile::GetString(const char *key, char *out, uint32 outlen){428if(!Exists(key)) return NULL;429ZeroMemory(out, outlen);430string o=Get(key);431if(outlen>0){432outlen--;433if(o.size()<outlen) outlen=o.size();434memcpy(out, o.data(), outlen);435}436return out;437}438439const char *ConfigFile::GetString(const char *key, const char *def){440set<ConfigEntry, ConfigEntry::key_less>::iterator i;441i=data.find(ConfigEntry(key));442if(i==data.end())443{444if(defaultAutoAdd) SetString(key,""); //SetString(key, def?def:"");445return def;446}447i->used=true;448// This should be OK, until this key gets removed449const std::string &iVal = i->val;450return iVal.c_str();451}452453char *ConfigFile::GetStringDup(const char *key, const char *def){454const char *c=GetString(key, def);455if(c==NULL) return NULL;456return strdup(c);457}458459bool ConfigFile::SetString(const char *key, string val, const char *comment){460set<ConfigEntry, ConfigEntry::key_less>::iterator i;461bool ret=false;462bool found;463464ConfigEntry e(key, val);465if(comment && *comment) e.comment = comment;466e.used=true;467468i=data.find(e);469found=(i==data.end());470if(!found){471e.line=i->line;472data.erase(e);473sectionSizes.DecreaseSectionSize(e.section);474ret=true;475}476if((found && (!alphaSort || timeSort)) || (!alphaSort && timeSort))477e.line = linectr++;478479data.insert(e);480sectionSizes.IncreaseSectionSize(e.section);481return ret;482}483484int32 ConfigFile::GetInt(const char *key, int32 def, bool *bad){485if(bad) *bad=false;486if(!Exists(key))487return def;488char *c;489int32 i;490string o=Get(key);491i=strtol(o.c_str(), &c, 10);492if(c!=NULL && *c!='\0'){493i=def;494if(bad) *bad=true;495}496return i;497}498499bool ConfigFile::SetInt(const char *key, int32 val, const char *comment){500char buf[20];501snprintf(buf, sizeof(buf), "%d", (int)val);502return SetString(key, buf, comment);503}504505uint32 ConfigFile::GetUInt(const char *key, uint32 def, int base, bool *bad){506if(bad) *bad=false;507if(!Exists(key))508return def;509if(base!=8 && base!=10 && base!=16) base=0;510char *c;511uint32 i;512string o=Get(key);513i=strtol(o.c_str(), &c, base);514if(c!=NULL && *c!='\0'){515i=def;516if(bad) *bad=true;517}518return i;519}520521bool ConfigFile::SetUInt(const char *key, uint32 val, int base, const char *comment){522char buf[20];523switch(base){524case 10:525default:526snprintf(buf, sizeof(buf), "%u", (unsigned int)val);527break;528case 8:529snprintf(buf, sizeof(buf), "%#o", (unsigned int)val);530break;531case 16:532snprintf(buf, sizeof(buf), "%#x", (unsigned int)val);533break;534}535return SetString(key, buf, comment);536}537538bool ConfigFile::GetBool(const char *key, bool def, bool *bad){539if(bad) *bad=false;540if(!Exists(key))541return def;542string o=Get(key);543const char *c=o.c_str();544if(!strcasecmp(c, "true") || !strcasecmp(c, "1") || !strcasecmp(c, "yes") || !strcasecmp(c, "on")) return true;545if(!strcasecmp(c, "false") || !strcasecmp(c, "0") || !strcasecmp(c, "no") || !strcasecmp(c, "off")) return false;546if(bad) *bad=true;547return def;548}549550bool ConfigFile::SetBool(const char *key, bool val, const char *true_val, const char *false_val, const char *comment){551return SetString(key, val?true_val:false_val, comment);552}553554const char* ConfigFile::GetComment(const char *key)555{556set<ConfigEntry, ConfigEntry::key_less>::iterator i;557i=data.find(ConfigEntry(key));558if(i==data.end())559return NULL;560561// This should be OK, until this key gets removed562const std::string &iCom = i->comment;563return iCom.c_str();564}565566bool ConfigFile::DeleteKey(const char *key){567ConfigEntry e = ConfigEntry(key);568if(data.erase(e)) {569sectionSizes.DecreaseSectionSize(e.section);570return true;571}572return false;573}574575/***********************************************/576577bool ConfigFile::DeleteSection(const char *section){578set<ConfigEntry, ConfigEntry::key_less>::iterator s, e;579580for(s=data.begin(); s!=data.end() && s->section!=section; s++) ;581if(s==data.end()) return false;582for(e=s; e!=data.end() && e->section==section; e++) ;583data.erase(s, e);584sectionSizes.DeleteSection(section);585return true;586}587588ConfigFile::secvec_t ConfigFile::GetSection(const char *section){589secvec_t v;590set<ConfigEntry, ConfigEntry::key_less>::iterator i;591592v.clear();593for(i=data.begin(); i!=data.end(); i++){594if(i->section!=section) continue;595v.push_back(std::pair<string,string>(i->key, i->val));596}597return v;598}599600int ConfigFile::GetSectionSize(const std::string section){601return sectionSizes.GetSectionSize(section);602}603604// Clears all key-value pairs that didn't receive a "Get" or "Exists" command605void ConfigFile::ClearUnused()606{607set<ConfigEntry, ConfigEntry::key_less>::iterator i;608again:609for(i=data.begin(); i!=data.end(); i++){610if(!i->used){611data.erase(i);612goto again;613}614}615}616617void ConfigFile::ClearLines()618{619set<ConfigEntry, ConfigEntry::key_less>::iterator i;620for(i=data.begin(); i!=data.end(); i++){621*(const_cast<int*>(&i->line)) = -1;622}623}624625bool ConfigFile::ConfigEntry::section_then_key_less::operator()(const ConfigEntry &a, const ConfigEntry &b) {626if(curConfigFile && a.section!=b.section){627const int sva = curConfigFile->GetSectionSize(a.section);628const int svb = curConfigFile->GetSectionSize(b.section);629if(sva<svb) return true;630if(sva>svb) return false;631return a.section<b.section;632}633return a.key<b.key;634}635636637void ConfigFile::SetDefaultAutoAdd(bool autoAdd)638{639defaultAutoAdd = autoAdd;640}641void ConfigFile::SetNiceAlignment(bool align)642{643niceAlignment = align;644}645void ConfigFile::SetShowComments(bool show)646{647showComments = show;648}649void ConfigFile::SetAlphaSort(bool sort)650{651alphaSort = sort;652}653void ConfigFile::SetTimeSort(bool sort)654{655timeSort = sort;656}657658659