/* * * postfish * * Copyright (C) 2002-2005 Monty * * Postfish is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * Postfish is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Postfish; see the file COPYING. If not, write to the * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * * */ #include "postfish.h" #include "config.h" #include #include #include typedef struct { char *key; int bank; int A; int B; int C; int vals; int *vec; char *string; } configentry; static int configentries=0; static configentry *config_list=0; static int look_for_key(char *key,int bank,int A, int B, int C){ int i; for(i=0;ic->vals){ if(!c->vec) c->vec=calloc(n,sizeof(*c->vec)); else{ c->vec=realloc(c->vec,n*sizeof(*c->vec)); memset(c->vec+c->vals,0,(n-c->vals)*sizeof(*c->vec)); } c->vals=n; } } /* dump changes back into existing local config state; this is mostly an elaborate means of meging changes into an existing file that may be a superset of what's currently running */ void config_set_string(char *key,int bank, int A, int B, int C, const char *s){ configentry *c=old_or_new(key,bank,A,B,C); c->string=strdup(s); } void config_set_integer(char *key,int bank, int A, int B, int C, int valnum, int val){ configentry *c=old_or_new(key,bank,A,B,C); extend_vec(c,valnum+1); c->vec[valnum]=val; } void config_set_vector(char *key,int bank, int A, int B, int C,int n, sig_atomic_t *v){ int i; configentry *c=old_or_new(key,bank,A,B,C); extend_vec(c,n); for(i=0;ivec[i]=v[i]; } int config_load(char *filename){ FILE *f=fopen(filename,"r"); char key[80]; int bank,A,B,C,width,rev; int errflag=0; fprintf(stderr,"Loading state configuration file %s... ",filename); sprintf(key,"[file beginning]"); if(!f){ fprintf(stderr,"No config file %s; will be created on save/exit.\n",filename); return 0; } /* search for magic */ if(fscanf(f,"Postfish rev %d",&rev)!=1 || rev!=2){ fprintf(stderr,"File %s is not a postfish state configuration file.\n",filename); fclose(f); return -1; } /* load file */ while(!feof(f)){ int c=fgetc(f); switch(c){ case '(': /* string type input */ if (fscanf(f,"%79s bank%d A%d B%d C%d l%d \"", key,&bank,&A,&B,&C,&width)==6){ char *buffer=calloc(width+1,sizeof(*buffer)); for(c=0;cstring) fprintf(f,"(%s bank%d A%d B%d C%d l%d \"%s\" )\n", c->key,c->bank,c->A,c->B,c->C,strlen(c->string),c->string); if(c->vec){ fprintf(f,"[%s bank%d A%d B%d C%d v%d ", c->key,c->bank,c->A,c->B,c->C,c->vals); for(j=0;jvals;j++) fprintf(f,"%d ",c->vec[j]); fprintf(f,"]\n"); } } fclose(f); fprintf(stderr," done.\n"); }