File: preferences.cxx

package info (click to toggle)
curves 0.8.7
  • links: PTS
  • area: main
  • in suites: slink
  • size: 704 kB
  • ctags: 1,001
  • sloc: cpp: 6,197; ansic: 519; makefile: 270; sh: 192; fortran: 149
file content (331 lines) | stat: -rw-r--r-- 8,651 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* preferences.cxx
     $Id: preferences.cxx,v 1.10 1998/09/10 20:50:50 elf Exp $

   written by Marc Singer
   27 October 1996

   This file is part of the project CurVeS.  See the file README for
   more information.

   Copyright (C) 1996 Marc Singer

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   in a file called COPYING along with this program; if not, write to
   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
   02139, USA.

   -----------
   DESCRIPTION
   -----------

   Preference options management class.  It handles queries and
   updates.  Essentially, this is a simple database class with keys
   and data.  Keys and data are both strings and we make no provisions
   for typing them.  We may choose to include methods to interpret the
   assumed data type, but this may NOT be done.


   --------------------
   IMPLEMENTATION NOTES
   --------------------

   -- Preferences File Syntax

   The file is line oriented.  Preferences are <keyword> = <data>
   pairs.  Whitespace around the keyword and the data are ignored.
   Comments lines start with '#' and all text on a line from the
   comment character to the end-of-line are ignored.  Blank lines are
   ignored, too.

   -- Set Representation

   We start with a VERY simple implementation for key/data matching.
   Presently it is a flat-file mapping.  We may choose to go to a
   hierarchy and to some sort of balanced tree such as AVL.

*/

#include "std.h"
#include <sys/stat.h>

#include "mman.h"
#include "preferences.h"
#include "path.h"

LPreferences g_preferences;	// Instantiate global preference object


/* LPreferences::as_*

   these functions return the preference data typed.  This is for the
   convenience of the user and the programmer.  For users, they can
   specify t, true, yes, y, or Y for boolean truth.  For programmers,
   they can make one call to determine the truth of the option.

*/

bool LPreferences::as_bool (const char* szKeyword)
{
  const char* szValue = fetch (szKeyword);
  if (!szValue || !*szValue)
    return false;

  switch (tolower (*szValue)) {
  case '1':
    if (szValue[1] == 0)
      return true;
    break;
  case '0':
    if (szValue[1] == 0)
      return false;
    break;
  case 'y':
    if (szValue[1] == 0
	|| strcasecmp (szValue, "yes") == 0
	|| strcasecmp (szValue, "yep") == 0)
      return true;
    break;
  case 't':
    if (szValue[1] == 0 || strcasecmp (szValue, "true") == 0)
      return true;
  case 'n':
    if (szValue[1] == 0
	|| strcasecmp (szValue, "no") == 0
	|| strcasecmp (szValue, "nope") == 0)
      return false;
    break;
  case 'f':
    if (szValue[1] == 0 || strcasecmp (szValue, "false") == 0)
      return false;
    break;
  }
  TRACE((T_PREFS_ERR, "unrecognized preference value %s='%s'",
     szKeyword, szValue));
  return false;
}

int LPreferences::as_int (const char* szKeyword)
{
  const char* szValue = fetch (szKeyword);
  int value = strtol (szValue, (char**) NULL, 0);
  return value;
}


/* LPreferences::associate

   matches a keyword to a value.  This is the user version that
   allocates space for copies of the strings if either is new.

*/

void LPreferences::associate (const char* szKeyword, const char* szValue)
{
  LPreferenceElement* pElement = _find (szKeyword);
  if (!pElement) {
    pElement = (LPreferenceElement*) malloc (sizeof (LPreferenceElement));
    memset (pElement, 0, sizeof (*pElement));
    pElement->pNext = m_pElements;
    m_pElements = pElement;
  }
  assert_ (pElement);
  if (!pElement->szKeyword) {
    char* sz = (char*) malloc (strlen (szKeyword) + 1);
    strcpy (sz, szKeyword);
    pElement->szKeyword = sz;
  }
  if (pElement->szValue)
    free (pElement->szValue);
  char* sz = (char*) malloc (strlen (szValue) + 1);
  strcpy (sz, szValue);
  pElement->szValue = sz;
}

void LPreferences::associate (const char* szKeyword, int value)
{
  char szValue[20];
  sprintf (szValue, "%d", value);
  associate (szKeyword, szValue);
}


/* LPreferences::associate

   matches a keyword to a value.  This is the internet version that
   assumes that space was already allocated for the keyword and value
   strings. 

*/

void LPreferences::_associate (char* szKeyword, char* szValue)
{
  LPreferenceElement* pElement = _find (szKeyword);
  if (!pElement) {
    pElement = (LPreferenceElement*) malloc (sizeof (LPreferenceElement));
    memset (pElement, 0, sizeof (*pElement));
    pElement->pNext = m_pElements;
    m_pElements = pElement;
  }
  assert_ (pElement);
  if (!pElement->szKeyword)
    pElement->szKeyword = szKeyword;
  else
    free (szKeyword);
  if (pElement->szValue)
    free (pElement->szValue);
  pElement->szValue = szValue;
				// Mitigation, won't be good, but it'll work
  if (strlen (szValue) > CB_PREFERENCE_MAX) {
    TRACE((T_PREFS_ERR, "truncation too long preference %s='%s'",
	   pElement->szKeyword, szValue));
    szValue[CB_PREFERENCE_MAX] = 0;
  }
}


const char* LPreferences::fetch (const char* szKeyword)
{
  for (LPreferenceElement* pElement = m_pElements; pElement; 
       pElement = pElement->pNext)
    if (strcasecmp (pElement->szKeyword, szKeyword) == 0)
      return pElement->szValue ? pElement->szValue : pElement->szValueDefault;
  return NULL;
}


LPreferenceElement* LPreferences::_find (const char* szKeyword)
{
  for (LPreferenceElement* pElement = m_pElements; pElement; 
       pElement = pElement->pNext)
    if (strcasecmp (pElement->szKeyword, szKeyword) == 0)
      return pElement;
  
  return NULL;
}
    

/* LPreferences::grab

   strips the whitespace that surrounds the keyword or data item.  It
   allocates a bit of memory for the string and returns a copy of it.

*/

char* LPreferences::grab (char* pb, int cch)
{
  while (cch && isspace (*pb))
    ++pb, --cch;		// Yeah, and I meant it.
  while (cch && isspace (pb[cch - 1]))
    --cch;

  char* sz = (char*) malloc (cch + 1);
  memcpy (sz, pb, cch);
  sz[cch] = 0;
  return sz;
}


void LPreferences::read (const char* szPath)
{
  LPath path (szPath);

  int fh = -1;
  char* pbMap = NULL;
  struct stat stat;
  if (!::stat (path, &stat) && stat.st_size
      && ((fh = open (path, O_RDONLY)) != -1)
      && (pbMap = (char*) mmap (NULL, stat.st_size + 1, PROT_READ, 
				MAP_FILE | MAP_PRIVATE, fh, 0))) {
    //    pbMap[stat.st_size] = 0;
    int iLine = 1;
    char* szKeyword = NULL;
    char* szValue = NULL;
    int state = 0;
    int cch;
    for (char* pb = pbMap; pb < pbMap + stat.st_size; pb += cch) {
      if (*pb == '\n') {
	++iLine;
	++pb;
      }
      cch = strcspn (pb, "\n=#");
      if (pb + cch > pbMap + stat.st_size)
	cch = pbMap + stat.st_size - pb;

      switch (state) {
      case 0:			// Looking for keyword
	if (pb[cch] != '=') {
	  cch += strcspn (pb + cch, "\n");
	  break;
	}
	szKeyword = grab (pb, cch);
	state = 1;
	++cch;			// Skip '='
	break;

      case 1:			// Looking for value
	szValue = grab (pb, cch);
	_associate (szKeyword, szValue);
	szKeyword = szValue = NULL;
	cch += strcspn (pb + cch, "\n");
	state = 0;
	break;
      }
    }      
  }
  if (pbMap)
    munmap (pbMap, stat.st_size);
  if (fh != -1)
    close (fh);
}


void LPreferences::release_entries (void)
{
  for (LPreferenceElement* pElement = m_pElements; pElement;
       pElement = m_pElements) {
    m_pElements = pElement->pNext;
    if (pElement->szKeyword)
      free (pElement->szKeyword);
    if (pElement->szValue)
      free (pElement->szValue);
    if (pElement->szValueDefault)
      free (pElement->szValueDefault);
    free (pElement);
  }
}

void LPreferences::release_settings (void)
{
  for (LPreferenceElement* pElement = m_pElements; pElement;
       pElement = pElement->pNext)
    if (pElement->szValue) {
      free (pElement->szValue);
      pElement->szValue = NULL;
    }
}

void LPreferences::release_this (void)
{
  release_entries ();
}

void LPreferences::transfer_defaults (void)
{
  for (LPreferenceElement* pElement = m_pElements; pElement;
       pElement = pElement->pNext) {
    if (pElement->szValueDefault)
      free (pElement->szValueDefault);
    pElement->szValueDefault = pElement->szValue;
    pElement->szValue = NULL;
  }
}