initial fce ultra 0.81 import
[fceu.git] / drivers / common / config.c
CommitLineData
c62d2810 1/* FCE Ultra - NES/Famicom Emulator
2 *
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/****************************************************************/
22/* FCE Ultra */
23/* */
24/* This file contains routines for reading/writing the */
25/* configuration file. */
26/* */
27/****************************************************************/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include "../../types.h"
34#include "config.h"
35
36static int FReadString(FILE *fp, char *str, int n)
37{
38 int x=0,z;
39 for(;;)
40 {
41 z=fgetc(fp);
42 str[x]=z;
43 x++;
44 if(z<=0) break;
45 if(x>=n) return 0;
46 }
47 if(z<0) return 0;
48 return 1;
49}
50
51static void GetValueR(FILE *fp, char *str, void *v, int c)
52{
53 char buf[256];
54 int s;
55
56 while(FReadString(fp,buf,256))
57 {
58 fread(&s,1,4,fp);
59 if(!strcmp(str, buf))
60 {
61 if(!c) // String, allocate some memory.
62 {
63 if(!(*(char **)v=malloc(s)))
64 goto gogl;
65 fread(*(char **)v,1,s,fp);
66 continue;
67 }
68 else if(s>c || s<c)
69 {
70 gogl:
71 fseek(fp,s,SEEK_CUR);
72 continue;
73 }
74 fread((uint8*)v,1,c,fp);
75 }
76 else
77 fseek(fp,s,SEEK_CUR);
78 }
79 fseek(fp,4,SEEK_SET);
80}
81
82static void SetValueR(FILE *fp, char *str, void *v, int c)
83{
84 fwrite(str,1,strlen(str)+1,fp);
85 fwrite((uint8*)&c,1,4,fp);
86 fwrite((uint8*)v,1,c,fp);
87}
88
89static void SaveParse(CFGSTRUCT *cfgst, FILE *fp)
90{
91 int x=0;
92
93 do
94 {
95 if(!cfgst[x].name) // Link to new config structure
96 {
97 SaveParse(cfgst[x].ptr,fp); // Recursion is sexy. I could
98 // save a little stack space if I made
99 // the file pointer a non-local
100 // variable...
101 x++;
102 continue;
103 }
104
105 if(cfgst[x].len) // Plain data
106 SetValueR(fp,cfgst[x].name,cfgst[x].ptr,cfgst[x].len);
107 else // String
108 if(*(char **)cfgst[x].ptr) // Only save it if there IS a string.
109 SetValueR(fp,cfgst[x].name,*(char **)cfgst[x].ptr,
110 strlen(*(char **)cfgst[x].ptr)+1);
111 x++;
112 } while(cfgst[x].ptr);
113}
114
115void SaveFCEUConfig(char *filename, CFGSTRUCT *cfgst)
116{
117 FILE *fp;
118
119 fp=fopen(filename,"wb");
120 if(fp==NULL) return;
121
122 SaveParse(cfgst,fp);
123
124 fclose(fp);
125}
126
127static void LoadParse(CFGSTRUCT *cfgst, FILE *fp)
128{
129 int x=0;
130 do
131 {
132 if(!cfgst[x].name) // Link to new config structure
133 {
134 LoadParse(cfgst[x].ptr,fp);
135 x++;
136 continue;
137 }
138 GetValueR(fp,cfgst[x].name,cfgst[x].ptr,cfgst[x].len);
139 x++;
140 } while(cfgst[x].ptr);
141}
142
143void LoadFCEUConfig(char *filename, CFGSTRUCT *cfgst)
144{
145 FILE *fp;
146
147 fp=fopen(filename,"rb");
148 if(fp==NULL) return;
149 LoadParse(cfgst,fp);
150 fclose(fp);
151}