Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#include "Alignment/Geners/interface/IOException.hh"
#include <cassert>
#include <cstring>
#include <fstream>
#include <sstream>

#include "Alignment/Geners/interface/CompressedIO.hh"
#include "Alignment/Geners/interface/IOException.hh"
#include "Alignment/Geners/interface/Reference.hh"
#include "Alignment/Geners/interface/stringArchiveIO.hh"

static bool suffix_matches(const char *filename, const char *suffix) {
  static const char default_suffix[] = ".gssaz";
  assert(filename);
  if (suffix == nullptr)
    suffix = default_suffix;
  const std::size_t lenSuffix = strlen(suffix);
  const std::size_t len = strlen(filename);
  bool suffixMatches = len >= lenSuffix;
  for (std::size_t i = 0; i < lenSuffix && suffixMatches; ++i)
    suffixMatches = suffix[i] == filename[len - lenSuffix + i];
  return suffixMatches;
}

namespace gs {
  bool writeStringArchive(const StringArchive &ar, const char *filename) {
    assert(filename);
    bool status = false;
    {
      std::ofstream of(filename, std::ios_base::binary);
      if (of.is_open()) {
        const_cast<StringArchive &>(ar).flush();
        status = write_item(of, ar);
      }
    }
    return status;
  }

  StringArchive *readStringArchive(const char *filename) {
    assert(filename);
    std::ifstream is(filename, std::ios_base::binary);
    if (!is.is_open())
      throw IOOpeningFailure("gs::readStringArchive", filename);
    std::unique_ptr<StringArchive> ar = read_item<StringArchive>(is);
    return ar.release();
  }

  bool writeCompressedStringArchive(const StringArchive &ar,
                                    const char *filename,
                                    const unsigned inCompressionMode,
                                    const int compressionLevel,
                                    const unsigned minSizeToCompress,
                                    const unsigned bufSize) {
    assert(filename);
    if (inCompressionMode > CStringStream::BZIP2)
      throw gs::IOInvalidArgument(
          "In gs::writeCompressedStringArchive: "
          "compression mode argument out of range");
    const CStringStream::CompressionMode m = static_cast<CStringStream::CompressionMode>(inCompressionMode);
    bool status = false;
    {
      std::ofstream of(filename, std::ios_base::binary);
      if (of.is_open()) {
        const_cast<StringArchive &>(ar).flush();
        status = write_compressed_item(of, ar, m, compressionLevel, minSizeToCompress, bufSize);
      }
    }
    return status;
  }

  StringArchive *readCompressedStringArchive(const char *filename) {
    assert(filename);
    std::ifstream is(filename, std::ios_base::binary);
    if (!is.is_open())
      throw IOOpeningFailure("gs::readCompressedStringArchive", filename);
    std::unique_ptr<StringArchive> ar = read_compressed_item<StringArchive>(is);
    return ar.release();
  }

  StringArchive *loadStringArchiveFromArchive(AbsArchive &arch, const unsigned long long id) {
    Reference<StringArchive> ref(arch, id);
    if (!ref.unique()) {
      std::ostringstream os;
      os << "In gs::loadStringArchiveFromArchive: "
         << "StringArchive item with id " << id << " not found";
      throw gs::IOInvalidArgument(os.str());
    }
    std::unique_ptr<StringArchive> p = ref.get(0);
    return p.release();
  }

  StringArchive *readCompressedStringArchiveExt(const char *filename, const char *suffix) {
    if (suffix_matches(filename, suffix))
      return readCompressedStringArchive(filename);
    else
      return readStringArchive(filename);
  }

  bool writeCompressedStringArchiveExt(const StringArchive &ar, const char *filename, const char *suffix) {
    if (suffix_matches(filename, suffix))
      return writeCompressedStringArchive(ar, filename);
    else
      return writeStringArchive(ar, filename);
  }
}  // namespace gs