📄 config.c
字号:
/******************************************************************************* Snes9x - Portable Super Nintendo Entertainment System (TM) emulator. (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com) and Jerremy Koot (jkoot@snes9x.com) (c) Copyright 2001 - 2004 John Weidman (jweidman@slip.net) (c) Copyright 2002 - 2004 Brad Jorsch (anomie@users.sourceforge.net), funkyass (funkyass@spam.shaw.ca), Joel Yliluoma (http://iki.fi/bisqwit/) Kris Bleakley (codeviolation@hotmail.com), Matthew Kendora, Nach (n-a-c-h@users.sourceforge.net), Peter Bortas (peter@bortas.org) and zones (kasumitokoduck@yahoo.com) C4 x86 assembler and some C emulation code (c) Copyright 2000 - 2003 zsKnight (zsknight@zsnes.com), _Demo_ (_demo_@zsnes.com), and Nach C4 C++ code (c) Copyright 2003 Brad Jorsch DSP-1 emulator code (c) Copyright 1998 - 2004 Ivar (ivar@snes9x.com), _Demo_, Gary Henderson, John Weidman, neviksti (neviksti@hotmail.com), Kris Bleakley, Andreas Naive DSP-2 emulator code (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and Lord Nightmare (lord_nightmare@users.sourceforge.net OBC1 emulator code (c) Copyright 2001 - 2004 zsKnight, pagefault (pagefault@zsnes.com) and Kris Bleakley Ported from x86 assembler to C by sanmaiwashi SPC7110 and RTC C++ emulator code (c) Copyright 2002 Matthew Kendora with research by zsKnight, John Weidman, and Dark Force S-DD1 C emulator code (c) Copyright 2003 Brad Jorsch with research by Andreas Naive and John Weidman S-RTC C emulator code (c) Copyright 2001 John Weidman ST010 C++ emulator code (c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora Super FX x86 assembler emulator code (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault Super FX C emulator code (c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman SH assembler code partly based on x86 assembler code (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se) Specific ports contains the works of other authors. See headers in individual files. Snes9x homepage: http://www.snes9x.com Permission to use, copy, modify and distribute Snes9x in both binary and source form, for non-commercial purposes, is hereby granted without fee, providing that this license information and copyright notice appear with all copies and any derived work. This software is provided 'as-is', without any express or implied warranty. In no event shall the authors be held liable for any damages arising from the use of this software. Snes9x is freeware for PERSONAL USE only. Commercial users should seek permission of the copyright holders first. Commercial use includes charging money for Snes9x or software derived from Snes9x. The copyright holders request that bug fixes and improvements to the code should be forwarded to them so everyone can benefit from the modifications in future versions. Super NES and Super Nintendo Entertainment System are trademarks of Nintendo Co., Limited and its subsidiary companies.*******************************************************************************//* ______ ___ ___ * /\ _ \ /\_ \ /\_ \ * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ * /\____/ * \_/__/ * By Shawn Hargreaves, * 1 Salisbury Road, * Market Drayton, * Shropshire, * England, TF9 1AJ. * * Configuration routines. * * See readme.txt for copyright information. */#include <stdlib.h>#include <stdio.h>#include <strings.h>#include <ctype.h>#include <dirent.h>#include <unistd.h>#include "snes9x.h"#ifndef MIN#define MIN(a,b) ((a) < (b) ? (a) : (b))#endiftypedef struct CONFIG_ENTRY{ char *name; /* variable name (NULL if comment) */ char *data; /* variable value */ struct CONFIG_ENTRY *next; /* linked list */} CONFIG_ENTRY;typedef struct CONFIG{ CONFIG_ENTRY *head; /* linked list of config entries */ char *filename; /* where were we loaded from? */ int dirty; /* has our data changed? */} CONFIG;#define MAX_CONFIGS 4static CONFIG *config[MAX_CONFIGS] = { NULL, NULL, NULL, NULL };static CONFIG *config_override = NULL;/* static int config_installed = FALSE; */ /* Dead code */char *get_filename(char *path){ int pos; for (pos=0; path[pos]; pos++) ; /* do nothing */ while ((pos>0) && (path[pos-1] != '\\') && (path[pos-1] != '/')) pos--; return path+pos;}long file_size (char *filename){ FILE *fs = fopen (filename, "r"); if (fs) { long len; fseek (fs, 0, SEEK_END); len = ftell (fs); fclose (fs); return (len); } return (0);}/* destroy_config: * Destroys a config structure, writing it out to disk if the contents * have changed. */static void destroy_config(CONFIG *cfg){ CONFIG_ENTRY *pos, *prev; if (cfg) { if (cfg->filename) { if (cfg->dirty) { /* write changed data to disk */ FILE *f = fopen(cfg->filename, "w"); if (f) { pos = cfg->head; while (pos) { if (pos->name) { fputs(pos->name, f); if (pos->name[0] != '[') fputs(" = ", f); } if (pos->data) fputs(pos->data, f); fputs("\n", f); pos = pos->next; } fclose(f); } } free(cfg->filename); } /* destroy the variable list */ pos = cfg->head; while (pos) { prev = pos; pos = pos->next; if (prev->name) free(prev->name); if (prev->data) free(prev->data); free(prev); } free(cfg); }}#if 0 /* Dead code *//* config_cleanup: * Called at shutdown time to free memory being used by the config routines, * and write any changed data out to disk. */static void config_cleanup(){ int i; for (i=0; i<MAX_CONFIGS; i++) { if (config[i]) { destroy_config(config[i]); config[i] = NULL; } } if (config_override) { destroy_config(config_override); config_override = NULL; }/* _remove_exit_func(config_cleanup);*/ config_installed = FALSE;}#endif /* Dead code *//* init_config: * Sets up the configuration routines ready for use, also loading the * default config file if the loaddata flag is set and no other config * file is in memory. */static void init_config(int loaddata){#if 0 char buf[4][1025]; char *s; int i; if (!config_installed) {/* _add_exit_func(config_cleanup);*/ config_installed = TRUE; } if ((loaddata) && (!config[0])) { /* look for allegro.cfg in the same directory as the program */ strcpy(buf[0], __crt0_argv[0]); strlwr(buf[0]); *get_filename(buf[0]) = 0; put_backslash(buf[0]); strcat(buf[0], "allegro.cfg"); /* if that fails, try sound.cfg */ strcpy(buf[1], __crt0_argv[0]); strlwr(buf[1]); *get_filename(buf[1]) = 0; put_backslash(buf[1]); strcat(buf[1], "sound.cfg"); /* no luck? try the ALLEGRO enviroment variable... */ s = getenv("ALLEGRO"); if (s) { strcpy(buf[2], s); strlwr(buf[2]); put_backslash(buf[2]); strcat(buf[2], "allegro.cfg"); strcpy(buf[3], s); strlwr(buf[3]); put_backslash(buf[3]); strcat(buf[3], "sound.cfg"); } else { strcpy(buf[2], buf[0]); strcpy(buf[3], buf[1]); } /* see which of these files actually exist */ for (i=0; i<4; i++) { if (access (buf[i], R_OK)) { set_config_file(buf[i]); break; } } if (i >= 4) set_config_file(buf[0]); }#endif }/* get_line: * Helper for splitting files up into individual lines. */static int get_line(char *data, int length, char *name, char *val){ char buf[256], buf2[256]; int pos, i, j; for (pos=0; (pos<length) && (pos<255); pos++) { if ((data[pos] == '\r') || (data[pos] == '\n')) { buf[pos] = 0; if ((pos < length-1) && (((data[pos] == '\r') && (data[pos+1] == '\n')) || ((data[pos] == '\n') && (data[pos+1] == '\r')))) { pos++; } pos++; break; } buf[pos] = data[pos]; } buf[MIN(pos,255)] = 0; /* skip leading spaces */ i = 0; while ((buf[i]) && (isspace((int)buf[i]))) i++; /* read name string */ j = 0; while ((buf[i]) && (!isspace((int)buf[i])) && (buf[i] != '=') && (buf[i] != '#')) buf2[j++] = buf[i++]; if (j) { /* got a variable */ buf2[j] = 0; strcpy(name, buf2); while ((buf[i]) && ((isspace((int)buf[i])) || (buf[i] == '='))) i++; strcpy(val, buf+i); /* strip trailing spaces */ i = strlen(val) - 1; while ((i >= 0) && (isspace((int)val[i]))) val[i--] = 0; } else { /* blank line or comment */ name[0] = 0; strcpy(val, buf); } return pos;}/* set_config: * Does the work of setting up a config structure. */static void set_config(CONFIG **config, char *data, int length, char *filename){ char name[256]; char val[256]; CONFIG_ENTRY **prev, *p; int pos; init_config(FALSE); if (*config) { destroy_config(*config); *config = NULL; } *config = (CONFIG *) malloc(sizeof(CONFIG)); (*config)->head = NULL; (*config)->dirty = FALSE; if (filename) { (*config)->filename = (char *) malloc(strlen(filename)+1); strcpy((*config)->filename, filename); } else (*config)->filename = NULL; prev = &(*config)->head; pos = 0; while (pos < length) { pos += get_line(data+pos, length-pos, name, val); p = (CONFIG_ENTRY *) malloc(sizeof(CONFIG_ENTRY)); if (name[0]) { p->name = (char *) malloc(strlen(name)+1); strcpy(p->name, name); } else p->name = NULL; p->data = (char *) malloc(strlen(val)+1); strcpy(p->data, val); p->next = NULL; *prev = p; prev = &p->next; }}/* load_config_file:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -