CMS 3D CMS Logo

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)
18  // std::cout << "isize = " << isize << std::endl;
19  blob.reserve(isize);
20  read(fname);
21 }
22 FileBlob::FileBlob(std::istream& is, bool zip) : isize(0) {
23  compressed = zip;
24  if (isize == 0)
26  blob.reserve(isize);
27  read(is);
28 }
29 
30 void FileBlob::read(std::istream& is) {
31  if (compressed) {
32  std::vector<unsigned char> in;
33  in.reserve(isize);
34  char c;
35  while (is.get(c))
36  in.push_back((unsigned char)c);
37  /*
38  for(int i=0;i<in.size();i++){
39  std::cout<<in[i];
40  }
41  std::cout<<std::endl;
42  */
43  blob.resize(isize);
44  uLongf destLen = compressBound(in.size());
45  int zerr = compress2(&*blob.begin(), &destLen, &*in.begin(), in.size(), 9);
46  if (zerr != 0)
47  edm::LogError("FileBlob") << "Compression error " << zerr;
48  blob.resize(destLen);
49  } else {
50  //std::cout << "reading uncompressed" << std::endl;
51  char c;
52  while (is.get(c))
53  blob.push_back((unsigned char)c);
54  blob.resize(blob.size());
55  isize = blob.size();
56  }
57 }
58 
59 void FileBlob::write(std::ostream& os) const {
60  if (compressed) {
61  std::vector<unsigned char> out(isize);
62  uLongf destLen = out.size();
63  int zerr = uncompress(&*out.begin(), &destLen, &*blob.begin(), blob.size());
64  if (zerr != 0 || out.size() != destLen)
65  edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
66  << destLen;
67  os.write(reinterpret_cast<const char*>(&*out.begin()), out.size());
68  } else {
69  os.write(reinterpret_cast<const char*>(&*blob.begin()), blob.size());
70  }
71 }
72 
73 std::unique_ptr<std::vector<unsigned char> > FileBlob::getUncompressedBlob() const {
74  std::unique_ptr<std::vector<unsigned char> > newblob;
75  if (compressed) {
76  newblob.reset(new std::vector<unsigned char>(isize));
77  uLongf destLen = newblob->size();
78  // std::cout<<"Store isize = "<<isize<<"; newblob->size() = "<<newblob->size()<<"; destLen = "<<destLen<<std::endl;
79  int zerr = uncompress(&*(newblob->begin()), &destLen, &*blob.begin(), blob.size());
80  if (zerr != 0 || newblob->size() != destLen)
81  edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
82  << destLen;
83  } else {
84  newblob.reset(new std::vector<unsigned char>(blob));
85  }
86  return newblob;
87 }
88 
89 void FileBlob::getUncompressedBlob(std::vector<unsigned char>& myblobcopy) const {
90  if (compressed) {
91  myblobcopy.reserve(isize);
92  uLongf destLen = isize;
93  int zerr = uncompress(&*myblobcopy.begin(), &destLen, &*blob.begin(), blob.size());
94  if (zerr != 0 || myblobcopy.size() != destLen)
95  edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
96  << destLen;
97  } else {
98  myblobcopy = blob;
99  }
100 }
101 
103  std::ifstream ifile(fname.c_str());
104  if (!ifile) {
105  edm::LogError("FileBlob") << "file " << fname << " does not exist...";
106  } else
107  read(ifile);
108  ifile.close();
109 }
110 
111 void FileBlob::write(const std::string& fname) const {
112  std::ofstream ofile(fname.c_str());
113  write(ofile);
114  ofile.close();
115 }
116 
118  unsigned int is = 0;
119  std::ifstream ifile(fname.c_str());
120  if (!ifile) {
121  edm::LogError("FileBlob") << "file " << fname << " does not exist...";
122  } else
123  is = computeStreamSize(ifile);
124  ifile.close();
125  return is;
126 }
127 
128 unsigned int FileBlob::computeStreamSize(std::istream& is) {
129  unsigned int rs = 0;
130  char c;
131  while (is.get(c))
132  rs++;
133  is.clear();
134  is.seekg(0);
135  return rs;
136 }
FileBlob::isize
unsigned int isize
Definition: FileBlob.h:45
MessageLogger.h
indexGen.ofile
ofile
Definition: indexGen.py:102
FileBlob::computeFileSize
static unsigned int computeFileSize(const std::string &)
Definition: FileBlob.cc:117
FileBlob::getUncompressedBlob
std::unique_ptr< std::vector< unsigned char > > getUncompressedBlob() const
i didn't want to do two copies ... hope this works.
Definition: FileBlob.cc:73
FileBlob.h
compare_using_db.ifile
ifile
Definition: compare_using_db.py:251
FileBlob::FileBlob
FileBlob()
Definition: FileBlob.h:12
FileBlob::blob
std::vector< unsigned char > blob
Definition: FileBlob.h:43
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
FileBlob::read
void read(const std::string &)
read from real file
Definition: FileBlob.cc:102
edm::LogError
Definition: MessageLogger.h:183
recoMuon::in
Definition: RecoMuonEnumerators.h:6
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
ComparisonHelper::zip
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
Definition: L1TStage2CaloLayer1.h:38
alignmentValidation.fname
string fname
main script
Definition: alignmentValidation.py:959
FileBlob::computeStreamSize
static unsigned int computeStreamSize(std::istream &)
Definition: FileBlob.cc:128
FileBlob::write
void write(const std::string &) const
write to real file
Definition: FileBlob.cc:111
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
spu::zerr
void zerr(int)
Definition: SherpackUtilities.cc:129
FileBlob::compressed
bool compressed
Definition: FileBlob.h:44