-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fambrogl
committed
Oct 30, 2009
1 parent
c898ddc
commit 02d2da5
Showing
6 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef CondFormats_FileBlob_h | ||
#define CondFormats_FileBlob_h | ||
|
||
#include <vector> | ||
#include <string> | ||
#include <iostream> | ||
|
||
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<unsigned char>* getUncompressedBlob() const; | ||
void getUncompressedBlob( std::vector<unsigned char>& myblobcopy ) const; | ||
|
||
private: | ||
static unsigned int computeFileSize(const std::string &); | ||
static unsigned int computeStreamSize(std::istream &); | ||
|
||
std::vector<unsigned char> blob; | ||
bool compressed; | ||
unsigned int isize; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#include "CondFormats/Common/interface/FileBlob.h" | ||
#include "FWCore/MessageLogger/interface/MessageLogger.h" | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
#include <string> | ||
#include <zlib.h> | ||
|
||
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<unsigned char> in; | ||
in.reserve(isize); | ||
char c; | ||
while (is.get(c)) | ||
in.push_back((unsigned char)c); | ||
/* | ||
for(int i=0;i<in.size();i++){ | ||
std::cout<<in[i]; | ||
} | ||
std::cout<<std::endl; | ||
*/ | ||
blob.resize(isize); | ||
uLongf destLen = compressBound(in.size()); | ||
int zerr = compress2(&*blob.begin(), &destLen, | ||
&*in.begin(), in.size(), | ||
9); | ||
if (zerr!=0) edm::LogError("FileBlob")<< "Compression error " << zerr; | ||
blob.resize(destLen); | ||
}else{ | ||
//std::cout << "reading uncompressed" << std::endl; | ||
char c; | ||
while (is.get(c)) | ||
blob.push_back( (unsigned char)c); | ||
blob.resize(blob.size()); | ||
isize=blob.size(); | ||
} | ||
} | ||
|
||
void FileBlob::write(std::ostream & os) const { | ||
if(compressed){ | ||
std::vector<unsigned char> 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<unsigned char>* FileBlob::getUncompressedBlob() const { | ||
std::vector<unsigned char>* newblob; | ||
if(compressed) | ||
{ | ||
newblob = new std::vector<unsigned char>(isize); | ||
uLongf destLen = newblob->size(); | ||
// std::cout<<"Store isize = "<<isize<<"; newblob->size() = "<<newblob->size()<<"; destLen = "<<destLen<<std::endl; | ||
int zerr = uncompress(&*(newblob->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<unsigned char>(blob); | ||
} | ||
return newblob; | ||
} | ||
|
||
void FileBlob::getUncompressedBlob( std::vector<unsigned char>& 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include "CondFormats/Common/interface/FileBlob.h" | ||
#include "FWCore/Framework/interface/eventsetupdata_registration_macro.h" | ||
|
||
EVENTSETUP_DATA_REG(FileBlob); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version='1.0' encoding="UTF-8"?> | ||
<!DOCTYPE PoolDatabase SYSTEM "InMemory"> | ||
<PoolDatabase > | ||
<PoolContainer name="FileBlob" > | ||
<Class table="FILEBLOB" id_columns="ID" name="FileBlob" mapping_version="FileBlob_automatic_default" > | ||
<Blob column="DBLOB" name="blob" /> | ||
<Primitive column="DCOMPRESSED" name="compressed" /> | ||
<Primitive column="DISIZE" name="isize" /> | ||
</Class > | ||
</PoolContainer > | ||
</PoolDatabase > |