CMS 3D CMS Logo

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