[Avida-SVN] r1951 - in development/source: main tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Thu Aug 16 13:38:20 PDT 2007


Author: brysonda
Date: 2007-08-16 16:38:20 -0400 (Thu, 16 Aug 2007)
New Revision: 1951

Modified:
   development/source/main/cAvidaConfig.cc
   development/source/main/cAvidaConfig.h
   development/source/tools/cInitFile.cc
   development/source/tools/cInitFile.h
   development/source/tools/tDictionary.h
   development/source/tools/tHashTable.h
Log:
Relax the requirement that -c be specified first.  Add support for preprocessor variables in cInitFile.

Modified: development/source/main/cAvidaConfig.cc
===================================================================
--- development/source/main/cAvidaConfig.cc	2007-08-16 20:06:02 UTC (rev 1950)
+++ development/source/main/cAvidaConfig.cc	2007-08-16 20:38:20 UTC (rev 1951)
@@ -52,11 +52,17 @@
   }
 }
 
-void cAvidaConfig::Load(const cString & filename, 
-                        const bool & crash_if_not_found = false)
+void cAvidaConfig::Load(const cString& filename, const bool& crash_if_not_found = false)
 {
+  tDictionary<cString> mappings;
+  Load(filename, mappings, crash_if_not_found);
+}
+
+
+void cAvidaConfig::Load(const cString& filename, const tDictionary<cString>& mappings, const bool& crash_if_not_found = false)
+{
   // Load the contents from the file.
-  cInitFile init_file(filename);
+  cInitFile init_file(filename, mappings);
   
   if (!init_file.WasOpened()) {
     if (crash_if_not_found) {
@@ -319,12 +325,8 @@
   }  
 }
 
-cAvidaConfig* cAvidaConfig::LoadWithArgs(cStringList &argv)
+cAvidaConfig* cAvidaConfig::LoadWithArgs(cStringList& argv)
 {
-  cString config_filename = "avida.cfg";
-  bool crash_if_not_found = false;
-  tDictionary<cString> sets;
-  
   int arg_num = 1;              // Argument number being looked at.
   
   // Load all of the args into string objects for ease of access.
@@ -336,30 +338,18 @@
     args[i] = list_it.Get();
   }
   
-  // -config option
-  if (argc > 1 && (args[1] == "-c" || args[1] == "-config")) {
-    if (argc < 3) {
-      cerr << "Error: Filename for configuration must be specified." << endl;
-      exit(0);
-    }
-    config_filename = args[2];
-    crash_if_not_found = true;
-    arg_num += 2;
-  } else if (argc > 1 && (args[1] == "-g" || args[1] == "-genesis")) {
-    cerr << "Warning: Use of -g[enesis] deprecated in favor of -c[onfig]." << endl;
-    if (argc < 3) {
-      cerr << "Error: Filename for configuration must be specified." << endl;
-      exit(0);
-    }
-    config_filename = args[2];
-    crash_if_not_found = true;
-    arg_num += 2;
-  }
+  cString config_filename = "avida.cfg";
+  bool crash_if_not_found = false;
+  tDictionary<cString> sets;
+  tDictionary<cString> def_mappings;
+
+  bool flag_analyze = false;
+  bool flag_interactive = false;
+  bool flag_load = false;         cString val_load;
+  bool flag_review = false;
+  bool flag_verbosity = false;    int val_verbosity;
+  bool flag_seed = false;         int val_seed = 0;
   
-  // Create Config object, load with values from configuration file
-  cAvidaConfig* cfg = new cAvidaConfig();
-  cfg->Load(config_filename, crash_if_not_found);
-  
   // Then scan through and process the rest of the args.
   while (arg_num < argc) {
     cString cur_arg = args[arg_num];
@@ -375,8 +365,7 @@
 
     // Review configuration options, listing those non-default.
     else if (cur_arg == "-review" || cur_arg == "-r") {
-      cfg->PrintReview();
-      exit(0);
+      flag_review = true;
     }
     
     else if (cur_arg == "--help" || cur_arg == "-help" || cur_arg == "-h") {
@@ -384,13 +373,14 @@
 	   << "  -a[nalyze]            Process analyze.cfg instead of normal "
                << "run." << endl
 	   << "  -c[onfig] <filename>  Set config file to be <filename>"<<endl
+     << "  -def <name> <value>   Define config include variables" << endl
 	   << "  -e; -actions          Print a list of all known actions"<< endl
 	   << "  -h[elp]               Help on options (this listing)"<<endl
 	   << "  -i[nteractive]        Run analyze mode interactively" << endl
 	   << "  -l[oad] <filename>    Load a clone file" << endl
 	   << "  -r[eview]             Review avida.cfg settings." << endl
 	   << "  -s[eed] <value>       Set random seed to <value>" << endl
-	   << "  -set <name> <value>   Overide values in avida.cfg" << endl
+	   << "  -set <name> <value>   Override values in avida.cfg" << endl
 	   << "  -v[ersion]            Prints the version number" << endl
 	   << "  -v0 -v1 -v2 -v3 -v4   Set output verbosity to 0..4" << endl
 	   << endl;
@@ -398,37 +388,33 @@
       exit(0);
     }
     else if (cur_arg == "-seed" || cur_arg == "-s") {
-      int in_seed = 0;
       if (arg_num + 1 == argc || args[arg_num + 1][0] == '-') {
         cerr << "Error: Must include a number as the random seed!" << endl;
         exit(0);
       } else {
         arg_num++;  if (arg_num < argc) cur_arg = args[arg_num];
-        in_seed = cur_arg.AsInt();
+        val_seed = cur_arg.AsInt();
       }
-      cfg->RANDOM_SEED.Set(in_seed);
+      flag_seed = true;
     } else if (cur_arg == "-analyze" || cur_arg == "-a") {
-      if (cfg->ANALYZE_MODE.Get() < 1) {
-        cfg->ANALYZE_MODE.Set(1);
-      }
+      flag_analyze = true;
     } else if (cur_arg == "-interactive" || cur_arg == "-i") {
-      if (cfg->ANALYZE_MODE.Get() < 2) {
-        cfg->ANALYZE_MODE.Set(2);
-      }
+      flag_interactive = true;
     } else if (cur_arg == "-load" || cur_arg == "-l") {
       if (arg_num + 1 == argc || args[arg_num + 1][0] == '-') {
         cerr << "Error: Must include a filename to load from." << endl;
         exit(0);
       } else {
         arg_num++;  if (arg_num < argc) cur_arg = args[arg_num];
-        cfg->CLONE_FILE.Set(cur_arg);
+        val_load = cur_arg;
       }
+      flag_load = true;
     } else if (cur_arg == "-version" || cur_arg == "-v") {
       // We've already showed version info, so just quit.
       exit(0);
     } else if (cur_arg.Substring(0, 2) == "-v") {
-      int level = cur_arg.Substring(2, cur_arg.GetSize() - 2).AsInt();
-      cfg->VERBOSITY.Set(level);
+      val_verbosity = cur_arg.Substring(2, cur_arg.GetSize() - 2).AsInt();
+      flag_verbosity = true;
     } else if (cur_arg == "-set") {
       if (arg_num + 1 == argc || arg_num + 2 == argc) {
         cerr << "'-set' option must be followed by name and value" << endl;
@@ -439,13 +425,24 @@
       arg_num++;  if (arg_num < argc) cur_arg = args[arg_num];
       cString value(cur_arg);
       sets.Add(name, value);
+    } else if (cur_arg == "-def") {
+      if (arg_num + 1 == argc || arg_num + 2 == argc) {
+        cerr << "'-def' option must be followed by name and value" << endl;
+        exit(0);
+      }
+      arg_num++;  if (arg_num < argc) cur_arg = args[arg_num];
+      cString name(cur_arg);
+      arg_num++;  if (arg_num < argc) cur_arg = args[arg_num];
+      cString value(cur_arg);
+      def_mappings.Add(name, value);
     } else if (cur_arg == "-c" || cur_arg == "-config") {
-      cerr << "Error: -c[onfig] option must be listed first." << endl;
-      exit(0);
-    } else if (cur_arg == "-g" || cur_arg == "-genesis") {
-      cerr << "Warning: Use of '-g[enesis]' deprecated in favor or -c[onfig]." << endl;
-      cerr << "Error: -c[onfig] option must be listed first." << endl;
-      exit(0);
+      if (arg_num + 1 == argc || args[arg_num + 1][0] == '-') {
+        cerr << "Error: Filename for configuration must be specified." << endl;
+        exit(0);
+      }
+      arg_num++;  if (arg_num < argc) cur_arg = args[arg_num];
+      config_filename = cur_arg;
+      crash_if_not_found = true;
     } else {
       cerr << "Error: Unknown Option '" << args[arg_num] << "'" << endl
       << "Type: \"" << args[0] << " -h\" for a full option list." << endl;
@@ -455,6 +452,18 @@
     arg_num++;  if (arg_num < argc) cur_arg = args[arg_num];
   }
   
+  // Create Config object, load with values from configuration file
+  cAvidaConfig* cfg = new cAvidaConfig();
+  cfg->Load(config_filename, def_mappings, crash_if_not_found);
+  
+  
+  if (flag_analyze) if (cfg->ANALYZE_MODE.Get() < 1) cfg->ANALYZE_MODE.Set(1);
+  if (flag_interactive) if (cfg->ANALYZE_MODE.Get() < 2) cfg->ANALYZE_MODE.Set(2);
+  if (flag_load) cfg->CLONE_FILE.Set(val_load);
+  if (flag_seed) cfg->RANDOM_SEED.Set(val_seed);
+  if (flag_verbosity) cfg->VERBOSITY.Set(val_verbosity);
+
+  
   // Loop through all groups, then all entries, and try to load each one.
   tListIterator<cBaseConfigGroup> group_it(cfg->group_list);
   cBaseConfigGroup* cur_group;
@@ -471,7 +480,13 @@
       }
     }
   }
