--- /dev/null
+#include "writeout.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <iostream>
+#include <time.h>
+#include <sstream>
+#include <dirent.h>
+#include <errno.h>
+
+using namespace std;
+
+string writeout::tstamp()
+{
+ stringstream sstr;
+ sstr << time (NULL);
+ return sstr.str();
+}
+
+writeout::writeout(const string& wdir, const string& signature,
+ const int& rank, const int& procs)
+{
+ if(wdir != ""){
+ numprocs = procs;
+ sprintf(cRank, "%d", rank);
+ fulldir = wdir + "/" + tstamp() + "_" + signature;
+ mkdir( fulldir.c_str(), 0775);
+
+ if(rank>0) of.open( (fulldir + "/rank" + cRank + ".tmp").c_str() );
+ else of.open( (fulldir + "/" + signature + ".dat").c_str() );
+
+ buf = of.rdbuf();
+ }
+ else{
+ buf = cout.rdbuf();
+ }
+ out = new ostream(buf);
+}
+
+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(myfile.good()){
+ string line;
+ getline(myfile, line);
+ of << line << flush;
+ }
+ myfile.close();
+ of << endl << flush;
+
+ remove( (fulldir + "/" + nextfile).c_str() );
+ jobsdone++;
+ }
+ }
+ of << "#end" << endl << flush;
+ of.close();
+ }
+ else
+ {
+ of.close();
+ rename((fulldir + "/rank" + cRank + ".tmp").c_str(),
+ (fulldir + "/rank" + cRank + ".part").c_str());
+ }
+ }
+}
+
+string writeout::getdatfile()
+{
+ string myfile;
+ DIR *dp;
+ struct dirent *dirp;
+
+ if((dp = opendir(fulldir.c_str())) == NULL) {
+ cerr << "Error(" << errno << ") opening " << fulldir << endl;
+ return "";
+ }
+
+ while ((dirp = readdir(dp)) != NULL)
+ {
+ myfile = string(dirp->d_name);
+ if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part")
+ return myfile;
+ }
+
+ return "";
+}