Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:56

0001 #include "Alignment/Geners/interface/IOException.hh"
0002 #include <cassert>
0003 #include <cstring>
0004 #include <fstream>
0005 #include <sstream>
0006 
0007 #include "Alignment/Geners/interface/CompressedIO.hh"
0008 #include "Alignment/Geners/interface/IOException.hh"
0009 #include "Alignment/Geners/interface/Reference.hh"
0010 #include "Alignment/Geners/interface/stringArchiveIO.hh"
0011 
0012 static bool suffix_matches(const char *filename, const char *suffix) {
0013   static const char default_suffix[] = ".gssaz";
0014   assert(filename);
0015   if (suffix == nullptr)
0016     suffix = default_suffix;
0017   const std::size_t lenSuffix = strlen(suffix);
0018   const std::size_t len = strlen(filename);
0019   bool suffixMatches = len >= lenSuffix;
0020   for (std::size_t i = 0; i < lenSuffix && suffixMatches; ++i)
0021     suffixMatches = suffix[i] == filename[len - lenSuffix + i];
0022   return suffixMatches;
0023 }
0024 
0025 namespace gs {
0026   bool writeStringArchive(const StringArchive &ar, const char *filename) {
0027     assert(filename);
0028     bool status = false;
0029     {
0030       std::ofstream of(filename, std::ios_base::binary);
0031       if (of.is_open()) {
0032         const_cast<StringArchive &>(ar).flush();
0033         status = write_item(of, ar);
0034       }
0035     }
0036     return status;
0037   }
0038 
0039   StringArchive *readStringArchive(const char *filename) {
0040     assert(filename);
0041     std::ifstream is(filename, std::ios_base::binary);
0042     if (!is.is_open())
0043       throw IOOpeningFailure("gs::readStringArchive", filename);
0044     CPP11_auto_ptr<StringArchive> ar = read_item<StringArchive>(is);
0045     return ar.release();
0046   }
0047 
0048   bool writeCompressedStringArchive(const StringArchive &ar,
0049                                     const char *filename,
0050                                     const unsigned inCompressionMode,
0051                                     const int compressionLevel,
0052                                     const unsigned minSizeToCompress,
0053                                     const unsigned bufSize) {
0054     assert(filename);
0055     if (inCompressionMode > CStringStream::BZIP2)
0056       throw gs::IOInvalidArgument(
0057           "In gs::writeCompressedStringArchive: "
0058           "compression mode argument out of range");
0059     const CStringStream::CompressionMode m = static_cast<CStringStream::CompressionMode>(inCompressionMode);
0060     bool status = false;
0061     {
0062       std::ofstream of(filename, std::ios_base::binary);
0063       if (of.is_open()) {
0064         const_cast<StringArchive &>(ar).flush();
0065         status = write_compressed_item(of, ar, m, compressionLevel, minSizeToCompress, bufSize);
0066       }
0067     }
0068     return status;
0069   }
0070 
0071   StringArchive *readCompressedStringArchive(const char *filename) {
0072     assert(filename);
0073     std::ifstream is(filename, std::ios_base::binary);
0074     if (!is.is_open())
0075       throw IOOpeningFailure("gs::readCompressedStringArchive", filename);
0076     CPP11_auto_ptr<StringArchive> ar = read_compressed_item<StringArchive>(is);
0077     return ar.release();
0078   }
0079 
0080   StringArchive *loadStringArchiveFromArchive(AbsArchive &arch, const unsigned long long id) {
0081     Reference<StringArchive> ref(arch, id);
0082     if (!ref.unique()) {
0083       std::ostringstream os;
0084       os << "In gs::loadStringArchiveFromArchive: "
0085          << "StringArchive item with id " << id << " not found";
0086       throw gs::IOInvalidArgument(os.str());
0087     }
0088     CPP11_auto_ptr<StringArchive> p = ref.get(0);
0089     return p.release();
0090   }
0091 
0092   StringArchive *readCompressedStringArchiveExt(const char *filename, const char *suffix) {
0093     if (suffix_matches(filename, suffix))
0094       return readCompressedStringArchive(filename);
0095     else
0096       return readStringArchive(filename);
0097   }
0098 
0099   bool writeCompressedStringArchiveExt(const StringArchive &ar, const char *filename, const char *suffix) {
0100     if (suffix_matches(filename, suffix))
0101       return writeCompressedStringArchive(ar, filename);
0102     else
0103       return writeStringArchive(ar, filename);
0104   }
0105 }  // namespace gs