+
   
+  if (flag_review) {
+    cfg->PrintReview();
+    exit(0);
+  }  
+  
   delete [] args;
   
   return cfg;

Modified: development/source/main/cAvidaConfig.h
===================================================================
--- development/source/main/cAvidaConfig.h	2007-08-16 20:06:02 UTC (rev 1950)
+++ development/source/main/cAvidaConfig.h	2007-08-16 20:38:20 UTC (rev 1951)
@@ -46,6 +46,8 @@
 #endif
 
 
+template <class T> class tDictionary;
+
 using namespace std;
 
 
@@ -408,6 +410,7 @@
 #endif
   
   void Load(const cString& filename, const bool& crash_if_not_found);
+  void Load(const cString& filename, const tDictionary<cString>& mappings, const bool& crash_if_not_found);
   void Print(const cString& filename);
   void Status();
   void PrintReview();

Modified: development/source/tools/cInitFile.cc
===================================================================
--- development/source/tools/cInitFile.cc	2007-08-16 20:06:02 UTC (rev 1950)
+++ development/source/tools/cInitFile.cc	2007-08-16 20:38:20 UTC (rev 1951)
@@ -39,6 +39,16 @@
   PostProcess(lines);
 }
 
+cInitFile::cInitFile(const cString& filename, const tDictionary<cString>& mappings)
+  : m_filename(filename), m_ftype("unknown")
+{
+  InitMappings(mappings);
+  tSmartArray<sLine*> lines;
+  m_opened = LoadFile(filename, lines);
+  PostProcess(lines);
+}
+
+
 cInitFile::cInitFile(istream& in_stream) : m_filename("(stream)"), m_ftype("unknown")
 {
   if (in_stream.good() == false) {
@@ -71,6 +81,20 @@
 }
 
 
+void cInitFile::InitMappings(const tDictionary<cString>& mappings)
+{
+  tList<cString> names;
+  mappings.GetKeys(names);
+  
+  tListIterator<cString> names_it(names);
+  while (names_it.Next() != NULL) {
+    cString* name = names_it.Get();
+    cString value;
+    if (mappings.Find(*name, value)) m_mappings.Add(*name, value);
+  }
+}
+
+
 bool cInitFile::LoadFile(const cString& filename, tSmartArray<sLine*>& lines)
 {
   cFile file(filename);
@@ -99,7 +123,19 @@
   cString cmd = cmdstr.PopWord();
   
   if (cmd == "#!include") {
-    LoadFile(cmdstr.PopWord(), lines); // @TODO - report error
+    cString dir = cmdstr.PopWord();
+    if (dir.GetSize() && dir[0] == '$') {
+      dir.ClipFront(1);
+      if (!m_mappings.Find(dir, dir)) {
+        // @TODO - report error
+        return;
+      }
+    }
+    LoadFile(dir, lines); // @TODO - report error
+  } else if (cmd == "#!default") {
+    cString name = cmdstr.PopWord();
+    cmdstr.Trim();
+    if (!m_mappings.HasEntry(name)) m_mappings.Add(name, cmdstr);
   }
 }
 

Modified: development/source/tools/cInitFile.h
===================================================================
--- development/source/tools/cInitFile.h	2007-08-16 20:06:02 UTC (rev 1950)
+++ development/source/tools/cInitFile.h	2007-08-16 20:38:20 UTC (rev 1951)
@@ -32,6 +32,9 @@
 #ifndef cStringList_h
 #include "cStringList.h"
 #endif
+#ifndef tDictionary_h
+#include "tDictionary.h"
+#endif
 #ifndef tSmartArray_h
 #include "tSmartArray.h"
 #endif
@@ -62,8 +65,11 @@
   tArray<sLine*> m_lines;
   cString m_ftype;
   cStringList m_format;
+  
+  tDictionary<cString> m_mappings;
 
-
+  
+  void InitMappings(const tDictionary<cString>& mappings);
   bool LoadFile(const cString& filename, tSmartArray<sLine*>& lines);
   void ProcessCommand(cString cmdstr, tSmartArray<sLine*>& lines);
   void PostProcess(tSmartArray<sLine*>& lines);
@@ -75,6 +81,7 @@
 
 public:
   cInitFile(const cString& filename);
+  cInitFile(const cString& filename, const tDictionary<cString>& mappings);
   cInitFile(std::istream& in_stream);
   ~cInitFile() { for (int i = 0; i < m_lines.GetSize(); i++) delete m_lines[i]; }
   

Modified: development/source/tools/tDictionary.h
===================================================================
--- development/source/tools/tDictionary.h	2007-08-16 20:06:02 UTC (rev 1950)
+++ development/source/tools/tDictionary.h	2007-08-16 20:38:20 UTC (rev 1951)
@@ -57,8 +57,9 @@
 private:
   tHashTable<cString, T> m_hash;
 
-  // disabled copy constructor.
-  tDictionary(const tDictionary &);
+  tDictionary(const tDictionary&); // @not_implemented
+
+
 public:
   inline tDictionary() { ; }
   inline tDictionary(int in_hash_size) : m_hash(in_hash_size) { ; }
@@ -75,6 +76,7 @@
   inline void AsLists(tList<cString>& name_list, tList<T>& value_list) const {
     m_hash.AsLists(name_list, value_list);
   }
+  inline void GetKeys(tList<cString>& names) const { m_hash.GetKeys(names); }
   
   // This function will take an input string and load its value into the
   // dictionary; it will only work for types that cStringUtil can convert to.

Modified: development/source/tools/tHashTable.h
===================================================================
--- development/source/tools/tHashTable.h	2007-08-16 20:06:02 UTC (rev 1950)
+++ development/source/tools/tHashTable.h	2007-08-16 20:38:20 UTC (rev 1951)
@@ -179,11 +179,12 @@
     // No matches found.
     return NULL;
   }
-private:
-    // disabled copy constructor.
-    tHashTable(const tHashTable &);
+
+  tHashTable(const tHashTable &); // @not_implemented
+
+  
 public:
-    tHashTable(int in_table_size=HASH_TABLE_SIZE_DEFAULT)
+  tHashTable(int in_table_size=HASH_TABLE_SIZE_DEFAULT)
     : entry_count(0)
     , table_size(in_table_size)
     , cell_array(in_table_size)
@@ -382,6 +383,14 @@
     }
   }
   
+  
+  void GetKeys(tList<HASH_TYPE>& key_list) const
+  {
+    list_it.Reset();
+    while (list_it.Next() != NULL) key_list.Push(&list_it.Get()->key);
+  }
+  
+  
   void GetValues(tList<DATA_TYPE>& value_list) const
   {
     list_it.Reset();




More information about the Avida-cvs mailing list