CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/src/CondFormats/Common/src/FileBlob.cc

Go to the documentation of this file.
00001 #include "CondFormats/Common/interface/FileBlob.h"
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 
00004 #include <iostream>
00005 #include <fstream>
00006 #include <string>
00007 #include <zlib.h>
00008 
00009 FileBlob::FileBlob(const std::string & fname, bool zip):isize(0){
00010   compressed = zip;
00011   /*  
00012   std::cout << "isize = " << isize 
00013             << "  zip = " << (zip? "true" : "false")
00014             << std::endl;
00015   */
00016   if (isize==0) isize= computeFileSize(fname);
00017   // std::cout << "isize = " << isize << std::endl;
00018   blob.reserve(isize);
00019   read(fname);
00020 }
00021 FileBlob::FileBlob(std::istream& is, bool zip):isize(0) {
00022   compressed = zip;
00023   if (isize==0) isize= computeStreamSize(is);
00024   blob.reserve(isize);
00025   read(is);
00026 }
00027 
00028 void FileBlob::read(std::istream & is) {
00029   if(compressed){
00030     std::vector<unsigned char> in;
00031     in.reserve(isize);
00032     char c;
00033     while (is.get(c))
00034       in.push_back((unsigned char)c);
00035     /*
00036     for(int i=0;i<in.size();i++){
00037       std::cout<<in[i];
00038     }
00039     std::cout<<std::endl;
00040     */
00041     blob.resize(isize);
00042     uLongf destLen = compressBound(in.size());
00043     int zerr =  compress2(&*blob.begin(), &destLen,
00044                           &*in.begin(), in.size(),
00045                           9);
00046     if (zerr!=0) edm::LogError("FileBlob")<< "Compression error " << zerr;
00047     blob.resize(destLen);  
00048   }else{
00049     //std::cout << "reading uncompressed" << std::endl;
00050     char c;
00051     while (is.get(c))
00052       blob.push_back( (unsigned char)c);
00053     blob.resize(blob.size());
00054     isize=blob.size();
00055   }
00056 }
00057 
00058 void FileBlob::write(std::ostream & os) const {
00059   if(compressed){
00060     std::vector<unsigned char> out(isize);
00061     uLongf destLen = out.size();
00062     int zerr =  uncompress(&*out.begin(),  &destLen,
00063                            &*blob.begin(), blob.size());
00064     if (zerr!=0 || out.size()!=destLen) 
00065       edm::LogError("FileBlob")<< "uncompressing error " << zerr
00066                                    << " original size was " << isize
00067                                    << " new size is " << destLen;
00068     os.write((const char *)(&*out.begin()),out.size());
00069   }else{
00070     os.write((char *)&*blob.begin(),blob.size());
00071   }
00072 }
00073 
00074 std::vector<unsigned char>* FileBlob::getUncompressedBlob() const { 
00075   std::vector<unsigned char>*  newblob;
00076   if(compressed)
00077   {
00078     newblob = new std::vector<unsigned char>(isize);
00079     uLongf destLen = newblob->size();
00080     //    std::cout<<"Store isize = "<<isize<<"; newblob->size() = "<<newblob->size()<<"; destLen = "<<destLen<<std::endl;
00081     int zerr =  uncompress(&*(newblob->begin()),  &destLen,
00082                            &*blob.begin(), blob.size());
00083     if (zerr!=0 || newblob->size()!=destLen) 
00084       edm::LogError("FileBlob")<< "uncompressing error " << zerr
00085                                    << " original size was " << isize
00086                                    << " new size is " << destLen;
00087   }else{
00088     newblob = new std::vector<unsigned char>(blob);
00089   }
00090   return newblob;
00091  }
00092 
00093 void FileBlob::getUncompressedBlob( std::vector<unsigned char>& myblobcopy ) const {
00094   if(compressed)
00095   {
00096     myblobcopy.reserve(isize);
00097     uLongf destLen = isize;
00098     int zerr =  uncompress(&*myblobcopy.begin(),  &destLen,
00099                            &*blob.begin(), blob.size());
00100     if (zerr!=0 || myblobcopy.size()!=destLen) 
00101       edm::LogError("FileBlob")<< "uncompressing error " << zerr
00102                                    << " original size was " << isize
00103                                    << " new size is " << destLen;
00104   }else{
00105     myblobcopy = blob;
00106   }
00107   
00108 }
00109 
00110 void FileBlob::read(const std::string & fname) {
00111      std::ifstream ifile(fname.c_str());
00112      if (!ifile) { edm::LogError("FileBlob")<< "file " << fname << " does not exist...";}
00113      else read(ifile);
00114      ifile.close();
00115 }
00116  
00117 void FileBlob::write(const std::string & fname) const {
00118   std::ofstream ofile(fname.c_str());
00119   write(ofile);
00120   ofile.close();
00121 }
00122 
00123 unsigned int FileBlob::computeFileSize(const std::string & fname) {
00124   unsigned int is=0;
00125   std::ifstream ifile(fname.c_str());
00126   if (!ifile) { edm::LogError("FileBlob")<< "file " << fname << " does not exist...";}
00127   else is = computeStreamSize(ifile);
00128   ifile.close();
00129   return is;
00130 }
00131 
00132 unsigned int FileBlob::computeStreamSize(std::istream & is) {
00133   unsigned int rs=0;
00134   char c;
00135   while (is.get(c)) rs++;
00136   is.clear();
00137   is.seekg(0);
00138   return rs;
00139 }