rnd-20050525-1-src
[rocksndiamonds.git] / src / libgame / setup.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * setup.c                                                  *
12 ***********************************************************/
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <dirent.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "setup.h"
21 #include "joystick.h"
22 #include "text.h"
23 #include "misc.h"
24 #include "hash.h"
25
26
27 #define NUM_LEVELCLASS_DESC     8
28
29 static char *levelclass_desc[NUM_LEVELCLASS_DESC] =
30 {
31   "Tutorial Levels",
32   "Classic Originals",
33   "Contributions",
34   "Private Levels",
35   "Boulderdash",
36   "Emerald Mine",
37   "Supaplex",
38   "DX Boulderdash"
39 };
40
41
42 #define LEVELCOLOR(n)   (IS_LEVELCLASS_TUTORIAL(n) ?            FC_BLUE :    \
43                          IS_LEVELCLASS_CLASSICS(n) ?            FC_RED :     \
44                          IS_LEVELCLASS_BD(n) ?                  FC_YELLOW :  \
45                          IS_LEVELCLASS_EM(n) ?                  FC_YELLOW :  \
46                          IS_LEVELCLASS_SP(n) ?                  FC_YELLOW :  \
47                          IS_LEVELCLASS_DX(n) ?                  FC_YELLOW :  \
48                          IS_LEVELCLASS_SB(n) ?                  FC_YELLOW :  \
49                          IS_LEVELCLASS_CONTRIB(n) ?             FC_GREEN :   \
50                          IS_LEVELCLASS_PRIVATE(n) ?             FC_RED :     \
51                          FC_BLUE)
52
53 #define LEVELSORTING(n) (IS_LEVELCLASS_TUTORIAL(n) ?            0 :     \
54                          IS_LEVELCLASS_CLASSICS(n) ?            1 :     \
55                          IS_LEVELCLASS_BD(n) ?                  2 :     \
56                          IS_LEVELCLASS_EM(n) ?                  3 :     \
57                          IS_LEVELCLASS_SP(n) ?                  4 :     \
58                          IS_LEVELCLASS_DX(n) ?                  5 :     \
59                          IS_LEVELCLASS_SB(n) ?                  6 :     \
60                          IS_LEVELCLASS_CONTRIB(n) ?             7 :     \
61                          IS_LEVELCLASS_PRIVATE(n) ?             8 :     \
62                          9)
63
64 #define ARTWORKCOLOR(n) (IS_ARTWORKCLASS_CLASSICS(n) ?          FC_RED :     \
65                          IS_ARTWORKCLASS_CONTRIB(n) ?           FC_GREEN :   \
66                          IS_ARTWORKCLASS_PRIVATE(n) ?           FC_RED :     \
67                          IS_ARTWORKCLASS_LEVEL(n) ?             FC_YELLOW :  \
68                          FC_BLUE)
69
70 #define ARTWORKSORTING(n) (IS_ARTWORKCLASS_CLASSICS(n) ?        0 :     \
71                            IS_ARTWORKCLASS_LEVEL(n) ?           1 :     \
72                            IS_ARTWORKCLASS_CONTRIB(n) ?         2 :     \
73                            IS_ARTWORKCLASS_PRIVATE(n) ?         3 :     \
74                            9)
75
76 #define TOKEN_VALUE_POSITION_SHORT              32
77 #define TOKEN_VALUE_POSITION_DEFAULT            40
78 #define TOKEN_COMMENT_POSITION_DEFAULT          60
79
80 #define MAX_COOKIE_LEN                          256
81
82 static int token_value_position   = TOKEN_VALUE_POSITION_DEFAULT;
83 static int token_comment_position = TOKEN_COMMENT_POSITION_DEFAULT;
84
85
86 /* ------------------------------------------------------------------------- */
87 /* file functions                                                            */
88 /* ------------------------------------------------------------------------- */
89
90 static char *getLevelClassDescription(TreeInfo *ldi)
91 {
92   int position = ldi->sort_priority / 100;
93
94   if (position >= 0 && position < NUM_LEVELCLASS_DESC)
95     return levelclass_desc[position];
96   else
97     return "Unknown Level Class";
98 }
99
100 static char *getUserLevelDir(char *level_subdir)
101 {
102   static char *userlevel_dir = NULL;
103   char *data_dir = getUserDataDir();
104   char *userlevel_subdir = LEVELS_DIRECTORY;
105
106   checked_free(userlevel_dir);
107
108   if (level_subdir != NULL)
109     userlevel_dir = getPath3(data_dir, userlevel_subdir, level_subdir);
110   else
111     userlevel_dir = getPath2(data_dir, userlevel_subdir);
112
113   return userlevel_dir;
114 }
115
116 static char *getScoreDir(char *level_subdir)
117 {
118   static char *score_dir = NULL;
119   char *data_dir = getCommonDataDir();
120   char *score_subdir = SCORES_DIRECTORY;
121
122   checked_free(score_dir);
123
124   if (level_subdir != NULL)
125     score_dir = getPath3(data_dir, score_subdir, level_subdir);
126   else
127     score_dir = getPath2(data_dir, score_subdir);
128
129   return score_dir;
130 }
131
132 static char *getLevelSetupDir(char *level_subdir)
133 {
134   static char *levelsetup_dir = NULL;
135   char *data_dir = getUserDataDir();
136   char *levelsetup_subdir = LEVELSETUP_DIRECTORY;
137
138   checked_free(levelsetup_dir);
139
140   if (level_subdir != NULL)
141     levelsetup_dir = getPath3(data_dir, levelsetup_subdir, level_subdir);
142   else
143     levelsetup_dir = getPath2(data_dir, levelsetup_subdir);
144
145   return levelsetup_dir;
146 }
147
148 static char *getLevelDirFromTreeInfo(TreeInfo *node)
149 {
150   static char *level_dir = NULL;
151
152   if (node == NULL)
153     return options.level_directory;
154
155   checked_free(level_dir);
156
157   level_dir = getPath2((node->in_user_dir ? getUserLevelDir(NULL) :
158                         options.level_directory), node->fullpath);
159
160   return level_dir;
161 }
162
163 char *getCurrentLevelDir()
164 {
165   return getLevelDirFromTreeInfo(leveldir_current);
166 }
167
168 static char *getTapeDir(char *level_subdir)
169 {
170   static char *tape_dir = NULL;
171   char *data_dir = getUserDataDir();
172   char *tape_subdir = TAPES_DIRECTORY;
173
174   checked_free(tape_dir);
175
176   if (level_subdir != NULL)
177     tape_dir = getPath3(data_dir, tape_subdir, level_subdir);
178   else
179     tape_dir = getPath2(data_dir, tape_subdir);
180
181   return tape_dir;
182 }
183
184 static char *getSolutionTapeDir()
185 {
186   static char *tape_dir = NULL;
187   char *data_dir = getCurrentLevelDir();
188   char *tape_subdir = TAPES_DIRECTORY;
189
190   checked_free(tape_dir);
191
192   tape_dir = getPath2(data_dir, tape_subdir);
193
194   return tape_dir;
195 }
196
197 static char *getDefaultGraphicsDir(char *graphics_subdir)
198 {
199   static char *graphics_dir = NULL;
200
201   if (graphics_subdir == NULL)
202     return options.graphics_directory;
203
204   checked_free(graphics_dir);
205
206   graphics_dir = getPath2(options.graphics_directory, graphics_subdir);
207
208   return graphics_dir;
209 }
210
211 static char *getDefaultSoundsDir(char *sounds_subdir)
212 {
213   static char *sounds_dir = NULL;
214
215   if (sounds_subdir == NULL)
216     return options.sounds_directory;
217
218   checked_free(sounds_dir);
219
220   sounds_dir = getPath2(options.sounds_directory, sounds_subdir);
221
222   return sounds_dir;
223 }
224
225 static char *getDefaultMusicDir(char *music_subdir)
226 {
227   static char *music_dir = NULL;
228
229   if (music_subdir == NULL)
230     return options.music_directory;
231
232   checked_free(music_dir);
233
234   music_dir = getPath2(options.music_directory, music_subdir);
235
236   return music_dir;
237 }
238
239 static char *getDefaultArtworkSet(int type)
240 {
241   return (type == TREE_TYPE_GRAPHICS_DIR ? GFX_CLASSIC_SUBDIR :
242           type == TREE_TYPE_SOUNDS_DIR   ? SND_CLASSIC_SUBDIR :
243           type == TREE_TYPE_MUSIC_DIR    ? MUS_CLASSIC_SUBDIR : "");
244 }
245
246 static char *getDefaultArtworkDir(int type)
247 {
248   return (type == TREE_TYPE_GRAPHICS_DIR ?
249           getDefaultGraphicsDir(GFX_CLASSIC_SUBDIR) :
250           type == TREE_TYPE_SOUNDS_DIR ?
251           getDefaultSoundsDir(SND_CLASSIC_SUBDIR) :
252           type == TREE_TYPE_MUSIC_DIR ?
253           getDefaultMusicDir(MUS_CLASSIC_SUBDIR) : "");
254 }
255
256 static char *getUserGraphicsDir()
257 {
258   static char *usergraphics_dir = NULL;
259
260   if (usergraphics_dir == NULL)
261     usergraphics_dir = getPath2(getUserDataDir(), GRAPHICS_DIRECTORY);
262
263   return usergraphics_dir;
264 }
265
266 static char *getUserSoundsDir()
267 {
268   static char *usersounds_dir = NULL;
269
270   if (usersounds_dir == NULL)
271     usersounds_dir = getPath2(getUserDataDir(), SOUNDS_DIRECTORY);
272
273   return usersounds_dir;
274 }
275
276 static char *getUserMusicDir()
277 {
278   static char *usermusic_dir = NULL;
279
280   if (usermusic_dir == NULL)
281     usermusic_dir = getPath2(getUserDataDir(), MUSIC_DIRECTORY);
282
283   return usermusic_dir;
284 }
285
286 static char *getSetupArtworkDir(TreeInfo *ti)
287 {
288   static char *artwork_dir = NULL;
289
290   checked_free(artwork_dir);
291
292   artwork_dir = getPath2(ti->basepath, ti->fullpath);
293
294   return artwork_dir;
295 }
296
297 char *setLevelArtworkDir(TreeInfo *ti)
298 {
299   char **artwork_path_ptr, **artwork_set_ptr;
300   TreeInfo *level_artwork;
301
302   if (ti == NULL || leveldir_current == NULL)
303     return NULL;
304
305   artwork_path_ptr = &(LEVELDIR_ARTWORK_PATH(leveldir_current, ti->type));
306   artwork_set_ptr  = &(LEVELDIR_ARTWORK_SET( leveldir_current, ti->type));
307
308   checked_free(*artwork_path_ptr);
309
310   if ((level_artwork = getTreeInfoFromIdentifier(ti, *artwork_set_ptr)))
311     *artwork_path_ptr = getStringCopy(getSetupArtworkDir(level_artwork));
312   else
313   {
314     /* No (or non-existing) artwork configured in "levelinfo.conf". This would
315        normally result in using the artwork configured in the setup menu. But
316        if an artwork subdirectory exists (which might contain custom artwork
317        or an artwork configuration file), this level artwork must be treated
318        as relative to the default "classic" artwork, not to the artwork that
319        is currently configured in the setup menu. */
320
321     char *dir = getPath2(getCurrentLevelDir(), ARTWORK_DIRECTORY(ti->type));
322
323     checked_free(*artwork_set_ptr);
324
325     if (fileExists(dir))
326     {
327       *artwork_path_ptr = getStringCopy(getDefaultArtworkDir(ti->type));
328       *artwork_set_ptr = getStringCopy(getDefaultArtworkSet(ti->type));
329     }
330     else
331     {
332       *artwork_path_ptr = getStringCopy(UNDEFINED_FILENAME);
333       *artwork_set_ptr = NULL;
334     }
335
336     free(dir);
337   }
338
339   return *artwork_set_ptr;
340 }
341
342 inline static char *getLevelArtworkSet(int type)
343 {
344   if (leveldir_current == NULL)
345     return NULL;
346
347   return LEVELDIR_ARTWORK_SET(leveldir_current, type);
348 }
349
350 inline static char *getLevelArtworkDir(int type)
351 {
352   if (leveldir_current == NULL)
353     return UNDEFINED_FILENAME;
354
355   return LEVELDIR_ARTWORK_PATH(leveldir_current, type);
356 }
357
358 char *getTapeFilename(int nr)
359 {
360   static char *filename = NULL;
361   char basename[MAX_FILENAME_LEN];
362
363   checked_free(filename);
364
365   sprintf(basename, "%03d.%s", nr, TAPEFILE_EXTENSION);
366   filename = getPath2(getTapeDir(leveldir_current->subdir), basename);
367
368   return filename;
369 }
370
371 char *getSolutionTapeFilename(int nr)
372 {
373   static char *filename = NULL;
374   char basename[MAX_FILENAME_LEN];
375
376   checked_free(filename);
377
378   sprintf(basename, "%03d.%s", nr, TAPEFILE_EXTENSION);
379   filename = getPath2(getSolutionTapeDir(), basename);
380
381   return filename;
382 }
383
384 char *getScoreFilename(int nr)
385 {
386   static char *filename = NULL;
387   char basename[MAX_FILENAME_LEN];
388
389   checked_free(filename);
390
391   sprintf(basename, "%03d.%s", nr, SCOREFILE_EXTENSION);
392   filename = getPath2(getScoreDir(leveldir_current->subdir), basename);
393
394   return filename;
395 }
396
397 char *getSetupFilename()
398 {
399   static char *filename = NULL;
400
401   checked_free(filename);
402
403   filename = getPath2(getSetupDir(), SETUP_FILENAME);
404
405   return filename;
406 }
407
408 char *getEditorSetupFilename()
409 {
410   static char *filename = NULL;
411
412   checked_free(filename);
413   filename = getPath2(getCurrentLevelDir(), EDITORSETUP_FILENAME);
414
415   if (fileExists(filename))
416     return filename;
417
418   checked_free(filename);
419   filename = getPath2(getSetupDir(), EDITORSETUP_FILENAME);
420
421   return filename;
422 }
423
424 char *getHelpAnimFilename()
425 {
426   static char *filename = NULL;
427
428   checked_free(filename);
429
430   filename = getPath2(getCurrentLevelDir(), HELPANIM_FILENAME);
431
432   return filename;
433 }
434
435 char *getHelpTextFilename()
436 {
437   static char *filename = NULL;
438
439   checked_free(filename);
440
441   filename = getPath2(getCurrentLevelDir(), HELPTEXT_FILENAME);
442
443   return filename;
444 }
445
446 char *getLevelSetInfoFilename()
447 {
448   static char *filename = NULL;
449   char *basenames[] =
450   {
451     "README",
452     "README.TXT",
453     "README.txt",
454     "Readme",
455     "Readme.txt",
456     "readme",
457     "readme.txt",
458
459     NULL
460   };
461   int i;
462
463   for (i = 0; basenames[i] != NULL; i++)
464   {
465     checked_free(filename);
466     filename = getPath2(getCurrentLevelDir(), basenames[i]);
467
468     if (fileExists(filename))
469       return filename;
470   }
471
472   return NULL;
473 }
474
475 static char *getCorrectedArtworkBasename(char *basename)
476 {
477   char *basename_corrected = basename;
478
479 #if defined(PLATFORM_MSDOS)
480   if (program.filename_prefix != NULL)
481   {
482     int prefix_len = strlen(program.filename_prefix);
483
484     if (strncmp(basename, program.filename_prefix, prefix_len) == 0)
485       basename_corrected = &basename[prefix_len];
486
487     /* if corrected filename is still longer than standard MS-DOS filename
488        size (8 characters + 1 dot + 3 characters file extension), shorten
489        filename by writing file extension after 8th basename character */
490     if (strlen(basename_corrected) > 8 + 1 + 3)
491     {
492       static char *msdos_filename = NULL;
493
494       checked_free(msdos_filename);
495
496       msdos_filename = getStringCopy(basename_corrected);
497       strncpy(&msdos_filename[8], &basename[strlen(basename) - (1+3)], 1+3 +1);
498
499       basename_corrected = msdos_filename;
500     }
501   }
502 #endif
503
504   return basename_corrected;
505 }
506
507 char *getCustomImageFilename(char *basename)
508 {
509   static char *filename = NULL;
510   boolean skip_setup_artwork = FALSE;
511
512   checked_free(filename);
513
514   basename = getCorrectedArtworkBasename(basename);
515
516   if (!setup.override_level_graphics)
517   {
518     /* 1st try: look for special artwork in current level series directory */
519     filename = getPath3(getCurrentLevelDir(), GRAPHICS_DIRECTORY, basename);
520     if (fileExists(filename))
521       return filename;
522
523     free(filename);
524
525     /* check if there is special artwork configured in level series config */
526     if (getLevelArtworkSet(ARTWORK_TYPE_GRAPHICS) != NULL)
527     {
528       /* 2nd try: look for special artwork configured in level series config */
529       filename = getPath2(getLevelArtworkDir(ARTWORK_TYPE_GRAPHICS), basename);
530       if (fileExists(filename))
531         return filename;
532
533       free(filename);
534
535       /* take missing artwork configured in level set config from default */
536       skip_setup_artwork = TRUE;
537     }
538   }
539
540   if (!skip_setup_artwork)
541   {
542     /* 3rd try: look for special artwork in configured artwork directory */
543     filename = getPath2(getSetupArtworkDir(artwork.gfx_current), basename);
544     if (fileExists(filename))
545       return filename;
546
547     free(filename);
548   }
549
550   /* 4th try: look for default artwork in new default artwork directory */
551   filename = getPath2(getDefaultGraphicsDir(GFX_CLASSIC_SUBDIR), basename);
552   if (fileExists(filename))
553     return filename;
554
555   free(filename);
556
557   /* 5th try: look for default artwork in old default artwork directory */
558   filename = getPath2(options.graphics_directory, basename);
559   if (fileExists(filename))
560     return filename;
561
562   return NULL;          /* cannot find specified artwork file anywhere */
563 }
564
565 char *getCustomSoundFilename(char *basename)
566 {
567   static char *filename = NULL;
568   boolean skip_setup_artwork = FALSE;
569
570   checked_free(filename);
571
572   basename = getCorrectedArtworkBasename(basename);
573
574   if (!setup.override_level_sounds)
575   {
576     /* 1st try: look for special artwork in current level series directory */
577     filename = getPath3(getCurrentLevelDir(), SOUNDS_DIRECTORY, basename);
578     if (fileExists(filename))
579       return filename;
580
581     free(filename);
582
583     /* check if there is special artwork configured in level series config */
584     if (getLevelArtworkSet(ARTWORK_TYPE_SOUNDS) != NULL)
585     {
586       /* 2nd try: look for special artwork configured in level series config */
587       filename = getPath2(getLevelArtworkDir(TREE_TYPE_SOUNDS_DIR), basename);
588       if (fileExists(filename))
589         return filename;
590
591       free(filename);
592
593       /* take missing artwork configured in level set config from default */
594       skip_setup_artwork = TRUE;
595     }
596   }
597
598   if (!skip_setup_artwork)
599   {
600     /* 3rd try: look for special artwork in configured artwork directory */
601     filename = getPath2(getSetupArtworkDir(artwork.snd_current), basename);
602     if (fileExists(filename))
603       return filename;
604
605     free(filename);
606   }
607
608   /* 4th try: look for default artwork in new default artwork directory */
609   filename = getPath2(getDefaultSoundsDir(SND_CLASSIC_SUBDIR), basename);
610   if (fileExists(filename))
611     return filename;
612
613   free(filename);
614
615   /* 5th try: look for default artwork in old default artwork directory */
616   filename = getPath2(options.sounds_directory, basename);
617   if (fileExists(filename))
618     return filename;
619
620   return NULL;          /* cannot find specified artwork file anywhere */
621 }
622
623 char *getCustomMusicFilename(char *basename)
624 {
625   static char *filename = NULL;
626   boolean skip_setup_artwork = FALSE;
627
628   checked_free(filename);
629
630   basename = getCorrectedArtworkBasename(basename);
631
632   if (!setup.override_level_music)
633   {
634     /* 1st try: look for special artwork in current level series directory */
635     filename = getPath3(getCurrentLevelDir(), MUSIC_DIRECTORY, basename);
636     if (fileExists(filename))
637       return filename;
638
639     free(filename);
640
641     /* check if there is special artwork configured in level series config */
642     if (getLevelArtworkSet(ARTWORK_TYPE_MUSIC) != NULL)
643     {
644       /* 2nd try: look for special artwork configured in level series config */
645       filename = getPath2(getLevelArtworkDir(TREE_TYPE_MUSIC_DIR), basename);
646       if (fileExists(filename))
647         return filename;
648
649       free(filename);
650
651       /* take missing artwork configured in level set config from default */
652       skip_setup_artwork = TRUE;
653     }
654   }
655
656   if (!skip_setup_artwork)
657   {
658     /* 3rd try: look for special artwork in configured artwork directory */
659     filename = getPath2(getSetupArtworkDir(artwork.mus_current), basename);
660     if (fileExists(filename))
661       return filename;
662
663     free(filename);
664   }
665
666   /* 4th try: look for default artwork in new default artwork directory */
667   filename = getPath2(getDefaultMusicDir(MUS_CLASSIC_SUBDIR), basename);
668   if (fileExists(filename))
669     return filename;
670
671   free(filename);
672
673   /* 5th try: look for default artwork in old default artwork directory */
674   filename = getPath2(options.music_directory, basename);
675   if (fileExists(filename))
676     return filename;
677
678   return NULL;          /* cannot find specified artwork file anywhere */
679 }
680
681 char *getCustomArtworkFilename(char *basename, int type)
682 {
683   if (type == ARTWORK_TYPE_GRAPHICS)
684     return getCustomImageFilename(basename);
685   else if (type == ARTWORK_TYPE_SOUNDS)
686     return getCustomSoundFilename(basename);
687   else if (type == ARTWORK_TYPE_MUSIC)
688     return getCustomMusicFilename(basename);
689   else
690     return UNDEFINED_FILENAME;
691 }
692
693 char *getCustomArtworkConfigFilename(int type)
694 {
695   return getCustomArtworkFilename(ARTWORKINFO_FILENAME(type), type);
696 }
697
698 char *getCustomArtworkLevelConfigFilename(int type)
699 {
700   static char *filename = NULL;
701
702   checked_free(filename);
703
704   filename = getPath2(getLevelArtworkDir(type), ARTWORKINFO_FILENAME(type));
705
706   return filename;
707 }
708
709 char *getCustomMusicDirectory(void)
710 {
711   static char *directory = NULL;
712   boolean skip_setup_artwork = FALSE;
713
714   checked_free(directory);
715
716   if (!setup.override_level_music)
717   {
718     /* 1st try: look for special artwork in current level series directory */
719     directory = getPath2(getCurrentLevelDir(), MUSIC_DIRECTORY);
720     if (fileExists(directory))
721       return directory;
722
723     free(directory);
724
725     /* check if there is special artwork configured in level series config */
726     if (getLevelArtworkSet(ARTWORK_TYPE_MUSIC) != NULL)
727     {
728       /* 2nd try: look for special artwork configured in level series config */
729       directory = getStringCopy(getLevelArtworkDir(TREE_TYPE_MUSIC_DIR));
730       if (fileExists(directory))
731         return directory;
732
733       free(directory);
734
735       /* take missing artwork configured in level set config from default */
736       skip_setup_artwork = TRUE;
737     }
738   }
739
740   if (!skip_setup_artwork)
741   {
742     /* 3rd try: look for special artwork in configured artwork directory */
743     directory = getStringCopy(getSetupArtworkDir(artwork.mus_current));
744     if (fileExists(directory))
745       return directory;
746
747     free(directory);
748   }
749
750   /* 4th try: look for default artwork in new default artwork directory */
751   directory = getStringCopy(getDefaultMusicDir(MUS_CLASSIC_SUBDIR));
752   if (fileExists(directory))
753     return directory;
754
755   free(directory);
756
757   /* 5th try: look for default artwork in old default artwork directory */
758   directory = getStringCopy(options.music_directory);
759   if (fileExists(directory))
760     return directory;
761
762   return NULL;          /* cannot find specified artwork file anywhere */
763 }
764
765 void InitTapeDirectory(char *level_subdir)
766 {
767   createDirectory(getUserDataDir(), "user data", PERMS_PRIVATE);
768   createDirectory(getTapeDir(NULL), "main tape", PERMS_PRIVATE);
769   createDirectory(getTapeDir(level_subdir), "level tape", PERMS_PRIVATE);
770 }
771
772 void InitScoreDirectory(char *level_subdir)
773 {
774   createDirectory(getCommonDataDir(), "common data", PERMS_PUBLIC);
775   createDirectory(getScoreDir(NULL), "main score", PERMS_PUBLIC);
776   createDirectory(getScoreDir(level_subdir), "level score", PERMS_PUBLIC);
777 }
778
779 static void SaveUserLevelInfo();
780
781 void InitUserLevelDirectory(char *level_subdir)
782 {
783   if (access(getUserLevelDir(level_subdir), F_OK) != 0)
784   {
785     createDirectory(getUserDataDir(), "user data", PERMS_PRIVATE);
786     createDirectory(getUserLevelDir(NULL), "main user level", PERMS_PRIVATE);
787     createDirectory(getUserLevelDir(level_subdir), "user level",PERMS_PRIVATE);
788
789     SaveUserLevelInfo();
790   }
791 }
792
793 void InitLevelSetupDirectory(char *level_subdir)
794 {
795   createDirectory(getUserDataDir(), "user data", PERMS_PRIVATE);
796   createDirectory(getLevelSetupDir(NULL), "main level setup", PERMS_PRIVATE);
797   createDirectory(getLevelSetupDir(level_subdir), "level setup",PERMS_PRIVATE);
798 }
799
800
801 /* ------------------------------------------------------------------------- */
802 /* some functions to handle lists of level directories                       */
803 /* ------------------------------------------------------------------------- */
804
805 TreeInfo *newTreeInfo()
806 {
807   return checked_calloc(sizeof(TreeInfo));
808 }
809
810 void pushTreeInfo(TreeInfo **node_first, TreeInfo *node_new)
811 {
812   node_new->next = *node_first;
813   *node_first = node_new;
814 }
815
816 int numTreeInfo(TreeInfo *node)
817 {
818   int num = 0;
819
820   while (node)
821   {
822     num++;
823     node = node->next;
824   }
825
826   return num;
827 }
828
829 boolean validLevelSeries(TreeInfo *node)
830 {
831   return (node != NULL && !node->node_group && !node->parent_link);
832 }
833
834 TreeInfo *getFirstValidTreeInfoEntry(TreeInfo *node)
835 {
836   if (node == NULL)
837     return NULL;
838
839   if (node->node_group)         /* enter level group (step down into tree) */
840     return getFirstValidTreeInfoEntry(node->node_group);
841   else if (node->parent_link)   /* skip start entry of level group */
842   {
843     if (node->next)             /* get first real level series entry */
844       return getFirstValidTreeInfoEntry(node->next);
845     else                        /* leave empty level group and go on */
846       return getFirstValidTreeInfoEntry(node->node_parent->next);
847   }
848   else                          /* this seems to be a regular level series */
849     return node;
850 }
851
852 TreeInfo *getTreeInfoFirstGroupEntry(TreeInfo *node)
853 {
854   if (node == NULL)
855     return NULL;
856
857   if (node->node_parent == NULL)                /* top level group */
858     return *node->node_top;
859   else                                          /* sub level group */
860     return node->node_parent->node_group;
861 }
862
863 int numTreeInfoInGroup(TreeInfo *node)
864 {
865   return numTreeInfo(getTreeInfoFirstGroupEntry(node));
866 }
867
868 int posTreeInfo(TreeInfo *node)
869 {
870   TreeInfo *node_cmp = getTreeInfoFirstGroupEntry(node);
871   int pos = 0;
872
873   while (node_cmp)
874   {
875     if (node_cmp == node)
876       return pos;
877
878     pos++;
879     node_cmp = node_cmp->next;
880   }
881
882   return 0;
883 }
884
885 TreeInfo *getTreeInfoFromPos(TreeInfo *node, int pos)
886 {
887   TreeInfo *node_default = node;
888   int pos_cmp = 0;
889
890   while (node)
891   {
892     if (pos_cmp == pos)
893       return node;
894
895     pos_cmp++;
896     node = node->next;
897   }
898
899   return node_default;
900 }
901
902 TreeInfo *getTreeInfoFromIdentifier(TreeInfo *node, char *identifier)
903 {
904   if (identifier == NULL)
905     return NULL;
906
907   while (node)
908   {
909     if (node->node_group)
910     {
911       TreeInfo *node_group;
912
913       node_group = getTreeInfoFromIdentifier(node->node_group, identifier);
914
915       if (node_group)
916         return node_group;
917     }
918     else if (!node->parent_link)
919     {
920       if (strcmp(identifier, node->identifier) == 0)
921         return node;
922     }
923
924     node = node->next;
925   }
926
927   return NULL;
928 }
929
930 void dumpTreeInfo(TreeInfo *node, int depth)
931 {
932   int i;
933
934   printf("Dumping TreeInfo:\n");
935
936   while (node)
937   {
938     for (i = 0; i < (depth + 1) * 3; i++)
939       printf(" ");
940
941 #if 1
942     printf("subdir == '%s' ['%s', '%s'] [%d])\n",
943            node->subdir, node->fullpath, node->basepath, node->in_user_dir);
944 #else
945     printf("subdir == '%s' (%s) [%s] (%d)\n",
946            node->subdir, node->name, node->identifier, node->sort_priority);
947 #endif
948
949     if (node->node_group != NULL)
950       dumpTreeInfo(node->node_group, depth + 1);
951
952     node = node->next;
953   }
954 }
955
956 void sortTreeInfo(TreeInfo **node_first,
957                   int (*compare_function)(const void *, const void *))
958 {
959   int num_nodes = numTreeInfo(*node_first);
960   TreeInfo **sort_array;
961   TreeInfo *node = *node_first;
962   int i = 0;
963
964   if (num_nodes == 0)
965     return;
966
967   /* allocate array for sorting structure pointers */
968   sort_array = checked_calloc(num_nodes * sizeof(TreeInfo *));
969
970   /* writing structure pointers to sorting array */
971   while (i < num_nodes && node)         /* double boundary check... */
972   {
973     sort_array[i] = node;
974
975     i++;
976     node = node->next;
977   }
978
979   /* sorting the structure pointers in the sorting array */
980   qsort(sort_array, num_nodes, sizeof(TreeInfo *),
981         compare_function);
982
983   /* update the linkage of list elements with the sorted node array */
984   for (i = 0; i < num_nodes - 1; i++)
985     sort_array[i]->next = sort_array[i + 1];
986   sort_array[num_nodes - 1]->next = NULL;
987
988   /* update the linkage of the main list anchor pointer */
989   *node_first = sort_array[0];
990
991   free(sort_array);
992
993   /* now recursively sort the level group structures */
994   node = *node_first;
995   while (node)
996   {
997     if (node->node_group != NULL)
998       sortTreeInfo(&node->node_group, compare_function);
999
1000     node = node->next;
1001   }
1002 }
1003
1004
1005 /* ========================================================================= */
1006 /* some stuff from "files.c"                                                 */
1007 /* ========================================================================= */
1008
1009 #if defined(PLATFORM_WIN32)
1010 #ifndef S_IRGRP
1011 #define S_IRGRP S_IRUSR
1012 #endif
1013 #ifndef S_IROTH
1014 #define S_IROTH S_IRUSR
1015 #endif
1016 #ifndef S_IWGRP
1017 #define S_IWGRP S_IWUSR
1018 #endif
1019 #ifndef S_IWOTH
1020 #define S_IWOTH S_IWUSR
1021 #endif
1022 #ifndef S_IXGRP
1023 #define S_IXGRP S_IXUSR
1024 #endif
1025 #ifndef S_IXOTH
1026 #define S_IXOTH S_IXUSR
1027 #endif
1028 #ifndef S_IRWXG
1029 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
1030 #endif
1031 #ifndef S_ISGID
1032 #define S_ISGID 0
1033 #endif
1034 #endif  /* PLATFORM_WIN32 */
1035
1036 /* file permissions for newly written files */
1037 #define MODE_R_ALL              (S_IRUSR | S_IRGRP | S_IROTH)
1038 #define MODE_W_ALL              (S_IWUSR | S_IWGRP | S_IWOTH)
1039 #define MODE_X_ALL              (S_IXUSR | S_IXGRP | S_IXOTH)
1040
1041 #define MODE_W_PRIVATE          (S_IWUSR)
1042 #define MODE_W_PUBLIC           (S_IWUSR | S_IWGRP)
1043 #define MODE_W_PUBLIC_DIR       (S_IWUSR | S_IWGRP | S_ISGID)
1044
1045 #define DIR_PERMS_PRIVATE       (MODE_R_ALL | MODE_X_ALL | MODE_W_PRIVATE)
1046 #define DIR_PERMS_PUBLIC        (MODE_R_ALL | MODE_X_ALL | MODE_W_PUBLIC_DIR)
1047
1048 #define FILE_PERMS_PRIVATE      (MODE_R_ALL | MODE_W_PRIVATE)
1049 #define FILE_PERMS_PUBLIC       (MODE_R_ALL | MODE_W_PUBLIC)
1050
1051 char *getUserDataDir(void)
1052 {
1053   static char *userdata_dir = NULL;
1054
1055   if (userdata_dir == NULL)
1056     userdata_dir = getPath2(getHomeDir(), program.userdata_directory);
1057
1058   return userdata_dir;
1059 }
1060
1061 char *getCommonDataDir(void)
1062 {
1063   static char *common_data_dir = NULL;
1064
1065 #if defined(PLATFORM_WIN32)
1066   if (common_data_dir == NULL)
1067   {
1068     char *dir = checked_malloc(MAX_PATH + 1);
1069
1070     if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL, 0, dir))
1071         && strcmp(dir, "") != 0)        /* empty for Windows 95/98 */
1072       common_data_dir = getPath2(dir, program.userdata_directory);
1073     else
1074       common_data_dir = options.rw_base_directory;
1075   }
1076 #else
1077   if (common_data_dir == NULL)
1078     common_data_dir = options.rw_base_directory;
1079 #endif
1080
1081   return common_data_dir;
1082 }
1083
1084 char *getSetupDir()
1085 {
1086   return getUserDataDir();
1087 }
1088
1089 static mode_t posix_umask(mode_t mask)
1090 {
1091 #if defined(PLATFORM_UNIX)
1092   return umask(mask);
1093 #else
1094   return 0;
1095 #endif
1096 }
1097
1098 static int posix_mkdir(const char *pathname, mode_t mode)
1099 {
1100 #if defined(PLATFORM_WIN32)
1101   return mkdir(pathname);
1102 #else
1103   return mkdir(pathname, mode);
1104 #endif
1105 }
1106
1107 void createDirectory(char *dir, char *text, int permission_class)
1108 {
1109   /* leave "other" permissions in umask untouched, but ensure group parts
1110      of USERDATA_DIR_MODE are not masked */
1111   mode_t dir_mode = (permission_class == PERMS_PRIVATE ?
1112                      DIR_PERMS_PRIVATE : DIR_PERMS_PUBLIC);
1113   mode_t normal_umask = posix_umask(0);
1114   mode_t group_umask = ~(dir_mode & S_IRWXG);
1115   posix_umask(normal_umask & group_umask);
1116
1117   if (access(dir, F_OK) != 0)
1118     if (posix_mkdir(dir, dir_mode) != 0)
1119       Error(ERR_WARN, "cannot create %s directory '%s'", text, dir);
1120
1121   posix_umask(normal_umask);            /* reset normal umask */
1122 }
1123
1124 void InitUserDataDirectory()
1125 {
1126   createDirectory(getUserDataDir(), "user data", PERMS_PRIVATE);
1127 }
1128
1129 void SetFilePermissions(char *filename, int permission_class)
1130 {
1131   chmod(filename, (permission_class == PERMS_PRIVATE ?
1132                    FILE_PERMS_PRIVATE : FILE_PERMS_PUBLIC));
1133 }
1134
1135 char *getCookie(char *file_type)
1136 {
1137   static char cookie[MAX_COOKIE_LEN + 1];
1138
1139   if (strlen(program.cookie_prefix) + 1 +
1140       strlen(file_type) + strlen("_FILE_VERSION_x.x") > MAX_COOKIE_LEN)
1141     return "[COOKIE ERROR]";    /* should never happen */
1142
1143   sprintf(cookie, "%s_%s_FILE_VERSION_%d.%d",
1144           program.cookie_prefix, file_type,
1145           program.version_major, program.version_minor);
1146
1147   return cookie;
1148 }
1149
1150 int getFileVersionFromCookieString(const char *cookie)
1151 {
1152   const char *ptr_cookie1, *ptr_cookie2;
1153   const char *pattern1 = "_FILE_VERSION_";
1154   const char *pattern2 = "?.?";
1155   const int len_cookie = strlen(cookie);
1156   const int len_pattern1 = strlen(pattern1);
1157   const int len_pattern2 = strlen(pattern2);
1158   const int len_pattern = len_pattern1 + len_pattern2;
1159   int version_major, version_minor;
1160
1161   if (len_cookie <= len_pattern)
1162     return -1;
1163
1164   ptr_cookie1 = &cookie[len_cookie - len_pattern];
1165   ptr_cookie2 = &cookie[len_cookie - len_pattern2];
1166
1167   if (strncmp(ptr_cookie1, pattern1, len_pattern1) != 0)
1168     return -1;
1169
1170   if (ptr_cookie2[0] < '0' || ptr_cookie2[0] > '9' ||
1171       ptr_cookie2[1] != '.' ||
1172       ptr_cookie2[2] < '0' || ptr_cookie2[2] > '9')
1173     return -1;
1174
1175   version_major = ptr_cookie2[0] - '0';
1176   version_minor = ptr_cookie2[2] - '0';
1177
1178   return VERSION_IDENT(version_major, version_minor, 0, 0);
1179 }
1180
1181 boolean checkCookieString(const char *cookie, const char *template)
1182 {
1183   const char *pattern = "_FILE_VERSION_?.?";
1184   const int len_cookie = strlen(cookie);
1185   const int len_template = strlen(template);
1186   const int len_pattern = strlen(pattern);
1187
1188   if (len_cookie != len_template)
1189     return FALSE;
1190
1191   if (strncmp(cookie, template, len_cookie - len_pattern) != 0)
1192     return FALSE;
1193
1194   return TRUE;
1195 }
1196
1197 /* ------------------------------------------------------------------------- */
1198 /* setup file list and hash handling functions                               */
1199 /* ------------------------------------------------------------------------- */
1200
1201 char *getFormattedSetupEntry(char *token, char *value)
1202 {
1203   int i;
1204   static char entry[MAX_LINE_LEN];
1205
1206   /* if value is an empty string, just return token without value */
1207   if (*value == '\0')
1208     return token;
1209
1210   /* start with the token and some spaces to format output line */
1211   sprintf(entry, "%s:", token);
1212   for (i = strlen(entry); i < token_value_position; i++)
1213     strcat(entry, " ");
1214
1215   /* continue with the token's value */
1216   strcat(entry, value);
1217
1218   return entry;
1219 }
1220
1221 SetupFileList *newSetupFileList(char *token, char *value)
1222 {
1223   SetupFileList *new = checked_malloc(sizeof(SetupFileList));
1224
1225   new->token = getStringCopy(token);
1226   new->value = getStringCopy(value);
1227
1228   new->next = NULL;
1229
1230   return new;
1231 }
1232
1233 void freeSetupFileList(SetupFileList *list)
1234 {
1235   if (list == NULL)
1236     return;
1237
1238   checked_free(list->token);
1239   checked_free(list->value);
1240
1241   if (list->next)
1242     freeSetupFileList(list->next);
1243
1244   free(list);
1245 }
1246
1247 char *getListEntry(SetupFileList *list, char *token)
1248 {
1249   if (list == NULL)
1250     return NULL;
1251
1252   if (strcmp(list->token, token) == 0)
1253     return list->value;
1254   else
1255     return getListEntry(list->next, token);
1256 }
1257
1258 SetupFileList *setListEntry(SetupFileList *list, char *token, char *value)
1259 {
1260   if (list == NULL)
1261     return NULL;
1262
1263   if (strcmp(list->token, token) == 0)
1264   {
1265     checked_free(list->value);
1266
1267     list->value = getStringCopy(value);
1268
1269     return list;
1270   }
1271   else if (list->next == NULL)
1272     return (list->next = newSetupFileList(token, value));
1273   else
1274     return setListEntry(list->next, token, value);
1275 }
1276
1277 SetupFileList *addListEntry(SetupFileList *list, char *token, char *value)
1278 {
1279   if (list == NULL)
1280     return NULL;
1281
1282   if (list->next == NULL)
1283     return (list->next = newSetupFileList(token, value));
1284   else
1285     return addListEntry(list->next, token, value);
1286 }
1287
1288 #ifdef DEBUG
1289 static void printSetupFileList(SetupFileList *list)
1290 {
1291   if (!list)
1292     return;
1293
1294   printf("token: '%s'\n", list->token);
1295   printf("value: '%s'\n", list->value);
1296
1297   printSetupFileList(list->next);
1298 }
1299 #endif
1300
1301 #ifdef DEBUG
1302 DEFINE_HASHTABLE_INSERT(insert_hash_entry, char, char);
1303 DEFINE_HASHTABLE_SEARCH(search_hash_entry, char, char);
1304 DEFINE_HASHTABLE_CHANGE(change_hash_entry, char, char);
1305 DEFINE_HASHTABLE_REMOVE(remove_hash_entry, char, char);
1306 #else
1307 #define insert_hash_entry hashtable_insert
1308 #define search_hash_entry hashtable_search
1309 #define change_hash_entry hashtable_change
1310 #define remove_hash_entry hashtable_remove
1311 #endif
1312
1313 static unsigned int get_hash_from_key(void *key)
1314 {
1315   /*
1316     djb2
1317
1318     This algorithm (k=33) was first reported by Dan Bernstein many years ago in
1319     'comp.lang.c'. Another version of this algorithm (now favored by Bernstein)
1320     uses XOR: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why
1321     it works better than many other constants, prime or not) has never been
1322     adequately explained.
1323
1324     If you just want to have a good hash function, and cannot wait, djb2
1325     is one of the best string hash functions i know. It has excellent
1326     distribution and speed on many different sets of keys and table sizes.
1327     You are not likely to do better with one of the "well known" functions
1328     such as PJW, K&R, etc.
1329
1330     Ozan (oz) Yigit [http://www.cs.yorku.ca/~oz/hash.html]
1331   */
1332
1333   char *str = (char *)key;
1334   unsigned int hash = 5381;
1335   int c;
1336
1337   while ((c = *str++))
1338     hash = ((hash << 5) + hash) + c;    /* hash * 33 + c */
1339
1340   return hash;
1341 }
1342
1343 static int keys_are_equal(void *key1, void *key2)
1344 {
1345   return (strcmp((char *)key1, (char *)key2) == 0);
1346 }
1347
1348 SetupFileHash *newSetupFileHash()
1349 {
1350   SetupFileHash *new_hash =
1351     create_hashtable(16, 0.75, get_hash_from_key, keys_are_equal);
1352
1353   if (new_hash == NULL)
1354     Error(ERR_EXIT, "create_hashtable() failed -- out of memory");
1355
1356   return new_hash;
1357 }
1358
1359 void freeSetupFileHash(SetupFileHash *hash)
1360 {
1361   if (hash == NULL)
1362     return;
1363
1364   hashtable_destroy(hash, 1);   /* 1 == also free values stored in hash */
1365 }
1366
1367 char *getHashEntry(SetupFileHash *hash, char *token)
1368 {
1369   if (hash == NULL)
1370     return NULL;
1371
1372   return search_hash_entry(hash, token);
1373 }
1374
1375 void setHashEntry(SetupFileHash *hash, char *token, char *value)
1376 {
1377   char *value_copy;
1378
1379   if (hash == NULL)
1380     return;
1381
1382   value_copy = getStringCopy(value);
1383
1384   /* change value; if it does not exist, insert it as new */
1385   if (!change_hash_entry(hash, token, value_copy))
1386     if (!insert_hash_entry(hash, getStringCopy(token), value_copy))
1387       Error(ERR_EXIT, "cannot insert into hash -- aborting");
1388 }
1389
1390 char *removeHashEntry(SetupFileHash *hash, char *token)
1391 {
1392   if (hash == NULL)
1393     return NULL;
1394
1395   return remove_hash_entry(hash, token);
1396 }
1397
1398 #if 0
1399 #ifdef DEBUG
1400 static void printSetupFileHash(SetupFileHash *hash)
1401 {
1402   BEGIN_HASH_ITERATION(hash, itr)
1403   {
1404     printf("token: '%s'\n", HASH_ITERATION_TOKEN(itr));
1405     printf("value: '%s'\n", HASH_ITERATION_VALUE(itr));
1406   }
1407   END_HASH_ITERATION(hash, itr)
1408 }
1409 #endif
1410 #endif
1411
1412 static void *loadSetupFileData(char *filename, boolean use_hash)
1413 {
1414   char line[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
1415   char *token, *value, *line_ptr;
1416   void *setup_file_data, *insert_ptr = NULL;
1417   boolean read_continued_line = FALSE;
1418   FILE *file;
1419
1420   if (use_hash)
1421     setup_file_data = newSetupFileHash();
1422   else
1423     insert_ptr = setup_file_data = newSetupFileList("", "");
1424
1425   if (!(file = fopen(filename, MODE_READ)))
1426   {
1427     Error(ERR_WARN, "cannot open configuration file '%s'", filename);
1428     return NULL;
1429   }
1430
1431   while (!feof(file))
1432   {
1433     /* read next line of input file */
1434     if (!fgets(line, MAX_LINE_LEN, file))
1435       break;
1436
1437     /* cut trailing newline or carriage return */
1438     for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
1439       if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
1440         *line_ptr = '\0';
1441
1442     if (read_continued_line)
1443     {
1444       /* cut leading whitespaces from input line */
1445       for (line_ptr = line; *line_ptr; line_ptr++)
1446         if (*line_ptr != ' ' && *line_ptr != '\t')
1447           break;
1448
1449       /* append new line to existing line, if there is enough space */
1450       if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN)
1451         strcat(previous_line, line_ptr);
1452
1453       strcpy(line, previous_line);      /* copy storage buffer to line */
1454
1455       read_continued_line = FALSE;
1456     }
1457
1458     /* if the last character is '\', continue at next line */
1459     if (strlen(line) > 0 && line[strlen(line) - 1] == '\\')
1460     {
1461       line[strlen(line) - 1] = '\0';    /* cut off trailing backslash */
1462       strcpy(previous_line, line);      /* copy line to storage buffer */
1463
1464       read_continued_line = TRUE;
1465
1466       continue;
1467     }
1468
1469     /* cut trailing comment from input line */
1470     for (line_ptr = line; *line_ptr; line_ptr++)
1471     {
1472       if (*line_ptr == '#')
1473       {
1474         *line_ptr = '\0';
1475         break;
1476       }
1477     }
1478
1479     /* cut trailing whitespaces from input line */
1480     for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
1481       if ((*line_ptr == ' ' || *line_ptr == '\t') && *(line_ptr + 1) == '\0')
1482         *line_ptr = '\0';
1483
1484     /* ignore empty lines */
1485     if (*line == '\0')
1486       continue;
1487
1488     /* cut leading whitespaces from token */
1489     for (token = line; *token; token++)
1490       if (*token != ' ' && *token != '\t')
1491         break;
1492
1493     /* start with empty value as reliable default */
1494     value = "";
1495
1496     /* find end of token to determine start of value */
1497     for (line_ptr = token; *line_ptr; line_ptr++)
1498     {
1499       if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':')
1500       {
1501         *line_ptr = '\0';               /* terminate token string */
1502         value = line_ptr + 1;           /* set beginning of value */
1503
1504         break;
1505       }
1506     }
1507
1508     /* cut leading whitespaces from value */
1509     for (; *value; value++)
1510       if (*value != ' ' && *value != '\t')
1511         break;
1512
1513 #if 0
1514     if (*value == '\0')
1515       value = "true";   /* treat tokens without value as "true" */
1516 #endif
1517
1518     if (*token)
1519     {
1520       if (use_hash)
1521         setHashEntry((SetupFileHash *)setup_file_data, token, value);
1522       else
1523         insert_ptr = addListEntry((SetupFileList *)insert_ptr, token, value);
1524     }
1525   }
1526
1527   fclose(file);
1528
1529   if (use_hash)
1530   {
1531     if (hashtable_count((SetupFileHash *)setup_file_data) == 0)
1532       Error(ERR_WARN, "configuration file '%s' is empty", filename);
1533   }
1534   else
1535   {
1536     SetupFileList *setup_file_list = (SetupFileList *)setup_file_data;
1537     SetupFileList *first_valid_list_entry = setup_file_list->next;
1538
1539     /* free empty list header */
1540     setup_file_list->next = NULL;
1541     freeSetupFileList(setup_file_list);
1542     setup_file_data = first_valid_list_entry;
1543
1544     if (first_valid_list_entry == NULL)
1545       Error(ERR_WARN, "configuration file '%s' is empty", filename);
1546   }
1547
1548   return setup_file_data;
1549 }
1550
1551 SetupFileList *loadSetupFileList(char *filename)
1552 {
1553   return (SetupFileList *)loadSetupFileData(filename, FALSE);
1554 }
1555
1556 SetupFileHash *loadSetupFileHash(char *filename)
1557 {
1558   return (SetupFileHash *)loadSetupFileData(filename, TRUE);
1559 }
1560
1561 void checkSetupFileHashIdentifier(SetupFileHash *setup_file_hash,
1562                                   char *identifier)
1563 {
1564   char *value = getHashEntry(setup_file_hash, TOKEN_STR_FILE_IDENTIFIER);
1565
1566   if (value == NULL)
1567     Error(ERR_WARN, "configuration file has no file identifier");
1568   else if (!checkCookieString(value, identifier))
1569     Error(ERR_WARN, "configuration file has wrong file identifier");
1570 }
1571
1572
1573 /* ========================================================================= */
1574 /* setup file stuff                                                          */
1575 /* ========================================================================= */
1576
1577 #define TOKEN_STR_LAST_LEVEL_SERIES     "last_level_series"
1578 #define TOKEN_STR_LAST_PLAYED_LEVEL     "last_played_level"
1579 #define TOKEN_STR_HANDICAP_LEVEL        "handicap_level"
1580
1581 /* level directory info */
1582 #define LEVELINFO_TOKEN_IDENTIFIER      0
1583 #define LEVELINFO_TOKEN_NAME            1
1584 #define LEVELINFO_TOKEN_NAME_SORTING    2
1585 #define LEVELINFO_TOKEN_AUTHOR          3
1586 #define LEVELINFO_TOKEN_IMPORTED_FROM   4
1587 #define LEVELINFO_TOKEN_IMPORTED_BY     5
1588 #define LEVELINFO_TOKEN_LEVELS          6
1589 #define LEVELINFO_TOKEN_FIRST_LEVEL     7
1590 #define LEVELINFO_TOKEN_SORT_PRIORITY   8
1591 #define LEVELINFO_TOKEN_LATEST_ENGINE   9
1592 #define LEVELINFO_TOKEN_LEVEL_GROUP     10
1593 #define LEVELINFO_TOKEN_READONLY        11
1594 #define LEVELINFO_TOKEN_GRAPHICS_SET    12
1595 #define LEVELINFO_TOKEN_SOUNDS_SET      13
1596 #define LEVELINFO_TOKEN_MUSIC_SET       14
1597 #define LEVELINFO_TOKEN_FILENAME        15
1598 #define LEVELINFO_TOKEN_FILETYPE        16
1599 #define LEVELINFO_TOKEN_HANDICAP        17
1600 #define LEVELINFO_TOKEN_SKIP_LEVELS     18
1601
1602 #define NUM_LEVELINFO_TOKENS            19
1603
1604 static LevelDirTree ldi;
1605
1606 static struct TokenInfo levelinfo_tokens[] =
1607 {
1608   /* level directory info */
1609   { TYPE_STRING,        &ldi.identifier,        "identifier"    },
1610   { TYPE_STRING,        &ldi.name,              "name"          },
1611   { TYPE_STRING,        &ldi.name_sorting,      "name_sorting"  },
1612   { TYPE_STRING,        &ldi.author,            "author"        },
1613   { TYPE_STRING,        &ldi.imported_from,     "imported_from" },
1614   { TYPE_STRING,        &ldi.imported_by,       "imported_by"   },
1615   { TYPE_INTEGER,       &ldi.levels,            "levels"        },
1616   { TYPE_INTEGER,       &ldi.first_level,       "first_level"   },
1617   { TYPE_INTEGER,       &ldi.sort_priority,     "sort_priority" },
1618   { TYPE_BOOLEAN,       &ldi.latest_engine,     "latest_engine" },
1619   { TYPE_BOOLEAN,       &ldi.level_group,       "level_group"   },
1620   { TYPE_BOOLEAN,       &ldi.readonly,          "readonly"      },
1621   { TYPE_STRING,        &ldi.graphics_set,      "graphics_set"  },
1622   { TYPE_STRING,        &ldi.sounds_set,        "sounds_set"    },
1623   { TYPE_STRING,        &ldi.music_set,         "music_set"     },
1624   { TYPE_STRING,        &ldi.level_filename,    "filename"      },
1625   { TYPE_STRING,        &ldi.level_filetype,    "filetype"      },
1626   { TYPE_BOOLEAN,       &ldi.handicap,          "handicap"      },
1627   { TYPE_BOOLEAN,       &ldi.skip_levels,       "skip_levels"   }
1628 };
1629
1630 static void setTreeInfoToDefaults(TreeInfo *ldi, int type)
1631 {
1632   ldi->type = type;
1633
1634   ldi->node_top = (ldi->type == TREE_TYPE_LEVEL_DIR ? &leveldir_first :
1635                    ldi->type == TREE_TYPE_GRAPHICS_DIR ? &artwork.gfx_first :
1636                    ldi->type == TREE_TYPE_SOUNDS_DIR ? &artwork.snd_first :
1637                    ldi->type == TREE_TYPE_MUSIC_DIR ? &artwork.mus_first :
1638                    NULL);
1639
1640   ldi->node_parent = NULL;
1641   ldi->node_group = NULL;
1642   ldi->next = NULL;
1643
1644   ldi->cl_first = -1;
1645   ldi->cl_cursor = -1;
1646
1647   ldi->subdir = NULL;
1648   ldi->fullpath = NULL;
1649   ldi->basepath = NULL;
1650   ldi->identifier = NULL;
1651   ldi->name = getStringCopy(ANONYMOUS_NAME);
1652   ldi->name_sorting = NULL;
1653   ldi->author = getStringCopy(ANONYMOUS_NAME);
1654
1655   ldi->sort_priority = LEVELCLASS_UNDEFINED;    /* default: least priority */
1656   ldi->latest_engine = FALSE;                   /* default: get from level */
1657   ldi->parent_link = FALSE;
1658   ldi->in_user_dir = FALSE;
1659   ldi->user_defined = FALSE;
1660   ldi->color = 0;
1661   ldi->class_desc = NULL;
1662
1663   if (ldi->type == TREE_TYPE_LEVEL_DIR)
1664   {
1665     ldi->imported_from = NULL;
1666     ldi->imported_by = NULL;
1667
1668     ldi->graphics_set = NULL;
1669     ldi->sounds_set = NULL;
1670     ldi->music_set = NULL;
1671     ldi->graphics_path = getStringCopy(UNDEFINED_FILENAME);
1672     ldi->sounds_path = getStringCopy(UNDEFINED_FILENAME);
1673     ldi->music_path = getStringCopy(UNDEFINED_FILENAME);
1674
1675     ldi->level_filename = NULL;
1676     ldi->level_filetype = NULL;
1677
1678     ldi->levels = 0;
1679     ldi->first_level = 0;
1680     ldi->last_level = 0;
1681     ldi->level_group = FALSE;
1682     ldi->handicap_level = 0;
1683     ldi->readonly = TRUE;
1684     ldi->handicap = TRUE;
1685     ldi->skip_levels = FALSE;
1686   }
1687 }
1688
1689 static void setTreeInfoToDefaultsFromParent(TreeInfo *ldi, TreeInfo *parent)
1690 {
1691   if (parent == NULL)
1692   {
1693     Error(ERR_WARN, "setTreeInfoToDefaultsFromParent(): parent == NULL");
1694
1695     setTreeInfoToDefaults(ldi, TREE_TYPE_UNDEFINED);
1696
1697     return;
1698   }
1699
1700 #if 1
1701   /* copy all values from the parent structure */
1702
1703   ldi->type = parent->type;
1704
1705   ldi->node_top = parent->node_top;
1706   ldi->node_parent = parent;
1707   ldi->node_group = NULL;
1708   ldi->next = NULL;
1709
1710   ldi->cl_first = -1;
1711   ldi->cl_cursor = -1;
1712
1713   ldi->subdir = NULL;
1714   ldi->fullpath = NULL;
1715   ldi->basepath = NULL;
1716   ldi->identifier = NULL;
1717   ldi->name = getStringCopy(ANONYMOUS_NAME);
1718   ldi->name_sorting = NULL;
1719   ldi->author = getStringCopy(parent->author);
1720
1721   ldi->sort_priority = parent->sort_priority;
1722   ldi->latest_engine = parent->latest_engine;
1723   ldi->parent_link = FALSE;
1724   ldi->in_user_dir = parent->in_user_dir;
1725   ldi->user_defined = parent->user_defined;
1726   ldi->color = parent->color;
1727   ldi->class_desc = getStringCopy(parent->class_desc);
1728
1729   if (ldi->type == TREE_TYPE_LEVEL_DIR)
1730   {
1731     ldi->imported_from = getStringCopy(parent->imported_from);
1732     ldi->imported_by = getStringCopy(parent->imported_by);
1733
1734     ldi->graphics_set = NULL;
1735     ldi->sounds_set = NULL;
1736     ldi->music_set = NULL;
1737     ldi->graphics_path = getStringCopy(UNDEFINED_FILENAME);
1738     ldi->sounds_path = getStringCopy(UNDEFINED_FILENAME);
1739     ldi->music_path = getStringCopy(UNDEFINED_FILENAME);
1740
1741     ldi->level_filename = NULL;
1742     ldi->level_filetype = NULL;
1743
1744     ldi->levels = 0;
1745     ldi->first_level = 0;
1746     ldi->last_level = 0;
1747     ldi->level_group = FALSE;
1748     ldi->handicap_level = 0;
1749     ldi->readonly = TRUE;
1750     ldi->handicap = TRUE;
1751     ldi->skip_levels = FALSE;
1752   }
1753
1754 #else
1755
1756   /* first copy all values from the parent structure ... */
1757   *ldi = *parent;
1758
1759   /* ... then set all fields to default that cannot be inherited from parent.
1760      This is especially important for all those fields that can be set from
1761      the 'levelinfo.conf' config file, because the function 'setSetupInfo()'
1762      calls 'free()' for all already set token values which requires that no
1763      other structure's pointer may point to them!
1764   */
1765
1766   ldi->subdir = NULL;
1767   ldi->fullpath = NULL;
1768   ldi->basepath = NULL;
1769   ldi->identifier = NULL;
1770   ldi->name = getStringCopy(ANONYMOUS_NAME);
1771   ldi->name_sorting = NULL;
1772   ldi->author = getStringCopy(parent->author);
1773
1774   ldi->imported_from = getStringCopy(parent->imported_from);
1775   ldi->imported_by = getStringCopy(parent->imported_by);
1776   ldi->class_desc = getStringCopy(parent->class_desc);
1777
1778   ldi->graphics_set = NULL;
1779   ldi->sounds_set = NULL;
1780   ldi->music_set = NULL;
1781   ldi->graphics_path = NULL;
1782   ldi->sounds_path = NULL;
1783   ldi->music_path = NULL;
1784
1785   ldi->level_group = FALSE;
1786   ldi->parent_link = FALSE;
1787
1788   ldi->node_top = parent->node_top;
1789   ldi->node_parent = parent;
1790   ldi->node_group = NULL;
1791   ldi->next = NULL;
1792
1793 #endif
1794 }
1795
1796 static void freeTreeInfo(TreeInfo *ldi)
1797 {
1798   checked_free(ldi->subdir);
1799   checked_free(ldi->fullpath);
1800   checked_free(ldi->basepath);
1801   checked_free(ldi->identifier);
1802
1803   checked_free(ldi->name);
1804   checked_free(ldi->name_sorting);
1805   checked_free(ldi->author);
1806
1807   checked_free(ldi->class_desc);
1808
1809   if (ldi->type == TREE_TYPE_LEVEL_DIR)
1810   {
1811     checked_free(ldi->imported_from);
1812     checked_free(ldi->imported_by);
1813
1814     checked_free(ldi->graphics_set);
1815     checked_free(ldi->sounds_set);
1816     checked_free(ldi->music_set);
1817
1818     checked_free(ldi->graphics_path);
1819     checked_free(ldi->sounds_path);
1820     checked_free(ldi->music_path);
1821
1822     checked_free(ldi->level_filename);
1823     checked_free(ldi->level_filetype);
1824   }
1825 }
1826
1827 void setSetupInfo(struct TokenInfo *token_info,
1828                   int token_nr, char *token_value)
1829 {
1830   int token_type = token_info[token_nr].type;
1831   void *setup_value = token_info[token_nr].value;
1832
1833   if (token_value == NULL)
1834     return;
1835
1836   /* set setup field to corresponding token value */
1837   switch (token_type)
1838   {
1839     case TYPE_BOOLEAN:
1840     case TYPE_SWITCH:
1841       *(boolean *)setup_value = get_boolean_from_string(token_value);
1842       break;
1843
1844     case TYPE_KEY:
1845       *(Key *)setup_value = getKeyFromKeyName(token_value);
1846       break;
1847
1848     case TYPE_KEY_X11:
1849       *(Key *)setup_value = getKeyFromX11KeyName(token_value);
1850       break;
1851
1852     case TYPE_INTEGER:
1853       *(int *)setup_value = get_integer_from_string(token_value);
1854       break;
1855
1856     case TYPE_STRING:
1857       checked_free(*(char **)setup_value);
1858       *(char **)setup_value = getStringCopy(token_value);
1859       break;
1860
1861     default:
1862       break;
1863   }
1864 }
1865
1866 static int compareTreeInfoEntries(const void *object1, const void *object2)
1867 {
1868   const TreeInfo *entry1 = *((TreeInfo **)object1);
1869   const TreeInfo *entry2 = *((TreeInfo **)object2);
1870   int class_sorting1, class_sorting2;
1871   int compare_result;
1872
1873   if (entry1->type == TREE_TYPE_LEVEL_DIR)
1874   {
1875     class_sorting1 = LEVELSORTING(entry1);
1876     class_sorting2 = LEVELSORTING(entry2);
1877   }
1878   else
1879   {
1880     class_sorting1 = ARTWORKSORTING(entry1);
1881     class_sorting2 = ARTWORKSORTING(entry2);
1882   }
1883
1884   if (entry1->parent_link || entry2->parent_link)
1885     compare_result = (entry1->parent_link ? -1 : +1);
1886   else if (entry1->sort_priority == entry2->sort_priority)
1887   {
1888     char *name1 = getStringToLower(entry1->name_sorting);
1889     char *name2 = getStringToLower(entry2->name_sorting);
1890
1891     compare_result = strcmp(name1, name2);
1892
1893     free(name1);
1894     free(name2);
1895   }
1896   else if (class_sorting1 == class_sorting2)
1897     compare_result = entry1->sort_priority - entry2->sort_priority;
1898   else
1899     compare_result = class_sorting1 - class_sorting2;
1900
1901   return compare_result;
1902 }
1903
1904 static void createParentTreeInfoNode(TreeInfo *node_parent)
1905 {
1906   TreeInfo *ti_new;
1907
1908   if (node_parent == NULL)
1909     return;
1910
1911   ti_new = newTreeInfo();
1912   setTreeInfoToDefaults(ti_new, node_parent->type);
1913
1914   ti_new->node_parent = node_parent;
1915   ti_new->parent_link = TRUE;
1916
1917 #if 1
1918   setString(&ti_new->identifier, node_parent->identifier);
1919   setString(&ti_new->name, ".. (parent directory)");
1920   setString(&ti_new->name_sorting, ti_new->name);
1921
1922   setString(&ti_new->subdir, "..");
1923   setString(&ti_new->fullpath, node_parent->fullpath);
1924
1925   ti_new->sort_priority = node_parent->sort_priority;
1926   ti_new->latest_engine = node_parent->latest_engine;
1927
1928   setString(&ti_new->class_desc, getLevelClassDescription(ti_new));
1929 #else
1930   ti_new->identifier = getStringCopy(node_parent->identifier);
1931   ti_new->name = ".. (parent directory)";
1932   ti_new->name_sorting = getStringCopy(ti_new->name);
1933
1934   ti_new->subdir = "..";
1935   ti_new->fullpath = getStringCopy(node_parent->fullpath);
1936
1937   ti_new->sort_priority = node_parent->sort_priority;
1938   ti_new->latest_engine = node_parent->latest_engine;
1939
1940   ti_new->class_desc = getLevelClassDescription(ti_new);
1941 #endif
1942
1943   pushTreeInfo(&node_parent->node_group, ti_new);
1944 }
1945
1946 /* forward declaration for recursive call by "LoadLevelInfoFromLevelDir()" */
1947 static void LoadLevelInfoFromLevelDir(TreeInfo **, TreeInfo *, char *);
1948
1949 static boolean LoadLevelInfoFromLevelConf(TreeInfo **node_first,
1950                                           TreeInfo *node_parent,
1951                                           char *level_directory,
1952                                           char *directory_name)
1953 {
1954   char *directory_path = getPath2(level_directory, directory_name);
1955   char *filename = getPath2(directory_path, LEVELINFO_FILENAME);
1956   SetupFileHash *setup_file_hash = loadSetupFileHash(filename);
1957   LevelDirTree *leveldir_new = NULL;
1958   int i;
1959
1960   if (setup_file_hash == NULL)
1961   {
1962     Error(ERR_WARN, "ignoring level directory '%s'", directory_path);
1963
1964     free(directory_path);
1965     free(filename);
1966
1967     return FALSE;
1968   }
1969
1970   leveldir_new = newTreeInfo();
1971
1972   if (node_parent)
1973     setTreeInfoToDefaultsFromParent(leveldir_new, node_parent);
1974   else
1975     setTreeInfoToDefaults(leveldir_new, TREE_TYPE_LEVEL_DIR);
1976
1977   leveldir_new->subdir = getStringCopy(directory_name);
1978
1979   checkSetupFileHashIdentifier(setup_file_hash, getCookie("LEVELINFO"));
1980
1981   /* set all structure fields according to the token/value pairs */
1982   ldi = *leveldir_new;
1983   for (i = 0; i < NUM_LEVELINFO_TOKENS; i++)
1984     setSetupInfo(levelinfo_tokens, i,
1985                  getHashEntry(setup_file_hash, levelinfo_tokens[i].text));
1986   *leveldir_new = ldi;
1987
1988 #if 1
1989   if (strcmp(leveldir_new->name, ANONYMOUS_NAME) == 0)
1990     setString(&leveldir_new->name, leveldir_new->subdir);
1991 #else
1992   if (strcmp(leveldir_new->name, ANONYMOUS_NAME) == 0)
1993   {
1994     free(leveldir_new->name);
1995     leveldir_new->name = getStringCopy(leveldir_new->subdir);
1996   }
1997 #endif
1998
1999   DrawInitText(leveldir_new->name, 150, FC_YELLOW);
2000
2001   if (leveldir_new->identifier == NULL)
2002     leveldir_new->identifier = getStringCopy(leveldir_new->subdir);
2003
2004   if (leveldir_new->name_sorting == NULL)
2005     leveldir_new->name_sorting = getStringCopy(leveldir_new->name);
2006
2007   if (node_parent == NULL)              /* top level group */
2008   {
2009     leveldir_new->basepath = getStringCopy(level_directory);
2010     leveldir_new->fullpath = getStringCopy(leveldir_new->subdir);
2011   }
2012   else                                  /* sub level group */
2013   {
2014     leveldir_new->basepath = getStringCopy(node_parent->basepath);
2015     leveldir_new->fullpath = getPath2(node_parent->fullpath, directory_name);
2016   }
2017
2018   if (leveldir_new->levels < 1)
2019     leveldir_new->levels = 1;
2020
2021   leveldir_new->last_level =
2022     leveldir_new->first_level + leveldir_new->levels - 1;
2023
2024 #if 1
2025   leveldir_new->in_user_dir =
2026     (strcmp(leveldir_new->basepath, options.level_directory) != 0);
2027 #else
2028   leveldir_new->in_user_dir =
2029     (leveldir_new->basepath == options.level_directory ? FALSE : TRUE);
2030 #endif
2031
2032 #if 1
2033   /* adjust sort priority if user's private level directory was detected */
2034   if (leveldir_new->sort_priority == LEVELCLASS_UNDEFINED &&
2035       leveldir_new->in_user_dir &&
2036       strcmp(leveldir_new->subdir, getLoginName()) == 0)
2037     leveldir_new->sort_priority = LEVELCLASS_PRIVATE_START;
2038 #endif
2039
2040   leveldir_new->user_defined =
2041     (leveldir_new->in_user_dir && IS_LEVELCLASS_PRIVATE(leveldir_new));
2042
2043   leveldir_new->color = LEVELCOLOR(leveldir_new);
2044 #if 1
2045   setString(&leveldir_new->class_desc, getLevelClassDescription(leveldir_new));
2046 #else
2047   leveldir_new->class_desc = getLevelClassDescription(leveldir_new);
2048 #endif
2049
2050   leveldir_new->handicap_level =        /* set handicap to default value */
2051     (leveldir_new->user_defined || !leveldir_new->handicap ?
2052      leveldir_new->last_level : leveldir_new->first_level);
2053
2054   pushTreeInfo(node_first, leveldir_new);
2055
2056   freeSetupFileHash(setup_file_hash);
2057
2058   if (leveldir_new->level_group)
2059   {
2060     /* create node to link back to current level directory */
2061     createParentTreeInfoNode(leveldir_new);
2062
2063     /* step into sub-directory and look for more level series */
2064     LoadLevelInfoFromLevelDir(&leveldir_new->node_group,
2065                               leveldir_new, directory_path);
2066   }
2067
2068   free(directory_path);
2069   free(filename);
2070
2071   return TRUE;
2072 }
2073
2074 static void LoadLevelInfoFromLevelDir(TreeInfo **node_first,
2075                                       TreeInfo *node_parent,
2076                                       char *level_directory)
2077 {
2078   DIR *dir;
2079   struct dirent *dir_entry;
2080   boolean valid_entry_found = FALSE;
2081
2082   if ((dir = opendir(level_directory)) == NULL)
2083   {
2084     Error(ERR_WARN, "cannot read level directory '%s'", level_directory);
2085     return;
2086   }
2087
2088   while ((dir_entry = readdir(dir)) != NULL)    /* loop until last dir entry */
2089   {
2090     struct stat file_status;
2091     char *directory_name = dir_entry->d_name;
2092     char *directory_path = getPath2(level_directory, directory_name);
2093
2094     /* skip entries for current and parent directory */
2095     if (strcmp(directory_name, ".")  == 0 ||
2096         strcmp(directory_name, "..") == 0)
2097     {
2098       free(directory_path);
2099       continue;
2100     }
2101
2102     /* find out if directory entry is itself a directory */
2103     if (stat(directory_path, &file_status) != 0 ||      /* cannot stat file */
2104         (file_status.st_mode & S_IFMT) != S_IFDIR)      /* not a directory */
2105     {
2106       free(directory_path);
2107       continue;
2108     }
2109
2110     free(directory_path);
2111
2112     if (strcmp(directory_name, GRAPHICS_DIRECTORY) == 0 ||
2113         strcmp(directory_name, SOUNDS_DIRECTORY) == 0 ||
2114         strcmp(directory_name, MUSIC_DIRECTORY) == 0)
2115       continue;
2116
2117     valid_entry_found |= LoadLevelInfoFromLevelConf(node_first, node_parent,
2118                                                     level_directory,
2119                                                     directory_name);
2120   }
2121
2122   closedir(dir);
2123
2124   if (!valid_entry_found)
2125   {
2126     /* check if this directory directly contains a file "levelinfo.conf" */
2127     valid_entry_found |= LoadLevelInfoFromLevelConf(node_first, node_parent,
2128                                                     level_directory, ".");
2129   }
2130
2131   if (!valid_entry_found)
2132     Error(ERR_WARN, "cannot find any valid level series in directory '%s'",
2133           level_directory);
2134 }
2135
2136 void LoadLevelInfo()
2137 {
2138   InitUserLevelDirectory(getLoginName());
2139
2140   DrawInitText("Loading level series:", 120, FC_GREEN);
2141
2142   LoadLevelInfoFromLevelDir(&leveldir_first, NULL, options.level_directory);
2143   LoadLevelInfoFromLevelDir(&leveldir_first, NULL, getUserLevelDir(NULL));
2144
2145   /* before sorting, the first entries will be from the user directory */
2146   leveldir_current = getFirstValidTreeInfoEntry(leveldir_first);
2147
2148   if (leveldir_first == NULL)
2149     Error(ERR_EXIT, "cannot find any valid level series in any directory");
2150
2151   sortTreeInfo(&leveldir_first, compareTreeInfoEntries);
2152
2153 #if 0
2154   dumpTreeInfo(leveldir_first, 0);
2155 #endif
2156 }
2157
2158 static boolean LoadArtworkInfoFromArtworkConf(TreeInfo **node_first,
2159                                               TreeInfo *node_parent,
2160                                               char *base_directory,
2161                                               char *directory_name, int type)
2162 {
2163   char *directory_path = getPath2(base_directory, directory_name);
2164   char *filename = getPath2(directory_path, ARTWORKINFO_FILENAME(type));
2165   SetupFileHash *setup_file_hash = NULL;
2166   TreeInfo *artwork_new = NULL;
2167   int i;
2168
2169   if (access(filename, F_OK) == 0)              /* file exists */
2170     setup_file_hash = loadSetupFileHash(filename);
2171
2172   if (setup_file_hash == NULL)  /* no config file -- look for artwork files */
2173   {
2174     DIR *dir;
2175     struct dirent *dir_entry;
2176     boolean valid_file_found = FALSE;
2177
2178     if ((dir = opendir(directory_path)) != NULL)
2179     {
2180       while ((dir_entry = readdir(dir)) != NULL)
2181       {
2182         char *entry_name = dir_entry->d_name;
2183
2184         if (FileIsArtworkType(entry_name, type))
2185         {
2186           valid_file_found = TRUE;
2187           break;
2188         }
2189       }
2190
2191       closedir(dir);
2192     }
2193
2194     if (!valid_file_found)
2195     {
2196       if (strcmp(directory_name, ".") != 0)
2197         Error(ERR_WARN, "ignoring artwork directory '%s'", directory_path);
2198
2199       free(directory_path);
2200       free(filename);
2201
2202       return FALSE;
2203     }
2204   }
2205
2206   artwork_new = newTreeInfo();
2207
2208   if (node_parent)
2209     setTreeInfoToDefaultsFromParent(artwork_new, node_parent);
2210   else
2211     setTreeInfoToDefaults(artwork_new, type);
2212
2213   artwork_new->subdir = getStringCopy(directory_name);
2214
2215   if (setup_file_hash)  /* (before defining ".color" and ".class_desc") */
2216   {
2217 #if 0
2218     checkSetupFileHashIdentifier(setup_file_hash, getCookie("..."));
2219 #endif
2220
2221     /* set all structure fields according to the token/value pairs */
2222     ldi = *artwork_new;
2223     for (i = 0; i < NUM_LEVELINFO_TOKENS; i++)
2224       setSetupInfo(levelinfo_tokens, i,
2225                    getHashEntry(setup_file_hash, levelinfo_tokens[i].text));
2226     *artwork_new = ldi;
2227
2228 #if 1
2229     if (strcmp(artwork_new->name, ANONYMOUS_NAME) == 0)
2230       setString(&artwork_new->name, artwork_new->subdir);
2231 #else
2232     if (strcmp(artwork_new->name, ANONYMOUS_NAME) == 0)
2233     {
2234       free(artwork_new->name);
2235       artwork_new->name = getStringCopy(artwork_new->subdir);
2236     }
2237 #endif
2238
2239 #if 0
2240     DrawInitText(artwork_new->name, 150, FC_YELLOW);
2241 #endif
2242
2243     if (artwork_new->identifier == NULL)
2244       artwork_new->identifier = getStringCopy(artwork_new->subdir);
2245
2246     if (artwork_new->name_sorting == NULL)
2247       artwork_new->name_sorting = getStringCopy(artwork_new->name);
2248   }
2249
2250   if (node_parent == NULL)              /* top level group */
2251   {
2252     artwork_new->basepath = getStringCopy(base_directory);
2253     artwork_new->fullpath = getStringCopy(artwork_new->subdir);
2254   }
2255   else                                  /* sub level group */
2256   {
2257     artwork_new->basepath = getStringCopy(node_parent->basepath);
2258     artwork_new->fullpath = getPath2(node_parent->fullpath, directory_name);
2259   }
2260
2261 #if 1
2262   artwork_new->in_user_dir =
2263     (strcmp(artwork_new->basepath, OPTIONS_ARTWORK_DIRECTORY(type)) != 0);
2264 #else
2265   artwork_new->in_user_dir =
2266     (artwork_new->basepath == OPTIONS_ARTWORK_DIRECTORY(type) ? FALSE : TRUE);
2267 #endif
2268
2269   /* (may use ".sort_priority" from "setup_file_hash" above) */
2270   artwork_new->color = ARTWORKCOLOR(artwork_new);
2271 #if 1
2272   setString(&artwork_new->class_desc, getLevelClassDescription(artwork_new));
2273 #else
2274   artwork_new->class_desc = getLevelClassDescription(artwork_new);
2275 #endif
2276
2277   if (setup_file_hash == NULL)  /* (after determining ".user_defined") */
2278   {
2279 #if 0
2280     if (artwork_new->name != NULL)
2281     {
2282       free(artwork_new->name);
2283       artwork_new->name = NULL;
2284     }
2285 #endif
2286
2287 #if 0
2288     if (artwork_new->identifier != NULL)
2289     {
2290       free(artwork_new->identifier);
2291       artwork_new->identifier = NULL;
2292     }
2293 #endif
2294
2295     if (strcmp(artwork_new->subdir, ".") == 0)
2296     {
2297       if (artwork_new->user_defined)
2298       {
2299 #if 1
2300         setString(&artwork_new->identifier, "private");
2301 #else
2302         artwork_new->identifier = getStringCopy("private");
2303 #endif
2304         artwork_new->sort_priority = ARTWORKCLASS_PRIVATE;
2305       }
2306       else
2307       {
2308 #if 1
2309         setString(&artwork_new->identifier, "classic");
2310 #else
2311         artwork_new->identifier = getStringCopy("classic");
2312 #endif
2313         artwork_new->sort_priority = ARTWORKCLASS_CLASSICS;
2314       }
2315
2316       /* set to new values after changing ".sort_priority" */
2317       artwork_new->color = ARTWORKCOLOR(artwork_new);
2318 #if 1
2319       setString(&artwork_new->class_desc,
2320                 getLevelClassDescription(artwork_new));
2321 #else
2322       artwork_new->class_desc = getLevelClassDescription(artwork_new);
2323 #endif
2324     }
2325     else
2326     {
2327 #if 1
2328       setString(&artwork_new->identifier, artwork_new->subdir);
2329 #else
2330       artwork_new->identifier = getStringCopy(artwork_new->subdir);
2331 #endif
2332     }
2333
2334 #if 1
2335     setString(&artwork_new->name, artwork_new->identifier);
2336     setString(&artwork_new->name_sorting, artwork_new->name);
2337 #else
2338     artwork_new->name = getStringCopy(artwork_new->identifier);
2339     artwork_new->name_sorting = getStringCopy(artwork_new->name);
2340 #endif
2341   }
2342
2343   DrawInitText(artwork_new->name, 150, FC_YELLOW);
2344
2345   pushTreeInfo(node_first, artwork_new);
2346
2347   freeSetupFileHash(setup_file_hash);
2348
2349   free(directory_path);
2350   free(filename);
2351
2352   return TRUE;
2353 }
2354
2355 static void LoadArtworkInfoFromArtworkDir(TreeInfo **node_first,
2356                                           TreeInfo *node_parent,
2357                                           char *base_directory, int type)
2358 {
2359   DIR *dir;
2360   struct dirent *dir_entry;
2361   boolean valid_entry_found = FALSE;
2362
2363   if ((dir = opendir(base_directory)) == NULL)
2364   {
2365     if (base_directory == OPTIONS_ARTWORK_DIRECTORY(type))
2366       Error(ERR_WARN, "cannot read directory '%s'", base_directory);
2367     return;
2368   }
2369
2370   while ((dir_entry = readdir(dir)) != NULL)    /* loop until last dir entry */
2371   {
2372     struct stat file_status;
2373     char *directory_name = dir_entry->d_name;
2374     char *directory_path = getPath2(base_directory, directory_name);
2375
2376     /* skip entries for current and parent directory */
2377     if (strcmp(directory_name, ".")  == 0 ||
2378         strcmp(directory_name, "..") == 0)
2379     {
2380       free(directory_path);
2381       continue;
2382     }
2383
2384     /* find out if directory entry is itself a directory */
2385     if (stat(directory_path, &file_status) != 0 ||      /* cannot stat file */
2386         (file_status.st_mode & S_IFMT) != S_IFDIR)      /* not a directory */
2387     {
2388       free(directory_path);
2389       continue;
2390     }
2391
2392     free(directory_path);
2393
2394     /* check if this directory contains artwork with or without config file */
2395     valid_entry_found |= LoadArtworkInfoFromArtworkConf(node_first,node_parent,
2396                                                         base_directory,
2397                                                         directory_name, type);
2398   }
2399
2400   closedir(dir);
2401
2402   /* check if this directory directly contains artwork itself */
2403   valid_entry_found |= LoadArtworkInfoFromArtworkConf(node_first,node_parent,
2404                                                       base_directory, ".",
2405                                                       type);
2406   if (!valid_entry_found)
2407     Error(ERR_WARN, "cannot find any valid artwork in directory '%s'",
2408           base_directory);
2409 }
2410
2411 static TreeInfo *getDummyArtworkInfo(int type)
2412 {
2413   /* this is only needed when there is completely no artwork available */
2414   TreeInfo *artwork_new = newTreeInfo();
2415
2416   setTreeInfoToDefaults(artwork_new, type);
2417
2418 #if 1
2419   setString(&artwork_new->subdir,   UNDEFINED_FILENAME);
2420   setString(&artwork_new->fullpath, UNDEFINED_FILENAME);
2421   setString(&artwork_new->basepath, UNDEFINED_FILENAME);
2422
2423   setString(&artwork_new->identifier,   UNDEFINED_FILENAME);
2424   setString(&artwork_new->name,         UNDEFINED_FILENAME);
2425   setString(&artwork_new->name_sorting, UNDEFINED_FILENAME);
2426 #else
2427   artwork_new->subdir   = getStringCopy(UNDEFINED_FILENAME);
2428   artwork_new->fullpath = getStringCopy(UNDEFINED_FILENAME);
2429   artwork_new->basepath = getStringCopy(UNDEFINED_FILENAME);
2430
2431   checked_free(artwork_new->name);
2432
2433   artwork_new->identifier   = getStringCopy(UNDEFINED_FILENAME);
2434   artwork_new->name         = getStringCopy(UNDEFINED_FILENAME);
2435   artwork_new->name_sorting = getStringCopy(UNDEFINED_FILENAME);
2436 #endif
2437
2438   return artwork_new;
2439 }
2440
2441 void LoadArtworkInfo()
2442 {
2443   DrawInitText("Looking for custom artwork:", 120, FC_GREEN);
2444
2445   LoadArtworkInfoFromArtworkDir(&artwork.gfx_first, NULL,
2446                                 options.graphics_directory,
2447                                 TREE_TYPE_GRAPHICS_DIR);
2448   LoadArtworkInfoFromArtworkDir(&artwork.gfx_first, NULL,
2449                                 getUserGraphicsDir(),
2450                                 TREE_TYPE_GRAPHICS_DIR);
2451
2452   LoadArtworkInfoFromArtworkDir(&artwork.snd_first, NULL,
2453                                 options.sounds_directory,
2454                                 TREE_TYPE_SOUNDS_DIR);
2455   LoadArtworkInfoFromArtworkDir(&artwork.snd_first, NULL,
2456                                 getUserSoundsDir(),
2457                                 TREE_TYPE_SOUNDS_DIR);
2458
2459   LoadArtworkInfoFromArtworkDir(&artwork.mus_first, NULL,
2460                                 options.music_directory,
2461                                 TREE_TYPE_MUSIC_DIR);
2462   LoadArtworkInfoFromArtworkDir(&artwork.mus_first, NULL,
2463                                 getUserMusicDir(),
2464                                 TREE_TYPE_MUSIC_DIR);
2465
2466   if (artwork.gfx_first == NULL)
2467     artwork.gfx_first = getDummyArtworkInfo(TREE_TYPE_GRAPHICS_DIR);
2468   if (artwork.snd_first == NULL)
2469     artwork.snd_first = getDummyArtworkInfo(TREE_TYPE_SOUNDS_DIR);
2470   if (artwork.mus_first == NULL)
2471     artwork.mus_first = getDummyArtworkInfo(TREE_TYPE_MUSIC_DIR);
2472
2473   /* before sorting, the first entries will be from the user directory */
2474   artwork.gfx_current =
2475     getTreeInfoFromIdentifier(artwork.gfx_first, setup.graphics_set);
2476   if (artwork.gfx_current == NULL)
2477     artwork.gfx_current =
2478       getTreeInfoFromIdentifier(artwork.gfx_first, GFX_CLASSIC_SUBDIR);
2479   if (artwork.gfx_current == NULL)
2480     artwork.gfx_current = getFirstValidTreeInfoEntry(artwork.gfx_first);
2481
2482   artwork.snd_current =
2483     getTreeInfoFromIdentifier(artwork.snd_first, setup.sounds_set);
2484   if (artwork.snd_current == NULL)
2485     artwork.snd_current =
2486       getTreeInfoFromIdentifier(artwork.snd_first, SND_CLASSIC_SUBDIR);
2487   if (artwork.snd_current == NULL)
2488     artwork.snd_current = getFirstValidTreeInfoEntry(artwork.snd_first);
2489
2490   artwork.mus_current =
2491     getTreeInfoFromIdentifier(artwork.mus_first, setup.music_set);
2492   if (artwork.mus_current == NULL)
2493     artwork.mus_current =
2494       getTreeInfoFromIdentifier(artwork.mus_first, MUS_CLASSIC_SUBDIR);
2495   if (artwork.mus_current == NULL)
2496     artwork.mus_current = getFirstValidTreeInfoEntry(artwork.mus_first);
2497
2498   artwork.gfx_current_identifier = artwork.gfx_current->identifier;
2499   artwork.snd_current_identifier = artwork.snd_current->identifier;
2500   artwork.mus_current_identifier = artwork.mus_current->identifier;
2501
2502 #if 0
2503   printf("graphics set == %s\n\n", artwork.gfx_current_identifier);
2504   printf("sounds set == %s\n\n", artwork.snd_current_identifier);
2505   printf("music set == %s\n\n", artwork.mus_current_identifier);
2506 #endif
2507
2508   sortTreeInfo(&artwork.gfx_first, compareTreeInfoEntries);
2509   sortTreeInfo(&artwork.snd_first, compareTreeInfoEntries);
2510   sortTreeInfo(&artwork.mus_first, compareTreeInfoEntries);
2511
2512 #if 0
2513   dumpTreeInfo(artwork.gfx_first, 0);
2514   dumpTreeInfo(artwork.snd_first, 0);
2515   dumpTreeInfo(artwork.mus_first, 0);
2516 #endif
2517 }
2518
2519 void LoadArtworkInfoFromLevelInfo(ArtworkDirTree **artwork_node,
2520                                   LevelDirTree *level_node)
2521 {
2522   /* recursively check all level directories for artwork sub-directories */
2523
2524   while (level_node)
2525   {
2526     char *path = getPath2(getLevelDirFromTreeInfo(level_node),
2527                           ARTWORK_DIRECTORY((*artwork_node)->type));
2528
2529 #if 0
2530     if (!level_node->parent_link)
2531       printf("CHECKING '%s' ['%s', '%s'] ...\n", path,
2532              level_node->subdir, level_node->name);
2533 #endif
2534
2535     if (!level_node->parent_link)
2536     {
2537       TreeInfo *topnode_last = *artwork_node;
2538
2539       LoadArtworkInfoFromArtworkDir(artwork_node, NULL, path,
2540                                     (*artwork_node)->type);
2541
2542       if (topnode_last != *artwork_node)
2543       {
2544         free((*artwork_node)->identifier);
2545         free((*artwork_node)->name);
2546         free((*artwork_node)->name_sorting);
2547
2548         (*artwork_node)->identifier   = getStringCopy(level_node->subdir);
2549         (*artwork_node)->name         = getStringCopy(level_node->name);
2550         (*artwork_node)->name_sorting = getStringCopy(level_node->name);
2551
2552         (*artwork_node)->sort_priority = level_node->sort_priority;
2553         (*artwork_node)->color = LEVELCOLOR((*artwork_node));
2554       }
2555     }
2556
2557     free(path);
2558
2559     if (level_node->node_group != NULL)
2560       LoadArtworkInfoFromLevelInfo(artwork_node, level_node->node_group);
2561
2562     level_node = level_node->next;
2563   }
2564 }
2565
2566 void LoadLevelArtworkInfo()
2567 {
2568   DrawInitText("Looking for custom level artwork:", 120, FC_GREEN);
2569
2570   LoadArtworkInfoFromLevelInfo(&artwork.gfx_first, leveldir_first);
2571   LoadArtworkInfoFromLevelInfo(&artwork.snd_first, leveldir_first);
2572   LoadArtworkInfoFromLevelInfo(&artwork.mus_first, leveldir_first);
2573
2574   /* needed for reloading level artwork not known at ealier stage */
2575
2576   if (strcmp(artwork.gfx_current_identifier, setup.graphics_set) != 0)
2577   {
2578     artwork.gfx_current =
2579       getTreeInfoFromIdentifier(artwork.gfx_first, setup.graphics_set);
2580     if (artwork.gfx_current == NULL)
2581       artwork.gfx_current =
2582         getTreeInfoFromIdentifier(artwork.gfx_first, GFX_CLASSIC_SUBDIR);
2583     if (artwork.gfx_current == NULL)
2584       artwork.gfx_current = getFirstValidTreeInfoEntry(artwork.gfx_first);
2585   }
2586
2587   if (strcmp(artwork.snd_current_identifier, setup.sounds_set) != 0)
2588   {
2589     artwork.snd_current =
2590       getTreeInfoFromIdentifier(artwork.snd_first, setup.sounds_set);
2591     if (artwork.snd_current == NULL)
2592       artwork.snd_current =
2593         getTreeInfoFromIdentifier(artwork.snd_first, SND_CLASSIC_SUBDIR);
2594     if (artwork.snd_current == NULL)
2595       artwork.snd_current = getFirstValidTreeInfoEntry(artwork.snd_first);
2596   }
2597
2598   if (strcmp(artwork.mus_current_identifier, setup.music_set) != 0)
2599   {
2600     artwork.mus_current =
2601       getTreeInfoFromIdentifier(artwork.mus_first, setup.music_set);
2602     if (artwork.mus_current == NULL)
2603       artwork.mus_current =
2604         getTreeInfoFromIdentifier(artwork.mus_first, MUS_CLASSIC_SUBDIR);
2605     if (artwork.mus_current == NULL)
2606       artwork.mus_current = getFirstValidTreeInfoEntry(artwork.mus_first);
2607   }
2608
2609   sortTreeInfo(&artwork.gfx_first, compareTreeInfoEntries);
2610   sortTreeInfo(&artwork.snd_first, compareTreeInfoEntries);
2611   sortTreeInfo(&artwork.mus_first, compareTreeInfoEntries);
2612
2613 #if 0
2614   dumpTreeInfo(artwork.gfx_first, 0);
2615   dumpTreeInfo(artwork.snd_first, 0);
2616   dumpTreeInfo(artwork.mus_first, 0);
2617 #endif
2618 }
2619
2620 static void SaveUserLevelInfo()
2621 {
2622   LevelDirTree *level_info;
2623   char *filename;
2624   FILE *file;
2625   int i;
2626
2627   filename = getPath2(getUserLevelDir(getLoginName()), LEVELINFO_FILENAME);
2628
2629   if (!(file = fopen(filename, MODE_WRITE)))
2630   {
2631     Error(ERR_WARN, "cannot write level info file '%s'", filename);
2632     free(filename);
2633     return;
2634   }
2635
2636   level_info = newTreeInfo();
2637
2638   /* always start with reliable default values */
2639   setTreeInfoToDefaults(level_info, TREE_TYPE_LEVEL_DIR);
2640
2641 #if 1
2642   setString(&level_info->name, getLoginName());
2643   setString(&level_info->author, getRealName());
2644   level_info->levels = 100;
2645   level_info->first_level = 1;
2646 #if 0
2647   level_info->sort_priority = LEVELCLASS_PRIVATE_START;
2648   level_info->readonly = FALSE;
2649   setString(&level_info->graphics_set, GFX_CLASSIC_SUBDIR);
2650   setString(&level_info->sounds_set,   SND_CLASSIC_SUBDIR);
2651   setString(&level_info->music_set,    MUS_CLASSIC_SUBDIR);
2652 #endif
2653 #else
2654   ldi.name = getStringCopy(getLoginName());
2655   ldi.author = getStringCopy(getRealName());
2656   ldi.levels = 100;
2657   ldi.first_level = 1;
2658   ldi.sort_priority = LEVELCLASS_PRIVATE_START;
2659   ldi.readonly = FALSE;
2660   ldi.graphics_set = getStringCopy(GFX_CLASSIC_SUBDIR);
2661   ldi.sounds_set = getStringCopy(SND_CLASSIC_SUBDIR);
2662   ldi.music_set = getStringCopy(MUS_CLASSIC_SUBDIR);
2663 #endif
2664
2665   token_value_position = TOKEN_VALUE_POSITION_SHORT;
2666
2667   fprintf(file, "%s\n\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER,
2668                                                  getCookie("LEVELINFO")));
2669
2670   ldi = *level_info;
2671   for (i = 0; i < NUM_LEVELINFO_TOKENS; i++)
2672   {
2673 #if 1
2674     if (i == LEVELINFO_TOKEN_NAME ||
2675         i == LEVELINFO_TOKEN_AUTHOR ||
2676         i == LEVELINFO_TOKEN_LEVELS ||
2677         i == LEVELINFO_TOKEN_FIRST_LEVEL)
2678       fprintf(file, "%s\n", getSetupLine(levelinfo_tokens, "", i));
2679
2680     /* just to make things nicer :) */
2681     if (i == LEVELINFO_TOKEN_AUTHOR)
2682       fprintf(file, "\n");      
2683 #else
2684     if (i != LEVELINFO_TOKEN_IDENTIFIER &&
2685         i != LEVELINFO_TOKEN_NAME_SORTING &&
2686         i != LEVELINFO_TOKEN_IMPORTED_FROM &&
2687         i != LEVELINFO_TOKEN_IMPORTED_BY &&
2688         i != LEVELINFO_TOKEN_FILENAME &&
2689         i != LEVELINFO_TOKEN_FILETYPE)
2690       fprintf(file, "%s\n", getSetupLine(levelinfo_tokens, "", i));
2691 #endif
2692   }
2693
2694   token_value_position = TOKEN_VALUE_POSITION_DEFAULT;
2695
2696   fclose(file);
2697
2698   SetFilePermissions(filename, PERMS_PRIVATE);
2699
2700   freeTreeInfo(level_info);
2701   free(filename);
2702 }
2703
2704 char *getSetupValue(int type, void *value)
2705 {
2706   static char value_string[MAX_LINE_LEN];
2707
2708   if (value == NULL)
2709     return NULL;
2710
2711   switch (type)
2712   {
2713     case TYPE_BOOLEAN:
2714       strcpy(value_string, (*(boolean *)value ? "true" : "false"));
2715       break;
2716
2717     case TYPE_SWITCH:
2718       strcpy(value_string, (*(boolean *)value ? "on" : "off"));
2719       break;
2720
2721     case TYPE_YES_NO:
2722       strcpy(value_string, (*(boolean *)value ? "yes" : "no"));
2723       break;
2724
2725     case TYPE_KEY:
2726       strcpy(value_string, getKeyNameFromKey(*(Key *)value));
2727       break;
2728
2729     case TYPE_KEY_X11:
2730       strcpy(value_string, getX11KeyNameFromKey(*(Key *)value));
2731       break;
2732
2733     case TYPE_INTEGER:
2734       sprintf(value_string, "%d", *(int *)value);
2735       break;
2736
2737     case TYPE_STRING:
2738       strcpy(value_string, *(char **)value);
2739       break;
2740
2741     default:
2742       value_string[0] = '\0';
2743       break;
2744   }
2745
2746   return value_string;
2747 }
2748
2749 char *getSetupLine(struct TokenInfo *token_info, char *prefix, int token_nr)
2750 {
2751   int i;
2752   char *line;
2753   static char token_string[MAX_LINE_LEN];
2754   int token_type = token_info[token_nr].type;
2755   void *setup_value = token_info[token_nr].value;
2756   char *token_text = token_info[token_nr].text;
2757   char *value_string = getSetupValue(token_type, setup_value);
2758
2759   /* build complete token string */
2760   sprintf(token_string, "%s%s", prefix, token_text);
2761
2762   /* build setup entry line */
2763   line = getFormattedSetupEntry(token_string, value_string);
2764
2765   if (token_type == TYPE_KEY_X11)
2766   {
2767     Key key = *(Key *)setup_value;
2768     char *keyname = getKeyNameFromKey(key);
2769
2770     /* add comment, if useful */
2771     if (strcmp(keyname, "(undefined)") != 0 &&
2772         strcmp(keyname, "(unknown)") != 0)
2773     {
2774       /* add at least one whitespace */
2775       strcat(line, " ");
2776       for (i = strlen(line); i < token_comment_position; i++)
2777         strcat(line, " ");
2778
2779       strcat(line, "# ");
2780       strcat(line, keyname);
2781     }
2782   }
2783
2784   return line;
2785 }
2786
2787 void LoadLevelSetup_LastSeries()
2788 {
2789   /* ----------------------------------------------------------------------- */
2790   /* ~/.<program>/levelsetup.conf                                            */
2791   /* ----------------------------------------------------------------------- */
2792
2793   char *filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME);
2794   SetupFileHash *level_setup_hash = NULL;
2795
2796   /* always start with reliable default values */
2797   leveldir_current = getFirstValidTreeInfoEntry(leveldir_first);
2798
2799   if ((level_setup_hash = loadSetupFileHash(filename)))
2800   {
2801     char *last_level_series =
2802       getHashEntry(level_setup_hash, TOKEN_STR_LAST_LEVEL_SERIES);
2803
2804     leveldir_current = getTreeInfoFromIdentifier(leveldir_first,
2805                                                  last_level_series);
2806     if (leveldir_current == NULL)
2807       leveldir_current = getFirstValidTreeInfoEntry(leveldir_first);
2808
2809     checkSetupFileHashIdentifier(level_setup_hash, getCookie("LEVELSETUP"));
2810
2811     freeSetupFileHash(level_setup_hash);
2812   }
2813   else
2814     Error(ERR_WARN, "using default setup values");
2815
2816   free(filename);
2817 }
2818
2819 void SaveLevelSetup_LastSeries()
2820 {
2821   /* ----------------------------------------------------------------------- */
2822   /* ~/.<program>/levelsetup.conf                                            */
2823   /* ----------------------------------------------------------------------- */
2824
2825   char *filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME);
2826   char *level_subdir = leveldir_current->subdir;
2827   FILE *file;
2828
2829   InitUserDataDirectory();
2830
2831   if (!(file = fopen(filename, MODE_WRITE)))
2832   {
2833     Error(ERR_WARN, "cannot write setup file '%s'", filename);
2834     free(filename);
2835     return;
2836   }
2837
2838   fprintf(file, "%s\n\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER,
2839                                                  getCookie("LEVELSETUP")));
2840   fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_LAST_LEVEL_SERIES,
2841                                                level_subdir));
2842
2843   fclose(file);
2844
2845   SetFilePermissions(filename, PERMS_PRIVATE);
2846
2847   free(filename);
2848 }
2849
2850 static void checkSeriesInfo()
2851 {
2852   static char *level_directory = NULL;
2853   DIR *dir;
2854   struct dirent *dir_entry;
2855
2856   /* check for more levels besides the 'levels' field of 'levelinfo.conf' */
2857
2858   level_directory = getPath2((leveldir_current->in_user_dir ?
2859                               getUserLevelDir(NULL) :
2860                               options.level_directory),
2861                              leveldir_current->fullpath);
2862
2863   if ((dir = opendir(level_directory)) == NULL)
2864   {
2865     Error(ERR_WARN, "cannot read level directory '%s'", level_directory);
2866     return;
2867   }
2868
2869   while ((dir_entry = readdir(dir)) != NULL)    /* last directory entry */
2870   {
2871     if (strlen(dir_entry->d_name) > 4 &&
2872         dir_entry->d_name[3] == '.' &&
2873         strcmp(&dir_entry->d_name[4], LEVELFILE_EXTENSION) == 0)
2874     {
2875       char levelnum_str[4];
2876       int levelnum_value;
2877
2878       strncpy(levelnum_str, dir_entry->d_name, 3);
2879       levelnum_str[3] = '\0';
2880
2881       levelnum_value = atoi(levelnum_str);
2882
2883 #if 0
2884       if (levelnum_value < leveldir_current->first_level)
2885       {
2886         Error(ERR_WARN, "additional level %d found", levelnum_value);
2887         leveldir_current->first_level = levelnum_value;
2888       }
2889       else if (levelnum_value > leveldir_current->last_level)
2890       {
2891         Error(ERR_WARN, "additional level %d found", levelnum_value);
2892         leveldir_current->last_level = levelnum_value;
2893       }
2894 #endif
2895     }
2896   }
2897
2898   closedir(dir);
2899 }
2900
2901 void LoadLevelSetup_SeriesInfo()
2902 {
2903   char *filename;
2904   SetupFileHash *level_setup_hash = NULL;
2905   char *level_subdir = leveldir_current->subdir;
2906
2907   /* always start with reliable default values */
2908   level_nr = leveldir_current->first_level;
2909
2910   checkSeriesInfo(leveldir_current);
2911
2912   /* ----------------------------------------------------------------------- */
2913   /* ~/.<program>/levelsetup/<level series>/levelsetup.conf                  */
2914   /* ----------------------------------------------------------------------- */
2915
2916   level_subdir = leveldir_current->subdir;
2917
2918   filename = getPath2(getLevelSetupDir(level_subdir), LEVELSETUP_FILENAME);
2919
2920   if ((level_setup_hash = loadSetupFileHash(filename)))
2921   {
2922     char *token_value;
2923
2924     token_value = getHashEntry(level_setup_hash, TOKEN_STR_LAST_PLAYED_LEVEL);
2925
2926     if (token_value)
2927     {
2928       level_nr = atoi(token_value);
2929
2930       if (level_nr < leveldir_current->first_level)
2931         level_nr = leveldir_current->first_level;
2932       if (level_nr > leveldir_current->last_level)
2933         level_nr = leveldir_current->last_level;
2934     }
2935
2936     token_value = getHashEntry(level_setup_hash, TOKEN_STR_HANDICAP_LEVEL);
2937
2938     if (token_value)
2939     {
2940       int level_nr = atoi(token_value);
2941
2942       if (level_nr < leveldir_current->first_level)
2943         level_nr = leveldir_current->first_level;
2944       if (level_nr > leveldir_current->last_level + 1)
2945         level_nr = leveldir_current->last_level;
2946
2947       if (leveldir_current->user_defined || !leveldir_current->handicap)
2948         level_nr = leveldir_current->last_level;
2949
2950       leveldir_current->handicap_level = level_nr;
2951     }
2952
2953     checkSetupFileHashIdentifier(level_setup_hash, getCookie("LEVELSETUP"));
2954
2955     freeSetupFileHash(level_setup_hash);
2956   }
2957   else
2958     Error(ERR_WARN, "using default setup values");
2959
2960   free(filename);
2961 }
2962
2963 void SaveLevelSetup_SeriesInfo()
2964 {
2965   char *filename;
2966   char *level_subdir = leveldir_current->subdir;
2967   char *level_nr_str = int2str(level_nr, 0);
2968   char *handicap_level_str = int2str(leveldir_current->handicap_level, 0);
2969   FILE *file;
2970
2971   /* ----------------------------------------------------------------------- */
2972   /* ~/.<program>/levelsetup/<level series>/levelsetup.conf                  */
2973   /* ----------------------------------------------------------------------- */
2974
2975   InitLevelSetupDirectory(level_subdir);
2976
2977   filename = getPath2(getLevelSetupDir(level_subdir), LEVELSETUP_FILENAME);
2978
2979   if (!(file = fopen(filename, MODE_WRITE)))
2980   {
2981     Error(ERR_WARN, "cannot write setup file '%s'", filename);
2982     free(filename);
2983     return;
2984   }
2985
2986   fprintf(file, "%s\n\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER,
2987                                                  getCookie("LEVELSETUP")));
2988   fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_LAST_PLAYED_LEVEL,
2989                                                level_nr_str));
2990   fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_HANDICAP_LEVEL,
2991                                                handicap_level_str));
2992
2993   fclose(file);
2994
2995   SetFilePermissions(filename, PERMS_PRIVATE);
2996
2997   free(filename);
2998 }