CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
stringArchiveIO.cc
Go to the documentation of this file.
1 #include "Alignment/Geners/interface/IOException.hh"
2 #include <cassert>
3 #include <cstring>
4 #include <fstream>
5 #include <sstream>
6 
7 #include "Alignment/Geners/interface/CompressedIO.hh"
8 #include "Alignment/Geners/interface/IOException.hh"
9 #include "Alignment/Geners/interface/Reference.hh"
10 #include "Alignment/Geners/interface/stringArchiveIO.hh"
11 
12 static bool suffix_matches(const char *filename, const char *suffix) {
13  static const char default_suffix[] = ".gssaz";
14  assert(filename);
15  if (suffix == nullptr)
16  suffix = default_suffix;
17  const std::size_t lenSuffix = strlen(suffix);
18  const std::size_t len = strlen(filename);
19  bool suffixMatches = len >= lenSuffix;
20  for (std::size_t i = 0; i < lenSuffix && suffixMatches; ++i)
21  suffixMatches = suffix[i] == filename[len - lenSuffix + i];
22  return suffixMatches;
23 }
24 
25 namespace gs {
26  bool writeStringArchive(const StringArchive &ar, const char *filename) {
27  assert(filename);
28  bool status = false;
29  {
30  std::ofstream of(filename, std::ios_base::binary);
31  if (of.is_open()) {
32  const_cast<StringArchive &>(ar).flush();
33  status = write_item(of, ar);
34  }
35  }
36  return status;
37  }
38 
39  StringArchive *readStringArchive(const char *filename) {
40  assert(filename);
41  std::ifstream is(filename, std::ios_base::binary);
42  if (!is.is_open())
43  throw IOOpeningFailure("gs::readStringArchive", filename);
44  CPP11_auto_ptr<StringArchive> ar = read_item<StringArchive>(is);
45  return ar.release();
46  }
47 
48  bool writeCompressedStringArchive(const StringArchive &ar,
49  const char *filename,
50  const unsigned inCompressionMode,
51  const int compressionLevel,
52  const unsigned minSizeToCompress,
53  const unsigned bufSize) {
54  assert(filename);
55  if (inCompressionMode > CStringStream::BZIP2)
56  throw gs::IOInvalidArgument(
57  "In gs::writeCompressedStringArchive: "
58  "compression mode argument out of range");
59  const CStringStream::CompressionMode m = static_cast<CStringStream::CompressionMode>(inCompressionMode);
60  bool status = false;
61  {
62  std::ofstream of(filename, std::ios_base::binary);
63  if (of.is_open()) {
64  const_cast<StringArchive &>(ar).flush();
65  status = write_compressed_item(of, ar, m, compressionLevel, minSizeToCompress, bufSize);
66  }
67  }
68  return status;
69  }
70 
71  StringArchive *readCompressedStringArchive(const char *filename) {
72  assert(filename);
73  std::ifstream is(filename, std::ios_base::binary);
74  if (!is.is_open())
75  throw IOOpeningFailure("gs::readCompressedStringArchive", filename);
76  CPP11_auto_ptr<StringArchive> ar = read_compressed_item<StringArchive>(is);
77  return ar.release();
78  }
79 
80  StringArchive *loadStringArchiveFromArchive(AbsArchive &arch, const unsigned long long id) {
81  Reference<StringArchive> ref(arch, id);
82  if (!ref.unique()) {
83  std::ostringstream os;
84  os << "In gs::loadStringArchiveFromArchive: "
85  << "StringArchive item with id " << id << " not found";
86  throw gs::IOInvalidArgument(os.str());
87  }
88  CPP11_auto_ptr<StringArchive> p = ref.get(0);
89  return p.release();
90  }
91 
92  StringArchive *readCompressedStringArchiveExt(const char *filename, const char *suffix) {
93  if (suffix_matches(filename, suffix))
94  return readCompressedStringArchive(filename);
95  else
96  return readStringArchive(filename);
97  }
98 
99  bool writeCompressedStringArchiveExt(const StringArchive &ar, const char *filename, const char *suffix) {
100  if (suffix_matches(filename, suffix))
101  return writeCompressedStringArchive(ar, filename);
102  else
103  return writeStringArchive(ar, filename);
104  }
105 } // namespace gs
bool writeCompressedStringArchiveExt(const StringArchive &ar, const char *filename, const char *suffix)
list status
Definition: mps_update.py:107
assert(be >=bs)
StringArchive * loadStringArchiveFromArchive(AbsArchive &arch, const unsigned long long id)
static bool suffix_matches(const char *filename, const char *suffix)
StringArchive * readCompressedStringArchive(const char *filename)
bool writeCompressedStringArchive(const StringArchive &ar, const char *filename, const unsigned inCompressionMode, const int compressionLevel, const unsigned minSizeToCompress, const unsigned bufSize)
tuple of
Definition: haddnano.py:40
StringArchive * readCompressedStringArchiveExt(const char *filename, const char *suffix)
StringArchive * readStringArchive(const char *filename)
tuple filename
Definition: lut2db_cfg.py:20
bool writeStringArchive(const StringArchive &ar, const char *filename)