[Commits] [svn:einsteintoolkit] Multipole/trunk/src/ (Rev. 76)

ian.hinder at aei.mpg.de ian.hinder at aei.mpg.de
Thu Mar 3 09:42:48 CST 2011


User: hinder
Date: 2011/03/03 09:42 AM

Modified:
 /trunk/src/
  utils.cc

Log:
 Correct check for whether to truncate output HDF5 file
 
 Previously a "first_time" variable was used to determine if truncation should happen.  When decomposing multiple variables, this logic is incorrect.  We now store the first_time information per output file.

File Changes:

Directory: /trunk/src/
======================

File [modified]: utils.cc
Delta lines: +21 -6
===================================================================
--- trunk/src/utils.cc	2011-02-23 14:23:41 UTC (rev 75)
+++ trunk/src/utils.cc	2011-03-03 15:42:47 UTC (rev 76)
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <math.h>
 #include <string>
+#include <map>
 #include <sys/stat.h>
 #include <errno.h>
 #include <iostream>
@@ -152,6 +153,12 @@
 
 #ifdef HAVE_CAPABILITY_HDF5
 
+static bool file_exists(const string &name)
+{
+  struct stat sts;
+  return !(stat(name.c_str(), &sts) == -1 && errno == ENOENT);
+}
+
 bool dataset_exists(hid_t file, const string &dataset_name)
 {
   // To test whether a dataset exists, the recommended way in API 1.6
@@ -182,15 +189,23 @@
 
   string output_name = out_dir + string("/") + basename;
   int status = 0;
-  static int first_time = true;
+  static map<string,bool> checked; // Has the given file been checked
+                                   // for truncation? map<*,bool>
+                                   // defaults to false
 
-  hid_t file =
-    (first_time && IO_TruncateOutputFiles(cctkGH)) ?
-    H5Fcreate(output_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT) :
-    H5Fopen(output_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
+  hid_t file;
 
-  first_time = false;
+  if (!file_exists(output_name) || (!checked[output_name] && IO_TruncateOutputFiles(cctkGH)))
+  {
+    file = H5Fcreate(output_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
+  }
+  else
+  {
+    file = H5Fopen(output_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
+  }
 
+  checked[output_name] = true;
+
   hid_t dataset = -1;
 
   if (dataset_exists(file, datasetname))



More information about the Commits mailing list