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 /****************************************************************/
33 #include "../../types.h"
36 static int FReadString(FILE *fp, char *str, int n)
51 static void GetValueR(FILE *fp, char *str, void *v, int c)
56 while(FReadString(fp,buf,256))
61 if(!c) // String, allocate some memory.
63 if(!(*(char **)v=malloc(s)))
65 fread(*(char **)v,1,s,fp);
74 fread((uint8*)v,1,c,fp);
82 static void SetValueR(FILE *fp, char *str, void *v, int c)
84 fwrite(str,1,strlen(str)+1,fp);
85 fwrite((uint8*)&c,1,4,fp);
86 fwrite((uint8*)v,1,c,fp);
89 static void SaveParse(CFGSTRUCT *cfgst, FILE *fp)
95 if(!cfgst[x].name) // Link to new config structure
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
105 if(cfgst[x].len) // Plain data
106 SetValueR(fp,cfgst[x].name,cfgst[x].ptr,cfgst[x].len);
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);
112 } while(cfgst[x].ptr);
115 void SaveFCEUConfig(char *filename, CFGSTRUCT *cfgst)
119 fp=fopen(filename,"wb");
127 static void LoadParse(CFGSTRUCT *cfgst, FILE *fp)
132 if(!cfgst[x].name) // Link to new config structure
134 LoadParse(cfgst[x].ptr,fp);
138 GetValueR(fp,cfgst[x].name,cfgst[x].ptr,cfgst[x].len);
140 } while(cfgst[x].ptr);
143 void LoadFCEUConfig(char *filename, CFGSTRUCT *cfgst)
147 fp=fopen(filename,"rb");