| 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 | #ifdef GP2X |
| 33 | #include <unistd.h> |
| 34 | #endif |
| 35 | |
| 36 | #include "../../types.h" |
| 37 | #include "config.h" |
| 38 | |
| 39 | static int FReadString(FILE *fp, char *str, int n) |
| 40 | { |
| 41 | int x=0,z; |
| 42 | for(;;) |
| 43 | { |
| 44 | z=fgetc(fp); |
| 45 | str[x]=z; |
| 46 | x++; |
| 47 | if(z<=0) break; |
| 48 | if(x>=n) return 0; |
| 49 | } |
| 50 | if(z<0) return 0; |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | static void GetValueR(FILE *fp, char *str, void *v, int c) |
| 55 | { |
| 56 | char buf[256]; |
| 57 | int s; |
| 58 | |
| 59 | while(FReadString(fp,buf,256)) |
| 60 | { |
| 61 | fread(&s,1,4,fp); |
| 62 | if(!strcmp(str, buf)) |
| 63 | { |
| 64 | if(!c) // String, allocate some memory. |
| 65 | { |
| 66 | if(!(*(char **)v=malloc(s))) |
| 67 | goto gogl; |
| 68 | fread(*(char **)v,1,s,fp); |
| 69 | continue; |
| 70 | } |
| 71 | else if(s>c || s<c) |
| 72 | { |
| 73 | gogl: |
| 74 | fseek(fp,s,SEEK_CUR); |
| 75 | continue; |
| 76 | } |
| 77 | fread((uint8*)v,1,c,fp); |
| 78 | } |
| 79 | else |
| 80 | fseek(fp,s,SEEK_CUR); |
| 81 | } |
| 82 | fseek(fp,4,SEEK_SET); |
| 83 | } |
| 84 | |
| 85 | static void SetValueR(FILE *fp, char *str, void *v, int c) |
| 86 | { |
| 87 | fwrite(str,1,strlen(str)+1,fp); |
| 88 | fwrite((uint8*)&c,1,4,fp); |
| 89 | fwrite((uint8*)v,1,c,fp); |
| 90 | } |
| 91 | |
| 92 | static void SaveParse(CFGSTRUCT *cfgst, FILE *fp) |
| 93 | { |
| 94 | int x=0; |
| 95 | |
| 96 | do |
| 97 | { |
| 98 | if(!cfgst[x].name) // Link to new config structure |
| 99 | { |
| 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 |
| 103 | // variable... |
| 104 | x++; |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | if(cfgst[x].len) // Plain data |
| 109 | SetValueR(fp,cfgst[x].name,cfgst[x].ptr,cfgst[x].len); |
| 110 | else // String |
| 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); |
| 114 | x++; |
| 115 | } while(cfgst[x].ptr); |
| 116 | } |
| 117 | |
| 118 | int SaveFCEUConfig(char *filename, CFGSTRUCT *cfgst) |
| 119 | { |
| 120 | FILE *fp; |
| 121 | |
| 122 | fp=fopen(filename,"wb"); |
| 123 | if(fp==NULL) return -1; |
| 124 | |
| 125 | SaveParse(cfgst,fp); |
| 126 | |
| 127 | fclose(fp); |
| 128 | #ifdef GP2X |
| 129 | sync(); |
| 130 | #endif |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static void LoadParse(CFGSTRUCT *cfgst, FILE *fp) |
| 135 | { |
| 136 | int x=0; |
| 137 | do |
| 138 | { |
| 139 | if(!cfgst[x].name) // Link to new config structure |
| 140 | { |
| 141 | LoadParse(cfgst[x].ptr,fp); |
| 142 | x++; |
| 143 | continue; |
| 144 | } |
| 145 | GetValueR(fp,cfgst[x].name,cfgst[x].ptr,cfgst[x].len); |
| 146 | x++; |
| 147 | } while(cfgst[x].ptr); |
| 148 | } |
| 149 | |
| 150 | int LoadFCEUConfig(char *filename, CFGSTRUCT *cfgst) |
| 151 | { |
| 152 | FILE *fp; |
| 153 | |
| 154 | fp=fopen(filename,"rb"); |
| 155 | if(fp==NULL) return -1; |
| 156 | LoadParse(cfgst,fp); |
| 157 | fclose(fp); |
| 158 | return 0; |
| 159 | } |