MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Tools/Graphical/libAlogTools/alogTools/indexWriter.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <string>
00004 #include <vector>
00005 #include <sstream>
00006 #include <algorithm>
00007 
00008 #include "FileNotFoundException.h"
00009 #include "recordTypes.h"
00010 #include "indexWriter.h"
00011 
00012 using namespace std;
00013 
00015 void indexWriter::writeIndexFile(string alogIndexName)
00016 {
00017     // sort the lines by timestamp and then write to index file
00018     sort(m_alogRecords.begin(), m_alogRecords.end());
00019 
00020     ofstream outfile(alogIndexName.c_str());
00021     if (!outfile.is_open())
00022     {
00023         // TODO: this is the wrong exception to throw!
00024         throw FileNotFoundException();
00025     }
00026 
00027     outfile << m_alogHeader;
00028     outfile << m_alogMsgList;
00029     outfile << m_alogSrcList;
00030 
00031     long recOffset = outfile.tellp();
00032     m_alogHeader.recsBegin = recOffset;
00033 
00034     // Go and rewrite header, now we know where the data begins
00035     outfile.seekp(0, ios_base::beg);
00036     outfile << m_alogHeader;
00037     outfile.seekp(recOffset);
00038 
00039     vector<aloglib::idxRec>::const_iterator it = m_alogRecords.begin();
00040     while (it != m_alogRecords.end())
00041     {
00042         const aloglib::idxRec &aline = *it;
00043         //printf("time: %f,  lBegin: %d,  lEnd: %d\n",aline.time,aline.lineBegin,aline.lineEnd);
00044         outfile << aline;
00045         ++it;
00046     }
00047 
00048     outfile.close();
00049 }
00050 
00052 void indexWriter::parseAlogFile(string alogFileName)
00053 {
00054     ifstream myfile(alogFileName.c_str());
00055     if (!myfile.is_open())
00056     {
00057         throw FileNotFoundException(alogFileName);
00058     }
00059 
00060     int numRecords = 0;
00061     while (!myfile.eof())
00062     {
00063         // Store byte offset to next line
00064         long int lineBegin = (long int) myfile.tellg();
00065 
00066         // Read the line in
00067         string line;
00068         getline(myfile, line);
00069 
00070         // Check for blank line
00071         size_t pos = line.find_first_not_of(" \t\r\n");
00072         bool blankLine = (pos == string::npos);
00073 
00074         if (!blankLine && !line.empty())
00075         {
00076             // Check for comment
00077             if (line.at(pos) == '%')
00078             {
00079                 size_t logstartpos = line.find("LOGSTART");
00080                 if (logstartpos != string::npos)
00081                 {
00082                     size_t startTimePos = line.find_first_of("0123456789.");
00083                     if (startTimePos != string::npos)
00084                     {
00085                         double dfTime = atof(line.substr(startTimePos).c_str());
00086                         m_alogHeader.startTime = dfTime;
00087                     }
00088                 }
00089 
00090                 continue;
00091             }
00092 
00093             // ignore negative timestamps
00094             // isdigit checks for [0-9] so will fail on -x.x
00095             if (isdigit(line.at(pos)) == 0)
00096             {
00097                 continue;
00098             }
00099 
00100 
00101             // Wrap a stream around the string, to allow easier parsing
00102             std::istringstream stm(line);
00103 
00104             double timeStamp;
00105             string varName;
00106             string srcName;
00107             stm >> timeStamp >> varName >> srcName;
00108 
00109             // Feed MOOS variable name into message list
00110             m_alogMsgList.insert(varName);
00111 
00112             // Feed MOOS source name into Source list
00113             m_alogSrcList.insert(srcName);
00114 
00115             long int lineEnd = (long int) myfile.tellg();
00116 
00117             aloglib::idxRec alogLine;
00118             alogLine.time = timeStamp;
00119             alogLine.lineBegin = lineBegin;
00120             alogLine.len = lineEnd - lineBegin;
00121 
00122             //printf("time: %f,  lineBegin: %ld,  len: %ld,  varName: %s\n", alogLine.time, alogLine.lineBegin, alogLine.len, varName.c_str() );
00123             m_alogRecords.push_back(alogLine);
00124 
00125             numRecords++;
00126         }
00127     }
00128     myfile.close();
00129 
00130     m_alogHeader.numRecs = numRecords;
00131 }
00132 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines