summaryrefslogtreecommitdiff
path: root/src/config.c
blob: 4035d22c09830f4f2b5600385f029d3a877180c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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 */