Added Lesstif-specific suppression of harmless but annoying warnings
[nedit.git] / util / prefFile.c
blob885e0329e71b6fb4a6cc783f5784f0926beb1c95
1 static const char CVSID[] = "$Id: prefFile.c,v 1.22 2003/12/13 18:16:10 yooden Exp $";
2 /*******************************************************************************
3 * *
4 * prefFile.c -- Nirvana utilities for providing application preferences files *
5 * *
6 * Copyright (C) 1999 Mark Edel *
7 * *
8 * This is free software; you can redistribute it and/or modify it under the *
9 * terms of the GNU General Public License as published by the Free Software *
10 * Foundation; either version 2 of the License, or (at your option) any later *
11 * version. *
12 * *
13 * This software is distributed in the hope that it will be useful, but WITHOUT *
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
16 * for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License along with *
19 * software; if not, write to the Free Software Foundation, Inc., 59 Temple *
20 * Place, Suite 330, Boston, MA 02111-1307 USA *
21 * *
22 * Nirvana Text Editor *
23 * June 3, 1993 *
24 * *
25 * Written by Mark Edel *
26 * *
27 *******************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 #include "../config.h"
31 #endif
33 #include "prefFile.h"
34 #include "fileUtils.h"
35 #include "utils.h"
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #ifdef VMS
41 #include "VMSparam.h"
42 #else
43 #ifndef __MVS__
44 #include <sys/param.h>
45 #endif
46 #endif
47 #include <Xm/Xm.h>
49 #ifdef HAVE_DEBUG_H
50 #include "../debug.h"
51 #endif
53 #define N_BOOLEAN_STRINGS 13
54 static const char *TrueStrings[N_BOOLEAN_STRINGS] = {"True", "true", "TRUE", "T", "t",
55 "Yes", "yes", "YES", "y", "Y", "on", "On", "ON"};
56 static const char *FalseStrings[N_BOOLEAN_STRINGS] = {"False", "false", "FALSE", "F", "f",
57 "No", "no", "NO", "n", "N", "off", "Off", "OFF"};
59 static void readPrefs(XrmDatabase prefDB, XrmDatabase appDB,
60 const char *appName, const char *appClass,
61 PrefDescripRec *rsrcDescrip, int nRsrc, int overlay);
62 static int stringToPref(const char *string, PrefDescripRec *rsrcDescrip);
63 static char *removeWhiteSpace(const char *string);
66 ** Preferences File
68 ** An application maintains a preferences file so that users can
69 ** quickly save and restore program options from within a program,
70 ** without being forced to learn the X resource mechanism.
72 ** Preference files are the same format as X resource files, and
73 ** are read using the X resource file reader. X-savvy users are allowed
74 ** to move resources out of a preferences file to their X resource
75 ** files. They would do so if they wanted to attach server-specific
76 ** preferences (such as fonts and colors) to different X servers, or to
77 ** combine additional preferences served only by X resources with those
78 ** provided by the program's menus.
82 ** Preference description table
84 ** A preference description table contains the information necessary
85 ** to read preference resources and store their values in a data
86 ** structure. The table can, so far, describe four types
87 ** of values (this will probably be expanded in the future to include
88 ** more types): ints, booleans, enumerations, and strings. Each entry
89 ** includes the name and class for saving and restoring the parameter
90 ** in X database format, the data type, a default value in the form of
91 ** a character string, and the address where the parameter value is
92 ** be stored. Strings and enumerations take an additional argument.
93 ** For strings, it is the maximum length string that can safely be
94 ** stored or NULL to indicate that new space should be allocated and a
95 ** pointer to it stored in the value address. For enums, it is an array
96 ** of string pointers to the names of each of its possible values. The
97 ** last value in a preference record is a flag for determining whether
98 ** the value should be written to the save file by SavePreferences.
102 ** CreatePreferencesDatabase
104 ** Process a preferences file and the command line options pertaining to
105 ** the X resources used to set those preferences. Create an X database
106 ** of the results. The reason for this odd set of functionality is
107 ** to process command line options before XtDisplayInitialize reads them
108 ** into the application database that the toolkit attaches to the display.
109 ** This allows command line arguments to properly override values specified
110 ** in the preferences file.
112 ** fileName Name only of the preferences file to be found
113 ** in the user's home directory
114 ** appName Application name to use in reading the preference
115 ** resources
116 ** opTable Xrm command line option table for the resources
117 ** used in the preferences file ONLY. Command line
118 ** options for other X resources should be processed
119 ** by XtDisplayInitialize.
120 ** nOptions Number of items in opTable
121 ** argcInOut Address of argument count. This will be altered
122 ** to remove the command line options that are
123 ** recognized in the option table.
124 ** argvInOut Argument vector. Will be altered as argcInOut.
126 XrmDatabase CreatePreferencesDatabase(const char *fullName, const char *appName,
127 XrmOptionDescList opTable, int nOptions, unsigned int *argcInOut,
128 char **argvInOut)
130 XrmDatabase db;
131 int argcCopy;
132 char **argvCopy;
133 char *fileString;
134 static XrmOptionDescRec xrmOnlyTable[] =
135 {{"-xrm", NULL, XrmoptionResArg, (caddr_t)NULL}};
137 /* read the preferences file into an X database.
138 On failure prefDB will be NULL. */
139 if (NULL == fullName)
141 db = NULL;
142 } else
144 fileString = ReadAnyTextFile(fullName);
145 if (NULL == fileString)
147 db = NULL;
148 } else
150 char* rsrcName;
151 db = XrmGetStringDatabase(fileString);
152 XtFree(fileString);
154 /* Add a resource to the database which remembers that
155 the file is read, so that NEdit will know it. */
156 rsrcName = (char*) XtMalloc(strlen(appName) + 14);
157 sprintf(rsrcName, "%s.prefFileRead", appName);
158 XrmPutStringResource(&db, rsrcName, "True");
159 XtFree(rsrcName);
163 /* parse the command line, storing results in the preferences database */
164 XrmParseCommand(&db, opTable, nOptions, appName, (int *)argcInOut,
165 argvInOut);
167 /* process -xrm (resource setting by resource name) arguments so those
168 pertaining to preference resources will be included in the database.
169 Don't remove -xrm arguments from the argument vector, however, so
170 XtDisplayInitialize can still read the non-preference resources */
171 argvCopy = (char**)XtMalloc(sizeof(char *) * *argcInOut);
172 memcpy(argvCopy, argvInOut, sizeof(char *) * *argcInOut);
173 argcCopy = *argcInOut;
174 XrmParseCommand(&db, xrmOnlyTable, XtNumber(xrmOnlyTable), appName,
175 &argcCopy, argvCopy);
176 XtFree((char *)argvCopy);
177 return db;
181 ** RestorePreferences
183 ** Fill in preferences data from two X databases, values in prefDB taking
184 ** precidence over those in appDB.
186 void RestorePreferences(XrmDatabase prefDB, XrmDatabase appDB,
187 const char *appName, const char *appClass, PrefDescripRec *rsrcDescrip, int nRsrc)
189 readPrefs(prefDB, appDB, appName, appClass, rsrcDescrip, nRsrc, False);
193 ** OverlayPreferences
195 ** Incorporate preference specified in database "prefDB", preserving (not
196 ** restoring to default) existing preferences, not mentioned in "prefDB"
198 void OverlayPreferences(XrmDatabase prefDB, const char *appName,
199 const char *appClass, PrefDescripRec *rsrcDescrip, int nRsrc)
201 readPrefs(NULL, prefDB, appName, appClass, rsrcDescrip, nRsrc, True);
204 static void readPrefs(XrmDatabase prefDB, XrmDatabase appDB,
205 const char *appName, const char *appClass, PrefDescripRec *rsrcDescrip,
206 int nRsrc, int overlay)
208 char rsrcName[256], rsrcClass[256], *valueString, *type;
209 XrmValue rsrcValue;
210 int i;
212 /* read each resource, trying first the preferences file database, then
213 the application database, then the default value if neither are found */
214 for (i=0; i<nRsrc; i++) {
215 sprintf(rsrcName,"%s.%s", appName, rsrcDescrip[i].name);
216 sprintf(rsrcClass, "%s.%s", appClass, rsrcDescrip[i].class);
217 if (prefDB!=NULL &&
218 XrmGetResource(prefDB, rsrcName, rsrcClass, &type, &rsrcValue)) {
219 if (strcmp(type, XmRString)) {
220 fprintf(stderr,"Internal Error: Unexpected resource type, %s\n",
221 type);
222 return;
224 valueString = rsrcValue.addr;
225 } else if (XrmGetResource(appDB,rsrcName,rsrcClass,&type,&rsrcValue)) {
226 if (strcmp(type, XmRString)) {
227 fprintf(stderr,"Internal Error: Unexpected resource type, %s\n",
228 type);
229 return;
231 valueString = rsrcValue.addr;
232 } else
233 valueString = rsrcDescrip[i].defaultString;
234 if (overlay && valueString == rsrcDescrip[i].defaultString)
235 continue;
236 if (!stringToPref(valueString, &rsrcDescrip[i]))
237 fprintf(stderr, "Could not read value of resource %s\n", rsrcName);
242 ** RestoreDefaultPreferences
244 ** Restore preferences to their default values as stored in rsrcDesrcip
246 void RestoreDefaultPreferences(PrefDescripRec *rsrcDescrip, int nRsrc)
248 int i;
250 for (i=0; i<nRsrc; i++)
251 stringToPref(rsrcDescrip[i].defaultString, &rsrcDescrip[i]);
255 ** SavePreferences
257 ** Create or replace an application preference file according to
258 ** the resource descriptions in rsrcDesrcip.
260 int SavePreferences(Display *display, const char *fullName,
261 const char *fileHeader, PrefDescripRec *rsrcDescrip, int nRsrc)
263 char *appName, *appClass, **enumStrings;
264 FILE *fp;
265 int type;
266 int i;
268 /* open the file */
269 if ((fp = fopen(fullName, "w")) == NULL)
270 return False;
272 /* write the file header text out to the file */
273 fprintf(fp, "%s\n", fileHeader);
275 /* write out the resources so they can be read by XrmGetFileDatabase */
276 XtGetApplicationNameAndClass(display, &appName, &appClass);
277 for (i=0; i<nRsrc; i++) {
278 if (rsrcDescrip[i].save) {
279 type = rsrcDescrip[i].dataType;
280 fprintf(fp, "%s.%s: ", appName, rsrcDescrip[i].name);
281 if (type == PREF_STRING)
282 fprintf(fp, "%s", (char *)rsrcDescrip[i].valueAddr);
283 if (type == PREF_ALLOC_STRING)
284 fprintf(fp, "%s", *(char **)rsrcDescrip[i].valueAddr);
285 else if (type == PREF_ENUM) {
286 enumStrings = (char **)rsrcDescrip[i].arg;
287 fprintf(fp,"%s", enumStrings[*(int *)rsrcDescrip[i].valueAddr]);
288 } else if (type == PREF_INT)
289 fprintf(fp, "%d", *(int *)rsrcDescrip[i].valueAddr);
290 else if (type == PREF_BOOLEAN) {
291 if (*(int *)rsrcDescrip[i].valueAddr)
292 fprintf(fp, "True");
293 else
294 fprintf(fp, "False");
296 fprintf(fp, "\n");
299 fclose(fp);
300 return True;
303 static int stringToPref(const char *string, PrefDescripRec *rsrcDescrip)
305 int i;
306 char *cleanStr, *endPtr, **enumStrings;
308 switch (rsrcDescrip->dataType) {
309 case PREF_INT:
310 cleanStr = removeWhiteSpace(string);
311 *(int *)rsrcDescrip->valueAddr =
312 strtol(cleanStr, &endPtr, 10);
313 if (strlen(cleanStr) == 0) { /* String is empty */
314 *(int *)rsrcDescrip->valueAddr = 0;
315 XtFree(cleanStr);
316 return False;
317 } else if (*endPtr != '\0') { /* Whole string not parsed */
318 *(int *)rsrcDescrip->valueAddr = 0;
319 XtFree(cleanStr);
320 return False;
322 XtFree(cleanStr);
323 return True;
324 case PREF_BOOLEAN:
325 cleanStr = removeWhiteSpace(string);
326 for (i=0; i<N_BOOLEAN_STRINGS; i++) {
327 if (!strcmp(TrueStrings[i], cleanStr)) {
328 *(int *)rsrcDescrip->valueAddr = True;
329 XtFree(cleanStr);
330 return True;
332 if (!strcmp(FalseStrings[i], cleanStr)) {
333 *(int *)rsrcDescrip->valueAddr = False;
334 XtFree(cleanStr);
335 return True;
338 XtFree(cleanStr);
339 *(int *)rsrcDescrip->valueAddr = False;
340 return False;
341 case PREF_ENUM:
342 cleanStr = removeWhiteSpace(string);
343 enumStrings = (char **)rsrcDescrip->arg;
344 for (i=0; enumStrings[i]!=NULL; i++) {
345 if (!strcmp(enumStrings[i], cleanStr)) {
346 *(int *)rsrcDescrip->valueAddr = i;
347 XtFree(cleanStr);
348 return True;
351 XtFree(cleanStr);
352 *(int *)rsrcDescrip->valueAddr = 0;
353 return False;
354 case PREF_STRING:
355 if ((int)strlen(string) >= (int)rsrcDescrip->arg)
356 return False;
357 strncpy(rsrcDescrip->valueAddr, string, (int)rsrcDescrip->arg);
358 return True;
359 case PREF_ALLOC_STRING:
360 *(char **)rsrcDescrip->valueAddr = XtMalloc(strlen(string) + 1);
361 strcpy(*(char **)rsrcDescrip->valueAddr, string);
362 return True;
364 return False;
368 ** Remove the white space (blanks and tabs) from a string and return
369 ** the result in a newly allocated string as the function value
371 static char *removeWhiteSpace(const char *string)
373 char *outPtr, *outString;
375 outPtr = outString = XtMalloc(strlen(string)+1);
376 while (TRUE) {
377 if (*string != ' ' && *string != '\t')
378 *(outPtr++) = *(string++);
379 else
380 string++;
381 if (*string == 0) {
382 *outPtr = 0;
383 return outString;
388 /*******************
389 Implementation Note:
390 Q: Why aren't you using the Xt type conversion services?
391 A: 1) To create a save file, you also need to convert values back to text form,
392 and there are no converters for that direction. 2) XtGetApplicationResources
393 can only be used on the resource database created by the X toolkit at
394 initialization time, and there is no way to intervene in the creation of
395 that database or store new resources in it reliably after it is created.
396 3) The alternative, XtConvertAndStore is not adequately documented. The
397 toolkit mauual does not explain why it overwrites its input value structure.
398 4) XtGetApplicationResources and XtConvertAndStore do not work well together
399 because they use different storage strategies for certain data types.
400 *******************/