summaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c176
1 files changed, 176 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c
new file mode 100644
index 0000000..4035d22
--- /dev/null
+++ b/src/config.c
@@ -0,0 +1,176 @@
+/*
+
+ espec - Sinclair Spectrum emulator
+
+ Copyright (C) 2003 Ian Cowburn (ianc@noddybox.demon.co.uk)
+
+ 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
+ GNU General Public License for more details.
+
+ 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
+
+ -------------------------------------------------------------------------
+
+ Config file
+
+*/
+static const char ident[]="$Id$";
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "exit.h"
+#include "config.h"
+
+static const char ident_h[]=ESPEC_CONFIG_H;
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+
+/* ---------------------------------------- CONFIG
+*/
+static char rompath[FILENAME_MAX]="./zx81.rom";
+static char tapedir[FILENAME_MAX]=".";
+static int fullscreen=FALSE;
+static int memsize=16;
+static int frames=50;
+static int scale=1;
+static int trace=0;
+
+static const struct
+{
+ const char *name;
+ void *var;
+ int is_int;
+} config[]= {
+ {"rompath", rompath, FALSE},
+ {"tapedir", tapedir, FALSE},
+ {"fullscreen", &fullscreen, TRUE},
+ {"memsize", &memsize, TRUE},
+ {"frames", &frames, TRUE},
+ {"scale", &scale, TRUE},
+ {"trace", &trace, TRUE},
+ {NULL, NULL, FALSE}
+ };
+
+
+/* ---------------------------------------- PRIVATE FUNCTIONS
+*/
+static void Parse(FILE *fp)
+{
+ char buff[1024];
+
+ while(fgets(buff,sizeof buff,fp))
+ {
+ size_t l;
+
+ l=strlen(buff);
+
+ if (buff[l-1]=='\n')
+ buff[--l]=0;
+
+ if (l>0 && buff[0]!='#')
+ {
+ char *t1=NULL;
+ char *t2=NULL;
+
+ t1=strtok(buff,"\t");
+ t2=strtok(NULL,"\t");
+
+ if (t2)
+ {
+ int f;
+
+ for(f=0;config[f].name;f++)
+ {
+ if (strcmp(config[f].name,t1)==0)
+ {
+ if (config[f].is_int)
+ {
+ int *i;
+
+ i=config[f].var;
+ *i=atoi(t2);
+ }
+ else
+ {
+ char *p;
+
+ p=config[f].var;
+ strcpy(p,t2);
+ }
+ }
+ }
+ }
+ else
+ {
+ fprintf(stderr,"Ignored bad config: %s %s\n",t1,t2 ? t2:"");
+ }
+ }
+ }
+}
+
+
+/* ---------------------------------------- EXPORTED INTERFACES
+*/
+void ConfigRead(void)
+{
+ FILE *fp;
+ char path[FILENAME_MAX]={0};
+
+ if (getenv("HOME"))
+ strcpy(path,getenv("HOME"));
+
+ strcat(path,"/.ezx81");
+
+ if ((fp=fopen(path,"r")))
+ {
+ Parse(fp);
+ fclose(fp);
+ }
+}
+
+
+int IConfig(IConfigVar v)
+{
+ static const int *vars[]=
+ {
+ &fullscreen,
+ &memsize,
+ &frames,
+ &scale,
+ &trace
+ };
+
+ return *vars[v];
+}
+
+
+const char *SConfig(SConfigVar v)
+{
+ static const char *vars[]=
+ {
+ rompath,
+ tapedir
+ };
+
+ return vars[v];
+}
+
+
+/* END OF FILE */