Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2004:
[Freeciv-Dev] (PR#7326) [RfP] expansion in filenames
Home

[Freeciv-Dev] (PR#7326) [RfP] expansion in filenames

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: badamson@xxxxxxxxxxx, jdorje@xxxxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] (PR#7326) [RfP] expansion in filenames
From: "Mateusz Stefek" <mstefek@xxxxxxxxx>
Date: Mon, 6 Sep 2004 06:01:29 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=7326 >

> [jdorje - Mon Sep 06 01:16:18 2004]:
> 
> > [badamson@xxxxxxxxxxx - Sun Sep 05 21:04:46 2004]:
> > 
> > The server uses readline, which can handily complete file names. 
> > However, readline understands ~ in as an abbreviation for your home 
> > directory, but the server itself (apparently) does not.
> > 
> > For example, if I (user benedict, with home directory /home/benedict) 
> > have a file of settings /home/benedict/civ/comp, I can type the server 
> > command 'read /home/benedict/civ/c<TAB>' and readline will complete the 
> > file name. readline will also complete 'read ~/civ/c<TAB>' as 'read 
> > ~/civ/comp'. However, the server will then respond with the error 
> > message "Cannot read command line scriptfile '~/civ/comp'"
> 
> Patch wanted.
> 
> jason
> 
Here you are.
Commands
/save
/load
/rulesetdir
are supported.

/read
/write
will come soon
--
mateusz
Index: registry.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/registry.c,v
retrieving revision 1.66
diff -u -r1.66 registry.c
--- registry.c  22 Jul 2004 19:04:33 -0000      1.66
+++ registry.c  6 Sep 2004 13:01:03 -0000
@@ -578,15 +578,34 @@
   return TRUE;
 }
 
+/***************************************************************************
+  Interpret ~/ in filename as home dir
+  New path is returned in buf of size buf_size
+***************************************************************************/
+static void interpret_tilda(char* buf, size_t buf_size, const char* filename)
+{
+  if (filename[0] == '~' && filename[1] == '/') {
+    my_snprintf(buf, buf_size, "%s/%s", user_home_dir(), filename + 2);
+  } else if (filename[0] == '~' && filename[1] == '\0') {
+    strncpy(buf, user_home_dir(), buf_size);
+  } else  {
+    strncpy(buf, filename, buf_size);
+  }
+}
+
 /**************************************************************************
 ...
 **************************************************************************/
 bool section_file_load(struct section_file *my_section_file,
                      const char *filename)
 {
-  struct inputfile *inf = inf_from_file(filename, datafilename);
+  char real_filename[1024];
+  struct inputfile *inf;
+
+  interpret_tilda(real_filename, sizeof(real_filename), filename);
+  inf = inf_from_file(real_filename, datafilename);
 
-  return section_file_read_dup(my_section_file, filename, inf, TRUE);
+  return section_file_read_dup(my_section_file, real_filename, inf, TRUE);
 }
 
 /**************************************************************************
@@ -595,9 +614,13 @@
 bool section_file_load_nodup(struct section_file *my_section_file,
                            const char *filename)
 {
-  struct inputfile *inf = inf_from_file(filename, datafilename);
+  char real_filename[1024];
+  struct inputfile *inf;
 
-  return section_file_read_dup(my_section_file, filename, inf, FALSE);
+  interpret_tilda(real_filename, sizeof(real_filename), filename);
+  inf = inf_from_file(real_filename, datafilename);
+
+  return section_file_read_dup(my_section_file, real_filename, inf, FALSE);
 }
 
 /**************************************************************************
@@ -630,14 +653,18 @@
  Below simply specifies FZ_ZLIB method, since fz_fromFile() automatically
  changes to FZ_PLAIN method when level==0.
 **************************************************************************/
-bool section_file_save(struct section_file *my_section_file, const char 
*filename,
-                     int compression_level)
+bool section_file_save(struct section_file *my_section_file,
+                       const char *filename,
+                      int compression_level)
 {
-  fz_FILE *fs = fz_from_file(filename, "w", FZ_ZLIB, compression_level);
-
+  char real_filename[1024];
+  fz_FILE *fs;
   struct genlist_link *ent_iter, *save_iter, *col_iter;
   struct entry *pentry, *col_pentry;
   int i;
+  
+  interpret_tilda(real_filename, sizeof(real_filename), filename);
+  fs = fz_from_file(real_filename, "w", FZ_ZLIB, compression_level);
 
   if (!fs)
     return FALSE;
@@ -721,7 +748,7 @@
          if((!pentry) || (strcmp(pentry->name, expect) != 0)) {
            if(icol != 0) {
              freelog(LOG_ERROR, "unexpected exit from tabular at %s (%s)",
-                     pentry->name, filename);
+                     pentry->name, real_filename);
              fz_fprintf(fs, "\n");
            }
            fz_fprintf(fs, "}\n");
@@ -774,13 +801,13 @@
   (void) moutstr(NULL);                /* free internal buffer */
 
   if (fz_ferror(fs) != 0) {
-    freelog(LOG_ERROR, "Error before closing %s: %s", filename,
+    freelog(LOG_ERROR, "Error before closing %s: %s", real_filename,
            fz_strerror(fs));
     fz_fclose(fs);
     return FALSE;
   }
   if (fz_fclose(fs) != 0) {
-    freelog(LOG_ERROR, "Error closing %s", filename);
+    freelog(LOG_ERROR, "Error closing %s", real_filename);
     return FALSE;
   }
 

[Prev in Thread] Current Thread [Next in Thread]