-
May 22nd, 2011, 01:34 PM
#1
No errors, but code not running
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include "definitions.h"
#include "system-utilities.h"
#include <string>
using namespace std;
class specialTokenElement {
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;
int openInputFile(char fname[]) {
infile.open(fname, ios::in);
if(infile.eof()) cout << "End of file.\n";
if(!infile.is_open()) return FILE_NOT_OPENED;
//if(infile) return FILE_NOT_OPENED;
else return 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;
cout<<token[0]<<"\n";
return 0; // Indicate token successfully found.
}
void specialTokenElement(char *sptok, int tokrep){
spectoklist[splistsz][0] = sptok; spectoklist[splistsz][1] = (char*) tokrep;
splistsz+=1;
cout<<spectoklist[splistsz][0]<<"|"<<spectoklist[splistsz][1];
}
void fillSpecialTokenList(){
specialTokenElement("<", LEFT_ANGLE);
specialTokenElement("</", LEFT_ANGLE_SLASH);
specialTokenElement(">", RIGHT_ANGLE);
specialTokenElement("/>", RIGHT_ANGLE_SLASH);
specialTokenElement("=", EQUAL);
specialTokenElement("document", DOCUMENT);
specialTokenElement("link", LINK);
specialTokenElement("image", IMAGE);
specialTokenElement("section", SECTION);
specialTokenElement("text", TEXT);
specialTokenElement("new_line", NEW_LINE);
specialTokenElement("report", REPORT);
specialTokenElement("done", DONE);
specialTokenElement("br", BR);
}
int getTokenNumber(char *s){
for (int i=0; i<=splistsz;i++){
if(strcmp(spectoklist[i][0],s)==0)
{ return (int)spectoklist[i][1];}
else{return (int)UNRECOGNIZED_TOKEN;}
}
}
//Main.cpp
#include <iostream>
#include <fstream>
#include "definitions.h"
#include "system-utilities.h"
#include <string>
using namespace std;
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){
char nxttk = getNextToken(&tk);
if(nxttk != 0) printError(nxttk);break;
int gtn = getTokenNumber(tk);
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);
}
system ("PAUSE");
return 0;}
-
May 22nd, 2011, 01:47 PM
#2
Re: No errors, but code not running
1. Define "not running".
2. Please, use Code tags and indentations. See Announcement: Before you post....
Victor Nijegorodov
-
May 22nd, 2011, 01:48 PM
#3
Re: No errors, but code not running
I narrowed down the problem to this function:
Given:
#define NUMBER_OF_SPECIAL_TOKENS 13
char *spectoklist[NUMBER_OF_SPECIAL_TOKENS*2][2];
int splistsz = 0;
void specialTokenElement(char *sptok, int tokrep){
spectoklist[splistsz][0] = sptok; spectoklist[splistsz][1] = (char*) tokrep;
splistsz+=1;
cout<<spectoklist[splistsz][0]<<"|"<<spectoklist[splistsz][1];
}
-
May 22nd, 2011, 01:55 PM
#4
Re: No errors, but code not running
Originally Posted by VictorN
Originally Posted by lucmicky
I narrowed down the problem to this function:
Given:
#define NUMBER_OF_SPECIAL_TOKENS 13
char *spectoklist[NUMBER_OF_SPECIAL_TOKENS*2][2];
int splistsz = 0;
void specialTokenElement(char *sptok, int tokrep){
spectoklist[splistsz][0] = sptok; spectoklist[splistsz][1] = (char*) tokrep;
splistsz+=1;
cout<<spectoklist[splistsz][0]<<"|"<<spectoklist[splistsz][1];
}
And? ...
Victor Nijegorodov
-
May 22nd, 2011, 04:59 PM
#5
Re: No errors, but code not running
I got it to work..i had to put (char *) before spectoklist[splistsz][1]
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|