]> git.treefish.org Git - phys/latlib.git/blobdiff - writeout.cpp
forgot to close dir.
[phys/latlib.git] / writeout.cpp
index 67e90d3b3b9f54fb826d3aa8a554010cd7e3e492..af67fb5fc913dfa9032aea8df877578e646007b4 100644 (file)
@@ -21,45 +21,54 @@ string writeout::tstamp(const long& timestamp)
   return sstr.str();
 }
 
   return sstr.str();
 }
 
-writeout::writeout(const string& wdir, const string& signature, 
-                  const int& rank, const int& procs, const long& timestamp)
+void writeout::newsub(string subname) {
+  of[subname] = new ofstream;
+
+  if ( fulldir != "" ) {
+    if(rank>0) of[subname]->open( (fulldir + "/rank" + cRank + "-" + subname + ".tmp").c_str() );
+    else of[subname]->open( (fulldir + "/" + signature + "-" + subname + ".dat").c_str() );
+
+    if ( !of[subname]->is_open() ) {
+      logf << "WRITEOUT: Could not open output-file!" << endl << flush;
+      exit(1);
+    }
+
+    buf[subname] = of[subname]->rdbuf();
+  }
+  else
+    buf[subname] = cout.rdbuf();
+
+  out[subname] = new ostream(buf[subname]);
+}
+
+writeout::writeout(const string& wdir, const string& _signature, 
+                  const int& _rank, const int& procs, const long& timestamp)
 {
 {
-  struct stat sb;
+  fulldir = "";
+  signature = _signature;
+  rank = _rank;
 
   if(wdir != ""){
     numprocs = procs;
     sprintf(cRank, "%d", rank);
     fulldir = wdir + "/" + tstamp(timestamp) + "_" + signature + ".tmp";
 
 
   if(wdir != ""){
     numprocs = procs;
     sprintf(cRank, "%d", rank);
     fulldir = wdir + "/" + tstamp(timestamp) + "_" + signature + ".tmp";
 
-    if ( rank == 0 )
-      for (int itry=0; itry < 10 && mkdir( fulldir.c_str(), 0775) != 0; itry++)
-       sleep(1);
-    else
-      while ( ! (stat(fulldir.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) )
-       sleep(1);
-
-    if(rank>0) of.open( (fulldir + "/rank" + cRank + ".tmp").c_str() );
-    else of.open( (fulldir + "/" + signature + ".dat").c_str() );
+    mkdir(fulldir.c_str(), 0775);
 
     logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
 
 
     logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
 
-    if ( (!of.is_open()) || (!logf.is_open()) ) {
-      cerr << "WRITEOUT: Could not open output- and/or log-file!" << endl << flush;
+    if ( !logf.is_open() ) {
+      cerr << "WRITEOUT: Could not open log-file!" << endl << flush;
       exit(1);
     }
 
     logf << "[ " << timestring() << " ] Log starts here." << endl;
     
       exit(1);
     }
 
     logf << "[ " << timestring() << " ] Log starts here." << endl;
     
-    buf = of.rdbuf();
     logbuf = logf.rdbuf();
     logbuf = logf.rdbuf();
-
-    cout << buf << endl;
   }
   else{
   }
   else{
-    buf = cout.rdbuf();
     logbuf = cerr.rdbuf();
   }
     logbuf = cerr.rdbuf();
   }
-  out = new ostream(buf);
   log = new ostream(logbuf);
 }
 
   log = new ostream(logbuf);
 }
 
@@ -74,63 +83,68 @@ string writeout::timestring()
 
 writeout::~writeout()
 {
 
 writeout::~writeout()
 {
-  if(fulldir != "")
-    {
-      if( cRank[0] == '0' )
-       {
-         int jobsdone=0;
-         while(jobsdone<numprocs-1)
-           {
-             string nextfile;
-             if( (nextfile=getdatfile()) == "" ) sleep(1);
-             else
-               {
-                 cerr << "collecting " << nextfile << endl;
-
-                 ifstream myfile( (fulldir + "/" + nextfile).c_str() );
-                 while(true){
-                   string line;
-                   getline(myfile, line);
-                   if( !myfile.good() ) break;
-                   of << line << endl << flush;
-                 }
-                 myfile.close();
-                 remove( (fulldir + "/" + nextfile).c_str() );
-                 jobsdone++;
-               }
+  if(fulldir != "") {
+    for (map<string,ofstream*>::iterator ofit = of.begin(); ofit != of.end(); ++ofit) {
+      if( cRank[0] == '0' ) {
+       int jobsdone=0;
+       while(jobsdone<numprocs-1) {
+         string nextfile;
+         if( (nextfile = getdatfile(ofit->first)) == "" ) 
+           sleep(1);
+         else {
+           logf << "collecting " << nextfile << endl;
+                   
+           ifstream myfile( (fulldir + "/" + nextfile).c_str() );
+           while(true) {
+             string line;
+             getline(myfile, line);
+             if( !myfile.good() ) break;
+             *ofit->second << line << endl << flush;
            }
            }
-         of << "#end" << endl << flush;
-         of.close();
-         rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
-       }
-      else
-       {
-         of.close();
-         rename((fulldir + "/rank" + cRank + ".tmp").c_str(),
-                (fulldir + "/rank" + cRank + ".part").c_str());
+           myfile.close();
+           remove( (fulldir + "/" + nextfile).c_str() );
+           jobsdone++;
+         }
        }
        }
+       *ofit->second << "#end" << endl << flush;
+       ofit->second->close();
+      }
+      else {
+       ofit->second->close();
+       rename((fulldir + "/rank" + cRank + "-" + ofit->first + ".tmp").c_str(),
+              (fulldir + "/rank" + cRank + "-" + ofit->first + ".part").c_str());
+      }
     }
     }
+    if( cRank[0] == '0' )
+      rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
+  }
   logf << "[ " << timestring() << " ] Log ends here." << endl;
   logf.close();
 }
 
   logf << "[ " << timestring() << " ] Log ends here." << endl;
   logf.close();
 }
 
-string writeout::getdatfile()
+string writeout::getdatfile(string subname)
 {
   string myfile;
   DIR *dp;
   struct dirent *dirp;
 
   if((dp  = opendir(fulldir.c_str())) == NULL) {
 {
   string myfile;
   DIR *dp;
   struct dirent *dirp;
 
   if((dp  = opendir(fulldir.c_str())) == NULL) {
-    cerr << "Error(" << errno << ") opening " << fulldir << endl;
+    logf << "Error(" << errno << ") opening " << fulldir << endl;
+    closedir(dp);
     return "";
   }
   
   while ((dirp = readdir(dp)) != NULL)
     {
       myfile = string(dirp->d_name);
     return "";
   }
   
   while ((dirp = readdir(dp)) != NULL)
     {
       myfile = string(dirp->d_name);
-      if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part")
+
+      if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part" &&
+        subname == myfile.substr( myfile.find("-")+1, myfile.rfind(".")-myfile.find("-")-1 ) ) {
+       closedir(dp);
        return myfile;
        return myfile;
+      }
     }
 
     }
 
+  closedir(dp);
   return "";
 }
   return "";
 }