// Programmer: Clayton Price date: 11/1/13
// File: fable_functs.cpp cs 53
// Purpose: This file contains the function definitions used in the
// fable changing program.
#include <fstream>
#include <cstdlib>
#include <iostream>
#include "fables.h"
using namespace std;
void get_random_word(char word[],const char filename[],const short size)
{
ifstream win;
win.open(filename);
short loc = rand()%size+1;
for(short i=1;i<=loc;i++)
win.getline(word,MAX_WORD_SIZE);
win.close();
return;
}
short file_length(const char filename[])
{
ifstream in(filename);
char line[MAX_LINE_SIZE];
short count = 0;
while (in.getline(line,MAX_LINE_SIZE))
count++;
return count;
}
string get_random_fable_file()
{
ifstream in;
short rand_position = rand()%NUM_FABLES;
return FABLE[rand_position];
}
void get_title_nouns(char word1[],char word2[],const string file)
{
ifstream fin;
char a_word[MAX_WORD_SIZE];
fin.open(file.c_str());
for (short i=1; i<=5; i++)
{
fin>>a_word;
if(2==i)
strcpy(word1,a_word);
else if (5==i)
strcpy(word2,a_word);
}
fin.close();
return;
}
void process_fable(const char noun1[],const char noun2[],
const char replacement1[],const char replacement2[],
const string fable_file,const short length_list2,
const short rantfile_length,const short moerals_length)
{
const char PRONOUN1[]="a", PRONOUN2[]="the";
static bool replace_after = false;
ifstream in(fable_file.c_str());
ifstream win;
char rand_word[MAX_WORD_SIZE],next[MAX_WORD_SIZE];
char rant[MAX_LINE_SIZE],moral[MAX_LINE_SIZE];
char the_punct;
bool has_punct=false, has_lead_quotes, has_tail_quotes, is_possessive;
short len=0; //used for the length of a word
short rand_num;
while(in>>next) //read WORDS until end of the file
{ // without using eof() !
has_lead_quotes=false; //reset flag for leading quotes
if ('"'==next[0]) //check for front quotes
{
has_lead_quotes=true; //flag leading quotes
len=strlen(next);
for(short i=0;i<len;i++) //shift down - strips leading quotes
next[i]=next[i+1];
}
has_tail_quotes=false; //reset flag for trailing quotes
if ('"'==next[strlen(next)-1]) //check for trailing quotes
{
has_tail_quotes=true;
next[strlen(next)-1]='\0'; //strip off trailing quotes
}
has_punct=false; //reset flag for punctuation
if (ispunct(next[strlen(next)-1])) //has punctuation?
{
the_punct = next[strlen(next)-1]; //capture punction
has_punct = true; //remember it was there
next[strlen(next)-1]='\0'; //removes punctuation
}
is_possessive = false;
len=strlen(next);
if ('\''==next[len-2] && 's'==next[len-1]) //ends in 's - possessive
{
is_possessive=true; //remember it is possessive
next[len-2]='\0'; //strip off 's
}
if(!strcmp(next,PRONOUN1) || !strcmp(next,PRONOUN2))
replace_after = true; //remember it follows pronoun
else if (!strcmp(noun1,next)) //replace first noun
{
strcpy(next,replacement1);
replace_after=false;
}
else if (!strcmp(noun2,next)) //replace second noun
{
strcpy(next,replacement2);
replace_after=false;
}
else if (replace_after)
{
win.open("list2.txt");
rand_num=rand()%length_list2;
for(short i=0;i<=rand_num;i++)
win.getline(rand_word,MAX_WORD_SIZE);
win.close();
strcpy(next,rand_word);
replace_after = false;
}
if(is_possessive) //replace possessiveness
{
len=strlen(next);
next[len]='\'';
next[len+1]='s';
next[len+2]='\0';
}
if(has_punct) //replace the punctuation
{
next[strlen(next)+1]='\0';
next[strlen(next)]=the_punct;
}
if(has_tail_quotes) //replace trailing quotes
{
len=strlen(next);
next[len]='"'; //put back quotes
next[len+1]='\0';
}
if(has_lead_quotes) //replace leading quotes
{
len=strlen(next);
for(short i=len;i>=0;i--) //shift
next[i+1]=next[i];
next[0]='"'; //put back quotes
}
cout<<next<<" ";
if(has_punct && (the_punct=='.' || the_punct=='?' || the_punct=='!')
&& rand()%100 < PROB_RANT)
{
win.open("moerants.txt");
rand_num=rand()%rantfile_length;
for(short i=0;i<=rand_num;i++)
win.getline(rant,MAX_LINE_SIZE);
win.close();
cout<<" "<<rant<<" ";
}
}
cout<<endl<<"The MOEral of the story is:\n\t ";
win.open("MOErals.txt");
rand_num=rand()%moerals_length;
for(short i=0;i<=rand_num;i++)
win.getline(moral,MAX_LINE_SIZE);
cout<<moral<<endl<<endl;
in.close();
win.close();
return;
}