Annotation of generator/gtkopts/gtkopts.c, revision 1.1.1.2

1.1       root        1: /* Generator is (c) James Ponder, 1997-2001 http://www.squish.net/generator/ */
                      2: 
                      3: /* gtk options loading/saving */
                      4: 
                      5: #define _GNU_SOURCE 1
                      6: #define _BSD_SOURCE 1
                      7: #define __EXTENSIONS__ 1
                      8: 
                      9: #include <stdio.h>
                     10: #include <string.h>
                     11: #include <errno.h>
                     12: #include <stdlib.h>
                     13: 
                     14: #include "generator.h"
                     15: #include "gtkopts.h"
                     16: 
                     17: t_conf *gtkopts_conf = NULL;
                     18: 
                     19: static const char *gtkopts_default(const char *key);
                     20: 
                     21: #define CONFLINELEN 1024
                     22: 
                     23: typedef struct {
                     24:   char *key;
                     25:   char *vals;
                     26:   char *def;
                     27:   char *desc;
                     28: } t_opts;
                     29: 
                     30: /* *INDENT-OFF* */
                     31: 
                     32: static t_opts gtkopts_opts[] = {
                     33:   { "view", "100, 200", "100",
                     34:     "view mode to use in percent" },
                     35:   { "region", "domestic, overseas", "overseas",
                     36:     "hardware region" },
                     37:   { "videostd", "ntsc, pal", "ntsc",
                     38:     "video standard" },
                     39:   { "autodetect", "on, off", "on",
                     40:     "automatic region, videostd detection" },
                     41:   { "plotter", "line, cell", "line",
                     42:     "plotter style - line is slower but much more accurate" },
                     43:   { "interlace", "bob, weave, weave-filter", "weave-filter",
                     44:     "style of de-interlacing to use in 200% view mode" },
                     45:   { "frameskip", "auto, 1..10", "auto",
                     46:     "skip frames to keep up - auto setting needs sound turned on" },
                     47:   { "hborder", "0..32", "8",
                     48:     "horizontal over-scan border surrounding main playing area" },
                     49:   { "vborder", "0..32", "8",
                     50:     "vertical retrace border surrounding main playing area" },
                     51:   { "sound", "on, off", "on",
                     52:     "sound system toggle, also turns off fm and psg (not z80)" },
                     53:   { "z80", "on, off", "on",
                     54:     "z80 cpu" },
                     55:   { "fm", "on, off", "on",
                     56:     "frequency modulation sound generation" },
                     57:   { "psg", "on, off", "on",
                     58:     "programmable sound generation" },
                     59:   { "sound_minfields", "integer", "5",
                     60:     "try to buffer this many fields of sound" },
                     61:   { "sound_maxfields", "integer", "10",
                     62:     "maximum buffered sound fields before blocking (waiting)" },
                     63:   { "loglevel", "integer (0-7)", "1", 
                     64:     "logging level" },
                     65:   { "debugsound", "on, off", "off",
                     66:     "extra sound debug logging" },
                     67:   { "statusbar", "on, off", "off",
                     68:     "status bar on main window (fps counter)" },
                     69:   { "key1_a", "gdk keysym", "a",
                     70:     "keyboard player 1 'A' button" },
                     71:   { "key1_b", "gdk keysym", "s",
                     72:     "keyboard player 1 'B' button" },
                     73:   { "key1_c", "gdk keysym", "d",
                     74:     "keyboard player 1 'C' button" },
                     75:   { "key1_up", "gdk keysym", "Up",
                     76:     "keyboard player 1 up" },
                     77:   { "key1_down", "gdk keysym", "Down",
                     78:     "keyboard player 1 down" },
                     79:   { "key1_left", "gdk keysym", "Left",
                     80:     "keyboard player 1 left" },
                     81:   { "key1_right", "gdk keysym", "Right",
                     82:     "keyboard player 1 right" },
                     83:   { "key1_start", "gdk keysym", "Return",
                     84:     "keyboard player 1 start button" },
                     85:   { "key2_a", "gdk keysym", "KP_Divide",
                     86:     "keyboard player 2 'A' button" },
                     87:   { "key2_b", "gdk keysym", "KP_Multiply",
                     88:     "keyboard player 2 'B' button" },
                     89:   { "key2_c", "gdk keysym", "KP_Subtract",
                     90:     "keyboard player 2 'C' button" },
                     91:   { "key2_up", "gdk keysym", "KP_8",
                     92:     "keyboard player 2 up" },
                     93:   { "key2_down", "gdk keysym", "KP_5",
                     94:     "keyboard player 2 down" },
                     95:   { "key2_left", "gdk keysym", "KP_4",
                     96:     "keyboard player 2 left" },
                     97:   { "key2_right", "gdk keysym", "KP_6",
                     98:     "keyboard player 2 right" },
                     99:   { "key2_start", "gdk keysym", "KP_Enter",
                    100:     "keyboard player 2 start button" },
1.1.1.2 ! root      101:   { "aviframeskip", "integer", "2",
        !           102:     "AVI forced skip, e.g. 2 is 30fps" },
        !           103:   { "aviformat", "rgb, jpeg", "jpeg",
        !           104:     "Type of output to be written to AVI" },
        !           105:   { "jpegquality", "0-100", "100",
        !           106:     "JPEG quality - 100 is least compression but best picture" },
        !           107:   { "lowpassfilter", "0-100", "50",
        !           108:     "Low-pass sound filter - 0 turns it off, 100 filters too much" },
1.1       root      109:   { NULL, NULL, NULL, NULL }
                    110: };
                    111: 
                    112: /* *INDENT-ON* */
                    113: 
                    114: int gtkopts_load(const char *file)
                    115: {
                    116:   char buffer[CONFLINELEN];
                    117:   FILE *fd;
                    118:   char *a, *p, *q, *t;
                    119:   int line = 0;
                    120:   t_conf *conf, *confi;
                    121: 
                    122:   if ((fd = fopen(file, "r")) == NULL) {
                    123:     fprintf(stderr, "%s: unable to open conf '%s' for reading: %s\n",
                    124:             PACKAGE, file, strerror(errno));
                    125:     return -1;
                    126:   }
                    127:   while (fgets(buffer, CONFLINELEN, fd)) {
                    128:     line++;
                    129:     if (buffer[strlen(buffer) - 1] != '\n') {
                    130:       fprintf(stderr, "%s: line %d too long in conf file, ignoring.\n",
                    131:               PACKAGE, line);
                    132:       while ((a = fgets(buffer, CONFLINELEN, fd))) {
                    133:         if (buffer[strlen(buffer) - 1] == '\n')
                    134:           continue;
                    135:       }
                    136:       if (!a)
                    137:         goto finished;
                    138:       continue;
                    139:     }
                    140:     buffer[strlen(buffer) - 1] = '\0';
                    141:     /* remove whitespace off start and end */
                    142:     p = buffer + strlen(buffer) - 1;
                    143:     while (p > buffer && *p == ' ')
                    144:       *p-- = '\0';
                    145:     p = buffer;
                    146:     while (*p == ' ')
                    147:       p++;
                    148:     /* check for comment */
                    149:     if (!*p || *p == '#' || *p == ';')
                    150:       continue;
                    151:     if ((conf = malloc(sizeof(t_conf))) == NULL) {
                    152:       fprintf(stderr, "%s: Out of memory adding to conf\n", PACKAGE);
                    153:       goto error;
                    154:     }
                    155:     q = p;
                    156:     strsep(&q, "=");
                    157:     if (q == NULL) {
                    158:       fprintf(stderr, "%s: line %d not understood in conf file\n", PACKAGE,
                    159:               line);
                    160:       goto error;
                    161:     }
                    162:     /* remove whitespace off end of p and start of q */
                    163:     t = p + strlen(p) - 1;
                    164:     while (t > p && *t == ' ')
                    165:       *t-- = '\0';
                    166:     while (*q == ' ')
                    167:       q++;
                    168:     if ((conf->key = malloc(strlen(p) + 1)) == NULL ||
                    169:         (conf->value = malloc(strlen(q) + 1)) == NULL) {
                    170:       fprintf(stderr, "%s: Out of memory building conf\n", PACKAGE);
                    171:       goto error;
                    172:     }
                    173:     strcpy(conf->key, p);
                    174:     strcpy(conf->value, q);
                    175:     conf->next = NULL;
                    176:     for (confi = gtkopts_conf; confi && confi->next; confi = confi->next);
                    177:     if (!confi)
                    178:       gtkopts_conf = conf;
                    179:     else
                    180:       confi->next = conf;
                    181:   }
                    182: finished:
                    183:   if (ferror(fd) || fclose(fd)) {
                    184:     fprintf(stderr, "%s: error whilst reading conf: %s\n", PACKAGE,
                    185:             strerror(errno));
                    186:     return -1;
                    187:   }
                    188:   return 0;
                    189: error:
                    190:   fclose(fd);
                    191:   return -1;
                    192: }
                    193: 
                    194: const char *gtkopts_getvalue(const char *key)
                    195: {
                    196:   t_conf *c;
                    197:   const char *v;
                    198: 
                    199:   for (c = gtkopts_conf; c; c = c->next) {
                    200:     if (!strcasecmp(key, c->key))
                    201:       return c->value;
                    202:   }
                    203:   v = gtkopts_default(key);
                    204:   if (!v)
                    205:     printf("Warning: invalid option '%s' requested.\n", key);
                    206:   return v;
                    207: }
                    208: 
                    209: int gtkopts_setvalue(const char *key, const char *value)
                    210: {
                    211:   t_conf *c;
                    212:   char *n;
                    213: 
                    214:   for (c = gtkopts_conf; c; c = c->next) {
                    215:     if (!strcasecmp(key, c->key)) {
                    216:       if ((n = malloc(strlen(value) + 1)) == NULL)
                    217:         return -1;
                    218:       strcpy(n, value);
                    219:       free(c->value);
                    220:       c->value = n;
                    221:       return 0;
                    222:     }
                    223:   }
                    224:   if ((c = malloc(sizeof(t_conf))) == NULL ||
                    225:       (c->key = malloc(strlen(key) + 1)) == NULL ||
                    226:       (c->value = malloc(strlen(value) + 1)) == NULL)
                    227:     return -1;
                    228:   strcpy(c->key, key);
                    229:   strcpy(c->value, value);
                    230:   c->next = gtkopts_conf;
                    231:   gtkopts_conf = c;
                    232:   return 0;
                    233: }
                    234: 
                    235: int gtkopts_save(const char *file)
                    236: {
                    237:   FILE *fd;
                    238:   t_opts *o;
                    239: 
                    240:   if ((fd = fopen(file, "w")) == NULL) {
                    241:     fprintf(stderr, "%s: unable to open conf '%s' for writing: %s\n",
                    242:             PACKAGE, file, strerror(errno));
                    243:     return -1;
                    244:   }
                    245:   fprintf(fd, "; Generator " VERSION " configuration file\n\n");
                    246:   fprintf(fd, "; for GTK keysyms see include/gtk-1.2/gdk/gdkkeysyms.h\n\n");
                    247:   for (o = gtkopts_opts; o->key; o++) {
                    248:     fprintf(fd, "; %s\n; values: %s\n", o->desc, o->vals);
                    249:     fprintf(fd, "%s = %s\n\n", o->key, gtkopts_getvalue(o->key));
                    250:   }
                    251:   if (fclose(fd)) {
                    252:     fprintf(stderr, "%s: error whilst writing conf: %s\n", PACKAGE,
                    253:             strerror(errno));
                    254:     return -1;
                    255:   }
                    256:   return 0;
                    257: }
                    258: 
                    259: static const char *gtkopts_default(const char *key)
                    260: {
                    261:   t_opts *o;
                    262: 
                    263:   for (o = gtkopts_opts; o->key; o++) {
                    264:     if (!strcasecmp(key, o->key))
                    265:       return o->def;
                    266:   }
                    267:   return NULL;
                    268: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.