CMS 3D CMS Logo

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";
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) {
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) {
41  std::ifstream is(filename, std::ios_base::binary);
42  if (!is.is_open())
43  throw IOOpeningFailure("gs::readStringArchive", filename);
44  std::unique_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) {
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) {
73  std::ifstream is(filename, std::ios_base::binary);
74  if (!is.is_open())
75  throw IOOpeningFailure("gs::readCompressedStringArchive", filename);
76  std::unique_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  std::unique_ptr<StringArchive> p = ref.get(0);
89  return p.release();
90  }
91 
92  StringArchive *readCompressedStringArchiveExt(const char *filename, const char *suffix) {
95  else
97  }
98 
99  bool writeCompressedStringArchiveExt(const StringArchive &ar, const char *filename, const char *suffix) {
102  else
103  return writeStringArchive(ar, filename);
104  }
105 } // namespace gs
bool writeCompressedStringArchiveExt(const StringArchive &ar, const char *filename, const char *suffix)
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)
StringArchive * readCompressedStringArchiveExt(const char *filename, const char *suffix)
StringArchive * readStringArchive(const char *filename)
bool writeStringArchive(const StringArchive &ar, const char *filename)
Definition: AbsArchive.cc:46