[Avida-cvs] [avida-svn] r760 - in development/source: analyze tools

brysonda@myxo.css.msu.edu brysonda at myxo.css.msu.edu
Mon Jun 19 18:12:33 PDT 2006


Author: brysonda
Date: 2006-06-19 21:12:32 -0400 (Mon, 19 Jun 2006)
New Revision: 760

Modified:
   development/source/analyze/cAnalyzeGenotype.h
   development/source/tools/cDataFileManager.cc
   development/source/tools/cString.cc
   development/source/tools/cString.h
Log:
Update cDataFileManager to detect relative paths and conditionally bypass target_dir prefixing.  This fixes the problem of seemingly random empty directory creation and file open errors when executing analyze scripts that make assumptions based on pure file system access (rdar://1509015).

Also add a Trim command to cString that removes leading and trailing whitespace (essentially a combined Left and Right justify).

Modified: development/source/analyze/cAnalyzeGenotype.h
===================================================================
--- development/source/analyze/cAnalyzeGenotype.h	2006-06-20 01:03:18 UTC (rev 759)
+++ development/source/analyze/cAnalyzeGenotype.h	2006-06-20 01:12:32 UTC (rev 760)
@@ -227,7 +227,7 @@
   int GetUpdateDead() const { return update_dead; }
   int GetDepth() const { return depth; }
 
-  const cString & GetParentMuts() const { return parent_muts; }
+  const cString& GetParentMuts() const { return parent_muts; }
 
   // Knockout accessors
   int GetKO_DeadCount() const;

Modified: development/source/tools/cDataFileManager.cc
===================================================================
--- development/source/tools/cDataFileManager.cc	2006-06-20 01:03:18 UTC (rev 759)
+++ development/source/tools/cDataFileManager.cc	2006-06-20 01:12:32 UTC (rev 760)
@@ -24,37 +24,57 @@
   while (list_it.Next() != NULL) delete *list_it.Get();
 }
 
-cDataFile & cDataFileManager::Get(const cString & name)
+cDataFile& cDataFileManager::Get(const cString& name)
 {
-  // Find the file by this name...
+  assert(name.GetSize());
+
   cDataFile* found_file;
-
-  // If it hasn't been found, create it...
-  if (!m_datafiles.Find(name, found_file)) {
-    // Create directory structure, as necessary
-    int i = 0;
-    while (i < name.GetSize()) {
-      int d = name.Find('/', i);
-      int b = name.Find('\\', i);
-      
-      // If a backslash is found, and earlier then next forward slash, use it instead
-      if (b != -1 && b < d) d = b;
-      
-      // Exit if neither slash is found
-      if (d == -1) break;
-      
-      // If directory name is not null
-      if (d - i > 0) cTools::MkDir(m_target_dir + name.Substring(0, d - i), false);
-      
-      // Adjust next directory name starting point
-      i = d + 1;
-    }
-    cString filename = m_target_dir + name;
-    found_file = new cDataFile(filename);
-    m_datafiles.Add(name, found_file);
+  
+  // If found, return file
+  if (m_datafiles.Find(name, found_file)) return *found_file;
+  
+  
+  // Create and sanitize a local copy of the file name
+  cString target(name);
+  target.Trim();
+  
+  // Determine directory prefix, default being the current data directory
+  cString dir_prefix(m_target_dir);
+  if (target[0] == '.' &&             // Must start with '.' to trigger further testing
+      (target.GetSize() == 1 ||       // If string is exactly "."
+       (target.GetSize() > 1 &&       //   or if it ".." or "./" or ".\"
+        (target[1] == '.' || target[1] == '/' || target[1] == '\\')
+       )
+      )
+     )
+  {
+    // Treat path as current working directory relative
+    dir_prefix = "";
   }
+  
+  // Create directory structure, as necessary
+  int i = 0;
+  while (i < target.GetSize()) {
+    int d = target.Find('/', i);
+    int b = target.Find('\\', i);
+    
+    // If a backslash is found, and earlier then next forward slash, use it instead
+    if (b != -1 && b < d) d = b;
+    
+    // Exit if neither slash is found
+    if (d == -1) break;
+    
+    // If directory name is not null
+    if (d - i > 0) cTools::MkDir(dir_prefix + target.Substring(0, d - i), false);
+    
+    // Adjust next directory name starting point
+    i = d + 1;
+  }
 
-  // and return it.
+  target = dir_prefix + target;
+  found_file = new cDataFile(target);
+  m_datafiles.Add(name, found_file);
+
   return *found_file;
 }
 

Modified: development/source/tools/cString.cc
===================================================================
--- development/source/tools/cString.cc	2006-06-20 01:03:18 UTC (rev 759)
+++ development/source/tools/cString.cc	2006-06-20 01:12:32 UTC (rev 760)
@@ -366,7 +366,19 @@
   return ws_count;
 }
 
+void cString::Trim()
+{
+  // Trim front
+  int ws_count = CountWhitespace();
+  if (ws_count > 0) InsertStr(0, NULL, 0, ws_count);
 
+  // Trim trailing
+  ws_count = 0;
+  while (GetSize() - ws_count - 1 > 0 && IsWhitespace(GetSize() - ws_count - 1)) ws_count++;
+  if (ws_count > 0) InsertStr(0, NULL, GetSize() - ws_count, ws_count);
+}
+
+
 cString cString::Pop(const char delim)
 {
   int pos;

Modified: development/source/tools/cString.h
===================================================================
--- development/source/tools/cString.h	2006-06-20 01:03:18 UTC (rev 759)
+++ development/source/tools/cString.h	2006-06-20 01:12:32 UTC (rev 760)
@@ -249,6 +249,11 @@
    * @return The number of characters removed.
    **/
   int RightJustify(); 
+
+  /**
+    * Remove beginning and ending whitespace.
+   **/
+  void Trim(); 
   
   /**
    * Reverse the order of the characters in the string.




More information about the Avida-cvs mailing list