#include
"Filter.h"
/**
* File : Filter.cpp
* Task :
Implement Filter.h
* Desc : Execute the filtering of the HTML page based
on
* constraint.
*
Used :
* // normal usage
with file name given
*
Filter filter;
*
filter.setConstraint("try"); //setConstraint(string)
* int found =
filter.filterFile("filename"); //filterFile(string)
* if(found >= 0)
* cout<<"found constraint:
"<<filter.showConstraint(found)<<endl;
* else
* cout<<"not found";
* Auth : Edison Chindrawaly
*
Vers : 1.0
* Date : Nov 14, 2001
*/
/**
* default constructor: initialize the space in
memory
* @param none
*
@return none
*/
Filter::Filter()
{
clean();
}
/**
*
default destructor: clean the memory space
* @param none
* @return none
*/
Filter::~Filter()
{
clean();
}
/**
* clean(): help constructor & destructor to claim space
*
@param none
* @return none
*/
void Filter::clean()
{
for(int i=0; i< MAX*3; i++)
{
constraints[i].word[0] = '\0';
}
numberOfConstraint = 0;
}
/**
* search(): To search for a constraint in a
string.
* if it finds
the constraint (by using strstr -
* find the first occurance), it will return
* the index position of the correspondence
* constraint from array of Constraint
* @param char* httpFile - string
to be examine
* @return int i if it finds the constraint in the string
* else it returns -1
*/
int Filter::search(const char* httpFile)
{
// cout<<"search:
"<<httpFile<<endl;
for(int i=0; i < numberOfConstraint; i++)
if(strstr(httpFile,constraints[i].word) !=
NULL)
return i;
return -1;
}
/**
*
filterFile(): To filter a file given by filename. If it
* finds constraint, it returns
the index
* position
of the constraint in the array.
* Else it returns -1
* @param char* inputFile - the filename
* @return int found - the
position of the index in constraint of
* array
* else return -1 if it is not found.
*/
int
Filter::filterFile(char* inputFile)
{
char buffer[LENGTH+1];
ifstream file;
file.open(inputFile,ios::in);
if(!file.is_open())
{
cout<<"error in
opening file"<<endl;
return 1;
}
int found = 0;
while(!file.eof())
{
file.getline(buffer,LENGTH+1);
found = search(buffer);
if(found >= 0)
break;
}
file.close();
return found;
}
/**
* setConstraint():
* To
set a new constraint into the array of Constraint
* It checks whether the number of constraint
has reach
* its max. if it has,
it just output error message and return
* It checks whether the length of new constraint is longer
* than the max allowed constraint
* It checks whether the new constraint has
existed
* if it does not exist
in Constraint, it adds the new
*
constraint into array of constraint and increase
* the counter -> number of constraint
* @param char* newConstraint -
constraint to be added
* @return none
*/
void
Filter::setConstraint(char* newConstraint)
{
if(numberOfConstraint > MAX * 3)
{
cout<<"ERROR: MAX number of constraint has been
reach: ";
cout<<MAX*3<<endl;
return;
}
if(strlen(newConstraint) >
MAX * 3)
{
cout<<"ERROR: The length of
new constraint exceed: ";
cout<<MAX*3<<" characters long."<<endl;
return;
}
int found = searchConstraint(newConstraint);
if(found < 0)
{
strcpy(constraints[numberOfConstraint].word,newConstraint);
numberOfConstraint++;
}
}
/**
*
deleteConstraint():
* To delete
constraint from array of constraint based on index
* @param int index
* @return none
*/
void Filter::deleteConstraint(int index)
{
for(;index<numberOfConstraint;index++)
strcpy(constraints[index].word,
constraints[index+1].word);
numberOfConstraint--;
}
/**
* showConstraint(): To show the constraint based on index
* position of the array of constraint
*
@param int index - position of
constraint
* @return char* the constraint
*/
char*
Filter::showConstraint(int index)
{
if((index > numberOfConstraint) || (index < 0))
return NULL;
return constraints[index].word;
}
/**
* searchConstraint():
* To check whether the constraint existed in the Constraint
* if it existed, it returns the index
position of the
* existing
constraint. else it returns -1
* @param
char* constraint - constraint to be check
* @return int index
position else return -1 if not found
*/
int
Filter::searchConstraint(char* constraint)
{
int index=0;
do
{
if(strcmp(constraints[index].word,constraint)==0)
return index;
index++;
} while(index <
numberOfConstraint);
return -1;
}