| 1 | /* Pcsx - Pc Psx Emulator |
| 2 | * Copyright (C) 1999-2002 Pcsx Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "Linux.h" |
| 26 | |
| 27 | /* TODO escaping/unescaping would be nice, as would maxchars */ |
| 28 | static void GetValue(char *src, char *name, char *outvar) { |
| 29 | char *tmp; |
| 30 | |
| 31 | *outvar = 0; |
| 32 | tmp = strstr(src, name); |
| 33 | if (tmp == NULL) return; |
| 34 | |
| 35 | tmp += strlen(name); |
| 36 | while ((*tmp == ' ') || (*tmp == '=')) tmp++; |
| 37 | |
| 38 | while (*tmp != '\n' && *tmp != 0) |
| 39 | *outvar++ = *tmp++; |
| 40 | |
| 41 | *outvar = 0; |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | static long GetValuel(char *src, char *name) { |
| 46 | char *tmp = strstr(src, name); |
| 47 | if (tmp != NULL) { |
| 48 | tmp += strlen(name); |
| 49 | while ((*tmp == ' ') || (*tmp == '=')) tmp++; |
| 50 | if (*tmp != '\n') return atol(tmp); |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static boolean GetValueb(char *src, char *name) { |
| 56 | char *tmp = strstr(src, name); |
| 57 | if (tmp != NULL) { |
| 58 | tmp += strlen(name); |
| 59 | while ((*tmp == ' ') || (*tmp == '=')) tmp++; |
| 60 | if (*tmp != '\n') return (atoi(tmp) != 0); |
| 61 | } |
| 62 | return FALSE; |
| 63 | } |
| 64 | |
| 65 | #define SetValue(name, var) \ |
| 66 | fprintf(f, "%s = %s\n", name, var); |
| 67 | |
| 68 | #define SetValuel(name, var) \ |
| 69 | fprintf(f, "%s = %x\n", name, var); |
| 70 | |
| 71 | #define SetValueb(name, var) \ |
| 72 | fprintf(f, "%s = %d\n", name, (var) ? 1 : 0); |
| 73 | |
| 74 | int LoadConfig(PcsxConfig *Conf) { |
| 75 | struct stat buf; |
| 76 | FILE *f; |
| 77 | int size; |
| 78 | char *data; |
| 79 | |
| 80 | /* TODO local var called cfgfile */ |
| 81 | |
| 82 | // Ryan says: use dotdir, dotdir is GOOD |
| 83 | // No giant homedir names |
| 84 | strncpy(cfgfile, getenv("HOME"), 200); |
| 85 | strcat(cfgfile, PCSX_DOT_DIR); |
| 86 | |
| 87 | // proceed to load the cfg file |
| 88 | // append its name |
| 89 | strcat(cfgfile, cfgfile_basename); |
| 90 | |
| 91 | // file is now ~/.pcsx/pcsx.cfg (or whatever cfgfile_basename is) |
| 92 | if (stat(cfgfile, &buf) == -1) { |
| 93 | // the config file doesn't exist! |
| 94 | /* TODO Error checking? */ |
| 95 | printf("Configuration file %s couldn't be found\n", cfgfile); |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | size = buf.st_size; |
| 100 | |
| 101 | /* TODO Error checking for the next two lines, and at least log failures */ |
| 102 | f = fopen(cfgfile, "r"); |
| 103 | if (f == NULL) return -1; |
| 104 | |
| 105 | data = (char *)malloc(size + 1); |
| 106 | if (data == NULL) return -1; |
| 107 | |
| 108 | fread(data, 1, buf.st_size, f); |
| 109 | fclose(f); |
| 110 | |
| 111 | data[size] = '\0'; |
| 112 | |
| 113 | GetValue(data, "Bios", Config.Bios); |
| 114 | GetValue(data, "Gpu", Config.Gpu); |
| 115 | GetValue(data, "Spu", Config.Spu); |
| 116 | GetValue(data, "Cdr", Config.Cdr); |
| 117 | GetValue(data, "Pad1", Config.Pad1); |
| 118 | GetValue(data, "Pad2", Config.Pad2); |
| 119 | GetValue(data, "Net", Config.Net); |
| 120 | GetValue(data, "Mcd1", Config.Mcd1); |
| 121 | GetValue(data, "Mcd2", Config.Mcd2); |
| 122 | GetValue(data, "BiosDir", Config.BiosDir); |
| 123 | GetValue(data, "PluginsDir", Config.PluginsDir); |
| 124 | |
| 125 | Config.Xa = GetValueb(data, "Xa"); |
| 126 | Config.Sio = GetValueb(data, "Sio"); |
| 127 | Config.Mdec = GetValueb(data, "Mdec"); |
| 128 | Config.PsxAuto = GetValueb(data, "PsxAuto"); |
| 129 | Config.Cdda = GetValueb(data, "Cdda"); |
| 130 | Config.Debug = GetValueb(data, "Dbg"); |
| 131 | Config.PsxOut = GetValueb(data, "PsxOut"); |
| 132 | Config.SpuIrq = GetValueb(data, "SpuIrq"); |
| 133 | Config.RCntFix = GetValueb(data, "RCntFix"); |
| 134 | Config.VSyncWA = GetValueb(data, "VSyncWA"); |
| 135 | |
| 136 | Config.Cpu = GetValuel(data, "Cpu"); |
| 137 | Config.PsxType = GetValuel(data, "PsxType"); |
| 138 | |
| 139 | free(data); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | void SaveConfig() { |
| 145 | FILE *f; |
| 146 | |
| 147 | /* TODO Error checking for the next two lines, and at least log |
| 148 | failures - suggest a file dialog to specify a new file or |
| 149 | create a new file */ |
| 150 | f = fopen(cfgfile, "w"); |
| 151 | if (f == NULL) return; |
| 152 | |
| 153 | SetValue("Bios", Config.Bios); |
| 154 | SetValue("Gpu", Config.Gpu); |
| 155 | SetValue("Spu", Config.Spu); |
| 156 | SetValue("Cdr", Config.Cdr); |
| 157 | SetValue("Net", Config.Net); |
| 158 | SetValue("Pad1", Config.Pad1); |
| 159 | SetValue("Pad2", Config.Pad2); |
| 160 | SetValue("Mcd1", Config.Mcd1); |
| 161 | SetValue("Mcd2", Config.Mcd2); |
| 162 | SetValue("BiosDir", Config.BiosDir); |
| 163 | SetValue("PluginsDir", Config.PluginsDir); |
| 164 | |
| 165 | SetValueb("Xa", Config.Xa); |
| 166 | SetValueb("Sio", Config.Sio); |
| 167 | SetValueb("Mdec", Config.Mdec); |
| 168 | SetValueb("PsxAuto", Config.PsxAuto); |
| 169 | SetValueb("Cdda", Config.Cdda); |
| 170 | SetValueb("Dbg", Config.Debug); |
| 171 | SetValueb("PsxOut", Config.PsxOut); |
| 172 | SetValueb("SpuIrq", Config.SpuIrq); |
| 173 | SetValueb("RCntFix", Config.RCntFix); |
| 174 | SetValueb("VSyncWA", Config.VSyncWA); |
| 175 | |
| 176 | SetValuel("Cpu", Config.Cpu); |
| 177 | SetValuel("PsxType", Config.PsxType); |
| 178 | |
| 179 | fclose(f); |
| 180 | } |