/*****************************************************************************/
/*                                                                           */
/*  Filename:     sample_reader.cpp                                          */
/*  Author(s):    Jim Skon, Bob Kasper                                       */
/*  Last Update:  2/10/2011
/*                                                                           */
/*  Sample Reader - a sample program to read and display                     */
/*                  the complete contents of a bible book.                   */
/*                                                                           */
/*****************************************************************************/

#include <iostream>
#include <fstream>
#include <string> 
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

#include "sample_reader.h"

#define delim  ".,;:'"" ?"

string GetNextToken(string& str,
	      const string& delimiters = " ")
{
  // Skip delimiters at beginning.
  string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  // Find first "non-delimiter".
  string::size_type pos     = str.find_first_of(delimiters, lastPos);

  // Found a token, remove it from string, and return it
  string next = str.substr(lastPos, pos - lastPos);
  string rest = str.substr(pos - lastPos + 1,string::npos);
  str = rest;
  return(next);
}

/**************************************************************************/
/*  parse_verse - parse the verse reference                               */
/*                                                                        */
/*  Inputs:  buffer - Complete verse buffer string                        */
/*                                                                        */
/*  Outputs: book - book number                                           */
/*           chap - chapter number                                        */
/*           verse - verse number                                         */
/**************************************************************************/

int parse_verse(string buffer, int& book, int& chap, int& verse, string& verseText) 
{
  // parse the reference
  // Get Book, chapter Verse
  book = atoi(GetNextToken(buffer,delim).c_str());
  chap = atoi(GetNextToken(buffer,delim).c_str());
  verse = atoi(GetNextToken(buffer,delim).c_str());

  // Sanity check the values
  if (book < 1 || chap < 1 || verse < 1)
    return(0);
  
  // What's left if the verse itself
  verseText = buffer;

  return(1);
}


/*****************************************************************************/
/*                                                                           */
/*      display_verse - Display play a verse nicely                          */
/*                      limit to 78 characters a line, and make sure not to  */
/*                      break in the middle of a word                        */
/*                                                                           */
/*      Inputs: verse - pointer to a string containing verse text            */
/*                                                                           */
/*****************************************************************************/
void display_verse(string verse) {

  int start = 0, end, i, len = verse.length();
  
  /* The following code makes sure the output does not break to a */
  /* newline in the middle of a word */
  /* do while there is more left in line */
  while (start < len) {
    /* If what is left will fit on a single line, just print it */
	if (start != 0) {
	}
    if (len-start <= 78) {
      cout << verse.substr(start,len) << endl;  // print what is left
	  start += len + 1;
    } else { 
      /* find the end of the last full word that will fit on this line */
	  end = verse.substr(start,78).find_last_of(" ,;-");
	  cout << verse.substr(start,end) << endl;
      start = start + end + 1;            /* advance past puncuation */
    }
  }
}

/*****************************************************************************/
/*                                                                           */
/*      display_book - given a book # and the input file object, display     */
/*      the given book.                                                      */
/*                                                                           */
/*      Input:  book - book number to display                                */
/*              infile - input file stream                                   */
/*****************************************************************************/
void display_book(int requestedBook, ifstream& infile) {

  string buffer, verseText;
  int book, chap, verse;

  /* read in every verse */
  do {
    /* get the next verse */
    getline(infile, buffer);

    /* See if it is the right book */
    if (parse_verse(buffer, book, chap, verse, verseText)) {

      if (requestedBook==book) {

	/* display the verse (if we got one) */
	display_verse(buffer);
	}
    }
  } while (requestedBook >= book && !infile.fail());
      
}
  
/*****************************************************************************/
/*                                                                           */
/*      Main program - command line arguments:                               */
/*                     book number                                           */
/*                                                                           */
/*****************************************************************************/
main (int argc, char ** argv) {

  ifstream  infile;     /* input file descriptor */
  string biblefile = "/home/class/csc3004/Bibles/kjv-complete";
  int book;

  /* Check for book name argument */
  if (argc != 2)
	{
	  cerr << "Usage: " << argv[0] <<  " Enter Bible Book Number (1-66)" << endl;
	  exit(2);
	}
  
  book = atoi(argv[1]);
  if (book < 1 || book > 66)
	{
	  cerr << "Error - must be in range 1 to 66" << endl;
	  exit(2);
	}
  /* open Bible file */
  infile.open(biblefile.c_str(), ios::in);
  if (!infile) {
	cerr << "Error - can't open input file: " << biblefile << endl;
	exit (2);
  }
  infile.unsetf(ios::skipws);   // include white space in reads
  /* process file */
  display_book(book,infile);
  return 0;
}

Topic revision: r1 - 2013-02-05 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback