~ubuntu-branches/ubuntu/maverick/glbsp/maverick

« back to all changes in this revision

Viewing changes to gui/cookie.cc

  • Committer: Bazaar Package Importer
  • Author(s): Darren Salt
  • Date: 2008-01-30 13:33:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080130133349-kgojg33vyiu8xbvp
Tags: 2.24-1
* New upstream release.
* Bumped the lib soname and the library package name due to one silly
  little binary incompatibility caused by changes in an exported struct.
  (Safe; nothing else currently in the archive has ever used libglbsp2.)
* Removed my patches since they're all applied upstream.
* Updated the list of documentation files.
* Build-time changes:
  - Switched from dh_movefiles to dh_install.
  - Updated my makefile to cope with upstream changes.
  - Corrected for debian-rules-ignores-make-clean-error.
  - Corrected for substvar-source-version-is-deprecated.
  - Link libglbsp, rather than glbsp, with libm and libz.
* Fixed shlibdeps. (Closes: #460387)
* Bumped standards version to 3.7.3 (no other changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//------------------------------------------------------------------------
 
2
// COOKIE : Unix/FLTK Persistence (INI/RC files)
 
3
//------------------------------------------------------------------------
 
4
//
 
5
//  GL-Friendly Node Builder (C) 2000-2007 Andrew Apted
 
6
//
 
7
//  Based on 'BSP 2.3' by Colin Reed, Lee Killough and others.
 
8
//
 
9
//  This program is free software; you can redistribute it and/or
 
10
//  modify it under the terms of the GNU General Public License
 
11
//  as published by the Free Software Foundation; either version 2
 
12
//  of the License, or (at your option) any later version.
 
13
//
 
14
//  This program is distributed in the hope that it will be useful,
 
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
//  GNU General Public License for more details.
 
18
//
 
19
//------------------------------------------------------------------------
 
20
 
 
21
// this includes everything we need
 
22
#include "local.h"
 
23
 
 
24
 
 
25
#define DEBUG_COOKIE  0
 
26
 
 
27
 
 
28
static const char *cookie_filename;
 
29
 
 
30
// current section parser func
 
31
static boolean_g (* cookie_section_parser)
 
32
    (const char *name, const char *value) = NULL;
 
33
 
 
34
 
 
35
static void CookieDebug(const char *str, ...)
 
36
{
 
37
#if DEBUG_COOKIE
 
38
  char buffer[2048];
 
39
  va_list args;
 
40
 
 
41
  va_start(args, str);
 
42
  vsprintf(buffer, str, args);
 
43
  va_end(args);
 
44
 
 
45
  fprintf(stderr, "COOKIE: %s", buffer);
 
46
#else
 
47
  (void) str;
 
48
#endif
 
49
}
 
50
 
 
51
static boolean_g SetBooleanVar(boolean_g& var, const char *value)
 
52
{
 
53
  if (strchr(value, '0'))
 
54
  {
 
55
    var = FALSE;
 
56
    return TRUE;
 
57
  }
 
58
 
 
59
  if (strchr(value, '1'))
 
60
  {
 
61
    var = TRUE;
 
62
    return TRUE;
 
63
  }
 
64
  
 
65
  // bad boolean value
 
66
  CookieDebug("Bad Boolean [%s]\n", value);
 
67
  return FALSE;
 
68
}
 
69
 
 
70
static boolean_g SetIntegerVar(int& var, const char *value)
 
71
{
 
72
  var = strtol(value, NULL, 10);
 
73
 
 
74
  return TRUE;
 
75
}
 
76
 
 
77
static boolean_g SetStringVar(const char *& var, const char *value)
 
78
{
 
79
  int len = strlen(value);
 
80
  char buffer[1024];
 
81
  
 
82
  if (value[0] != '"' || len < 2 || value[len-1] != '"' || len > 1020)
 
83
  {
 
84
    CookieDebug("Bad String [%s]\n", value);
 
85
    return FALSE;
 
86
  }
 
87
 
 
88
  strncpy(buffer, value+1, len-2);
 
89
  buffer[len-2] = 0;
 
90
 
 
91
  GlbspFree(var);
 
92
  var = GlbspStrDup(buffer);
 
93
 
 
94
  return TRUE;
 
95
}
 
96
 
 
97
 
 
98
//------------------------------------------------------------------------
 
99
 
 
100
 
 
101
static boolean_g CookieSetBuildVar(const char *name, const char *value)
 
102
{
 
103
  // file names and factor
 
104
  if (strcasecmp(name, "in_file") == 0)
 
105
    return SetStringVar(guix_info.input_file, value);
 
106
 
 
107
  if (strcasecmp(name, "out_file") == 0)
 
108
    return SetStringVar(guix_info.output_file, value);
 
109
 
 
110
  if (strcasecmp(name, "factor") == 0)
 
111
    return SetIntegerVar(guix_info.factor, value);
 
112
 
 
113
 
 
114
  // boolean values
 
115
  if (strcasecmp(name, "no_reject") == 0)
 
116
    return SetBooleanVar(guix_info.no_reject, value);
 
117
 
 
118
  if (strcasecmp(name, "mini_warnings") == 0)
 
119
    return SetBooleanVar(guix_info.mini_warnings, value);
 
120
 
 
121
  if (strcasecmp(name, "pack_sides") == 0)
 
122
    return SetBooleanVar(guix_info.pack_sides, value);
 
123
 
 
124
  // API change to main glbsp code: 'choose_fresh' --> 'fast'.
 
125
  if (strcasecmp(name, "choose_fresh") == 0)
 
126
  {
 
127
    if (! SetBooleanVar(guix_info.fast, value))
 
128
      return FALSE;
 
129
 
 
130
    guix_info.fast = ! guix_info.fast;
 
131
    return TRUE;
 
132
  }
 
133
 
 
134
 
 
135
  // block limit
 
136
  if (strcasecmp(name, "block_limit") == 0)
 
137
    return SetIntegerVar(guix_info.block_limit, value);
 
138
 
 
139
 
 
140
  // build mode
 
141
  if (strcasecmp(name, "gwa_mode") == 0)
 
142
    return SetBooleanVar(guix_info.gwa_mode, value);
 
143
 
 
144
  if (strcasecmp(name, "no_normal") == 0)
 
145
    return SetBooleanVar(guix_info.no_normal, value);
 
146
 
 
147
  if (strcasecmp(name, "force_normal") == 0)
 
148
    return SetBooleanVar(guix_info.force_normal, value);
 
149
 
 
150
 
 
151
  // Unknown build variable !
 
152
  CookieDebug("Unknown Build VAR [%s]\n", name);
 
153
  return FALSE;
 
154
}
 
155
 
 
156
static boolean_g CookieSetPrefVar(const char *name, const char *value)
 
157
{
 
158
  // overwrite warning
 
159
  if (strcasecmp(name, "overwrite_warn") == 0)
 
160
    return SetBooleanVar(guix_prefs.overwrite_warn, value);
 
161
 
 
162
  // same file warning
 
163
  if (strcasecmp(name, "same_file_warn") == 0)
 
164
    return SetBooleanVar(guix_prefs.same_file_warn, value);
 
165
 
 
166
  // missing extension warning
 
167
  if (strcasecmp(name, "lack_ext_warn") == 0)
 
168
    return SetBooleanVar(guix_prefs.lack_ext_warn, value);
 
169
 
 
170
  // save log filename
 
171
  if (strcasecmp(name, "save_log_file") == 0)
 
172
    return SetStringVar(guix_prefs.save_log_file, value);
 
173
 
 
174
 
 
175
  // Unknown preference variable !
 
176
  CookieDebug("Unknown Pref VAR [%s]\n", name);
 
177
  return FALSE;
 
178
}
 
179
 
 
180
static boolean_g CookieSetWinPosVar(const char *name, const char *value)
 
181
{
 
182
  // main window position and size
 
183
  if (strcasecmp(name, "window_x") == 0)
 
184
    return SetIntegerVar(guix_prefs.win_x, value);
 
185
 
 
186
  if (strcasecmp(name, "window_y") == 0)
 
187
    return SetIntegerVar(guix_prefs.win_y, value);
 
188
 
 
189
  if (strcasecmp(name, "window_w") == 0)
 
190
    return SetIntegerVar(guix_prefs.win_w, value);
 
191
 
 
192
  if (strcasecmp(name, "window_h") == 0)
 
193
    return SetIntegerVar(guix_prefs.win_h, value);
 
194
 
 
195
 
 
196
  // other window positions
 
197
  if (strcasecmp(name, "progress_x") == 0)
 
198
    return SetIntegerVar(guix_prefs.progress_x, value);
 
199
 
 
200
  if (strcasecmp(name, "progress_y") == 0)
 
201
    return SetIntegerVar(guix_prefs.progress_y, value);
 
202
 
 
203
  if (strcasecmp(name, "dialog_x") == 0)
 
204
    return SetIntegerVar(guix_prefs.dialog_x, value);
 
205
 
 
206
  if (strcasecmp(name, "dialog_y") == 0)
 
207
    return SetIntegerVar(guix_prefs.dialog_y, value);
 
208
 
 
209
  if (strcasecmp(name, "other_x") == 0)
 
210
    return SetIntegerVar(guix_prefs.other_x, value);
 
211
 
 
212
  if (strcasecmp(name, "other_y") == 0)
 
213
    return SetIntegerVar(guix_prefs.other_y, value);
 
214
 
 
215
 
 
216
  // manual window positions
 
217
  if (strcasecmp(name, "manual_x") == 0)
 
218
    return SetIntegerVar(guix_prefs.manual_x, value);
 
219
 
 
220
  if (strcasecmp(name, "manual_y") == 0)
 
221
    return SetIntegerVar(guix_prefs.manual_y, value);
 
222
 
 
223
  if (strcasecmp(name, "manual_w") == 0)
 
224
    return SetIntegerVar(guix_prefs.manual_w, value);
 
225
 
 
226
  if (strcasecmp(name, "manual_h") == 0)
 
227
    return SetIntegerVar(guix_prefs.manual_h, value);
 
228
 
 
229
  if (strcasecmp(name, "manual_page") == 0)
 
230
    return SetIntegerVar(guix_prefs.manual_page, value);
 
231
 
 
232
 
 
233
  // Unknown window pos variable !
 
234
  CookieDebug("Unknown WindowPos VAR [%s]\n", name);
 
235
  return FALSE;
 
236
}
 
237
 
 
238
 
 
239
//------------------------------------------------------------------------
 
240
 
 
241
 
 
242
// returns TRUE if line parsed OK.  Note: modifies the buffer.
 
243
static boolean_g CookieParseLine(char *buf)
 
244
{
 
245
  char *name;
 
246
  int len;
 
247
 
 
248
  while (isspace(*buf))
 
249
    buf++;
 
250
 
 
251
  // blank line ?
 
252
  if (*buf == 0)
 
253
    return TRUE;
 
254
 
 
255
  // remove trailing whitespace
 
256
  len = strlen(buf);
 
257
 
 
258
  while (len > 0 && isspace(buf[len-1]))
 
259
    buf[--len] = 0;
 
260
 
 
261
  CookieDebug("PRE-PARSE: '%s'\n", buf);
 
262
 
 
263
  // comment ?
 
264
  if (*buf == '#')
 
265
    return TRUE;
 
266
 
 
267
  // section ?
 
268
  if (*buf == '[')
 
269
  {
 
270
    if (strncasecmp(buf+1, "BUILD_INFO]", 11) == 0)
 
271
    {
 
272
      cookie_section_parser = CookieSetBuildVar;
 
273
      return TRUE;
 
274
    }
 
275
 
 
276
    if (strncasecmp(buf+1, "PREFERENCES]", 12) == 0)
 
277
    {
 
278
      cookie_section_parser = CookieSetPrefVar;
 
279
      return TRUE;
 
280
    }
 
281
 
 
282
    if (strncasecmp(buf+1, "WINDOW_POS]", 11) == 0)
 
283
    {
 
284
      cookie_section_parser = CookieSetWinPosVar;
 
285
      return TRUE;
 
286
    }
 
287
 
 
288
    // unknown section !
 
289
    cookie_section_parser = NULL;
 
290
    CookieDebug("Unknown Section: %s\n", buf);
 
291
    return FALSE;
 
292
  }
 
293
 
 
294
  if (! isalpha(*buf))
 
295
    return FALSE;
 
296
 
 
297
  // Righteo, line starts with an identifier.  It should be of the
 
298
  // form "name = value".  We'll terminate the identifier, and pass
 
299
  // the name/value strings to a section-dependent handler.
 
300
 
 
301
  name = buf;
 
302
  buf++;
 
303
 
 
304
  for (buf++; isalpha(*buf) || *buf == '_'; buf++)
 
305
  { /* nothing here */ }
 
306
 
 
307
  while (isspace(*buf))
 
308
    *buf++ = 0;
 
309
  
 
310
  if (*buf != '=')
 
311
    return FALSE;
 
312
 
 
313
  *buf++ = 0;
 
314
 
 
315
  while (isspace(*buf))
 
316
    buf++;
 
317
 
 
318
  if (*buf == 0)
 
319
    return FALSE;
 
320
 
 
321
  CookieDebug("Name: [%s]  Value: [%s]\n", name, buf);
 
322
 
 
323
  if (cookie_section_parser)
 
324
  {
 
325
    return (* cookie_section_parser)(name, buf);
 
326
  }
 
327
 
 
328
  // variables were found outside of any section
 
329
  return FALSE;
 
330
}
 
331
 
 
332
//
 
333
// CookieCheckEm
 
334
//
 
335
// Increments 'problems' each time something is fixed up.
 
336
//
 
337
void CookieCheckEm(int& problems)
 
338
{
 
339
  // check main window size
 
340
  if (guix_prefs.win_w < MAIN_WINDOW_MIN_W)
 
341
  {
 
342
    guix_prefs.win_w = MAIN_WINDOW_MIN_W;
 
343
    problems++;
 
344
  }
 
345
 
 
346
  if (guix_prefs.win_h < MAIN_WINDOW_MIN_H)
 
347
  {
 
348
    guix_prefs.win_h = MAIN_WINDOW_MIN_H;
 
349
    problems++;
 
350
  }
 
351
 
 
352
  // check manual/license window size
 
353
  if (guix_prefs.manual_w < MANUAL_WINDOW_MIN_W)
 
354
  {
 
355
    guix_prefs.manual_w = MANUAL_WINDOW_MIN_W;
 
356
    problems++;
 
357
  }
 
358
 
 
359
  if (guix_prefs.manual_h < MANUAL_WINDOW_MIN_H)
 
360
  {
 
361
    guix_prefs.manual_h = MANUAL_WINDOW_MIN_H;
 
362
    problems++;
 
363
  }
 
364
 
 
365
  if (guix_info.factor > 32)
 
366
  {
 
367
    guix_info.factor = 32;
 
368
    problems++;
 
369
  }
 
370
}
 
371
 
 
372
//
 
373
// CookieSetPath
 
374
//
 
375
// Determine the path and filename of the cookie file.
 
376
//
 
377
void CookieSetPath(const char *argv0)
 
378
{
 
379
  if (cookie_filename)
 
380
    GlbspFree(cookie_filename);
 
381
 
 
382
  char buffer[1024];
 
383
 
 
384
  buffer[0] = 0;
 
385
 
 
386
#if defined(WIN32)
 
387
 
 
388
  if (HelperFilenameValid(argv0))
 
389
  {
 
390
    strcpy(buffer, HelperReplaceExt(argv0, "ini"));
 
391
  }
 
392
  else
 
393
    strcpy(buffer, "glBSPX.ini");
 
394
 
 
395
#elif defined(MACOSX)
 
396
 
 
397
  if (getenv("HOME"))
 
398
  {
 
399
    strcpy(buffer, getenv("HOME"));
 
400
    strcat(buffer, "/Library/Preferences/");
 
401
  }
 
402
 
 
403
  strcat(buffer, "glBSPX.rc");
 
404
 
 
405
#else  // LINUX
 
406
 
 
407
  if (getenv("HOME"))
 
408
  {
 
409
    strcpy(buffer, getenv("HOME"));
 
410
    strcat(buffer, "/");
 
411
  }
 
412
 
 
413
  // backwards compatibility (glbspX != glBSPX)
 
414
 
 
415
  strcat(buffer, ".glbspX_rc");
 
416
 
 
417
  if (! HelperFileExists(buffer))
 
418
  {
 
419
    char *bsp = strrchr(buffer, 'b');
 
420
 
 
421
    if (bsp) strncpy(bsp, "BSP", 3);
 
422
  }
 
423
#endif
 
424
 
 
425
  cookie_filename = GlbspStrDup(buffer);
 
426
 
 
427
  CookieDebug("CookieFilename: '%s'\n", cookie_filename);
 
428
}
 
429
 
 
430
//
 
431
// CookieReadAll
 
432
//
 
433
// Reads the cookie file.
 
434
// Returns one of the COOKIE_E_* values.
 
435
// 
 
436
// All the variables that can be read here should be initialised to
 
437
// default values before calling, in case we fail or the variable is
 
438
// missing in the cookie file.
 
439
// 
 
440
cookie_status_t CookieReadAll(void)
 
441
{
 
442
  CookieDebug("CookieReadAll BEGUN.\n");
 
443
 
 
444
  // open file for reading
 
445
  FILE *fp = fopen(cookie_filename, "r");
 
446
 
 
447
  if (! fp)
 
448
    return COOKIE_E_NO_FILE;
 
449
 
 
450
  cookie_section_parser = NULL;
 
451
 
 
452
  // simple line-by-line parser
 
453
  char buffer[2048];
 
454
 
 
455
  int parse_probs = 0;
 
456
  int check_probs = 0;
 
457
 
 
458
  while (fgets(buffer, sizeof(buffer) - 2, fp))
 
459
  {
 
460
    if (! CookieParseLine(buffer))
 
461
      parse_probs += 1;
 
462
  }
 
463
 
 
464
  CookieCheckEm(check_probs);
 
465
 
 
466
  CookieDebug("CookieReadAll DONE.  %d ParseProbs  %d CheckProbs\n", 
 
467
      parse_probs, check_probs);
 
468
 
 
469
  // parsing problems take precedence
 
470
  if (parse_probs > 0)
 
471
    return COOKIE_E_PARSE_ERRORS;
 
472
 
 
473
  if (check_probs > 0)
 
474
    return COOKIE_E_CHECK_ERRORS;
 
475
 
 
476
  return COOKIE_E_OK;
 
477
}
 
478
 
 
479
 
 
480
//------------------------------------------------------------------------
 
481
 
 
482
 
 
483
static void CookieWriteHeader(FILE *fp)
 
484
{
 
485
  fprintf(fp, "# GLBSPX %s Persistent Data\n", GLBSP_VER);
 
486
  fprintf(fp, "# Editing this file by hand is not recommended.\n");
 
487
 
 
488
  fprintf(fp, "\n");
 
489
  fflush(fp);
 
490
}
 
491
 
 
492
static void CookieWriteBuildInfo(FILE *fp)
 
493
{
 
494
  // identifying section
 
495
  fprintf(fp, "[BUILD_INFO]\n");
 
496
 
 
497
  // file names
 
498
  fprintf(fp, "in_file = \"%s\"\n", guix_info.input_file ?
 
499
      guix_info.input_file : "");
 
500
 
 
501
  fprintf(fp, "out_file = \"%s\"\n", guix_info.output_file ?
 
502
      guix_info.output_file : "");
 
503
 
 
504
  // factor
 
505
  fprintf(fp, "factor = %d\n", guix_info.factor);
 
506
 
 
507
  // boolean values
 
508
  fprintf(fp, "no_reject = %d\n", guix_info.no_reject ? 1 : 0); 
 
509
  fprintf(fp, "mini_warnings = %d\n", guix_info.mini_warnings ? 1 : 0); 
 
510
  fprintf(fp, "pack_sides = %d\n", guix_info.pack_sides ? 1 : 0); 
 
511
  fprintf(fp, "choose_fresh = %d\n", guix_info.fast ? 0 : 1);  // API change
 
512
 
 
513
  // block limit
 
514
  fprintf(fp, "block_limit = %d\n", guix_info.block_limit); 
 
515
 
 
516
  // build-mode
 
517
  fprintf(fp, "gwa_mode = %d\n", guix_info.gwa_mode ? 1 : 0);
 
518
  fprintf(fp, "no_normal = %d\n", guix_info.no_normal ? 1 : 0);
 
519
  fprintf(fp, "force_normal = %d\n", guix_info.force_normal ? 1 : 0);
 
520
 
 
521
  // NOT HERE:
 
522
  //   no_progress: not useful for GUI mode.
 
523
  //   keep_sect, no_prune: not used in GUI mode (yet).
 
524
  //   load_all: determined automatically.
 
525
 
 
526
  // keep a blank line between sections
 
527
  fprintf(fp, "\n");
 
528
  fflush(fp);
 
529
}
 
530
 
 
531
static void CookieWritePrefs(FILE *fp)
 
532
{
 
533
  // identifying section
 
534
  fprintf(fp, "[PREFERENCES]\n");
 
535
  
 
536
  fprintf(fp, "overwrite_warn = %d\n", guix_prefs.overwrite_warn);
 
537
  fprintf(fp, "same_file_warn = %d\n", guix_prefs.same_file_warn);
 
538
  fprintf(fp, "lack_ext_warn = %d\n", guix_prefs.lack_ext_warn);
 
539
 
 
540
  fprintf(fp, "save_log_file = \"%s\"\n", guix_prefs.save_log_file ?
 
541
      guix_prefs.save_log_file : "");
 
542
 
 
543
  fprintf(fp, "\n");
 
544
  fflush(fp);
 
545
}
 
546
 
 
547
static void CookieWriteWindowPos(FILE *fp)
 
548
{
 
549
  // identifying section
 
550
  fprintf(fp, "[WINDOW_POS]\n");
 
551
 
 
552
  // main window position and size
 
553
  fprintf(fp, "window_x = %d\n", guix_prefs.win_x);
 
554
  fprintf(fp, "window_y = %d\n", guix_prefs.win_y);
 
555
  fprintf(fp, "window_w = %d\n", guix_prefs.win_w);
 
556
  fprintf(fp, "window_h = %d\n", guix_prefs.win_h);
 
557
 
 
558
  // other window positions
 
559
  fprintf(fp, "progress_x = %d\n", guix_prefs.progress_x);
 
560
  fprintf(fp, "progress_y = %d\n", guix_prefs.progress_y);
 
561
  fprintf(fp, "dialog_x = %d\n", guix_prefs.dialog_x);
 
562
  fprintf(fp, "dialog_y = %d\n", guix_prefs.dialog_y);
 
563
  fprintf(fp, "other_x = %d\n", guix_prefs.other_x);
 
564
  fprintf(fp, "other_y = %d\n", guix_prefs.other_y);
 
565
 
 
566
  fprintf(fp, "manual_x = %d\n", guix_prefs.manual_x);
 
567
  fprintf(fp, "manual_y = %d\n", guix_prefs.manual_y);
 
568
  fprintf(fp, "manual_w = %d\n", guix_prefs.manual_w);
 
569
  fprintf(fp, "manual_h = %d\n", guix_prefs.manual_h);
 
570
  fprintf(fp, "manual_page = %d\n", guix_prefs.manual_page);
 
571
 
 
572
  fprintf(fp, "\n");
 
573
  fflush(fp);
 
574
}
 
575
 
 
576
 
 
577
//
 
578
// CookieWriteAll
 
579
//
 
580
// Writes the cookie file.
 
581
// Returns one of the COOKIE_E_* values.
 
582
//
 
583
cookie_status_t CookieWriteAll(void)
 
584
{
 
585
  CookieDebug("CookieWriteAll BEGUN.\n");
 
586
  
 
587
  // create file (overwrite if exists)
 
588
  FILE *fp = fopen(cookie_filename, "w");
 
589
 
 
590
  if (! fp)
 
591
    return COOKIE_E_NO_FILE;
 
592
 
 
593
  CookieWriteHeader(fp);
 
594
  CookieWriteBuildInfo(fp);
 
595
  CookieWritePrefs(fp);
 
596
  CookieWriteWindowPos(fp);
 
597
 
 
598
  // all done
 
599
  fclose(fp);
 
600
 
 
601
  CookieDebug("CookieWriteAll DONE.\n");
 
602
 
 
603
  return COOKIE_E_OK;
 
604
}
 
605