[cig-commits] r18526 - seismo/2D/SPECFEM2D/trunk/src/shared

dkomati1 at geodynamics.org dkomati1 at geodynamics.org
Thu Jun 2 09:43:12 PDT 2011


Author: dkomati1
Date: 2011-06-02 09:43:11 -0700 (Thu, 02 Jun 2011)
New Revision: 18526

Modified:
   seismo/2D/SPECFEM2D/trunk/src/shared/param_reader.c
Log:
Dennis McRitchie fixed the multi-word value problem in the 2D version of param_reader.c


Modified: seismo/2D/SPECFEM2D/trunk/src/shared/param_reader.c
===================================================================
--- seismo/2D/SPECFEM2D/trunk/src/shared/param_reader.c	2011-06-02 04:07:28 UTC (rev 18525)
+++ seismo/2D/SPECFEM2D/trunk/src/shared/param_reader.c	2011-06-02 16:43:11 UTC (rev 18526)
@@ -46,8 +46,11 @@
 /*
 
  by Dennis McRitchie (Princeton University, USA)
+ and others
 
- January 7, 2010 - par_file parsing
+ January 7, 2010 - par_file parsing for SPECFEM3D_GLOBE
+ ??? - Modifications (by others) to work with SPECFEM2D
+ June 1, 2011 - Updated to support multi-word values; also a few bug fixes.
  ..
  You'll notice that the heart of the parser is a complex regular
  expression that is compiled within the C code, and then used to split
@@ -63,7 +66,7 @@
  already six C files that make up part of the build (though they all are
  related to the pyre-framework).
  ..
- */
+*/
 
 #define _GNU_SOURCE
 #include "config.h"
@@ -128,8 +131,6 @@
   fclose(fid);
 }
 
-
-// Parses file to read in parameter
 void
 FC_FUNC_(param_read,PARAM_READ)(char * string_read, int * string_read_len, char * name, int * name_len, int * ierr)
 {
@@ -159,16 +160,15 @@
   }
   /* Regular expression for parsing lines from param file.
    ** Good luck reading this regular expression.  Basically, the lines of
-   ** the parameter file should be of the form 'parameter = value'.  Blank
+   ** the parameter file should be of the form 'parameter = value',
+   ** optionally followed by a #-delimited comment.
+   ** 'value' can be any number of space- or tab-separated words. Blank
    ** lines, lines containing only white space and lines whose first non-
    ** whitespace character is '#' are ignored.  White space is generally
    ** ignored.  As you will see later in the code, if both parameter and
    ** value are not specified the line is ignored.
    */
-  // line must include a "=" character and end with a comment
-  //char pattern[] = "^[ \t]*([^# \t]*)[ \t]*=[ \t]*([^# \t]*)[ \t]*(#.*){0,1}$";
-  // line must include a "=" character (no need to end with a comment)
-  char pattern[] = "^[ \t]*([^# \t]*)[ \t]*=[ \t]*([^# \t]*)[ \t]*";
+  char pattern[] = "^[ \t]*([^# \t]+)[ \t]*=[ \t]*([^# \t]+([ \t]+[^# \t]+)*)";
 
   // Compile the regular expression.
   status = regcomp(&compiled_pattern, pattern, REG_EXTENDED);
@@ -203,11 +203,7 @@
       regfree(&compiled_pattern);
       return;
     }
-    //printf("Line read: %s\n", line);
-    //keyword = strndup(line+parameter[0].rm_so, parameter[0].rm_eo-parameter[0].rm_so);
-    //printf("string 0: %s\n", keyword);
-    //free(keyword);
-
+    // printf("Line read = %s\n", line);
     // If we have a match, extract the keyword from the line.
     keyword = strndup(line+parameter[1].rm_so, parameter[1].rm_eo-parameter[1].rm_so);
     //printf("keyword: %s\n", keyword);
@@ -226,13 +222,14 @@
     // Clear out the return string with blanks, copy the value into it, and return.
     memset(string_read, ' ', *string_read_len);
     strncpy(string_read, value, strlen(value));
