1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
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.
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.
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
21 /****************************************************************/
24 /* This file contains routines for reading/writing the */
25 /* configuration file. */
27 /****************************************************************/
36 #include "../../types.h"
39 static int FReadString(FILE *fp, char *str, int n)
54 static void GetValueR(FILE *fp, char *str, void *v, int c)
59 while(FReadString(fp,buf,256))
64 if(!c) // String, allocate some memory.
66 if(!(*(char **)v=malloc(s)))
68 fread(*(char **)v,1,s,fp);
77 fread((uint8*)v,1,c,fp);
85 static void SetValueR(FILE *fp, char *str, void *v, int c)
87 fwrite(str,1,strlen(str)+1,fp);
88 fwrite((uint8*)&c,1,4,fp);
89 fwrite((uint8*)v,1,c,fp);
92 static void SaveParse(CFGSTRUCT *cfgst, FILE *fp)
98 if(!cfgst[x].name) // Link to new config structure
100 SaveParse(cfgst[x].ptr,fp); // Recursion is sexy. I could
101 // save a little stack space if I made
102 // the file pointer a non-local
108 if(cfgst[x].len) // Plain data
109 SetValueR(fp,cfgst[x].name,cfgst[x].ptr,cfgst[x].len);
111 if(*(char **)cfgst[x].ptr) // Only save it if there IS a string.
112 SetValueR(fp,cfgst[x].name,*(char **)cfgst[x].ptr,
113 strlen(*(char **)cfgst[x].ptr)+1);
115 } while(cfgst[x].ptr);
118 void SaveFCEUConfig(char *filename, CFGSTRUCT *cfgst)
122 fp=fopen(filename,"wb");
133 static void LoadParse(CFGSTRUCT *cfgst, FILE *fp)
138 if(!cfgst[x].name) // Link to new config structure
140 LoadParse(cfgst[x].ptr,fp);
144 GetValueR(fp,cfgst[x].name,cfgst[x].ptr,cfgst[x].len);
146 } while(cfgst[x].ptr);
149 void LoadFCEUConfig(char *filename, CFGSTRUCT *cfgst)
153 fp=fopen(filename,"rb");