Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

src/FileReader.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Traplas visualisation.
00004 
00005 Copyright (C) 2006 Herbert de Vos & Willem Drost
00006 
00007 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00010 
00011 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00012 
00013 (For full Licence see ../GPL-Licence.txt)
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     // Skip delimiters at beginning.
00054     string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00055     // Find first "non-delimiter".
00056     string::size_type pos     = str.find_first_of(delimiters, lastPos);
00057 
00058     while (string::npos != pos || string::npos != lastPos)
00059     {
00060         // Found a token, add it to the vector.
00061         shared_ptr<string> temp( new string( str.substr(lastPos, pos - lastPos) ) );
00062         tokens->push_back(temp);
00063         // Skip delimiters.  Note the "not_of"
00064         lastPos = str.find_first_not_of(delimiters, pos);
00065         // Find next "non-delimiter"
00066         pos = str.find_first_of(delimiters, lastPos);
00067     }
00068 }
00069 
00070 //      TODO Because the C mode of Flex is used instead of the C++ mode a global declaration of the filereader instance is needed to allow the send method to send messages to the messagecontroller. It is not nice to circumvent the OO construction but it works for the moment.
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                         /*      Get new line from file, tokenize it using " " as a delimiter and put it in the vector messages.*/
00115                         validline = true;
00116                         getline (myfile, line);
00117                         //      Ignore comments
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                                 /* Messagebody is properly constructed. Now do something useful with it.*/
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 //      FileReader code.
00142 
00143 FileReader::FileReader(string nfilename, MessageController* ptr) {
00144         msgcontroller = ptr;
00145         myr = this;
00146         filename = nfilename;
00147         filereadthread_run = true;
00148 
00149         /*
00150          *      We have 2 ways to read the inputfile. The 1st one is a dumb
00151          *      filereader which doesn't look at what it reads, It only ignores
00152          *      commentlines and empty line. The 2nd one uses flex to recognise
00153          *      messages and invoke the messagecontroller. The flex method is far
00154          *      superior and should be used at all times.
00155          */
00156         //pthread_create( &filereadthread, NULL, readFile, (void*)this);
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 }

Generated on Mon Jun 19 10:22:04 2006 for TraplasVisualisation by  doxygen 1.4.4