+    // printf("'%s'='%s'\n", namecopy2, value);
     free(value);
     free(namecopy);
     *ierr = 0;
     return;
   }
   // If no keyword matches, print out error and die.
-  printf("No match in parameter file for keyword %s\n", namecopy2);
+  printf("No match in parameter file for keyword '%s'\n", namecopy);
   free(namecopy);
   regfree(&compiled_pattern);
   *ierr = 1;
@@ -240,7 +237,7 @@
 }
 
 
-// reads next line in file to read in parameters without need of given parameter name
+// reads next line in file to read in parameter
 void
 FC_FUNC_(param_read_nextparam,PARAM_READ_NEXTPARAM)(char * string_read, int * string_read_len, char * name, int * name_len, int * ierr)
 {
@@ -271,16 +268,15 @@
 
   /* Regular expression for parsing lines from param file.
    ** Good luck reading this regular expression.  Basically, the lines of
-   ** the parameter file should be of the form 'parameter = value'.  Blank
+   ** the parameter file should be of the form 'parameter = value',
+   ** optionally followed by a #-delimited comment.
+   ** 'value' can be any number of space- or tab-separated words. Blank
    ** lines, lines containing only white space and lines whose first non-
    ** whitespace character is '#' are ignored.  White space is generally
    ** ignored.  As you will see later in the code, if both parameter and
    ** value are not specified the line is ignored.
    */
-  // line must include a "=" character and end with a comment
-  //char pattern[] = "^[ \t]*([^# \t]*)[ \t]*=[ \t]*([^# \t]*)[ \t]*(#.*){0,1}$";
-  // line must include a "=" character (no need to end with a comment
-  char pattern[] = "^[ \t]*([^# \t]*)[ \t]*=[ \t]*([^# \t]*)[ \t]*";
+  char pattern[] = "^[ \t]*([^# \t]+)[ \t]*=[ \t]*([^# \t]+([ \t]+[^# \t]+)*)";
 
   // Compile the regular expression.
   status = regcomp(&compiled_pattern, pattern, REG_EXTENDED);
@@ -288,18 +284,9 @@
     printf("regcomp returned error %d\n", status);
   }
 
-  // skips this... takes current position where file pointer is now...
-  /*
-   // Position the open file to the beginning.
-   if (fseek(fid, 0, SEEK_SET) != 0) {
-   printf("Can't seek to begining of parameter file\n");
-   *ierr = 1;
-   regfree(&compiled_pattern);
-   return;
-   }
-   */
+  // Do not reposition to BOF - use current position where file pointer is now...
 
-  // Read every line in the file.
+  // Read next lines in the file until we have a match.
   while (fgets(line, LINE_MAX, fid) != NULL) {
     // Get rid of the ending newline.
     int linelen = strlen(line);
@@ -344,14 +331,15 @@
     // Clear out the return string with blanks, copy the value into it, and return.
     memset(string_read, ' ', *string_read_len);
     strncpy(string_read, value, strlen(value));
+    // printf("'%s'='%s'\n", namecopy2, value);
     free(value);
     free(namecopy);
     *ierr = 0;
     return;
   }
   // If no next line matches, print out error and die.
-  printf("No match in parameter file for %s on next line \n",namecopy2);
-  //free(namecopy);
+  printf("No match in parameter file for keyword '%s' on next line\n", namecopy);
+  free(namecopy);
   regfree(&compiled_pattern);
   *ierr = 1;
   return;
