/* shakespearesearch.cpp
 An example application to parse in Jon Bosak's Shakespeare play
  documents and then search through them for a dialog string.
  http://projects.zillabit.com/xml.html

Copyright (c) 2002, Earl Levine 
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted 
provided that the following conditions are met:

-Redistributions of source code must retain the above copyright notice, this list of conditions 
and the following disclaimer.

-The name of Earl Levine may not be used to endorse or promote products derived from this 
software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR 
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE. 
*/

#include "shakespearedoc.h"
#include <stdio.h>
#include <string.h>

bool getSearchString(char *searchString) {
  printf("\nEnter a search string, or RETURN to quit searching:\n");
  if(NULL == gets(searchString)) return false;
  return(strlen(searchString) > 0);
}

/* caller lists the xml filenames on the command line */
int main(int argc, void *argv[]) {
  char searchString[2000];
  bool showWarnings;
  FILE *warnlog;
  int i;
  int numfiles;
  ShakespeareDoc **docs;

  // Set up warning logging how we want it
  showWarnings = false;
  warnlog = showWarnings ? stderr : NULL;

  // Arg #0 is the name of the executable itself.
  numfiles = argc - 1;

  // Initialize docs array
  docs = new ShakespeareDoc* [numfiles];
  for(i=0; i< numfiles; i++) {
    docs[i] = NULL;
  }

  // Attempt to open and parse all the files
  for(i=0; i< numfiles; i++) {
    char *filename;
    filename = (char*)argv[1 + i];
    fprintf(stderr, "Parsing file : %s\n", filename);

    // Attempt to open the file
    FILE *docFile = fopen(filename, "r");
    if(!docFile) {
      fprintf(stderr, "Failed to open file %s\n", filename);
    } else {
      // Attempt to parse the file as a Shakespeare play
      docs[i] = new ShakespeareDoc(warnlog); // construct a new doc object
      expatParser parser(docs[i]); // construct a parser that makes callbacks to this doc object
      char *parseErrorString = NULL;
      int parseErrorLineNumber;
      const int readBufSize = 1024;
      char readBuffer[readBufSize];
      bool parseResult;
      bool endOfFile;
      int numRead;
      do {
	numRead = fread(readBuffer, 1, readBufSize, docFile); // read more from the file
	endOfFile = (readBufSize != numRead);
	parseResult = // parse whatever we just read from the file
	  parser.parse(readBuffer, numRead, endOfFile,
		       &parseErrorString, &parseErrorLineNumber);
      } while (!endOfFile && parseResult);
      if(!parseResult) {
	fprintf(stderr, "Failed to parse file %s\n\tline: %d\n\terror: %s\n", 
		filename, parseErrorLineNumber, parseErrorString);
	delete docs[i];
	docs[i] = NULL;
      }
      delete[] parseErrorString;

      fclose(docFile);
    }
  }

  while(getSearchString(searchString)) {
    // Here's the fun part: use the document objects.
    // All the work to code the doc object and the parsing now pays off.
  
    // Perform the search for the string.
    int numOccurances = 0;
    for(i=0; i<numfiles; i++) {
      if(docs[i]) {
	const ShakespeareDocElementPlay *play = docs[i]->getPlay();
	if(play) {
	  int actNum;
	  for(actNum=0; actNum<play->getNumActs(); actNum++) {
	    const ShakespeareDocElementAct *act = play->getAct(actNum);
	    if(act) {
	      int sceneNum;
	      for(sceneNum=0; sceneNum<act->getNumScenes(); sceneNum++) {
		const ShakespeareDocElementScene *scene = act->getScene(sceneNum);
		if(scene) {
		  int speechNum;
		  for(speechNum=0; speechNum<scene->getNumSpeeches(); speechNum++) {
		    const ShakespeareDocElementSpeech *speech = scene->getSpeech(speechNum);
		    if(speech) {
		      int lineNum;
		      for(lineNum=0; lineNum<speech->getNumLines(); lineNum++) {
			const ShakespeareDocElementLine *line = speech->getLine(lineNum);
			if(line && line->getCharacterData()) {
			  if(strstr(line->getCharacterData(), searchString)) {
			    numOccurances++;
			    printf("FOUND OCCURANCE number %d:\n", numOccurances);
			    printf(" %s %d: %s\n", play->getElementName(), 1+i, 
				   (play->getTitle() && play->getTitle()->getCharacterData()) ?
				   play->getTitle()->getCharacterData() : "(not found)");
			    printf(" %s %d: %s\n", act->getElementName(), 1+actNum, 
				   (act->getTitle() && act->getTitle()->getCharacterData()) ?
				   act->getTitle()->getCharacterData() : "(not found)");
			    printf(" %s %d: %s\n", scene->getElementName(), 1+sceneNum, 
				   (scene->getTitle() && scene->getTitle()->getCharacterData()) ?
				   scene->getTitle()->getCharacterData() : "(not found)");
			    printf(" %s %d: %s\n", speech->getElementName(), 1+speechNum, 
				   (speech->getSpeaker() && speech->getSpeaker()->getCharacterData()) ?
				   speech->getSpeaker()->getCharacterData() : "(not found)");
			    printf(" %s %d: %s\n", line->getElementName(), 1+lineNum, 
				   line->getCharacterData() ?
				   line->getCharacterData() : "(not found)");
			    printf("\n");
			  }
			}
		      }
		    }
		  }
		}
	      }
	    }
	  }
	}
      }
    }
    
  }

  // Deinitialize docs array
  for(i=0; i< numfiles; i++) {
    delete docs[i];
  }
  delete docs;

  return 0;
}
