CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FileBlob.cc
Go to the documentation of this file.
3 
4 #include <iostream>
5 #include <fstream>
6 #include <string>
7 #include <zlib.h>
8 
9 FileBlob::FileBlob(const std::string & fname, bool zip):isize(0){
10  compressed = zip;
11  /*
12  std::cout << "isize = " << isize
13  << " zip = " << (zip? "true" : "false")
14  << std::endl;
15  */
16  if (isize==0) isize= computeFileSize(fname);
17  // std::cout << "isize = " << isize << std::endl;
18  blob.reserve(isize);
19  read(fname);
20 }
21 FileBlob::FileBlob(std::istream& is, bool zip):isize(0) {
22  compressed = zip;
23  if (isize==0) isize= computeStreamSize(is);
24  blob.reserve(isize);
25  read(is);
26 }
27 
28 void FileBlob::read(std::istream & is) {
29  if(compressed){
30  std::vector<unsigned char> in;
31  in.reserve(isize);
32  char c;
33  while (is.get(c))
34  in.push_back((unsigned char)c);
35  /*
36  for(int i=0;i<in.size();i++){
37  std::cout<<in[i];
38  }
39  std::cout<<std::endl;
40  */
41  blob.resize(isize);
42  uLongf destLen = compressBound(in.size());
43  int zerr = compress2(&*blob.begin(), &destLen,
44  &*in.begin(), in.size(),
45  9);
46  if (zerr!=0) edm::LogError("FileBlob")<< "Compression error " << zerr;
47  blob.resize(destLen);
48  }else{
49  //std::cout << "reading uncompressed" << std::endl;
50  char c;
51  while (is.get(c))
52  blob.push_back( (unsigned char)c);
53  blob.resize(blob.size());
54  isize=blob.size();
55  }
56 }
57 
58 void FileBlob::write(std::ostream & os) const {
59  if(compressed){
60  std::vector<unsigned char> out(isize);
61  uLongf destLen = out.size();
62  int zerr = uncompress(&*out.begin(), &destLen,
63  &*blob.begin(), blob.size());
64  if (zerr!=0 || out.size()!=destLen)
65  edm::LogError("FileBlob")<< "uncompressing error " << zerr
66  << " original size was " << isize
67  << " new size is " << destLen;
68  os.write((const char *)(&*out.begin()),out.size());
69  }else{
70  os.write((char *)&*blob.begin(),blob.size());
71  }
72 }
73 
74 std::vector<unsigned char>* FileBlob::getUncompressedBlob() const {
75  std::vector<unsigned char>* newblob;
76  if(compressed)
77  {
78  newblob = new std::vector<unsigned char>(isize);
79  uLongf destLen = newblob->size();
80  // std::cout<<"Store isize = "<<isize<<"; newblob->size() = "<<newblob->size()<<"; destLen = "<<destLen<<std::endl;
81  int zerr = uncompress(&*(newblob->begin()), &destLen,
82  &*blob.begin(), blob.size());
83  if (zerr!=0 || newblob->size()!=destLen)
84  edm::LogError("FileBlob")<< "uncompressing error " << zerr
85  << " original size was " << isize
86  << " new size is " << destLen;
87  }else{
88  newblob = new std::vector<unsigned char>(blob);
89  }
90  return newblob;
91  }
92 
93 void FileBlob::getUncompressedBlob( std::vector<unsigned char>& myblobcopy ) const {
94  if(compressed)
95  {
96  myblobcopy.reserve(isize);
97  uLongf destLen = isize;
98  int zerr = uncompress(&*myblobcopy.begin(), &destLen,
99  &*blob.begin(), blob.size());
100  if (zerr!=0 || myblobcopy.size()!=destLen)
101  edm::LogError("FileBlob")<< "uncompressing error " << zerr
102  << " original size was " << isize
103  << " new size is " << destLen;
104  }else{
105  myblobcopy = blob;
106  }
107 
108 }
109 
111  std::ifstream ifile(fname.c_str());
112  if (!ifile) { edm::LogError("FileBlob")<< "file " << fname << " does not exist...";}
113  else read(ifile);
114  ifile.close();
115 }
116 
117 void FileBlob::write(const std::string & fname) const {
118  std::ofstream ofile(fname.c_str());
119  write(ofile);
120  ofile.close();
121 }
122 
124  unsigned int is=0;
125  std::ifstream ifile(fname.c_str());
126  if (!ifile) { edm::LogError("FileBlob")<< "file " << fname << " does not exist...";}
127  else is = computeStreamSize(ifile);
128  ifile.close();
129  return is;
130 }
131 
132 unsigned int FileBlob::computeStreamSize(std::istream & is) {
133  unsigned int rs=0;
134  char c;
135  while (is.get(c)) rs++;
136  is.clear();
137  is.seekg(0);
138  return rs;
139 }
void zerr(int)
std::vector< unsigned char > blob
Definition: FileBlob.h:40
static unsigned int computeFileSize(const std::string &)
Definition: FileBlob.cc:123
bool compressed
Definition: FileBlob.h:41
static unsigned int computeStreamSize(std::istream &)
Definition: FileBlob.cc:132
FileBlob()
Definition: FileBlob.h:11
tuple out
Definition: dbtoconf.py:99
void write(const std::string &) const
write to real file
Definition: FileBlob.cc:117
std::vector< unsigned char > * getUncompressedBlob() const
i didn&#39;t want to do two copies ... hope this works.
Definition: FileBlob.cc:74
string fname
main script
unsigned int isize
Definition: FileBlob.h:42
void read(const std::string &)
read from real file
Definition: FileBlob.cc:110