lcio/src/cpp/include/UTIL
diff -N LCSplitWriter.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ LCSplitWriter.h 7 Mar 2006 09:32:29 -0000 1.1
@@ -0,0 +1,128 @@
+#ifndef UTIL_LCSplitWriter_H
+#define UTIL_LCSplitWriter_H 1
+
+#include "IO/LCWriter.h"
+#include "LCIOTypes.h"
+#include <string>
+
+namespace UTIL{
+
+ /** LCWriter wrapper that automatically splits files if a given
+ * number of bytes is exceeded. File names are extended by
+ * a file number of the form .000 - .999 ... <br>
+ * Note: this is experimental beta code - please use for testing only !
+ *
+ * @author gaede
+ * @version $Id: LCSplitWriter.h,v 1.1 2006/03/07 09:32:29 gaede Exp $
+ */
+ class LCSplitWriter : public IO::LCWriter {
+
+ public:
+
+ /** The constructor. Takes a pointer to a valid LCWriter instance and
+ * the file size in bytes that causes a new file to be opened if exceeded.<b>
+ * example:<br>
+ * // replace LCWriter* lcWrt = LCFactory::getInstance()->createLCWriter() ; // with
+ * LCSplitWriter* lcWrt = new LCSplitWriter( LCFactory::getInstance()->createLCWriter() , 2040109465 ) ;<br>
+ * to automatically split files after 1.9 GByte file size has been exceeded.
+ */
+
+ LCSplitWriter( IO::LCWriter* wrt, EVENT::long64 maxBytes ) :
+ _wrt( wrt ),
+ _maxBytes(maxBytes),
+ _baseFilename(""),
+ _extension(""),
+ _count(0) {
+ }
+
+
+ /** Destructor.
+ */
+ virtual ~LCSplitWriter() {;}
+
+
+ //-------------------- the LCWriter interface: -------------------------------------
+
+
+ /** Opens a file for writing where the filename has to include the extension but not the number, e.g.
+ * myfile.slcio. Note that this is different from the LCWriter specification.
+ *
+ *@throws IO::IOException
+ */
+ virtual void open(const std::string & filename) throw (IO::IOException, std::exception ) ;
+
+ /** Not implemented - will throw an Exception if called.
+ * Overwriting of or appending to split files is not straight forward. Pleas use the default write mode
+ * and remove exisiting files.
+ *
+ * @throws IO::IOException
+ */
+ virtual void open(const std::string & filename, int writeMode) throw (IO::IOException, std::exception ) ;
+
+ /** Writes the given run header to file. Opens a new file if the given file size is already exceeded
+ * before the execution of the write access.
+ *
+ * @throws IO::IOException
+ */
+ virtual void writeRunHeader(const EVENT::LCRunHeader * hdr) throw (IO::IOException, std::exception ) ;
+
+
+ /** Writes the given file to file. Opens a new file if the given file size is already exceeded
+ * before the execution of the write access.
+ *
+ *@throws IO::IOException
+ */
+ virtual void writeEvent(const EVENT::LCEvent * evt) throw (IO::IOException, std::exception ) ;
+
+ /** Closes the output file/stream.
+ *
+ *@throws IO::IOException
+ */
+ virtual void close() throw (IO::IOException, std::exception ) ;
+
+
+ /** Flushes the output file/stream.
+ *
+ *@throws IO::IOException
+ */
+ virtual void flush() throw (IO::IOException, std::exception ) ;
+
+
+ //---------------------------------------------------------------------------------------------
+
+ /** Return the current file size in bytes.
+ */
+ EVENT::long64 fileSize() ;
+
+
+
+ protected:
+ LCSplitWriter( );
+
+ /** Helper function that returns the file size in bytes - o if the file does not exist
+ */
+ EVENT::long64 file_size( const char *fname) ;
+
+ /** Get the complete filename w/o extensiomn,e.g. MyFilename.007
+ */
+ const std::string& getFilename() ;
+
+ /** Helper function to determine base file name, i.e. w/o extension.
+ */
+ void setBaseFilename( const std::string& filename ) ;
+
+ /** Returns the string representation of the file number, e.g. "007".
+ */
+ std::string getCountingString(unsigned count) ;
+
+ IO::LCWriter* _wrt ;
+ EVENT::long64 _maxBytes ;
+ std::string _baseFilename ;
+ std::string _extension ;
+ unsigned _count ;
+
+ }; // class
+
+} // namespace UTIL
+
+#endif /* ifndef UTIL_LCSplitWriter_H */
lcio/src/cpp/src/UTIL
diff -N LCSplitWriter.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ LCSplitWriter.cc 7 Mar 2006 09:32:30 -0000 1.1
@@ -0,0 +1,154 @@
+
+#include "UTIL/LCSplitWriter.h"
+
+#include <sys/stat.h>
+
+#include <sstream>
+#include <iomanip>
+#include <iostream>
+
+#ifdef SPLIT_WRITER_NDIGITS
+#define NDIGITS SPLIT_WRITER_NDIGITS
+#else
+#define NDIGITS 3
+#endif
+
+using namespace EVENT ;
+
+
+namespace UTIL{
+
+
+
+ void LCSplitWriter::open(const std::string & filename) throw (IO::IOException, std::exception ) {
+ _count = 0 ;
+ setBaseFilename( filename ) ;
+ _wrt->open( getFilename() ) ;
+ }
+
+ void LCSplitWriter::open(const std::string & filename, int writeMode) throw (IO::IOException, std::exception ) {
+ throw Exception(" LCSplitWriter doesn't support NEW and APPEND mode ! "
+ " Please remove your old file(s) and use the default mode." ) ;
+
+ // _count = 0 ;
+ // setBaseFilename( filename ) ;
+ // _wrt->open( getFilename() , writeMode ) ;
+ }
+
+ void LCSplitWriter::writeRunHeader(const EVENT::LCRunHeader * hdr) throw (IO::IOException, std::exception ) {
+
+ _wrt->flush() ;
+ if( fileSize() > _maxBytes ) {
+ _wrt->close() ;
+ ++_count ;
+ _wrt->open( getFilename() ) ;
+ }
+
+ _wrt->writeRunHeader( hdr ) ;
+ }
+
+ void LCSplitWriter::writeEvent(const EVENT::LCEvent * evt) throw (IO::IOException, std::exception ) {
+
+ _wrt->flush() ;
+
+ if( fileSize() > _maxBytes ) {
+
+// std::cout << " switching to new file - old size : " << fileSize() << " > " << _maxBytes << std::endl ;
+
+ _wrt->close() ;
+ ++_count ;
+ _wrt->open( getFilename() ) ;
+
+// std::cout << " switching new file size : " << fileSize() << " - file " << getFilename() << std::endl ;
+
+ }
+
+ _wrt->writeEvent( evt ) ;
+ }
+
+ void LCSplitWriter::close() throw (IO::IOException, std::exception ) {
+ _wrt->close() ;
+ }
+
+ void LCSplitWriter::flush() throw (IO::IOException, std::exception ) {
+ _wrt->flush() ;
+ }
+
+
+
+
+ const std::string& LCSplitWriter::getFilename() {
+
+ static unsigned int lastCount = 4294967295UL ;
+ static std::string filename ;
+
+ if( _count != lastCount )
+ filename = std::string( _baseFilename + "." + getCountingString( _count ) + _extension ) ;
+
+ lastCount = _count ;
+
+ return filename ;
+ }
+
+
+
+ long64 LCSplitWriter::file_size( const char *fname) {
+
+ struct stat64 sbuf;
+
+ int ret = stat64(fname, &sbuf);
+
+ if( ret < 0 )
+ return -1 ;
+
+ return sbuf.st_size ;
+ }
+
+ long64 LCSplitWriter::fileSize() {
+
+// // #include <sys/stat.h>
+// struct stat64 sbuf;
+
+// int ret = stat64( getFilename().c_str() , &sbuf);
+
+// if( ret < 0 ) return -1 ;
+
+// return sbuf.st_size ;
+
+ return file_size( getFilename().c_str() ) ;
+ }
+
+
+ void LCSplitWriter::setBaseFilename( const std::string& filename ) {
+
+ unsigned dotPos = filename.find_last_of('.') ;
+
+ if( ( dotPos > 0 ) && // we have a basefile name
+ ( dotPos == filename.length() - 6 ) && // with a 5 character extension
+ ( filename.rfind("lcio") == dotPos + 2 ) ) { // that ends on lcio
+
+ _baseFilename = filename.substr( 0 , filename.length() - 6 ) ;
+ _extension = filename.substr( filename.length() - 6 , filename.length() ) ;
+
+ }else{
+
+ throw Exception(" LCSplitWriter only works with complete file names including extension, e.g. myfile.slcio" ) ;
+
+// _baseFilename = filename ;
+ }
+ }
+
+ std::string LCSplitWriter::getCountingString(unsigned count) {
+
+ std::stringstream countStream ;
+
+
+ countStream << std::setw( NDIGITS ) << std::setfill('0') << count ;
+
+// std::cout << " ------------ getCountingString(" << count << ") " << countStream.str() << std::endl ;
+
+ return countStream.str() ;
+ }
+
+
+}