Fix our use of $em_tab_dist after it was changed to 0 for 'turned of'.
[nedit.git] / util / prefFile.c
blobbd5025c00a5d06d762a620397b9fba2f16e85867
1 static const char CVSID[] = "$Id: prefFile.c,v 1.27 2008/08/20 14:57:36 lebert 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. In addition, you may distribute version of this program linked to *
12 * Motif or Open Motif. See README for details. *
13 * *
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 *
17 * for more details. *
18 * *
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 *
22 * *
23 * Nirvana Text Editor *
24 * June 3, 1993 *
25 * *
26 * Written by Mark Edel *
27 * *
28 *******************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "../config.h"
32 #endif
34 #include "prefFile.h"
35 #include "fileUtils.h"
36 #include "utils.h"
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #ifdef VMS
42 #include "VMSparam.h"
43 #else
44 #ifndef __MVS__
45 #include <sys/param.h>
46 #endif
47 #endif
48 #include <Xm/Xm.h>
50 #ifdef HAVE_DEBUG_H
51 #include "../debug.h"
52 #endif
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);
67 ** Preferences File
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
116 ** resources
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,
129 char **argvInOut)
131 XrmDatabase db;
132 int argcCopy;
133 char **argvCopy;
134 char *fileString;
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)
142 db = NULL;
143 } else
145 fileString = ReadAnyTextFile(fullName, False);
146 if (NULL == fileString)
148 db = NULL;
149 } else
151 char* rsrcName;
152 db = XrmGetStringDatabase(fileString);
153 XtFree(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");
160 XtFree(rsrcName);
164 /* parse the command line, storing results in the preferences database */
165 XrmParseCommand(&db, opTable, nOptions, appName, (int *)argcInOut,
166 argvInOut);
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);
178 return db;
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;
210 XrmValue rsrcValue;
211 int i;
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);
218 if (prefDB!=NULL &&
219 XrmGetResource(prefDB, rsrcName, rsrcClass, &type, &rsrcValue)) {
220 if (strcmp(type, XmRString)) {
221 fprintf(stderr,"nedit: Internal Error: Unexpected resource type, %s\n",
222 type);
223 return;
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",
229 type);
230 return;
232 valueString = rsrcValue.addr;
233 } else
234 valueString = rsrcDescrip[i].defaultString;
235 if (overlay && valueString == rsrcDescrip[i].defaultString)
236 continue;
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)
249 int i;
251 for (i=0; i<nRsrc; i++)
252 stringToPref(rsrcDescrip[i].defaultString, &rsrcDescrip[i]);
256 ** SavePreferences
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;
265 FILE *fp;
266 int type;
267 int i;
269 /* open the file */
270 if ((fp = fopen(fullName, "w")) == NULL)
271 return False;
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)
293 fprintf(fp, "True");
294 else
295 fprintf(fp, "False");
297 fprintf(fp, "\n");
300 fclose(fp);
301 return True;
304 static int stringToPref(const char *string, PrefDescripRec *rsrcDescrip)
306 int i;
307 char *cleanStr, *endPtr, **enumStrings;
309 switch (rsrcDescrip->dataType) {
310 case PREF_INT:
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;
316 XtFree(cleanStr);
317 return False;
318 } else if (*endPtr != '\0') { /* Whole string not parsed */
319 *(int *)rsrcDescrip->valueAddr = 0;
320 XtFree(cleanStr);
321 return False;
323 XtFree(cleanStr);
324 return True;
325 case PREF_BOOLEAN:
326 cleanStr = removeWhiteSpace(string);
327 for (i=0; i<N_BOOLEAN_STRINGS; i++) {
328 if (!strcmp(TrueStrings[i], cleanStr)) {
329 *(int *)rsrcDescrip->valueAddr = True;
330 XtFree(cleanStr);
331 return True;
333 if (!strcmp(FalseStrings[i], cleanStr)) {
334 *(int *)rsrcDescrip->valueAddr = False;
335 XtFree(cleanStr);
336 return True;
339 XtFree(cleanStr);
340 *(int *)rsrcDescrip->valueAddr = False;
341 return False;
342 case PREF_ENUM:
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;
348 XtFree(cleanStr);
349 return True;
352 XtFree(cleanStr);
353 *(int *)rsrcDescrip->valueAddr = 0;
354 return False;
355 case PREF_STRING:
356 if ((int)strlen(string) >= (int)rsrcDescrip->arg)
357 return False;
358 strncpy(rsrcDescrip->valueAddr, string, (int)rsrcDescrip->arg);
359 return True;
360 case PREF_ALLOC_STRING:
361 *(char **)rsrcDescrip->valueAddr = XtMalloc(strlen(string) + 1);
362 strcpy(*(char **)rsrcDescrip->valueAddr, string);
363 return True;
365 return False;
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);
377 while (TRUE) {
378 if (*string != ' ' && *string != '\t')
379 *(outPtr++) = *(string++);
380 else
381 string++;
382 if (*string == 0) {
383 *outPtr = 0;
384 return outString;
389 /*******************
390 Implementation Note:
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.
401 *******************/