@@ -362,65 +350,23 @@
 void
 FC_FUNC_(param_read_nextline,PARAM_READ_NEXTLINE)(char * string_read, int * string_read_len, int * ierr)
 {
-  //char * namecopy;
-  //char * blank;
-  //char * namecopy2;
   int status;
   regex_t compiled_pattern;
   char line[LINE_MAX];
   int regret;
   regmatch_t parameter[1];
-  //char * keyword;
   char * value;
 
-  // no parameter name appears on this line ...
-  /*
-  // Trim the keyword name we're looking for.
-  namecopy = strndup(name, *name_len);
-  blank = strchr(namecopy, ' ');
-  if (blank != NULL) {
-    namecopy[blank - namecopy] = '\0';
-  }
-  // Then get rid of any dot-terminated prefix.
-  namecopy2 = strchr(namecopy, '.');
-  if (namecopy2 != NULL) {
-    namecopy2 += 1;
-  } else {
-    namecopy2 = namecopy;
-  }
-  */
+  // Regular expression to skip any comment lines.
+  char pattern[] = "^[ \t]*[^#]";
 
-  /* Regular expression for parsing lines from param file.
-   ** Good luck reading this regular expression.  Basically, the lines of
-   ** the parameter file should be of the form 'parameter = value'.  Blank
-   ** lines, lines containing only white space and lines whose first non-
-   ** whitespace character is '#' are ignored.  White space is generally
-   ** ignored.  As you will see later in the code, if both parameter and
-   ** value are not specified the line is ignored.
-   */
-  // line must include a "=" character and end with a comment
-  //char pattern[] = "^[ \t]*([^# \t]*)[ \t]*=[ \t]*([^# \t]*)[ \t]*(#.*){0,1}$";
-  // line must include a "=" character (no need to end with a comment)
-  //char pattern[] = "^[ \t]*([^# \t]*)[ \t]*=[ \t]*([^# \t]*)[ \t]*";
-  // line must include numbers
-  char pattern[] = "^[ \t]*[^# \t][0-9]*";
-
   // Compile the regular expression.
   status = regcomp(&compiled_pattern, pattern, REG_EXTENDED);
   if (status != 0) {
     printf("regcomp returned error %d\n", status);
   }
 
-  // skips this... takes current position where file pointer is now...
-  /*
-  // Position the open file to the beginning.
-  if (fseek(fid, 0, SEEK_SET) != 0) {
-    printf("Can't seek to begining of parameter file\n");
-    *ierr = 1;
-    regfree(&compiled_pattern);
-    return;
-  }
-  */
+  // Do not reposition to BOF - use current position where file pointer is now...
 
   // Read every line in the file.
   while (fgets(line, LINE_MAX, fid) != NULL) {
@@ -429,10 +375,9 @@
     if (line[linelen-1] == '\n') {
       line[linelen-1] = '\0';
     }
-    /* Test if line matches the regular expression pattern, if so
-     ** return position of keyword and value */
+    // Test if line matches the regular expression pattern
     regret = regexec(&compiled_pattern, line, 1, parameter, 0);
-    // If no match, check the next line.
+    // If no match (i.e., comment line), check the next line.
     if (regret == REG_NOMATCH) {
       continue;
     }
@@ -443,37 +388,17 @@
       regfree(&compiled_pattern);
       return;
     }
+    regfree(&compiled_pattern);
     //printf("Line nextline read: %s\n", line);
 
-    // no comparison with parameter needed
-    /*
-    // If we have a match, extract the keyword from the line.
-    keyword = strndup(line+parameter[1].rm_so, parameter[1].rm_eo-parameter[1].rm_so);
-
-    // If the keyword is not the one we're looking for, check the next line.
-    if (strcmp(keyword, namecopy2) != 0) {
-      free(keyword);
-      continue;
-    }
-    free(keyword);
-     */
-    regfree(&compiled_pattern);
-
-    // If it matches, extract the value from the line.
-    value = strndup(line+parameter[0].rm_so, strlen(line));
-    //printf("value: %s\n", value);
-
     // Clear out the return string with blanks, copy the value into it, and return.
     memset(string_read, ' ', *string_read_len);
-    strncpy(string_read, value, strlen(value));
-    free(value);
-    //free(namecopy);
+    strncpy(string_read, line, strlen(line));
     *ierr = 0;
     return;
   }
   // If no next line matches, print out error and die.
   printf("No match in parameter file for next line \n");
-  //free(namecopy);
   regfree(&compiled_pattern);
   *ierr = 1;
   return;



More information about the CIG-COMMITS mailing list