4 * Preferences loading and saving
6 * © 1993-1998 Straylight
9 /*----- Licensing note ----------------------------------------------------*
11 * This file is part of Straylight's Steel library.
13 * Steel is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * Steel is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Steel. If not, write to the Free Software Foundation,
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 #define unused(x) ((x)=(x))
43 static prefs_prefstr *prefs__prefs;
46 * char *prefs__match(char *a,char *b)
49 * Checks to see if the first part of a matches b. If it does, it returns
53 * char *a == an input string
54 * char *b == a keyword
57 * The part of a following b, or NULL if no match
60 static char *prefs__match(char *a,char *b)
66 if (tolower(*(a++))!=tolower(*(b++)))
79 * char *prefs_readBoolean(char *tag,char *args,void *handle)
82 * Reads a Boolean variable (i.e. one that can be either TRUE or FALSE).
85 char *prefs_readBoolean(char *tag,char *args,void *handle)
88 if (utils_caselessCmp(args,"true")==0)
90 else if (utils_caselessCmp(args,"false")==0)
91 *(BOOL *)handle=FALSE;
93 return (msgs_lookup("prefsRBB:Bad boolean variable '%s' at line %i (must be 'true' or 'false')"));
98 * char *prefs_writeBoolean(char *tag,FILE *fp,void *handle)
101 * Writes a Boolean variable to the preferences file.
104 char *prefs_writeBoolean(char *tag,FILE *fp,void *handle)
106 fprintf(fp,"%s=%s\n",tag,*(BOOL *)handle ? "true" : "false");
111 * char *prefs_readNumeric(char *tag,char *args,void *handle)
114 * Reads a signed integer from the preferences file.
117 char *prefs_readNumeric(char *tag,char *args,void *handle)
120 sscanf(args,"%d",(int *)handle);
125 * char *prefs_writeNumeric(char *tag,FILE *fp,void *handle)
128 * Writes a signed integer to the preferences file.
131 char *prefs_writeNumeric(char *tag,FILE *fp,void *handle)
133 fprintf(fp,"%s=%i\n",tag,*(int *)handle);
138 * char *prefs_readString(char *tag,char *args,void *handle)
141 * Reads a string from the preferences file. It is assumed that the buffer
142 * pointed to by handle is big enough.
145 char *prefs_readString(char *tag,char *args,void *handle)
151 for (p=++args;*p && *p!='\'';p++)
155 strcpy((char *)handle,args);
160 * char *prefs_writeString(char *tag,FILE *fp,void *handle)
163 * Writes a signed integer to the preferences file.
166 char *prefs_writeString(char *tag,FILE *fp,void *handle)
168 fprintf(fp,"%s='%s'\n",tag,(char *)handle);
173 * void prefs_preferences(prefs_prefstr *p)
176 * Sets up the prefs system to use the preferences definition specified.
177 * 'p' should be a pointer to an array of prefs_prefstrs, terminated by an
178 * entry with a null 'tag'. The preferences file created will be called
179 * 'Choices' in the current application's directory (<App$Dir>.Choices).
182 * prefs_prefstr *p == pointer to an array of preferences definitions
185 void prefs_preferences(prefs_prefstr *p)
191 * void prefs_read(void)
194 * Reads preferences from the preferences file.
197 void prefs_read(void)
206 char *preffile=choices_name("Choices",FALSE);
208 /* --- Make sure the Preferences file exists --- *
210 * If we can't find the Preferences, we leave the user's buffer unchanged
214 if (!res_fileExists(preffile))
217 /* --- Open the Preferences file --- */
219 fp=fopen(preffile,"r");
223 msgs_lookup("prefsERP:Error reading preferences. "
224 "Assuming default preferences."));
228 /* --- Read the file a line at a time and parse it --- */
230 while (fgets(buff,256,fp))
232 /* --- Bump line count for error messages --- */
236 /* --- Strip leading spaces --- */
238 for (p=buff;isspace(*p);p++)
241 /* --- fgets leaves a return on the end -- kill it --- */
243 utils_ctermToNterm(p);
245 /* --- Do something about the line --- */
249 /* --- Discard blank lines and comments --- */
254 /* --- Otherwise find the parser for this variable and call it --- */
257 for (i=0;prefs__prefs[i].tag;i++)
259 if (args=prefs__match(p,prefs__prefs[i].tag),args)
261 if (prefs__prefs[i].read)
263 if (error=prefs__prefs[i].read(prefs__prefs[i].tag,
265 prefs__prefs[i].handle),error)
267 werr(FALSE,error,prefs__prefs[i].tag,line);
282 * void prefs_write(void)
285 * Writes preferences out to disk.
288 void prefs_write(void)
291 FILE *fp=fopen(choices_name("Choices",TRUE),"w");
294 /* --- Complain if we can't write the file --- */
299 msgs_lookup("prefsEWP:Error writing preferences: '%s'"),
300 _kernel_last_oserror()->errmess);
304 /* --- Go through the entries one by one calling the output fns --- */
306 for (i=0;prefs__prefs[i].tag;i++)
308 if (prefs__prefs[i].tag[0]==';' || prefs__prefs[i].tag[0]=='\n')
309 fprintf(fp,"%s",prefs__prefs[i].tag);
310 else if (!prefs__prefs[i].write)
312 else if (error=prefs__prefs[i].write(prefs__prefs[i].tag,
314 prefs__prefs[i].handle),error)
316 werr(FALSE,error,prefs__prefs[i].tag);
322 /* --- Close the file and exit --- */