From 02d2da5028f68c906b262892fd0c944c48847274 Mon Sep 17 00:00:00 2001 From: fambrogl Date: Fri, 30 Oct 2009 16:43:17 +0000 Subject: [PATCH] add the general fileblob object --- CondFormats/Common/interface/FileBlob.h | 45 ++++++ CondFormats/Common/src/FileBlob.cc | 139 ++++++++++++++++++ .../Common/src/T_EventSetup_FileBlob.cc | 4 + CondFormats/Common/src/classes.h | 1 + CondFormats/Common/src/classes_def.xml | 4 + .../xml/FileBlob_automatic_default_0.xml | 11 ++ 6 files changed, 204 insertions(+) create mode 100644 CondFormats/Common/interface/FileBlob.h create mode 100644 CondFormats/Common/src/FileBlob.cc create mode 100644 CondFormats/Common/src/T_EventSetup_FileBlob.cc create mode 100644 CondFormats/Common/xml/FileBlob_automatic_default_0.xml diff --git a/CondFormats/Common/interface/FileBlob.h b/CondFormats/Common/interface/FileBlob.h new file mode 100644 index 0000000000000..9a2f7944164c4 --- /dev/null +++ b/CondFormats/Common/interface/FileBlob.h @@ -0,0 +1,45 @@ +#ifndef CondFormats_FileBlob_h +#define CondFormats_FileBlob_h + +#include +#include +#include + +class FileBlob{ + + public: + FileBlob() {}; + /// constructor from file to read + FileBlob(const std::string & fname, bool zip); + /// constructor from stream to read + FileBlob(std::istream & is, bool zip); + + ~FileBlob(){}; + + /// read from real file + void read(const std::string&); + /// write to real file + void write(const std::string&) const; + + /// read from istream + void read(std::istream &); + /// write to ostream + void write(std::ostream &) const; + + bool isCompressed() const {return compressed;}; + + int size() const {return isize;}; + /// i didn't want to do two copies ... hope this works. + std::vector* getUncompressedBlob() const; + void getUncompressedBlob( std::vector& myblobcopy ) const; + + private: + static unsigned int computeFileSize(const std::string &); + static unsigned int computeStreamSize(std::istream &); + + std::vector blob; + bool compressed; + unsigned int isize; +}; + +#endif diff --git a/CondFormats/Common/src/FileBlob.cc b/CondFormats/Common/src/FileBlob.cc new file mode 100644 index 0000000000000..1bae66fd48c06 --- /dev/null +++ b/CondFormats/Common/src/FileBlob.cc @@ -0,0 +1,139 @@ +#include "CondFormats/Common/interface/FileBlob.h" +#include "FWCore/MessageLogger/interface/MessageLogger.h" + +#include +#include +#include +#include + +FileBlob::FileBlob(const std::string & fname, bool zip):isize(0){ + compressed = zip; + /* + std::cout << "isize = " << isize + << " zip = " << (zip? "true" : "false") + << std::endl; + */ + if (isize==0) isize= computeFileSize(fname); + // std::cout << "isize = " << isize << std::endl; + blob.reserve(isize); + read(fname); +} +FileBlob::FileBlob(std::istream& is, bool zip):isize(0) { + compressed = zip; + if (isize==0) isize= computeStreamSize(is); + blob.reserve(isize); + read(is); +} + +void FileBlob::read(std::istream & is) { + if(compressed){ + std::vector in; + in.reserve(isize); + char c; + while (is.get(c)) + in.push_back((unsigned char)c); + /* + for(int i=0;i out(isize); + uLongf destLen = out.size(); + int zerr = uncompress(&*out.begin(), &destLen, + &*blob.begin(), blob.size()); + if (zerr!=0 || out.size()!=destLen) + edm::LogError("FileBlob")<< "uncompressing error " << zerr + << " original size was " << isize + << " new size is " << destLen; + os.write((const char *)(&*out.begin()),out.size()); + }else{ + os.write((char *)&*blob.begin(),blob.size()); + } +} + +std::vector* FileBlob::getUncompressedBlob() const { + std::vector* newblob; + if(compressed) + { + newblob = new std::vector(isize); + uLongf destLen = newblob->size(); + // std::cout<<"Store isize = "<size() = "<size()<<"; destLen = "<begin()), &destLen, + &*blob.begin(), blob.size()); + if (zerr!=0 || newblob->size()!=destLen) + edm::LogError("FileBlob")<< "uncompressing error " << zerr + << " original size was " << isize + << " new size is " << destLen; + }else{ + newblob = new std::vector(blob); + } + return newblob; + } + +void FileBlob::getUncompressedBlob( std::vector& myblobcopy ) const { + if(compressed) + { + myblobcopy.reserve(isize); + uLongf destLen = isize; + int zerr = uncompress(&*myblobcopy.begin(), &destLen, + &*blob.begin(), blob.size()); + if (zerr!=0 || myblobcopy.size()!=destLen) + edm::LogError("FileBlob")<< "uncompressing error " << zerr + << " original size was " << isize + << " new size is " << destLen; + }else{ + myblobcopy = blob; + } + +} + +void FileBlob::read(const std::string & fname) { + std::ifstream ifile(fname.c_str()); + if (!ifile) { edm::LogError("FileBlob")<< "file " << fname << " does not exist...";} + else read(ifile); + ifile.close(); +} + +void FileBlob::write(const std::string & fname) const { + std::ofstream ofile(fname.c_str()); + write(ofile); + ofile.close(); +} + +unsigned int FileBlob::computeFileSize(const std::string & fname) { + unsigned int is=0; + std::ifstream ifile(fname.c_str()); + if (!ifile) { edm::LogError("FileBlob")<< "file " << fname << " does not exist...";} + else is = computeStreamSize(ifile); + ifile.close(); + return is; +} + +unsigned int FileBlob::computeStreamSize(std::istream & is) { + unsigned int rs=0; + char c; + while (is.get(c)) rs++; + is.clear(); + is.seekg(0); + return rs; +} diff --git a/CondFormats/Common/src/T_EventSetup_FileBlob.cc b/CondFormats/Common/src/T_EventSetup_FileBlob.cc new file mode 100644 index 0000000000000..eaf3fb86ab67a --- /dev/null +++ b/CondFormats/Common/src/T_EventSetup_FileBlob.cc @@ -0,0 +1,4 @@ +#include "CondFormats/Common/interface/FileBlob.h" +#include "FWCore/Framework/interface/eventsetupdata_registration_macro.h" + +EVENTSETUP_DATA_REG(FileBlob); diff --git a/CondFormats/Common/src/classes.h b/CondFormats/Common/src/classes.h index f2a8712c0ad0c..75a8a232ac02c 100644 --- a/CondFormats/Common/src/classes.h +++ b/CondFormats/Common/src/classes.h @@ -1,6 +1,7 @@ #include "CondFormats/Common/interface/IOVSequence.h" #include "CondFormats/Common/interface/PayloadWrapper.h" #include "CondFormats/Common/interface/GenericSummary.h" +#include "CondFormats/Common/interface/FileBlob.h" #include "CondFormats/Common/interface/BaseKeyed.h" #include "CondFormats/Common/interface/IOVKeysDescription.h" diff --git a/CondFormats/Common/src/classes_def.xml b/CondFormats/Common/src/classes_def.xml index 422071cab6320..72d4d85c4ef80 100644 --- a/CondFormats/Common/src/classes_def.xml +++ b/CondFormats/Common/src/classes_def.xml @@ -14,6 +14,10 @@ + + + + diff --git a/CondFormats/Common/xml/FileBlob_automatic_default_0.xml b/CondFormats/Common/xml/FileBlob_automatic_default_0.xml new file mode 100644 index 0000000000000..84b44e6191f55 --- /dev/null +++ b/CondFormats/Common/xml/FileBlob_automatic_default_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + +