00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _GLIBCXX_VECTOR
00018 #include <vector>
00019 #endif
00020
00021 #ifndef _GLIBCXX_STRING
00022 #include <string>
00023 #endif
00024
00025 #ifndef _GLIBCXX_QUEUE
00026 #include <queue>
00027 #endif
00028
00029 #ifndef _GLIBCXX_SSTREAM
00030 #include <sstream>
00031 #endif
00032
00033 #ifndef _GLIBCXX_FSTREAM
00034 #include <fstream>
00035 #endif
00036
00037 #ifndef _GLIBCXX_IOSTREAM
00038 #include <iostream>
00039 #endif
00040
00041 #ifndef FILEREADER_H
00042 #include "FileReader.h"
00043 #endif
00044
00045 #ifndef MESSAGECONTROLLER_H
00046 #include "MessageController.h"
00047 #endif
00048
00049 void Tokenize(const string& str,
00050 shared_ptr< vector< shared_ptr<string> > > tokens,
00051 const string& delimiters = " ")
00052 {
00053
00054 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00055
00056 string::size_type pos = str.find_first_of(delimiters, lastPos);
00057
00058 while (string::npos != pos || string::npos != lastPos)
00059 {
00060
00061 shared_ptr<string> temp( new string( str.substr(lastPos, pos - lastPos) ) );
00062 tokens->push_back(temp);
00063
00064 lastPos = str.find_first_not_of(delimiters, pos);
00065
00066 pos = str.find_first_of(delimiters, lastPos);
00067 }
00068 }
00069
00070
00071 FileReader *myr;
00072
00073 bool send(char* line)
00074 {
00075 stringstream s;
00076 s << line;
00077
00078 shared_ptr< vector <shared_ptr<string> > > message( new vector<shared_ptr<string> >() );
00079
00080 Tokenize(s.str(), message, " ");
00081
00082 myr->msgcontroller->processMessageFromComm(message);
00083
00084 return true;
00085 }
00086
00087 #include "lex.yy.c"
00088
00089 void *flexRead( void *ptr )
00090 {
00091 FileReader *myreader = (FileReader*) ptr;
00092 yyin = fopen((myreader->filename).c_str() , "r");
00093
00094 yylex();
00095
00096 int status = 0;
00097 pthread_exit(&status);
00098 }
00099
00100 void *readFile( void *ptr )
00101 {
00102 FileReader *myreader = (FileReader*) ptr;
00103
00104 cout << "Attempting to read from file: " << myreader->filename << endl;
00105 string line;
00106 bool validline = true;
00107
00108 ifstream myfile ((myreader->filename).c_str());
00109
00110 if (myfile.is_open())
00111 {
00112 while (!myfile.eof() && myreader->filereadthread_run )
00113 {
00114
00115 validline = true;
00116 getline (myfile, line);
00117
00118 if ( !line.empty() ) {
00119 if ( line.at(0) == '#' ) {
00120 validline = false;
00121 }
00122 } else validline = false;
00123
00124 if( validline ) {
00125 shared_ptr< vector <shared_ptr<string> > > message( new vector<shared_ptr<string> >() );
00126 Tokenize(line, message, " ");
00127
00128
00129 myreader->msgcontroller->processMessageFromComm(message);
00130 }
00131 }
00132 myfile.close();
00133 }
00134 else cout << "Unable to open file" << endl;
00135
00136 int status = 0;
00137 pthread_exit(&status);
00138 }
00139
00140
00141
00142
00143 FileReader::FileReader(string nfilename, MessageController* ptr) {
00144 msgcontroller = ptr;
00145 myr = this;
00146 filename = nfilename;
00147 filereadthread_run = true;
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 pthread_create( &filereadthread, NULL, flexRead, (void*)this);
00158 }
00159
00160 FileReader::~FileReader() {
00161 cout << "FileReader destructor." << endl;
00162
00163 filereadthread_run = false;
00164
00165 pthread_join( filereadthread, NULL);
00166 }
00167
00168 bool FileReader::sendMessage( shared_ptr< vector< shared_ptr< string > > > ) {
00169 cout << "FileReader cannot send messages since it has nowhere to send them." << endl;
00170 return false;
00171 }