Path: blob/main/misc/emulator/xnes/snes9x/conffile.h
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#ifndef _CONFIG_H_179#define _CONFIG_H_180181#include <set>182#include <map>183#include <vector>184#include <string>185186#ifdef UNZIP_SUPPORT187#include "unzip/unzip.h"188#endif189#include "snes9x.h"190#include "reader.h"191192#ifndef MAX193# define MAX(a,b) ((a) > (b)? (a) : (b))194# define MIN(a,b) ((a) < (b)? (a) : (b))195#endif196197class ConfigFile {198public:199ConfigFile(void);200201void Clear(void);202203// return false on failure204bool LoadFile(const char *filename);205void LoadFile(Reader *r, const char *name=NULL);206207// return false if key does not exist or is empty208bool Exists(const char *key);209210// return the value / default211std::string GetString(const char *key, std::string def);212char *GetString(const char *key, char *out, uint32 outlen); // return NULL if it doesn't exist, out not affected213const char *GetString(const char *key, const char *def=NULL); // NOTE: returned pointer becomes invalid when key is deleted/modified, or the ConfigFile is Clear()ed or deleted.214char *GetStringDup(const char *key, const char *def=NULL); // Much like "strdup(GetString(key, def))"215int32 GetInt(const char *key, int32 def=-1, bool *bad=NULL);216uint32 GetUInt(const char *key, uint32 def=0, int base=0, bool *bad=NULL); // base = 0, 8, 10, or 16217bool GetBool(const char *key, bool def=false, bool *bad=NULL);218const char* GetComment(const char *key); // NOTE: returned pointer becomes invalid when key is deleted/modified, or the ConfigFile is Clear()ed or deleted.219220// return true if the key existed prior to setting221bool SetString(const char *key, std::string val, const char *comment="");222bool SetInt(const char *key, int32 val, const char *comment="");223bool SetUInt(const char *key, uint32 val, int base=10, const char *comment=""); // base = 8, 10, or 16224bool SetBool(const char *key, bool val, const char *true_val="TRUE", const char *false_val="FALSE", const char *comment="");225bool DeleteKey(const char *key);226227// Operation on entire sections228bool DeleteSection(const char *section);229typedef std::vector<std::pair<std::string,std::string> > secvec_t;230secvec_t GetSection(const char *section);231int GetSectionSize(const std::string section);232233// Clears all key-value pairs that didn't receive a Set command, or a Get command with autoAdd on234void ClearUnused(void);235236// Clears all stored line numbers237void ClearLines(void);238239bool SaveTo(const char *filename);240241static void SetDefaultAutoAdd(bool autoAdd);242static void SetNiceAlignment(bool align);243static void SetShowComments(bool show);244static void SetAlphaSort(bool sort);245static void SetTimeSort(bool sort);246247private:248std::string Get(const char *key);249bool Has(const char *key);250251class ConfigEntry {252protected:253int line;254std::string section;255std::string key;256std::string val;257std::string comment;258mutable bool used;259260struct section_then_key_less {261bool operator()(const ConfigEntry &a, const ConfigEntry &b);262};263264struct key_less {265bool operator()(const ConfigEntry &a, const ConfigEntry &b) const{266if(a.section!=b.section) return a.section<b.section;267return a.key<b.key;268}269};270271struct line_less {272bool operator()(const ConfigEntry &a, const ConfigEntry &b){273if(a.line==b.line) return (b.val.empty() && !a.val.empty()) || a.key<b.key;274if(b.line<0) return true;275if(a.line<0) return false;276return a.line<b.line;277}278};279280static void trim(std::string &s){281int i;282i=s.find_first_not_of(" \f\n\r\t\v");283if(i==-1){284s.clear();285return;286}287if(i>0) s.erase(0, i); // erase leading whitespace288i=s.find_last_not_of(" \f\n\r\t\v");289if(i!=-1) s.erase(i+1); // erase trailing whitespace290return;291}292293// trims comments and leading/trailing whitespace from s, and returns any trimmed comments294// make sure not to call this more than once on the same string295static std::string trimCommented(std::string &s){296std::string cmt;297int i;298i=s.find_first_not_of(" \f\n\r\t\v");299if(i==-1){300s.clear();301return cmt;302}303if(i>0) s.erase(0, i); // erase leading whitespace304int off=0;305for(;;){306i=s.find('#',off); // find trailing comment307if(i>=0)308{309if((int)s.length()>i+1 && s.at(i+1) == '#') {310s.erase(i,1); // ignore ## and change to #311off = i+1;312continue;313} else {314int j=s.find_first_not_of(" \f\n\r\t\v",i+1);315if(j!=-1) cmt = s.substr(j); // store316s.erase(i); // erase trailing comment317}318}319break;320}321i=s.find_last_not_of(" \f\n\r\t\v");322if(i!=-1) s.erase(i+1); // erase trailing whitespace323return cmt;324}325326public:327ConfigEntry(int l, const std::string &s, const std::string &k, const std::string &v) :328line(l), section(s), key(k), val(v) {329trim(section);330trim(key);331used=false;332}333334void parse_key(const std::string &k){335int i=k.find("::");336if(i==-1){337section="Uncategorized"; key=k;338} else {339section=k.substr(0,i); key=k.substr(i+2);340}341trim(section);342trim(key);343used=false;344}345346ConfigEntry(const std::string k){347parse_key(k);348}349350ConfigEntry(const std::string k, const std::string &v) : line(-1), val(v) {351parse_key(k);352}353354friend class ConfigFile;355friend struct key_less;356friend struct line_less;357};358class SectionSizes {359protected:360std::map<std::string,uint32> sections;361362public:363uint32 GetSectionSize(const std::string section) {364uint32 count=0;365uint32 seclen;366std::map<std::string,uint32>::iterator it;367for(it=sections.begin(); it!=sections.end(); it++) {368seclen = MIN(section.size(),it->first.size());369if(it->first==section || !section.compare(0,seclen,it->first,0,seclen)) count+=it->second;370}371return count;372}373374void IncreaseSectionSize(const std::string section) {375std::map<std::string,uint32>::iterator it=sections.find(section);376if(it!=sections.end())377it->second++;378else379sections.insert(std::pair<std::string,uint32>(section,1));380}381382void DecreaseSectionSize(const std::string section) {383std::map<std::string,uint32>::iterator it=sections.find(section);384if(it!=sections.end())385it->second--;386}387388void ClearSections() {389sections.clear();390}391392void DeleteSection(const std::string section) {393sections.erase(section);394}395396};397std::set<ConfigEntry, ConfigEntry::key_less> data;398SectionSizes sectionSizes;399int linectr;400static bool defaultAutoAdd;401static bool niceAlignment;402static bool showComments;403static bool alphaSort;404static bool timeSort;405};406407/* Config file format:408*409* Comments are any lines whose first non-whitespace character is ';' or '#'.410* Note that comments can also follow a value, on the same line.411* To intentionally have a '#' character in the value, use ##412*413* All parameters fall into sections. To name a section, the first414* non-whitespace character on the line will be '[', and the last will be ']'.415*416* Parameters are simple key=value pairs. Whitespace around the '=', and at the417* beginning or end of the line is ignored. Key names may not contain '=' nor418* begin with '[', however values can. If the last character of the value is419* '\', the next line (sans leading/trailing whitespace) is considered part of420* the value as well. Programmatically, the key "K" in section "S" is referred421* to as "S::K", much like C++ namespaces. For example:422* [Section1]423* # this is a comment424* foo = bar \425* baz\426* quux \427* ## this is not a comment! # this IS a comment428* means the value of "Section1::foo" is "bar bazquux # this is not a comment!"429*430* Parameters may be of several types:431* String - Bare characters. If the first and last characters are both '"',432* they are removed (so just double them if you really want quotes433* there)434* Int - A decimal number from -2147483648 to 2147483647435* UInt - A number in decimal, hex, or octal from 0 to 4294967295 (or436* 0xffffffff, or 037777777777)437* Bool - true/false, 0/1, on/off, yes/no438*439* Of course, the actual accepted values for a parameter may be further440* restricted ;)441*/442443444/* You must write this for your port */445void S9xParsePortConfig(ConfigFile &, int pass);446447/* This may or may not be useful to you */448const char *S9xParseDisplayConfig(ConfigFile &, int pass);449450#endif451452453