/* stashStackTest.cpp */

#include "Stack2.h"
#include "Stack2.cpp"
#include "Stash2.h"
#include "Stash2.cpp"
#include "../require.h"
#include <iostream>
#include <fstream>

using namespace std;


int main(int argc, char *argv[]) {
  const int bufsize = 80;         // Max line length of file
  const int numLines = 5;         // Number of lines stored in a Stash

  ifstream in("stashStackTest.cpp");
  assure(in, "stashStackTest.cpp");

  Stack stashStack;

  // Read file and store groups of numLines lines in a stash:
  // Stashes are stacked.
  string line;
  int linecnt = 0;

  Stash *pStash;
  while (getline(in, line)) {
    if (linecnt == 0) {
      pStash = new Stash(sizeof(char) * bufsize);
      stashStack.push(pStash);
    }

    linecnt = (linecnt + 1) % numLines;
    pStash->add(line.c_str());
  }


  // Pop the stashes from the stack and print lines
  // in stash in reverse order:
  Stash *qStash;
  while ((qStash = (Stash *)stashStack.pop()) != 0) {
    char *cp;
    int k = qStash->count();
    while (k) {
      cp = (char *)qStash->fetch(--k);
      cout << cp << endl;
    }
  }
}
