dosbox-wii/src/misc/setup.cpp

520 lines
13 KiB
C++
Raw Normal View History

2009-05-02 23:03:37 +02:00
/*
2009-05-03 00:28:34 +02:00
* Copyright (C) 2002-2007 The DOSBox Team
2009-05-02 23:03:37 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-05-03 00:02:15 +02:00
* GNU General Public License for more details.
2009-05-02 23:03:37 +02:00
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
2009-05-03 00:28:34 +02:00
/* $Id: setup.cpp,v 1.38 2007/01/08 19:45:41 qbix79 Exp $ */
2009-05-02 23:53:27 +02:00
2009-05-02 23:03:37 +02:00
#include "dosbox.h"
#include "cross.h"
#include "setup.h"
2009-05-02 23:20:05 +02:00
#include <fstream>
#include <string>
#include <list>
#include <stdlib.h>
#include <stdio.h>
#include "support.h"
2009-05-02 23:03:37 +02:00
2009-05-02 23:20:05 +02:00
using namespace std;
2009-05-02 23:03:37 +02:00
2009-05-02 23:53:27 +02:00
void Prop_float::SetValue(char* input){
input=trim(input);
2009-05-03 00:18:08 +02:00
value._float= static_cast<float>(atof(input));
2009-05-02 23:53:27 +02:00
}
2009-05-02 23:03:37 +02:00
2009-05-02 23:20:05 +02:00
void Prop_int::SetValue(char* input){
input=trim(input);
2009-05-03 00:18:08 +02:00
value._int= atoi(input);
2009-05-02 23:20:05 +02:00
}
2009-05-02 23:03:37 +02:00
2009-05-02 23:20:05 +02:00
void Prop_string::SetValue(char* input){
input=trim(input);
2009-05-03 00:18:08 +02:00
value._string->assign(input);
2009-05-02 23:20:05 +02:00
}
void Prop_bool::SetValue(char* input){
2009-05-03 00:02:15 +02:00
input=lowcase(trim(input));
/* valid false entries: 0 ,d*, of* ,f* everything else gets true */
if((input[0]=='0') || (input[0]=='d') || ( (input[0]=='o') && (input[1]=='f')) || (input[0]=='f')){
2009-05-03 00:18:08 +02:00
value._bool=false;
2009-05-02 23:20:05 +02:00
}else{
2009-05-03 00:18:08 +02:00
value._bool=true;
2009-05-02 23:20:05 +02:00
}
}
void Prop_hex::SetValue(char* input){
input=trim(input);
2009-05-03 00:18:08 +02:00
if(!sscanf(input,"%X",&(value._hex))) value._hex=0;
2009-05-02 23:20:05 +02:00
}
2009-05-02 23:03:37 +02:00
2009-05-03 00:28:34 +02:00
void Prop_int::GetValuestring(char* str) const{
2009-05-03 00:18:08 +02:00
sprintf(str,"%d",value._int);
2009-05-02 23:20:05 +02:00
}
2009-05-02 23:03:37 +02:00
2009-05-03 00:28:34 +02:00
void Prop_string::GetValuestring(char* str) const{
2009-05-03 00:18:08 +02:00
sprintf(str,"%s",value._string->c_str());
2009-05-02 23:20:05 +02:00
}
2009-05-03 00:28:34 +02:00
void Prop_bool::GetValuestring(char* str) const{
2009-05-03 00:18:08 +02:00
sprintf(str,"%s",value._bool?"true":"false");
2009-05-02 23:20:05 +02:00
}
2009-05-03 00:28:34 +02:00
void Prop_float::GetValuestring(char* str) const {
2009-05-03 00:18:08 +02:00
sprintf(str,"%1.2f",value._float);
2009-05-02 23:53:27 +02:00
}
2009-05-03 00:28:34 +02:00
void Prop_hex::GetValuestring(char* str) const {
2009-05-03 00:18:08 +02:00
sprintf(str,"%X",value._hex);
2009-05-02 23:20:05 +02:00
}
2009-05-03 00:28:34 +02:00
void Section_prop::Add_float(char const * const _propname, float _value) {
2009-05-02 23:53:27 +02:00
Property* test=new Prop_float(_propname,_value);
properties.push_back(test);
}
2009-05-02 23:20:05 +02:00
void Section_prop::Add_int(const char* _propname, int _value) {
Property* test=new Prop_int(_propname,_value);
properties.push_back(test);
}
2009-05-03 00:28:34 +02:00
void Section_prop::Add_string(char const * const _propname, char const * const _value) {
2009-05-02 23:20:05 +02:00
Property* test=new Prop_string(_propname,_value);
properties.push_back(test);
}
2009-05-03 00:28:34 +02:00
void Section_prop::Add_bool(char const * const _propname, bool _value) {
2009-05-02 23:20:05 +02:00
Property* test=new Prop_bool(_propname,_value);
properties.push_back(test);
}
void Section_prop::Add_hex(const char* _propname, int _value) {
Property* test=new Prop_hex(_propname,_value);
properties.push_back(test);
}
2009-05-03 00:28:34 +02:00
int Section_prop::Get_int(char const * const _propname) const {
for(const_it tel=properties.begin();tel!=properties.end();tel++){
2009-05-02 23:20:05 +02:00
if((*tel)->propname==_propname){
return ((*tel)->GetValue())._int;
}
}
return 0;
}
2009-05-03 00:28:34 +02:00
bool Section_prop::Get_bool(char const * const _propname) const {
for(const_it tel=properties.begin();tel!=properties.end();tel++){
2009-05-02 23:20:05 +02:00
if((*tel)->propname==_propname){
return ((*tel)->GetValue())._bool;
}
}
return false;
}
2009-05-03 00:28:34 +02:00
float Section_prop::Get_float(char const * const _propname) const {
for(const_it tel=properties.begin();tel!=properties.end();tel++){
2009-05-02 23:53:27 +02:00
if((*tel)->propname==_propname){
return ((*tel)->GetValue())._float;
}
}
return false;
}
2009-05-02 23:20:05 +02:00
2009-05-03 00:28:34 +02:00
const char* Section_prop::Get_string(char const * const _propname) const {
for(const_it tel=properties.begin();tel!=properties.end();tel++){
2009-05-02 23:20:05 +02:00
if((*tel)->propname==_propname){
return ((*tel)->GetValue())._string->c_str();
}
}
2009-05-02 23:35:44 +02:00
return "";
2009-05-02 23:20:05 +02:00
}
2009-05-03 00:28:34 +02:00
int Section_prop::Get_hex(char const * const _propname) const {
for(const_it tel=properties.begin();tel!=properties.end();tel++){
2009-05-02 23:20:05 +02:00
if((*tel)->propname==_propname){
return ((*tel)->GetValue())._hex;
}
}
return 0;
}
void Section_prop::HandleInputline(char *gegevens){
char * rest=strrchr(gegevens,'=');
2009-05-03 00:18:08 +02:00
if(!rest) return;
*rest = 0;
2009-05-02 23:20:05 +02:00
gegevens=trim(gegevens);
for(it tel=properties.begin();tel!=properties.end();tel++){
if(!strcasecmp((*tel)->propname.c_str(),gegevens)){
(*tel)->SetValue(rest+1);
return;
}
}
}
2009-05-03 00:28:34 +02:00
void Section_prop::PrintData(FILE* outfile) const {
2009-05-02 23:20:05 +02:00
char temp[1000]; /* Should be enough for the properties */
/* Now print out the individual section entries */
2009-05-03 00:28:34 +02:00
for(const_it tel=properties.begin();tel!=properties.end();tel++){
2009-05-02 23:20:05 +02:00
(*tel)->GetValuestring(temp);
fprintf(outfile,"%s=%s\n",(*tel)->propname.c_str(),temp);
}
}
2009-05-03 00:18:08 +02:00
static char buffer[1024];
2009-05-03 00:28:34 +02:00
char const * Section_prop::GetPropValue(char const * const _property) const{
for(const_it tel=properties.begin();tel!=properties.end();tel++){
2009-05-03 00:18:08 +02:00
if(!strcasecmp((*tel)->propname.c_str(),_property)){
(*tel)->GetValuestring(buffer);
return buffer;
}
}
return NULL;
}
2009-05-02 23:20:05 +02:00
void Section_line::HandleInputline(char* line){
data+=line;
data+="\n";
}
2009-05-03 00:28:34 +02:00
void Section_line::PrintData(FILE* outfile) const {
2009-05-02 23:20:05 +02:00
fprintf(outfile,"%s",data.c_str());
}
2009-05-03 00:28:34 +02:00
char const* Section_line::GetPropValue(char const * const /* _property*/) const {
2009-05-03 00:18:08 +02:00
return NULL;
}
2009-05-03 00:28:34 +02:00
void Config::PrintConfig(char const * const configfilename) const {
2009-05-02 23:43:00 +02:00
char temp[50];char helpline[256];
2009-05-02 23:27:47 +02:00
FILE* outfile=fopen(configfilename,"w+t");
2009-05-02 23:20:05 +02:00
if(outfile==NULL) return;
2009-05-03 00:28:34 +02:00
for (const_it tel=sectionlist.begin(); tel!=sectionlist.end(); tel++){
2009-05-02 23:20:05 +02:00
/* Print out the Section header */
2009-05-03 00:18:08 +02:00
strcpy(temp,(*tel)->GetName());
2009-05-02 23:20:05 +02:00
lowcase(temp);
fprintf(outfile,"[%s]\n",temp);
upcase(temp);
strcat(temp,"_CONFIGFILE_HELP");
2009-05-02 23:43:00 +02:00
const char * helpstr=MSG_Get(temp);
char * helpwrite=helpline;
while (*helpstr) {
*helpwrite++=*helpstr;
if (*helpstr == '\n') {
*helpwrite=0;
fprintf(outfile,"# %s",helpline);
helpwrite=helpline;
}
helpstr++;
}
fprintf(outfile,"\n");
2009-05-02 23:20:05 +02:00
(*tel)->PrintData(outfile);
2009-05-02 23:43:00 +02:00
fprintf(outfile,"\n"); /* Always an empty line between sections */
2009-05-02 23:20:05 +02:00
}
fclose(outfile);
}
2009-05-03 00:28:34 +02:00
Section_prop* Config::AddSection_prop(char const * const _name,void (*_initfunction)(Section*),bool canchange){
2009-05-02 23:20:05 +02:00
Section_prop* blah = new Section_prop(_name);
2009-05-03 00:18:08 +02:00
blah->AddInitFunction(_initfunction,canchange);
2009-05-02 23:20:05 +02:00
sectionlist.push_back(blah);
return blah;
}
2009-05-03 00:18:08 +02:00
Section_prop::~Section_prop() {
//ExecuteDestroy should be here else the destroy functions use destroyed properties
ExecuteDestroy(true);
/* Delete properties themself (properties stores the pointer of a prop */
for(it prop = properties.begin(); prop != properties.end(); prop++)
delete (*prop);
}
2009-05-03 00:28:34 +02:00
Section_line* Config::AddSection_line(char const * const _name,void (*_initfunction)(Section*)){
2009-05-02 23:20:05 +02:00
Section_line* blah = new Section_line(_name);
blah->AddInitFunction(_initfunction);
sectionlist.push_back(blah);
return blah;
}
2009-05-03 00:28:34 +02:00
void Config::Init() {
for (const_it tel=sectionlist.begin(); tel!=sectionlist.end(); tel++){
2009-05-02 23:20:05 +02:00
(*tel)->ExecuteInit();
}
}
2009-05-03 00:18:08 +02:00
void Section::ExecuteInit(bool initall) {
typedef std::list<Function_wrapper>::iterator func_it;
for (func_it tel=initfunctions.begin(); tel!=initfunctions.end(); tel++) {
if(initall || (*tel).canchange) (*tel).function(this);
}
}
void Section::ExecuteDestroy(bool destroyall) {
typedef std::list<Function_wrapper>::iterator func_it;
for (func_it tel=destroyfunctions.begin(); tel!=destroyfunctions.end(); ) {
if(destroyall || (*tel).canchange) {
(*tel).function(this);
tel=destroyfunctions.erase(tel); //Remove destroyfunction once used
} else tel++;
}
}
2009-05-02 23:20:05 +02:00
Config::~Config() {
reverse_it cnt=sectionlist.rbegin();
while (cnt!=sectionlist.rend()) {
delete (*cnt);
cnt++;
}
}
2009-05-03 00:28:34 +02:00
Section* Config::GetSection(char const * const _sectionname) const{
for (const_it tel=sectionlist.begin(); tel!=sectionlist.end(); tel++){
2009-05-03 00:18:08 +02:00
if (!strcasecmp((*tel)->GetName(),_sectionname)) return (*tel);
2009-05-02 23:20:05 +02:00
}
return NULL;
}
2009-05-03 00:28:34 +02:00
Section* Config::GetSectionFromProperty(char const * const prop) const{
for (const_it tel=sectionlist.begin(); tel!=sectionlist.end(); tel++){
2009-05-03 00:18:08 +02:00
if ((*tel)->GetPropValue(prop)) return (*tel);
}
return NULL;
}
2009-05-03 00:28:34 +02:00
bool Config::ParseConfigFile(char const * const configfilename){
static bool first_configfile = true;
2009-05-02 23:20:05 +02:00
ifstream in(configfilename);
2009-05-02 23:53:27 +02:00
if (!in) return false;
2009-05-03 00:28:34 +02:00
const char * settings_type = first_configfile?"primary":"additional";
first_configfile = false;
LOG_MSG("CONFIG:Loading %s settings from config file %s", settings_type,configfilename);
2009-05-02 23:53:27 +02:00
char gegevens[1024];
2009-05-02 23:35:44 +02:00
Section* currentsection = NULL;
Section* testsec = NULL;
2009-05-02 23:20:05 +02:00
while (in) {
2009-05-02 23:53:27 +02:00
in.getline(gegevens,1024);
2009-05-02 23:20:05 +02:00
char* temp;
2009-05-02 23:53:27 +02:00
char* s;
int len;
s = gegevens;
/* strip trailing whitespace */
for (len = strlen(s); len > 0 && isspace(s[len - 1]); len--) {
/* nothing */
}
s[len] = 0;
/* strip leading whitespace */
while (isspace(s[0])) {
s++;
}
switch(s[0]){
2009-05-02 23:20:05 +02:00
case '%':
case '\0':
2009-05-02 23:53:27 +02:00
case '#':
2009-05-02 23:20:05 +02:00
case ' ':
2009-05-02 23:53:27 +02:00
case '\n':
2009-05-02 23:20:05 +02:00
continue;
break;
case '[':
2009-05-02 23:53:27 +02:00
temp = strrchr(s,']');
2009-05-02 23:20:05 +02:00
*temp=0;
2009-05-02 23:53:27 +02:00
testsec = GetSection(&s[1]);
2009-05-02 23:35:44 +02:00
if(testsec != NULL ) currentsection = testsec;
testsec = NULL;
2009-05-02 23:20:05 +02:00
break;
default:
try{
2009-05-03 00:08:43 +02:00
if(currentsection) currentsection->HandleInputline(s);
2009-05-02 23:20:05 +02:00
}catch(const char* message){
message=0;
//EXIT with message
}
break;
}
}
2009-05-02 23:53:27 +02:00
return true;
2009-05-02 23:20:05 +02:00
}
2009-05-02 23:35:44 +02:00
void Config::ParseEnv(char ** envp) {
for(char** env=envp; *env;env++) {
char copy[1024];
2009-05-03 00:18:08 +02:00
safe_strncpy(copy,*env,1024);
2009-05-02 23:35:44 +02:00
if(strncasecmp(copy,"DOSBOX_",7))
continue;
char* sec_name = &copy[7];
2009-05-03 00:18:08 +02:00
if(!(*sec_name))
continue;
2009-05-02 23:35:44 +02:00
char* prop_name = strrchr(sec_name,'_');
2009-05-03 00:18:08 +02:00
if(!prop_name || !(*prop_name))
continue;
2009-05-02 23:35:44 +02:00
*prop_name++=0;
Section* sect = GetSection(sec_name);
2009-05-03 00:18:08 +02:00
if(!sect)
2009-05-02 23:35:44 +02:00
continue;
sect->HandleInputline(prop_name);
}
}
2009-05-02 23:20:05 +02:00
void Config::SetStartUp(void (*_function)(void)) {
_start_function=_function;
}
void Config::StartUp(void) {
(*_start_function)();
}
2009-05-03 00:28:34 +02:00
bool CommandLine::FindExist(char const * const name,bool remove) {
2009-05-02 23:20:05 +02:00
cmd_it it;
if (!(FindEntry(name,it,false))) return false;
if (remove) cmds.erase(it);
return true;
}
2009-05-03 00:28:34 +02:00
bool CommandLine::FindHex(char const * const name,int & value,bool remove) {
2009-05-02 23:20:05 +02:00
cmd_it it,it_next;
if (!(FindEntry(name,it,true))) return false;
it_next=it;it_next++;
sscanf((*it_next).c_str(),"%X",&value);
if (remove) cmds.erase(it,++it_next);
return true;
}
2009-05-03 00:28:34 +02:00
bool CommandLine::FindInt(char const * const name,int & value,bool remove) {
2009-05-02 23:20:05 +02:00
cmd_it it,it_next;
if (!(FindEntry(name,it,true))) return false;
it_next=it;it_next++;
value=atoi((*it_next).c_str());
if (remove) cmds.erase(it,++it_next);
return true;
}
2009-05-03 00:28:34 +02:00
bool CommandLine::FindString(char const * const name,std::string & value,bool remove) {
2009-05-02 23:20:05 +02:00
cmd_it it,it_next;
if (!(FindEntry(name,it,true))) return false;
it_next=it;it_next++;
value=*it_next;
if (remove) cmds.erase(it,++it_next);
return true;
}
2009-05-02 23:43:00 +02:00
bool CommandLine::FindCommand(unsigned int which,std::string & value) {
2009-05-02 23:20:05 +02:00
if (which<1) return false;
if (which>cmds.size()) return false;
cmd_it it=cmds.begin();
for (;which>1;which--) it++;
value=(*it);
return true;
}
2009-05-03 00:28:34 +02:00
bool CommandLine::FindEntry(char const * const name,cmd_it & it,bool neednext) {
2009-05-02 23:20:05 +02:00
for (it=cmds.begin();it!=cmds.end();it++) {
if (!strcasecmp((*it).c_str(),name)) {
cmd_it itnext=it;itnext++;
if (neednext && (itnext==cmds.end())) return false;
return true;
}
}
return false;
}
2009-05-03 00:28:34 +02:00
bool CommandLine::FindStringBegin(char const* const begin,std::string & value, bool remove) {
size_t len = strlen(begin);
for (cmd_it it=cmds.begin();it!=cmds.end();it++) {
if (strncmp(begin,(*it).c_str(),len)==0) {
value=((*it).c_str() + len);
2009-05-03 00:02:15 +02:00
if (remove) cmds.erase(it);
2009-05-02 23:20:05 +02:00
return true;
}
}
return false;
}
2009-05-03 00:28:34 +02:00
bool CommandLine::FindStringRemain(char const * const name,std::string & value) {
2009-05-02 23:27:47 +02:00
cmd_it it;value="";
if (!FindEntry(name,it)) return false;
it++;
for (;it!=cmds.end();it++) {
value+=" ";
value+=(*it);
}
return true;
}
2009-05-03 00:18:08 +02:00
bool CommandLine::GetStringRemain(std::string & value) {
if(!cmds.size()) return false;
cmd_it it=cmds.begin();value=(*it++);
for(;it != cmds.end();it++) {
value+=" ";
value+=(*it);
}
return true;
}
2009-05-02 23:27:47 +02:00
2009-05-02 23:43:00 +02:00
unsigned int CommandLine::GetCount(void) {
2009-05-02 23:20:05 +02:00
return cmds.size();
}
2009-05-03 00:28:34 +02:00
CommandLine::CommandLine(int argc,char const * const argv[]) {
2009-05-02 23:20:05 +02:00
if (argc>0) {
file_name=argv[0];
}
int i=1;
while (i<argc) {
cmds.push_back(argv[i]);
i++;
}
}
2009-05-03 00:28:34 +02:00
CommandLine::CommandLine(char const * const name,char const * const cmdline) {
2009-05-02 23:20:05 +02:00
if (name) file_name=name;
/* Parse the cmds and put them in the list */
bool inword,inquote;char c;
inword=false;inquote=false;
std::string str;
2009-05-03 00:28:34 +02:00
const char * c_cmdline=cmdline;
while ((c=*c_cmdline)!=0) {
2009-05-02 23:20:05 +02:00
if (inquote) {
if (c!='"') str+=c;
else {
inquote=false;
cmds.push_back(str);
str.erase();
}
}else if (inword) {
if (c!=' ') str+=c;
else {
inword=false;
cmds.push_back(str);
str.erase();
}
}
else if (c=='"') { inquote=true;}
else if (c!=' ') { str+=c;inword=true;}
2009-05-03 00:28:34 +02:00
c_cmdline++;
2009-05-02 23:20:05 +02:00
}
if (inword || inquote) cmds.push_back(str);
}
2009-05-03 00:28:34 +02:00
void CommandLine::Shift(unsigned int amount) {
while(amount--) {
file_name = cmds.size()?(*(cmds.begin())):"";
if(cmds.size()) cmds.erase(cmds.begin());
}
}