public: char * sptok;
int tokrep;
specialTokenElement(char *sptok, int tokrep);
};
// File level variables. These are used on successive function calls and therefore
// cannot be local variables.
ifstream infile; // The input file itself
char line[MAX_LINE_LENGTH]; // Holds characters on one line up to limit
int lineLength=0; // Number of characters on the current line
int currentPosition=1; // Current position on the line, ie the last position
// used in the last token
char textLine[1000]; // Collects the string for a text tag.
char *spectoklist[NUMBER_OF_SPECIAL_TOKENS*2][2];
int splistsz = 0;
void printError(int errcode)
{
switch (errcode)
{
case 201:
cout<<"End of file :"<<errcode<<"\n";
break;
case 202:
cout<<"File not opened:"<<errcode<<"\n";
break;
case 301:
cout<<"Token not found:"<<errcode<<"\n";
break;
case 255:
cout<<"Max line length:"<<errcode<<"\n";
break;
default:
cout<<"An unknown error has occured:"<<errcode<<"\n";
}
}//system-utilities.cpp
int getNextToken(char **token) {
int tmpPos; // Searches through the characters on the input line.
int endPos; // Marks the end (last character) of the token.
char ch; // The character in the line being searched as terminator.
int tLen; // The number of characters in the token.
int quotedToken; // Tells whether the token is a quoted token or not.
// Find non_blank character. If we reach the end of the line, try to read
// another line and start at the beginning again.
tmpPos = currentPosition;
do {
tmpPos += 1;
if(tmpPos>=lineLength) {
if(infile.eof()) {
printError(END_OF_FILE);
return TOKEN_NOT_FOUND;
}
memset(line,0,sizeof(line));
infile.getline(line,sizeof(line));
lineLength = strlen(line);
currentPosition = 0;
tmpPos = 0;
}
} while(line[tmpPos]==' ');
quotedToken = 0;
// Check first for special cases - <, </, >, />, and =. If none of these,
// its either a simple string or a quoted token.
if(line[tmpPos]=='<') {
if(line[tmpPos+1]=='/') endPos = tmpPos+1;
else endPos = tmpPos;
}
else if((line[tmpPos]=='/') && (line[tmpPos+1]=='>')) {
endPos = tmpPos+1;
}
else if(line[tmpPos]=='>') {
endPos = tmpPos;
}
else if(line[tmpPos]=='=') {
endPos = tmpPos;
}
else {
// Not a special token. Check for starting with quote mark or not.
if(line[tmpPos]=='"') {
// Quoted token. Scan for next quote mark. Remember that token
// starts at the character AFTER the initial quote mark, and the
// terminating quote mark is not part of the token either.
quotedToken = 1;
tmpPos += 1;
endPos = tmpPos+1;
while(line[endPos]!='"') endPos++;
endPos--;
}
else {
// Non-quoted token. Scan for a blank, >, /, =, or end of line.
endPos = tmpPos+1; ch = line[endPos];
while( (ch!='>') && (ch!='/') && (ch!=' ') && (ch!='=') && (ch!=0) )
ch = line[++endPos];
endPos--;
}
}
// Now copy the token out and reset currentPosition for next time.
tLen = endPos-tmpPos+1;
* token = (char *) malloc(tLen+1);
memcpy(*token,line+tmpPos,tLen); // Copy the characters
(*token)[tLen] = 0; // Add the string terminator 0.
if(quotedToken) currentPosition = endPos+1;
else currentPosition = endPos;
int main()
{
int fileStatus = openInputFile("p5inputa.txt");
char *tk="";
if(fileStatus==0)
{
fillSpecialTokenList();
while(int a = 1){
char nxttk = getNextToken(&tk);
if(nxttk != 0) printError(nxttk);break;
int gtn = getTokenNumber(tk);
if (gtn != (int)LEFT_ANGLE && gtn != (int)LEFT_ANGLE_SLASH) printError(UNEXPECTED_TAG);
else if (LEFT_ANGLE){
char nxttk = getNextToken(&tk);
if(nxttk != 0) printError(nxttk);break;
int gtn = getTokenNumber(tk);
switch(tk[0]){
case DOCUMENT:
cout<<"Document tag recognized\n";
break;
case SECTION:
cout<<"Section tag recognized\n";
break;
case IMAGE:
cout<<"Image tag recognized\n";
break;
case LINK:
cout<<"Link tag recognized\n";
break;
case TEXT:
cout<<"Text tag recognized\n";
break;
case BR:
cout<<"BR tag recognized";
break;
case REPORT:
cout<<"Report tag recognized\n";
break;
case DONE:
cout<<"Done tag recognized\n";
break;
default:
printError(IMPROPER_TAG_TYPE);
}
}
else if (LEFT_ANGLE_SLASH){
switch(tk[0]){
case DOCUMENT:
cout<<"Document tag recognized";
break;
case SECTION:
cout<<"Section tag recognized";
break;
case LINK:
cout<<"Link tag recognized";
break;
case TEXT:
cout<<"Text tag recognized";
break;
default:
printError(IMPROPER_TAG_TYPE);
}
} else{}
}
}
else
{
printError(fileStatus);
}
Bookmarks