//===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Adds or updates an index (symbol table) for an LLVM archive file.
//
//===----------------------------------------------------------------------===//

#include "llvm/Module.h"
#include "llvm/Bytecode/Archive.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/System/Signals.h"
#include <iostream>
#include <iomanip>

#include <unistd.h>

using namespace llvm;

void printMembers(Archive* TheArchive) {
  std::cout << "\nArchive Symbol Table:\n";
  for (Archive::MembersList::const_iterator I=TheArchive->begin(), E=TheArchive->end();
       I != E; ++I ) {
    const char* data = (const char*) I->getData();
    std::cout << "length: " << I->getSize() << " data ptr: " << I->getData() << " first byte: " << (int)*data << "\n";
  }
}

int main(int argc, char **argv) {

  const char* filename = "GNU.a";

  // Print a stack trace if we signal out.
  sys::PrintStackTraceOnErrorSignal();

    sys::Path ArchivePath( filename );

    std::string err_msg;
    std::auto_ptr<Archive>
      AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg));
    Archive* TheArchive = AutoArchive.get();
    if (!TheArchive)
      throw err_msg;

    printMembers(TheArchive);
    truncate( filename, 0 );
    printMembers(TheArchive);

  return 0;
}
