1 static const char CVSID
[] = "$Id: prefFile.c,v 1.19 2003/04/03 19:05:37 jlous Exp $";
2 /*******************************************************************************
4 * prefFile.c -- Nirvana utilities for providing application preferences files *
6 * Copyright (C) 1999 Mark Edel *
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 *
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 *
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 *
22 * Nirvana Text Editor *
25 * Written by Mark Edel *
27 *******************************************************************************/
30 #include "../config.h"
34 #include "fileUtils.h"
44 #include <sys/param.h>
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
);
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
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
,
135 static XrmOptionDescRec xrmOnlyTable
[] =
136 {{"-xrm", NULL
, XrmoptionResArg
, (caddr_t
)NULL
}};
138 /* read the preferences file into an X database.
139 On failure prefDB will be NULL. */
140 if (fullName
== NULL
)
145 fileString
= ReadAnyTextFile(fullName
);
146 if (fileString
== NULL
){
150 db
= XrmGetStringDatabase(fileString
);
155 /* parse the command line, storing results in the preferences database */
156 XrmParseCommand(&db
, opTable
, nOptions
, appName
, (int *)argcInOut
,
159 /* process -xrm (resource setting by resource name) arguments so those
160 pertaining to preference resources will be included in the database.
161 Don't remove -xrm arguments from the argument vector, however, so
162 XtDisplayInitialize can still read the non-preference resources */
163 argvCopy
= (char**)XtMalloc(sizeof(char *) * *argcInOut
);
164 memcpy(argvCopy
, argvInOut
, sizeof(char *) * *argcInOut
);
165 argcCopy
= *argcInOut
;
166 XrmParseCommand(&db
, xrmOnlyTable
, XtNumber(xrmOnlyTable
), appName
,
167 &argcCopy
, argvCopy
);
168 XtFree((char *)argvCopy
);
170 /* If the resource database file existed, add a resource to the database
171 which notes this fact, so that programs will know whether the file
172 was really ever found or not */
173 if ((fp
= fopen(fullName
, "r")) != NULL
) {
174 char *rsrcName
= (char*)XtMalloc(strlen(appName
) + 14);
176 sprintf(rsrcName
, "%s.prefFileRead", appName
);
177 XrmPutStringResource(&db
, rsrcName
, "True");
185 ** RestorePreferences
187 ** Fill in preferences data from two X databases, values in prefDB taking
188 ** precidence over those in appDB.
190 void RestorePreferences(XrmDatabase prefDB
, XrmDatabase appDB
,
191 const char *appName
, const char *appClass
, PrefDescripRec
*rsrcDescrip
, int nRsrc
)
193 readPrefs(prefDB
, appDB
, appName
, appClass
, rsrcDescrip
, nRsrc
, False
);
197 ** OverlayPreferences
199 ** Incorporate preference specified in database "prefDB", preserving (not
200 ** restoring to default) existing preferences, not mentioned in "prefDB"
202 void OverlayPreferences(XrmDatabase prefDB
, const char *appName
,
203 const char *appClass
, PrefDescripRec
*rsrcDescrip
, int nRsrc
)
205 readPrefs(NULL
, prefDB
, appName
, appClass
, rsrcDescrip
, nRsrc
, True
);
208 static void readPrefs(XrmDatabase prefDB
, XrmDatabase appDB
,
209 const char *appName
, const char *appClass
, PrefDescripRec
*rsrcDescrip
,
210 int nRsrc
, int overlay
)
212 char rsrcName
[256], rsrcClass
[256], *valueString
, *type
;
216 /* read each resource, trying first the preferences file database, then
217 the application database, then the default value if neither are found */
218 for (i
=0; i
<nRsrc
; i
++) {
219 sprintf(rsrcName
,"%s.%s", appName
, rsrcDescrip
[i
].name
);
220 sprintf(rsrcClass
, "%s.%s", appClass
, rsrcDescrip
[i
].class);
222 XrmGetResource(prefDB
, rsrcName
, rsrcClass
, &type
, &rsrcValue
)) {
223 if (strcmp(type
, XmRString
)) {
224 fprintf(stderr
,"Internal Error: Unexpected resource type, %s\n",
228 valueString
= rsrcValue
.addr
;
229 } else if (XrmGetResource(appDB
,rsrcName
,rsrcClass
,&type
,&rsrcValue
)) {
230 if (strcmp(type
, XmRString
)) {
231 fprintf(stderr
,"Internal Error: Unexpected resource type, %s\n",
235 valueString
= rsrcValue
.addr
;
237 valueString
= rsrcDescrip
[i
].defaultString
;
238 if (overlay
&& valueString
== rsrcDescrip
[i
].defaultString
)
240 if (!stringToPref(valueString
, &rsrcDescrip
[i
]))
241 fprintf(stderr
, "Could not read value of resource %s\n", rsrcName
);
246 ** RestoreDefaultPreferences
248 ** Restore preferences to their default values as stored in rsrcDesrcip
250 void RestoreDefaultPreferences(PrefDescripRec
*rsrcDescrip
, int nRsrc
)
254 for (i
=0; i
<nRsrc
; i
++)
255 stringToPref(rsrcDescrip
[i
].defaultString
, &rsrcDescrip
[i
]);
261 ** Create or replace an application preference file according to
262 ** the resource descriptions in rsrcDesrcip.
264 int SavePreferences(Display
*display
, const char *fullName
,
265 const char *fileHeader
, PrefDescripRec
*rsrcDescrip
, int nRsrc
)
267 char *appName
, *appClass
, **enumStrings
;
273 if ((fp
= fopen(fullName
, "w")) == NULL
)
276 /* write the file header text out to the file */
277 fprintf(fp
, "%s\n", fileHeader
);
279 /* write out the resources so they can be read by XrmGetFileDatabase */
280 XtGetApplicationNameAndClass(display
, &appName
, &appClass
);
281 for (i
=0; i
<nRsrc
; i
++) {
282 if (rsrcDescrip
[i
].save
) {
283 type
= rsrcDescrip
[i
].dataType
;
284 fprintf(fp
, "%s.%s: ", appName
, rsrcDescrip
[i
].name
);
285 if (type
== PREF_STRING
)
286 fprintf(fp
, "%s", (char *)rsrcDescrip
[i
].valueAddr
);
287 if (type
== PREF_ALLOC_STRING
)
288 fprintf(fp
, "%s", *(char **)rsrcDescrip
[i
].valueAddr
);
289 else if (type
== PREF_ENUM
) {
290 enumStrings
= (char **)rsrcDescrip
[i
].arg
;
291 fprintf(fp
,"%s", enumStrings
[*(int *)rsrcDescrip
[i
].valueAddr
]);
292 } else if (type
== PREF_INT
)
293 fprintf(fp
, "%d", *(int *)rsrcDescrip
[i
].valueAddr
);
294 else if (type
== PREF_BOOLEAN
) {
295 if (*(int *)rsrcDescrip
[i
].valueAddr
)
298 fprintf(fp
, "False");
307 static int stringToPref(const char *string
, PrefDescripRec
*rsrcDescrip
)
310 char *cleanStr
, *endPtr
, **enumStrings
;
312 switch (rsrcDescrip
->dataType
) {
314 cleanStr
= removeWhiteSpace(string
);
315 *(int *)rsrcDescrip
->valueAddr
=
316 strtol(cleanStr
, &endPtr
, 10);
317 if (strlen(cleanStr
) == 0) { /* String is empty */
318 *(int *)rsrcDescrip
->valueAddr
= 0;
321 } else if (*endPtr
!= '\0') { /* Whole string not parsed */
322 *(int *)rsrcDescrip
->valueAddr
= 0;
329 cleanStr
= removeWhiteSpace(string
);
330 for (i
=0; i
<N_BOOLEAN_STRINGS
; i
++) {
331 if (!strcmp(TrueStrings
[i
], cleanStr
)) {
332 *(int *)rsrcDescrip
->valueAddr
= True
;
336 if (!strcmp(FalseStrings
[i
], cleanStr
)) {
337 *(int *)rsrcDescrip
->valueAddr
= False
;
343 *(int *)rsrcDescrip
->valueAddr
= False
;
346 cleanStr
= removeWhiteSpace(string
);
347 enumStrings
= (char **)rsrcDescrip
->arg
;
348 for (i
=0; enumStrings
[i
]!=NULL
; i
++) {
349 if (!strcmp(enumStrings
[i
], cleanStr
)) {
350 *(int *)rsrcDescrip
->valueAddr
= i
;
356 *(int *)rsrcDescrip
->valueAddr
= 0;
359 if ((int)strlen(string
) >= (int)rsrcDescrip
->arg
)
361 strncpy(rsrcDescrip
->valueAddr
, string
, (int)rsrcDescrip
->arg
);
363 case PREF_ALLOC_STRING
:
364 *(char **)rsrcDescrip
->valueAddr
= XtMalloc(strlen(string
) + 1);
365 strcpy(*(char **)rsrcDescrip
->valueAddr
, string
);
372 ** Remove the white space (blanks and tabs) from a string and return
373 ** the result in a newly allocated string as the function value
375 static char *removeWhiteSpace(const char *string
)
377 char *outPtr
, *outString
;
379 outPtr
= outString
= XtMalloc(strlen(string
)+1);
381 if (*string
!= ' ' && *string
!= '\t')
382 *(outPtr
++) = *(string
++);
394 Q: Why aren't you using the Xt type conversion services?
395 A: 1) To create a save file, you also need to convert values back to text form,
396 and there are no converters for that direction. 2) XtGetApplicationResources
397 can only be used on the resource database created by the X toolkit at
398 initialization time, and there is no way to intervene in the creation of
399 that database or store new resources in it reliably after it is created.
400 3) The alternative, XtConvertAndStore is not adequately documented. The
401 toolkit mauual does not explain why it overwrites its input value structure.
402 4) XtGetApplicationResources and XtConvertAndStore do not work well together
403 because they use different storage strategies for certain data types.