Author: cazfi
Date: Fri Sep 11 17:48:10 2015
New Revision: 29862

URL: http://svn.gna.org/viewcvs/freeciv?rev=29862&view=rev
Log:
Support reading contents of a separate file as value of secfile string entry.

See patch #6336

Modified:
    branches/S2_6/utility/inputfile.c
    branches/S2_6/utility/registry_ini.c
    branches/S2_6/utility/section_file.c

Modified: branches/S2_6/utility/inputfile.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/utility/inputfile.c?rev=29862&r1=29861&r2=29862&view=diff
==============================================================================
--- branches/S2_6/utility/inputfile.c   (original)
+++ branches/S2_6/utility/inputfile.c   Fri Sep 11 17:48:10 2015
@@ -827,7 +827,68 @@
 
   border_character = *c;
 
-  if (border_character != '\"'
+  if (border_character == '*') {
+    const char *rfname;
+    fz_FILE *fp;
+    bool eof;
+    int pos;
+
+    c++;
+
+    start = c;
+    while (*c != '*') {
+      if (*c == '\0' || *c == '\n') {
+        return NULL;
+      }
+      c++;
+    }
+    c++;
+    /* check that the trailing stuff is ok: */
+    if (!(*c == '\0' || *c == ',' || fc_isspace(*c) || is_comment(*c))) {
+      return NULL;
+    }
+    /* We don't want to obliterate ending '*' permanently,
+     * so remember it: */
+    trailing = *(c - 1);
+    *((char *) (c - 1)) = '\0';     /* Tricky. */
+
+    rfname = fileinfoname(get_data_dirs(), start);
+    if (rfname == NULL) {
+      inf_log(inf, LOG_ERROR, 
+              _("Cannot find stringfile \"%s\"."), start);
+      *((char *) c) = trailing; /* Revert. */
+      return NULL;
+    }
+    *((char *) c) = trailing; /* Revert. */
+    fp = fz_from_file(rfname, "r", -1, 0);
+    if (!fp) {
+      inf_log(inf, LOG_ERROR,
+              _("Cannot open stringfile \"%s\"."), rfname);
+      return NULL;
+    }
+    log_debug("Stringfile \"%s\" opened ok", start);
+    *((char *) (c - 1)) = trailing; /* Revert. */
+    astr_set(&inf->token, "*"); /* Mark as a string read from a file */
+
+    eof = FALSE;
+    pos = 1; /* Past 'filestring' marker */
+    while (!eof) {
+      char *ret;
+
+      ret = fz_fgets((char *) astr_str(&inf->token) + pos,
+                     astr_capacity(&inf->token) - pos, fp);
+      if (ret == NULL) {
+        eof = TRUE;
+      } else {
+        pos = astr_len(&inf->token);
+        astr_reserve(&inf->token, pos + 200);
+      }
+    }
+
+    inf->cur_line_pos = c + 1 - astr_str(&inf->cur_line);
+
+    return astr_str(&inf->token);
+  } else if (border_character != '\"'
       && border_character != '\''
       && border_character != '$') {
     /* A one-word string: maybe FALSE or TRUE. */
@@ -840,7 +901,7 @@
       return NULL;
     }
     /* If its a comma, we don't want to obliterate it permanently,
-     * so rememeber it: */
+     * so remember it: */
     trailing = *c;
     *((char *) c) = '\0';       /* Tricky. */
 

Modified: branches/S2_6/utility/registry_ini.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/utility/registry_ini.c?rev=29862&r1=29861&r2=29862&view=diff
==============================================================================
--- branches/S2_6/utility/registry_ini.c        (original)
+++ branches/S2_6/utility/registry_ini.c        Fri Sep 11 17:48:10 2015
@@ -70,6 +70,9 @@
     foo = _("\nThis is a string\nover multiple lines\n")
   Note that if you missplace the trailing doublequote you can
   easily end up with strange errors reading the file...
+
+  - Strings read from a file: A file can be read as a string value:
+    foo = *filename.txt*
 
   - Vector format: An entry can have multiple values separated
   by commas, eg:

Modified: branches/S2_6/utility/section_file.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/utility/section_file.c?rev=29862&r1=29861&r2=29862&view=diff
==============================================================================
--- branches/S2_6/utility/section_file.c        (original)
+++ branches/S2_6/utility/section_file.c        Fri Sep 11 17:48:10 2015
@@ -166,6 +166,15 @@
 bool entry_from_token(struct section *psection, const char *name,
                       const char *tok)
 {
+  if ('*' == tok[0]) {
+    char buf[strlen(tok) + 1];
+
+    remove_escapes(tok + 1, FALSE, buf, sizeof(buf));
+    (void) section_entry_str_new(psection, name, buf, FALSE);
+    DEBUG_ENTRIES("entry %s '%s'", name, buf);
+    return TRUE;
+  }
+
   if ('$' == tok[0] || '"' == tok[0]) {
     char buf[strlen(tok) + 1];
     bool escaped = ('"' == tok[0]);


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to