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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
// The following program dumps contents of an object catalog stored
// in a "geners" binary metafile to the standard output

#include "Alignment/Geners/interface/IOException.hh"
#include <fstream>
#include <iostream>
#include <map>

#include <memory>
#include "Alignment/Geners/interface/CStringStream.hh"
#include "Alignment/Geners/interface/CatalogIO.hh"
#include "Alignment/Geners/interface/ContiguousCatalog.hh"

#include "CmdLine.hh"

using namespace gs;
using namespace std;

static void print_usage(const char *progname) {
  cout << "\nUsage: " << progname << " [-a] [-c] [-f] [-i] [-s] filename\n\n"
       << "This program prints the contents of \"geners\" catalog files to the "
          "standard\n"
       << "output. These files can be usually recognized by their \".gsbmf\" "
          "extension\n"
       << "(Generic Serialization Binary MetaFile). Normally, the program "
          "prints class\n"
       << "names, item names in the archive, and archive categories for all "
          "items in\n"
       << "the catalog. This default behavior can be modified with option "
          "switches.\n"
       << "The meaning of the switches is as follows:\n\n"
       << " -a   Print catalog annotations, if any.\n\n"
       << " -c   Print default archive compression mode.\n\n"
       << " -f   Full dump. Print complete info for each catalog entry.\n\n"
       << " -i   Include the catalog item ids into the printout.\n\n"
       << " -s   Print only the summary statistics for item types. If option "
          "\"-f\" is given\n"
       << "      together with \"-s\", the summary will be printed after the "
          "full dump.\n"
       << endl;
}

int main(int argc, char const *argv[]) {
  CmdLine cmdline(argc, argv);

  if (argc == 1) {
    print_usage(cmdline.progname());
    return 0;
  }

  std::string inputfile;
  bool printAnnotations = false;
  bool printCompressionMode = false;
  bool fullDump = false;
  bool printIds = false;
  bool summaryMode = false;

  try {
    printAnnotations = cmdline.has("-a");
    printCompressionMode = cmdline.has("-c");
    fullDump = cmdline.has("-f");
    printIds = cmdline.has("-i");
    summaryMode = cmdline.has("-s");

    cmdline.optend();

    if (cmdline.argc() != 1)
      throw CmdLineError("wrong number of command line arguments");
    cmdline >> inputfile;
  } catch (CmdLineError &e) {
    cerr << "Error in " << cmdline.progname() << ": " << e.str() << endl;
    print_usage(cmdline.progname());
    return 1;
  }

  ifstream in(inputfile.c_str(), ios_base::binary);
  if (!in.is_open()) {
    cerr << "Error: failed to open file \"" << inputfile << "\"" << endl;
    return 1;
  }

  unsigned compressionCode = 0, mergeLevel = 0;
  std::vector<std::string> annotations;
  std::unique_ptr<ContiguousCatalog> cat;
  try {
    cat = std::unique_ptr<ContiguousCatalog>(
        readBinaryCatalog<ContiguousCatalog>(in, &compressionCode, &mergeLevel, &annotations, true));
  } catch (std::exception &e) {
    cerr << "Failed to read catalog from file \"" << inputfile << "\". " << e.what() << endl;
    return 1;
  }

  if (printCompressionMode) {
    CStringStream::CompressionMode mode = static_cast<CStringStream::CompressionMode>(compressionCode);
    cout << "Default compression mode: " << CStringStream::compressionModeName(mode, false) << endl;
  }
  if (printAnnotations) {
    const unsigned nAnnotations = annotations.size();
    for (unsigned i = 0; i < nAnnotations; ++i) {
      if (i)
        cout << '\n';
      cout << "Annotation " << i << ": " << annotations[i] << endl;
    }
    if (!nAnnotations)
      cout << "This catalog does not have any annotations" << endl;
  }

  std::map<std::string, unsigned> typecount;

  const unsigned long long first = cat->smallestId();
  const unsigned long long last = cat->largestId();
  for (unsigned long long id = first; id <= last; ++id) {
    if (!cat->itemExists(id))
      continue;

    std::shared_ptr<const CatalogEntry> e = cat->retrieveEntry(id);
    if (fullDump) {
      if (id != first)
        cout << '\n';
      e->humanReadable(cout);
    } else if (!summaryMode) {
      if (printIds)
        cout << e->id() << "  ";
      cout << e->type().name() << "  " << '"' << e->name() << '"' << "  " << '"' << e->category() << '"' << endl;
    }
    if (summaryMode)
      typecount[e->type().name()]++;
  }

  if (summaryMode) {
    if (fullDump)
      cout << '\n';
    for (std::map<std::string, unsigned>::const_iterator it = typecount.begin(); it != typecount.end(); ++it)
      cout << it->second << ' ' << it->first << endl;
  }

  return 0;
}