1 static const char CVSID
[] = "$Id: prefFile.c,v 1.27 2008/08/20 14:57:36 lebert 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 *
11 * version. In addition, you may distribute version of this program linked to *
12 * Motif or Open Motif. See README for details. *
14 * This software is distributed in the hope that it will be useful, but WITHOUT *
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
19 * You should have received a copy of the GNU General Public License along with *
20 * software; if not, write to the Free Software Foundation, Inc., 59 Temple *
21 * Place, Suite 330, Boston, MA 02111-1307 USA *
23 * Nirvana Text Editor *
26 * Written by Mark Edel *
28 *******************************************************************************/
31 #include "../config.h"
35 #include "fileUtils.h"
45 #include <sys/param.h>
54 #define N_BOOLEAN_STRINGS 13
55 static const char *TrueStrings
[N_BOOLEAN_STRINGS
] = {"True", "true", "TRUE", "T", "t",
56 "Yes", "yes", "YES", "y", "Y", "on", "On", "ON"};
57 static const char *FalseStrings
[N_BOOLEAN_STRINGS
] = {"False", "false", "FALSE", "F", "f",
58 "No", "no", "NO", "n", "N", "off", "Off", "OFF"};
60 static void readPrefs(XrmDatabase prefDB
, XrmDatabase appDB
,
61 const char *appName
, const char *appClass
,
62 PrefDescripRec
*rsrcDescrip
, int nRsrc
, int overlay
);
63 static int stringToPref(const char *string
, PrefDescripRec
*rsrcDescrip
);
64 static char *removeWhiteSpace(const char *string
);
69 ** An application maintains a preferences file so that users can
70 ** quickly save and restore program options from within a program,
71 ** without being forced to learn the X resource mechanism.
73 ** Preference files are the same format as X resource files, and
74 ** are read using the X resource file reader. X-savvy users are allowed
75 ** to move resources out of a preferences file to their X resource
76 ** files. They would do so if they wanted to attach server-specific
77 ** preferences (such as fonts and colors) to different X servers, or to
78 ** combine additional preferences served only by X resources with those
79 ** provided by the program's menus.
83 ** Preference description table
85 ** A preference description table contains the information necessary
86 ** to read preference resources and store their values in a data
87 ** structure. The table can, so far, describe four types
88 ** of values (this will probably be expanded in the future to include
89 ** more types): ints, booleans, enumerations, and strings. Each entry
90 ** includes the name and class for saving and restoring the parameter
91 ** in X database format, the data type, a default value in the form of
92 ** a character string, and the address where the parameter value is
93 ** be stored. Strings and enumerations take an additional argument.
94 ** For strings, it is the maximum length string that can safely be
95 ** stored or NULL to indicate that new space should be allocated and a
96 ** pointer to it stored in the value address. For enums, it is an array
97 ** of string pointers to the names of each of its possible values. The
98 ** last value in a preference record is a flag for determining whether
99 ** the value should be written to the save file by SavePreferences.
103 ** CreatePreferencesDatabase
105 ** Process a preferences file and the command line options pertaining to
106 ** the X resources used to set those preferences. Create an X database
107 ** of the results. The reason for this odd set of functionality is
108 ** to process command line options before XtDisplayInitialize reads them
109 ** into the application database that the toolkit attaches to the display.
110 ** This allows command line arguments to properly override values specified
111 ** in the preferences file.
113 ** fileName Name only of the preferences file to be found
114 ** in the user's home directory
115 ** appName Application name to use in reading the preference
117 ** opTable Xrm command line option table for the resources
118 ** used in the preferences file ONLY. Command line
119 ** options for other X resources should be processed
120 ** by XtDisplayInitialize.
121 ** nOptions Number of items in opTable
122 ** argcInOut Address of argument count. This will be altered
123 ** to remove the command line options that are
124 ** recognized in the option table.
125 ** argvInOut Argument vector. Will be altered as argcInOut.
127 XrmDatabase
CreatePreferencesDatabase(const char *fullName
, const char *appName
,
128 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 (NULL
== fullName
)
145 fileString
= ReadAnyTextFile(fullName
, False
);
146 if (NULL
== fileString
)
152 db
= XrmGetStringDatabase(fileString
);
155 /* Add a resource to the database which remembers that
156 the file is read, so that NEdit will know it. */
157 rsrcName
= (char*) XtMalloc(strlen(appName
) + 14);
158 sprintf(rsrcName
, "%s.prefFileRead", appName
);
159 XrmPutStringResource(&db
, rsrcName
, "True");
164 /* parse the command line, storing results in the preferences database */
165 XrmParseCommand(&db
, opTable
, nOptions
, appName
, (int *)argcInOut
,
168 /* process -xrm (resource setting by resource name) arguments so those
169 pertaining to preference resources will be included in the database.
170 Don't remove -xrm arguments from the argument vector, however, so
171 XtDisplayInitialize can still read the non-preference resources */
172 argvCopy
= (char**)XtMalloc(sizeof(char *) * *argcInOut
);
173 memcpy(argvCopy
, argvInOut
, sizeof(char *) * *argcInOut
);
174 argcCopy
= *argcInOut
;
175 XrmParseCommand(&db
, xrmOnlyTable
, XtNumber(xrmOnlyTable
), appName
,
176 &argcCopy
, argvCopy
);
177 XtFree((char *)argvCopy
);
182 ** RestorePreferences
184 ** Fill in preferences data from two X databases, values in prefDB taking
185 ** precidence over those in appDB.
187 void RestorePreferences(XrmDatabase prefDB
, XrmDatabase appDB
,
188 const char *appName
, const char *appClass
, PrefDescripRec
*rsrcDescrip
, int nRsrc
)
190 readPrefs(prefDB
, appDB
, appName
, appClass
, rsrcDescrip
, nRsrc
, False
);
194 ** OverlayPreferences
196 ** Incorporate preference specified in database "prefDB", preserving (not
197 ** restoring to default) existing preferences, not mentioned in "prefDB"
199 void OverlayPreferences(XrmDatabase prefDB
, const char *appName
,
200 const char *appClass
, PrefDescripRec
*rsrcDescrip
, int nRsrc
)
202 readPrefs(NULL
, prefDB
, appName
, appClass
, rsrcDescrip
, nRsrc
, True
);
205 static void readPrefs(XrmDatabase prefDB
, XrmDatabase appDB
,
206 const char *appName
, const char *appClass
, PrefDescripRec
*rsrcDescrip
,
207 int nRsrc
, int overlay
)
209 char rsrcName
[256], rsrcClass
[256], *valueString
, *type
;
213 /* read each resource, trying first the preferences file database, then
214 the application database, then the default value if neither are found */
215 for (i
=0; i
<nRsrc
; i
++) {
216 sprintf(rsrcName
,"%s.%s", appName
, rsrcDescrip
[i
].name
);
217 sprintf(rsrcClass
, "%s.%s", appClass
, rsrcDescrip
[i
].class);
219 XrmGetResource(prefDB
, rsrcName
, rsrcClass
, &type
, &rsrcValue
)) {
220 if (strcmp(type
, XmRString
)) {
221 fprintf(stderr
,"nedit: Internal Error: Unexpected resource type, %s\n",
225 valueString
= rsrcValue
.addr
;
226 } else if (XrmGetResource(appDB
,rsrcName
,rsrcClass
,&type
,&rsrcValue
)) {
227 if (strcmp(type
, XmRString
)) {
228 fprintf(stderr
,"nedit: Internal Error: Unexpected resource type, %s\n",
232 valueString
= rsrcValue
.addr
;
234 valueString
= rsrcDescrip
[i
].defaultString
;
235 if (overlay
&& valueString
== rsrcDescrip
[i
].defaultString
)
237 if (!stringToPref(valueString
, &rsrcDescrip
[i
]))
238 fprintf(stderr
, "nedit: Could not read value of resource %s\n", rsrcName
);
243 ** RestoreDefaultPreferences
245 ** Restore preferences to their default values as stored in rsrcDesrcip
247 void RestoreDefaultPreferences(PrefDescripRec
*rsrcDescrip
, int nRsrc
)
251 for (i
=0; i
<nRsrc
; i
++)
252 stringToPref(rsrcDescrip
[i
].defaultString
, &rsrcDescrip
[i
]);
258 ** Create or replace an application preference file according to
259 ** the resource descriptions in rsrcDesrcip.
261 int SavePreferences(Display
*display
, const char *fullName
,
262 const char *fileHeader
, PrefDescripRec
*rsrcDescrip
, int nRsrc
)
264 char *appName
, *appClass
, **enumStrings
;
270 if ((fp
= fopen(fullName
, "w")) == NULL
)
273 /* write the file header text out to the file */
274 fprintf(fp
, "%s\n", fileHeader
);
276 /* write out the resources so they can be read by XrmGetFileDatabase */
277 XtGetApplicationNameAndClass(display
, &appName
, &appClass
);
278 for (i
=0; i
<nRsrc
; i
++) {
279 if (rsrcDescrip
[i
].save
) {
280 type
= rsrcDescrip
[i
].dataType
;
281 fprintf(fp
, "%s.%s: ", appName
, rsrcDescrip
[i
].name
);
282 if (type
== PREF_STRING
)
283 fprintf(fp
, "%s", (char *)rsrcDescrip
[i
].valueAddr
);
284 if (type
== PREF_ALLOC_STRING
)
285 fprintf(fp
, "%s", *(char **)rsrcDescrip
[i
].valueAddr
);
286 else if (type
== PREF_ENUM
) {
287 enumStrings
= (char **)rsrcDescrip
[i
].arg
;
288 fprintf(fp
,"%s", enumStrings
[*(int *)rsrcDescrip
[i
].valueAddr
]);
289 } else if (type
== PREF_INT
)
290 fprintf(fp
, "%d", *(int *)rsrcDescrip
[i
].valueAddr
);
291 else if (type
== PREF_BOOLEAN
) {
292 if (*(int *)rsrcDescrip
[i
].valueAddr
)
295 fprintf(fp
, "False");
304 static int stringToPref(const char *string
, PrefDescripRec
*rsrcDescrip
)
307 char *cleanStr
, *endPtr
, **enumStrings
;
309 switch (rsrcDescrip
->dataType
) {
311 cleanStr
= removeWhiteSpace(string
);
312 *(int *)rsrcDescrip
->valueAddr
=
313 strtol(cleanStr
, &endPtr
, 10);
314 if (strlen(cleanStr
) == 0) { /* String is empty */
315 *(int *)rsrcDescrip
->valueAddr
= 0;
318 } else if (*endPtr
!= '\0') { /* Whole string not parsed */
319 *(int *)rsrcDescrip
->valueAddr
= 0;
326 cleanStr
= removeWhiteSpace(string
);
327 for (i
=0; i
<N_BOOLEAN_STRINGS
; i
++) {
328 if (!strcmp(TrueStrings
[i
], cleanStr
)) {
329 *(int *)rsrcDescrip
->valueAddr
= True
;
333 if (!strcmp(FalseStrings
[i
], cleanStr
)) {
334 *(int *)rsrcDescrip
->valueAddr
= False
;
340 *(int *)rsrcDescrip
->valueAddr
= False
;
343 cleanStr
= removeWhiteSpace(string
);
344 enumStrings
= (char **)rsrcDescrip
->arg
;
345 for (i
=0; enumStrings
[i
]!=NULL
; i
++) {
346 if (!strcmp(enumStrings
[i
], cleanStr
)) {
347 *(int *)rsrcDescrip
->valueAddr
= i
;
353 *(int *)rsrcDescrip
->valueAddr
= 0;
356 if ((int)strlen(string
) >= (int)rsrcDescrip
->arg
)
358 strncpy(rsrcDescrip
->valueAddr
, string
, (int)rsrcDescrip
->arg
);
360 case PREF_ALLOC_STRING
:
361 *(char **)rsrcDescrip
->valueAddr
= XtMalloc(strlen(string
) + 1);
362 strcpy(*(char **)rsrcDescrip
->valueAddr
, string
);
369 ** Remove the white space (blanks and tabs) from a string and return
370 ** the result in a newly allocated string as the function value
372 static char *removeWhiteSpace(const char *string
)
374 char *outPtr
, *outString
;
376 outPtr
= outString
= XtMalloc(strlen(string
)+1);
378 if (*string
!= ' ' && *string
!= '\t')
379 *(outPtr
++) = *(string
++);
391 Q: Why aren't you using the Xt type conversion services?
392 A: 1) To create a save file, you also need to convert values back to text form,
393 and there are no converters for that direction. 2) XtGetApplicationResources
394 can only be used on the resource database created by the X toolkit at
395 initialization time, and there is no way to intervene in the creation of
396 that database or store new resources in it reliably after it is created.
397 3) The alternative, XtConvertAndStore is not adequately documented. The
398 toolkit mauual does not explain why it overwrites its input value structure.
399 4) XtGetApplicationResources and XtConvertAndStore do not work well together
400 because they use different storage strategies for certain data types.