Skip to content

Commit

Permalink
add the general fileblob object
Browse files Browse the repository at this point in the history
  • Loading branch information
fambrogl committed Oct 30, 2009
1 parent c898ddc commit 02d2da5
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
45 changes: 45 additions & 0 deletions CondFormats/Common/interface/FileBlob.h
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
139 changes: 139 additions & 0 deletions CondFormats/Common/src/FileBlob.cc
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;
}
4 changes: 4 additions & 0 deletions CondFormats/Common/src/T_EventSetup_FileBlob.cc
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);
1 change: 1 addition & 0 deletions CondFormats/Common/src/classes.h
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 4 additions & 0 deletions CondFormats/Common/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<class name="cond::IOVDescription"/>
<class name="cond::IOVUserMetaData"/>

<class name="FileBlob" class_version="0">
<field name="blob" mapping="blob"/>
</class>


<class name="cond::SmallWORMDict"/>
<class name="cond::IOVKeysDescription"/>
Expand Down
11 changes: 11 additions & 0 deletions CondFormats/Common/xml/FileBlob_automatic_default_0.xml
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 >

0 comments on commit 02d2da5

Please sign in to comment.