Main Page | Data Structures | Directories | File List | Data Fields | Globals

fileutil.c

Go to the documentation of this file.
00001  /* ---------------------------------------------------------------------- *
00002  * fileutil.c
00003  * This file is part of lincity.
00004  * Lincity is copyright (c) I J Peters 1995-1997, (c) Greg Sharp 1997-2001.
00005  * ---------------------------------------------------------------------- */
00006 #include "lcconfig.h"
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <stdarg.h> /* XXX: GCS FIX: What does configure need to know? */
00010 #include "lcintl.h"
00011 #include "lcstring.h"
00012 #include "ldsvgui.h"
00013 
00014 /* this is for OS/2 - RVI */
00015 #ifdef __EMX__
00016 #include <sys/select.h>
00017 #include <X11/Xlibint.h>      /* required for __XOS2RedirRoot */
00018 #define chown(x,y,z)
00019 #define OS2_DEFAULT_LIBDIR "/XFree86/lib/X11/lincity"
00020 #endif
00021 
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 #include <fcntl.h>
00025 
00026 #if defined (TIME_WITH_SYS_TIME)
00027 #include <time.h>
00028 #include <sys/time.h>
00029 #else
00030 #if defined (HAVE_SYS_TIME_H)
00031 #include <sys/time.h>
00032 #else
00033 #include <time.h>
00034 #endif
00035 #endif
00036 
00037 #if defined (WIN32)
00038 #include <winsock.h>
00039 #if defined (__BORLANDC__)
00040 #include <dir.h>
00041 #include <dirent.h>
00042 #include <dos.h>
00043 #endif
00044 #include <io.h>
00045 #include <direct.h>
00046 #include <process.h>
00047 #endif
00048 
00049 #if defined (HAVE_DIRENT_H)
00050 #include <dirent.h>
00051 #define NAMLEN(dirent) strlen((dirent)->d_name)
00052 #else
00053 #define dirent direct
00054 #define NAMLEN(dirent) (dirent)->d_namlen
00055 #if defined (HAVE_SYS_NDIR_H)
00056 #include <sys/ndir.h>
00057 #endif
00058 #if defined (HAVE_SYS_DIR_H)
00059 #include <sys/dir.h>
00060 #endif
00061 #if defined (HAVE_NDIR_H)
00062 #include <ndir.h>
00063 #endif
00064 #endif
00065 
00066 #include <ctype.h>
00067 #include "common.h"
00068 #ifdef LC_X11
00069 #include <X11/cursorfont.h>
00070 #endif
00071 #include "lctypes.h"
00072 #include "lin-city.h"
00073 #include "cliglobs.h"
00074 #include "engglobs.h"
00075 #include "fileutil.h"
00076 
00077 /* GCS: This is from dcgettext.c in the gettext package.      */
00078 /* XPG3 defines the result of `setlocale (category, NULL)' as:
00079    ``Directs `setlocale()' to query `category' and return the current
00080      setting of `local'.''
00081    However it does not specify the exact format.  And even worse: POSIX
00082    defines this not at all.  So we can use this feature only on selected
00083    system (e.g. those using GNU C Library).  */
00084 #ifdef _LIBC
00085 # define HAVE_LOCALE_NULL
00086 #endif
00087 
00088 #define DEBUG_PRINTF_TO_FILE 0
00089 void debug_printf (char* fmt, ...);
00090 
00091 /* ---------------------------------------------------------------------- *
00092  * Private Fn Prototypes
00093  * ---------------------------------------------------------------------- */
00094 void dump_screen (void);
00095 void verify_package (void);
00096 static const char *guess_category_value (int category, 
00097                                          const char *categoryname);
00098 
00099 /* ---------------------------------------------------------------------- *
00100  * Public Global Variables
00101  * ---------------------------------------------------------------------- */
00102 char LIBDIR[LC_PATH_MAX];
00103 
00104 char *lc_save_dir;
00105 int lc_save_dir_len;
00106 static char *lc_temp_filename;
00107 
00108 char given_scene[LC_PATH_MAX];
00109 char colour_pal_file[LC_PATH_MAX];
00110 char opening_pic[LC_PATH_MAX];
00111 char graphic_path[LC_PATH_MAX];
00112 char fontfile[LC_PATH_MAX];
00113 char opening_path[LC_PATH_MAX];
00114 char help_path[LC_PATH_MAX];
00115 char message_path[LC_PATH_MAX];
00116 char lc_textdomain_directory[LC_PATH_MAX];
00117 char lincityrc_file[LC_PATH_MAX];
00118 
00119 /* The variable make_dir_ok_flag has 2 uses.
00120 
00121    First, it is initialized to 1, and set to zero if the directory 
00122    already exists.  Later, if it is found to be 1, the "create directory 
00123    help page (ask-dir) is display, and then set to zero.  
00124 
00125    Next, in the help handler, if the user says it's OK to create the 
00126    directory, it is reset to 1.  Finally, in the main loop, if it is 
00127    found to have value 1, the directory is created and it is reset to 0.
00128    */
00129 #if defined (commentout)
00130 int make_dir_ok_flag;
00131 #endif
00132 
00133 /* ---------------------------------------------------------------------- *
00134  * Public Functions
00135  * ---------------------------------------------------------------------- */
00136 #if defined (__BORLANDC__)
00137 int
00138 _chdir (const char *dirname)
00139 {
00140     return chdir (dirname);
00141 }
00142 
00143 int 
00144 _access (const char *path, int mode)
00145 {
00146     return access (path, mode)
00147 }
00148 #endif
00149 
00150 /* Executes a system command */
00151 int
00152 execute_command (char *cmd, char *p1, char *p2, char *p3)
00153 {
00154   char *sys_cmd = (char *) malloc (strlen (cmd) + strlen (p1) + strlen (p2)
00155                                    + strlen (p3) + 4);
00156   int ret_value;
00157 
00158   if (sys_cmd == 0) {
00159     malloc_failure ();
00160   }
00161   sprintf (sys_cmd, "%s %s %s %s", cmd, p1, p2, p3);
00162   ret_value = system (sys_cmd);
00163 /* fprintf(stderr, "system(%s)=%i\n", sys_cmd, ret_value); */
00164   free (sys_cmd);
00165   return ret_value;
00166 }
00167 
00168 void
00169 copy_file (char *f1, char *f2)
00170 {
00171   int ret_value = execute_command ("cp", f1, f2, "");
00172   if (ret_value != 0)
00173     {
00174       /* GCS FIX:  Need to make do_error into var_args fn? */
00175       printf ("Tried to cp %s %s\n", f1, f2);
00176       do_error ("Can't copy requested file");
00177     }
00178 }
00179 
00180 void
00181 gunzip_file (char *f1, char *f2)
00182 {
00183   int ret_value = execute_command ("gzip -c -d", f1, ">", f2);
00184   if (ret_value != 0)
00185     {
00186       /* GCS FIX:  Need to make do_error into var_args fn? */
00187       printf ("Tried to gzip -c -d %s > %s\n", f1, f2);
00188       do_error ("Can't gunzip requested file");
00189     }
00190 }
00191 
00192 FILE* 
00193 fopen_read_gzipped (char* fn)
00194 {
00195     FILE* fp;
00196 
00197 #if defined (HAVE_GZIP) && defined (HAVE_POPEN)
00198 #ifdef __EMX__
00199     const char* cmd_str = "gzip -d -c < %s 2> nul";
00200 #else
00201     const char* cmd_str = "gzip -d -c < %s 2> /dev/null";
00202 #endif
00203     char *cmd = (char*) malloc (strlen (cmd_str) + strlen (fn) + 1);
00204     
00205     sprintf (cmd, cmd_str, fn);
00206 #ifdef __EMX__
00207     fp=popen(cmd,"rb");
00208 #else
00209     fp=popen(cmd,"r");
00210 #endif
00211     if (fp==NULL) {
00212        fprintf(stderr, "Failed to open pipe cmd: %s\n", cmd);
00213     }
00214     free(cmd);
00215 
00216 #elif defined (HAVE_GZIP) && !defined (HAVE_POPEN)
00217     gunzip_file (fn, lc_temp_filename);
00218     fp = fopen (lc_temp_filename, "rb");
00219 
00220 #else /* No gzip */
00221     fp = fopen (fn, "rb");
00222 #endif
00223 
00224     return fp;
00225 }
00226 
00227 void 
00228 fclose_read_gzipped (FILE* fp)
00229 {
00230 #if defined (HAVE_GZIP) && defined (HAVE_POPEN)
00231     pclose (fp);
00232 #elif defined (HAVE_GZIP) && !defined (HAVE_POPEN)
00233     fclose (fp);
00234     remove (lc_temp_filename);
00235 #else
00236     fclose (fp);
00237 #endif
00238 }
00239 
00240 int
00241 directory_exists (char *dir)
00242 {
00243 #if defined (WIN32)
00244     if (_chdir (dir) == -1) {
00245         _chdir (LIBDIR);                /* go back... */
00246         return 0;
00247     }
00248     _chdir (LIBDIR);            /* go back... */
00249 #else /* UNIX */
00250     DIR *dp;
00251     if ((dp = opendir (dir)) == NULL) {
00252         return 0;
00253     }
00254     closedir (dp);
00255 #endif
00256     return 1;
00257 }
00258 
00259 int
00260 file_exists (char *filename)
00261 {
00262     FILE* fp;
00263     fp = fopen (filename,"rb");
00264     if (fp == NULL) {
00265         return 0;
00266     }
00267     fclose (fp);
00268     return 1;
00269 }
00270 
00271 #if defined (WIN32)
00272 void
00273 find_libdir (void)
00274 {
00275     const char searchfile[] = "Colour.pal";
00276     /* default_dir will be something like "C:\\LINCITY1.11" */
00277     const char default_dir[] = "C:\\LINCITY" VERSION;
00278 //    const char default_dir[] = "D:\\LINCITY"; /* For GCS's use */
00279 
00280     /* Check 1: environment variable */
00281     _searchenv (searchfile, "LINCITY_HOME", LIBDIR);
00282     if (*LIBDIR != '\0') {
00283         int endofpath_offset = strlen (LIBDIR) - strlen (searchfile) - 1;
00284         LIBDIR[endofpath_offset] = '\0';
00285         return;
00286     }
00287 
00288     /* Check 2: default location */
00289     if ((_access (default_dir, 0)) != -1) {
00290         strcpy (LIBDIR, default_dir);
00291         return;
00292     }
00293 
00294     /* Finally give up */
00295     HandleError (_("Error. Can't find LINCITY_HOME"), FATAL);
00296 }
00297 
00298 #elif defined (__EMX__)
00299 void
00300 find_libdir (void)
00301 {
00302     strcpy(LIBDIR, __XOS2RedirRoot(OS2_DEFAULT_LIBDIR));
00303 }
00304 
00305 #else /* Unix with configure */
00306 void
00307 find_libdir (void)
00308 {
00309     const char searchfile[] = "colour.pal";
00310     char *home_dir, *cwd;
00311     char cwd_buf[LC_PATH_MAX];
00312     char filename_buf[LC_PATH_MAX];
00313 
00314     /* Check 1: environment variable */
00315     home_dir = getenv ("LINCITY_HOME");
00316     if (home_dir) {
00317         snprintf (filename_buf, LC_PATH_MAX, "%s%c%s", 
00318                   home_dir, PATH_SLASH, searchfile);
00319         if (file_exists(filename_buf)) {
00320             strncpy (LIBDIR, home_dir, LC_PATH_MAX);
00321             return;
00322         }
00323     }
00324 
00325     /* Check 2: current working directory */
00326     cwd = getcwd (cwd_buf, LC_PATH_MAX);
00327     if (cwd) {
00328         snprintf (filename_buf, LC_PATH_MAX, "%s%c%s", 
00329                   cwd_buf, PATH_SLASH, searchfile);
00330         if (file_exists(filename_buf)) {
00331             strncpy (LIBDIR, cwd_buf, LC_PATH_MAX);
00332             return;
00333         }
00334     }
00335 
00336     /* Check 3: default (configuration) directory */
00337     snprintf (filename_buf, LC_PATH_MAX, "%s%c%s", 
00338               DEFAULT_LIBDIR, PATH_SLASH, searchfile);
00339     if (file_exists(filename_buf)) {
00340         strncpy (LIBDIR, DEFAULT_LIBDIR, LC_PATH_MAX);
00341         return;
00342     }
00343 
00344     /* Finally give up */
00345     HandleError (_("Error. Can't find LINCITY_HOME"), FATAL);
00346 }
00347 #endif
00348 
00349 
00350 /* GCS:  This function comes from dcgettext.c in the gettext package.      */
00351 /* Guess value of current locale from value of the environment variables.  */
00352 /* GCS Feb 23, 2003.  This was updated in gettext, but I'm going with the  */
00353 /* old version here. */
00354 static const char *
00355 guess_category_value (int category, const char *categoryname)
00356 {
00357     const char *retval;
00358 
00359     /* The highest priority value is the `LANGUAGE' environment
00360        variable.  This is a GNU extension.  */
00361     retval = getenv ("LANGUAGE");
00362     if (retval != NULL && retval[0] != '\0')
00363         return retval;
00364 
00365     /* `LANGUAGE' is not set.  So we have to proceed with the POSIX
00366        methods of looking to `LC_ALL', `LC_xxx', and `LANG'.  On some
00367        systems this can be done by the `setlocale' function itself.  */
00368 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
00369     retval = setlocale (category, NULL);
00370     if (retval != NULL)
00371       return retval;
00372     else
00373       return "C";
00374 #else
00375     /* Setting of LC_ALL overwrites all other.  */
00376     retval = getenv ("LC_ALL");
00377     if (retval != NULL && retval[0] != '\0')
00378         return retval;
00379 
00380     /* Next comes the name of the desired category.  */
00381     retval = getenv (categoryname);
00382     if (retval != NULL && retval[0] != '\0')
00383         return retval;
00384 
00385     /* Last possibility is the LANG environment variable.  */
00386     retval = getenv ("LANG");
00387     if (retval != NULL && retval[0] != '\0')
00388         return retval;
00389 
00390     /* We use C as the default domain.  POSIX says this is implementation
00391        defined.  */
00392     return "C";
00393 #endif
00394 }
00395 
00396 /* GCS:  This function is modified from gettext.  It finds the language 
00397    portion of the locale. */
00398 static void 
00399 lincity_nl_find_language (char *name)
00400 {
00401   while (name[0] != '\0' && name[0] != '_' && name[0] != '@'
00402          && name[0] != '+' && name[0] != ',')
00403     ++name;
00404 
00405   *name = '\0';
00406 }
00407 
00408 
00409 void
00410 find_localized_paths (void)
00411 {
00412   int messages_done = 0;
00413   int help_done = 0;
00414 
00415   const char* intl_suffix = "";
00416   char intl_lang[128];
00417 
00418   /* First, try the locale "as is" */
00419 #if defined (ENABLE_NLS) && defined (HAVE_LC_MESSAGES)
00420   intl_suffix = guess_category_value(LC_MESSAGES,"LC_MESSAGES");
00421 #else
00422   intl_suffix = guess_category_value(0,"LC_MESSAGES");
00423 #endif
00424   debug_printf ("GUESS 1 -- intl_suffix is %s\n", intl_suffix);
00425   if (strcmp(intl_suffix,"C") && strcmp(intl_suffix,"")) {
00426     sprintf (message_path, "%s%c%s%c%s%c", LIBDIR, PATH_SLASH, "messages",
00427              PATH_SLASH, intl_suffix, PATH_SLASH);
00428     debug_printf ("Trying Message Path %s\n", message_path);
00429     if (directory_exists(message_path)) {
00430       debug_printf ("Set Message Path %s\n", message_path);
00431       messages_done = 1;
00432     }
00433     sprintf (help_path, "%s%c%s%c%s%c", LIBDIR, PATH_SLASH, "help",
00434              PATH_SLASH, intl_suffix, PATH_SLASH);
00435     debug_printf ("Trying Help Path %s\n", help_path);
00436     if (directory_exists(help_path)) {
00437       debug_printf ("Set Help Path %s\n", help_path);
00438       help_done = 1;
00439     }
00440   }
00441   if (messages_done && help_done) return;
00442 
00443   /* Next, try stripping off the country suffix */
00444   strncpy (intl_lang, intl_suffix, 128);
00445   intl_lang[127] = '\0';
00446   lincity_nl_find_language (intl_lang);
00447   intl_suffix = intl_lang;
00448   debug_printf ("GUESS 2 -- intl_suffix is %s\n", intl_suffix);
00449   if (strcmp(intl_suffix,"C") && strcmp(intl_suffix,"")) {
00450     if (!messages_done) {
00451       sprintf (message_path, "%s%c%s%c%s%c", LIBDIR, PATH_SLASH, "messages",
00452                PATH_SLASH, intl_suffix, PATH_SLASH);
00453       debug_printf ("Trying Message Path %s\n", message_path);
00454       if (directory_exists(message_path)) {
00455         debug_printf ("Set Message Path %s\n", message_path);
00456         messages_done = 1;
00457       }
00458     }
00459     if (!help_done) {
00460       sprintf (help_path, "%s%c%s%c%s%c", LIBDIR, PATH_SLASH, "help",
00461                PATH_SLASH, intl_suffix, PATH_SLASH);
00462       debug_printf ("Trying Help Path %s\n", help_path);
00463       if (directory_exists(help_path)) {
00464         debug_printf ("Set Help Path %s\n", help_path);
00465         help_done = 1;
00466       }
00467     }
00468   }
00469   if (messages_done && help_done) return;
00470     
00471   /* Finally, settle for default English messages */
00472   if (!messages_done) {
00473     sprintf (message_path, "%s%c%s%c", LIBDIR, PATH_SLASH, "messages",
00474              PATH_SLASH);
00475     debug_printf ("Settling for message Path %s\n", message_path);
00476   }
00477   if (!help_done) {
00478     sprintf (help_path, "%s%c%s%c", LIBDIR, PATH_SLASH, "help",
00479              PATH_SLASH);
00480     debug_printf ("Settling for help Path %s\n", help_path);
00481   }
00482 }
00483 
00484 
00485 void
00486 init_path_strings (void)
00487 {
00488     char* homedir = NULL;
00489     const char* intl_suffix = "";
00490     char* dm = NULL;
00491     char* td = NULL;
00492 
00493     find_libdir ();
00494 
00495 #if defined (WIN32)
00496     homedir = LIBDIR;
00497 #elif defined (__EMX__)
00498     homedir = getenv ("HOME");
00499 #else
00500     homedir = getenv ("HOME");
00501 #endif
00502 
00503     /* Various dirs and files */
00504     lc_save_dir_len = strlen (homedir) + strlen (LC_SAVE_DIR) + 1;
00505     if ((lc_save_dir = (char *) malloc (lc_save_dir_len + 1)) == 0)
00506         malloc_failure ();
00507     sprintf (lc_save_dir, "%s%c%s", homedir, PATH_SLASH, LC_SAVE_DIR);
00508     sprintf (colour_pal_file, "%s%c%s", LIBDIR, PATH_SLASH, "colour.pal");
00509     sprintf (opening_path, "%s%c%s", LIBDIR, PATH_SLASH, "opening");
00510 #if defined (WIN32)
00511     sprintf (opening_pic, "%s%c%s",opening_path,PATH_SLASH,"open.tga");
00512 #else
00513     sprintf (opening_pic, "%s%c%s",opening_path,PATH_SLASH,"open.tga.gz");
00514 #endif
00515     sprintf (graphic_path, "%s%c%s%c", LIBDIR, PATH_SLASH, "icons",
00516              PATH_SLASH);
00517     sprintf (lincityrc_file, "%s%c%s", homedir, PATH_SLASH, 
00518         LINCITYRC_FILENAME);
00519 
00520     /* Paths for message & help files, etc */
00521     find_localized_paths ();
00522 
00523     /* Font stuff */
00524     sprintf (fontfile, "%s%c%s", opening_path, PATH_SLASH,
00525              "iso8859-1-8x8.raw");
00526 #if defined (WIN32)
00527     /* GCS: Use windows font for extra speed */
00528     strcpy (windowsfontfile, LIBDIR);
00529 #if defined (commentout)
00530     if (!pix_double)
00531         strcat (windowsfontfile, "\\opening\\iso8859-1-8x8.fnt");
00532     else
00533         strcat (windowsfontfile, "\\opening\\iso8859-1-9x15.fnt");
00534 #endif
00535     if (!pix_double)
00536         strcat (windowsfontfile, "\\opening\\winfont_8x8.fnt");
00537     else
00538         strcat (windowsfontfile, "\\opening\\winfont_16x16.fnt");
00539 #endif
00540 
00541     /* Temp file for results */
00542     lc_temp_filename = (char *) malloc (lc_save_dir_len + 16);
00543     if (lc_temp_filename == 0) {
00544         malloc_failure ();
00545     }
00546     sprintf (lc_temp_filename, "%s%c%s", lc_save_dir, PATH_SLASH, "tmp-file");
00547 
00548     /* Path for localization */
00549 #if defined (ENABLE_NLS)
00550 #if defined (WIN32)
00551     sprintf (lc_textdomain_directory, "%s%c%s", LIBDIR, PATH_SLASH, "locale");
00552 #else
00553     strcpy (lc_textdomain_directory, LOCALEDIR);
00554 #endif
00555     dm = bindtextdomain (PACKAGE, lc_textdomain_directory);
00556     debug_printf ("Bound textdomain directory is %s\n", dm);
00557     td = textdomain (PACKAGE);
00558     debug_printf ("Textdomain is %s\n", td);
00559 #endif
00560 }
00561 
00562 void
00563 verify_package (void)
00564 {
00565     FILE *fp = fopen (colour_pal_file,"rb");
00566     if (!fp) {
00567         do_error (_("Error verifying package. Can't find colour.pal."));
00568     }
00569     fclose (fp);
00570 }
00571 
00572 void
00573 make_savedir (void)
00574 {
00575 #if !defined (WIN32)
00576     DIR *dp;
00577 #endif
00578 
00579 #if defined (commentout)
00580     if (make_dir_ok_flag == 0)
00581         return;
00582 #endif
00583 
00584 #if defined (WIN32)
00585     if (_mkdir (lc_save_dir)) {
00586         printf (_("Couldn't create the save directory %s\n"), lc_save_dir);
00587         exit (-1);
00588     }
00589 #else
00590     mkdir (lc_save_dir, 0755);
00591     chown (lc_save_dir, getuid (), getgid ());
00592     if ((dp = opendir (lc_save_dir)) == NULL)
00593     {
00594         /* change this to a screen message. */
00595         printf (_("Couldn't create the save directory %s\n"), lc_save_dir);
00596         exit (1);
00597     }
00598     closedir (dp);
00599 #endif
00600 
00601 #if defined (commentout)
00602     make_dir_ok_flag = 0;
00603 #endif
00604 }
00605 
00606 void
00607 check_savedir (void)
00608 {
00609     int i = 0, j, k, r, l;
00610 
00611     if (!directory_exists (lc_save_dir)) {
00612         make_savedir ();
00613 #if defined (commentout)
00614         l = lc_save_dir_len;
00615         if (l > 160) {
00616             i = l - 160;
00617             l = 160;
00618         }
00619         askdir_lines = l / 40 + ((l % 40) ? 1 : 0);
00620         r = l / askdir_lines + ((l % askdir_lines) ? 1 : 0);
00621         for (j = 0; j < askdir_lines; j++) {
00622             if ((askdir_path[j] = (char *) malloc (r + 1)) == 0)
00623                 malloc_failure ();
00624             for (k = 0; k < r; k++, i++)
00625                 *(askdir_path[j] + k) = lc_save_dir[i];
00626             *(askdir_path[j] + k) = 0;
00627         }
00628         return;
00629 #endif
00630     }
00631 #if defined (commentout)
00632     make_dir_ok_flag = 0;               /* don't load the ask-dir */
00633 #endif
00634 }
00635 
00636 void
00637 malloc_failure (void)
00638 {
00639   printf (_("Out of memory: malloc failure\n"));
00640   exit (1);
00641 }
00642 
00643 char*
00644 load_graphic(char *s)
00645 {
00646     int x,l;
00647     char ss[LC_PATH_MAX],*graphic;
00648     FILE *inf;
00649     strcpy(ss,graphic_path);
00650     strcat(ss,s);
00651     if ((inf=fopen(ss,"rb"))==NULL)
00652     {
00653         strcat(ss," -- UNABLE TO LOAD");
00654         do_error(ss);
00655     }
00656     fseek(inf,0L,SEEK_END);
00657     l=ftell(inf);
00658     fseek(inf,0L,SEEK_SET);
00659     graphic=(char *)malloc(l);
00660     for (x=0;x<l;x++)
00661         *(graphic+x)=fgetc(inf);
00662     fclose(inf);
00663     return(graphic);
00664 }
00665 
00666 void
00667 load_lincityrc (void)
00668 {
00669     FILE *fp;
00670     int arg;
00671     char buf[128];
00672 
00673     if ((fp = fopen (lincityrc_file, "r")) == 0) {
00674         save_lincityrc();
00675         return;
00676     }
00677     while (fgets (buf,128,fp)) {
00678         if (sscanf(buf,"overwrite_transport=%d",&arg)==1) {
00679             overwrite_transport_flag = !!arg;
00680             continue;
00681         }
00682         if (sscanf(buf,"no_init_help=%d",&arg)==1) {
00683             /* Careful here ... */
00684             no_init_help = !!arg;
00685             continue;
00686         }
00687         if (sscanf(buf,"skip_splash_screen=%d",&arg)==1) {
00688             skip_splash_screen = !!arg;
00689             continue;
00690         }
00691         if (sscanf(buf,"suppress_firsttime_module_help=%d",&arg)==1) {
00692             suppress_firsttime_module_help = !!arg;
00693             continue;
00694         }
00695         if (sscanf(buf,"suppress_popups=%d",&arg)==1) {
00696             suppress_popups = !!arg;
00697             continue;
00698         }
00699         if (sscanf(buf,"time_multiplex_stats=%d",&arg)==1) {
00700             time_multiplex_stats = !!arg;
00701             continue;
00702         }
00703         if (sscanf(buf,"x_confine_pointer=%d",&arg)==1) {
00704             confine_flag = !!arg;
00705             continue;
00706         }
00707     }
00708     fclose (fp);
00709 }
00710 
00711 void
00712 save_lincityrc (void)
00713 {
00714     FILE *fp;
00715 
00716     if ((fp = fopen (lincityrc_file, "w")) == 0) {
00717         return;
00718     }
00719 
00720     fprintf (fp, 
00721         "# Set this if you want to be able to overwrite one\n"
00722         "# kind of transport with another.\n"
00723         "overwrite_transport=%d\n\n",
00724         overwrite_transport_flag);
00725     fprintf (fp, 
00726         "# Set this if you don't want the opening help screen.\n"
00727         "no_init_help=%d\n\n",
00728         no_init_help
00729         );
00730     fprintf (fp,
00731         "# Set this if you don't want the opening splash screen.\n"
00732         "skip_splash_screen=%d\n\n",
00733         skip_splash_screen
00734         );
00735     fprintf (fp,
00736         "# Set this if you don't want help the first time you\n"
00737         "# click to place an item.\n"
00738         "suppress_firsttime_module_help=%d\n\n", 
00739         suppress_firsttime_module_help
00740         );
00741     fprintf (fp,
00742         "# Set this if don't want modal dialog boxes which you\n"
00743         "# are required to click OK.  Instead, report the dialog\n"
00744         "# box information to the message area.\n"
00745         "suppress_popups=%d\n\n",
00746         suppress_popups
00747         );
00748     fprintf (fp,
00749         "# Set this if want the different statistic windows to cycle\n"
00750         "# through the right panel.\n"
00751         "time_multiplex_stats=%d\n\n",
00752         time_multiplex_stats
00753         );
00754     fprintf (fp,
00755         "# (X Windows only) Set this if you want to confine the pointer\n"
00756         "# to within the window.\n"
00757         "x_confine_pointer=%d\n\n",
00758         confine_flag
00759         );
00760 
00761     fclose (fp);
00762 }
00763 
00764 void
00765 undosify_string (char *s)
00766 {
00767     /* Convert '\r\n' to '\n' in string */
00768     char prev_char = 0;
00769     char *p = s, *q = s;
00770     while (*p) {
00771         if (*p != '\r') {
00772             if (prev_char == '\r' && *p != '\n') {
00773                 *q++ = '\n';
00774             }
00775             *q++ = *p;
00776         }
00777         prev_char = *p;
00778         p++;
00779     }
00780     if (prev_char == '\r') {
00781         *q++ = '\n';
00782     }
00783     *q = '\0';
00784 }
00785 
00786 void
00787 debug_printf (char* fmt, ...)
00788 {
00789 #if (DEBUG_PRINTF_TO_FILE)
00790     static int initialized = 0;
00791     char* filename = "debug.txt";
00792     FILE* fp;
00793 #endif
00794     va_list argptr;
00795 
00796 #if (DEBUG_PRINTF_TO_FILE)
00797     va_start (argptr, fmt);
00798     fp = fopen(filename, "a");
00799     if (!initialized) {
00800         initialized = 1;
00801         fprintf (fp, "=========================\n");
00802     }
00803     vfprintf (fp, fmt, argptr);
00804 #endif
00805 
00806     if (command_line_debug) {
00807 #if (!DEBUG_PRINTF_TO_FILE)
00808       va_start (argptr, fmt);
00809 #endif
00810       vprintf (fmt, argptr);
00811 #if (!DEBUG_PRINTF_TO_FILE)
00812       va_end (argptr);
00813 #endif
00814     }
00815 
00816 #if (DEBUG_PRINTF_TO_FILE)
00817     va_end (argptr);
00818     fclose (fp);
00819 #endif
00820 }
00821 

Generated on Sun Dec 26 11:23:23 2004 for lincity by  doxygen 1.3.9.1