Garmin Fleet Management Controller  2.19.0
LogParser.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * MODULE NAME:
4 * LogParser.cpp
5 *
6 * Copyright 2008-2009 by Garmin Ltd. or its subsidiaries.
7 *---------------------------------------------------------------------
8 * $NoKeywords$
9 *********************************************************************/
10 
11 #include "stdafx.h"
12 #include <fstream>
13 #include "util.h"
14 
15 #include "LogParser.h"
16 
17 using namespace std;
18 
19 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
24 {
25 }
26 
27 //----------------------------------------------------------------------
30 //----------------------------------------------------------------------
32 {
33 }
34 
35 //----------------------------------------------------------------------
43 //----------------------------------------------------------------------
45 {
46  BOOL updated = FALSE;
47 
48  int filenameLength = WideCharToMultiByte( CP_ACP, 0, mLogFilename, -1, NULL, 0, NULL, NULL );
49  char *filenameAnsi = new char[filenameLength];
50  WideCharToMultiByte( CP_ACP, 0, mLogFilename, -1, filenameAnsi, filenameLength, NULL, NULL );
51  ifstream logFile( filenameAnsi, ios_base::in );
52  delete[] filenameAnsi;
53 
54  if( logFile.good() )
55  {
56  const char * logLine;
57  std::string logLineString;
58  logFile.seekg( mParseEndOffset, ios_base::beg );
59  if( mParseEndOffset == 0 )
60  {
61  char * number;
62  char timeString[20];
63 
64  memset( timeString, 0, sizeof( timeString ) );
65  getline( logFile, logLineString );
66  logLine = logLineString.c_str();
67  strncpy( timeString, logLine, cnt_of_array( timeString ) - 1 );
68  mParseEndOffset = logFile.tellg(); //doesn't count '\n'
69  char* context = NULL;
70 
71  number = strtok_s( timeString, ",", &context );
72  if( number != NULL ) mLogStartHr = atoi( number );
73 
74  number = strtok_s( NULL, ",", &context );
75  if( number != NULL ) mLogStartMin = atoi( number );
76 
77  number = strtok_s( NULL, ",", &context );
78  if( number != NULL ) mLogStartSec = atoi( number );
79 
80  number = strtok_s( NULL, ",", &context );
81  if( number != NULL ) mLogStartMillis = atoi( number );
82 
83  if( mLogStartHr < 12 )
84  {
85  mIsMorning = TRUE;
86  if( mLogStartHr == 0 )
87  {
88  mLogStartHr = 12;
89  }
90  }
91  else
92  {
93  mIsMorning = FALSE;
94  if( mLogStartHr != 12 )
95  {
96  mLogStartHr -= 12;
97  }
98  }
99  }
100  if( !logFile.eof() )
101  updated = TRUE;
102  while( !logFile.eof() )
103  {
104  std::streamoff packetStartOffset;
105 
106  packetStartOffset = logFile.tellg();
107  getline( logFile, logLineString );
108  logLine = logLineString.c_str();
109  mParseEndOffset = logFile.tellg();
110  if( logLine[0] != '\0' && mParseEndOffset != -1 )
111  {
112  // update the index
113  mLineOffset[ mLineCount ] = packetStartOffset;
114  mLineCount++;
115  }
116  else
117  { // at the end of the file
118  mParseEndOffset = packetStartOffset;
119  break;
120  }
121  }
122  }
123  logFile.close();
124  return updated;
125 } /* readLog() */
126 
127 //----------------------------------------------------------------------
129 //----------------------------------------------------------------------
131 {
132  mParseEndOffset = 0;
133  mLineCount = 0;
134  mLineOffset.clear();
135 }
136 
137 //----------------------------------------------------------------------
140 //----------------------------------------------------------------------
142 {
143  return mLineCount;
144 }
145 
146 //----------------------------------------------------------------------
149 //----------------------------------------------------------------------
151  (
152  int aWidth
153  )
154 {
155  mRenderWidth = aWidth;
156 }
157 
158 //----------------------------------------------------------------------
163 //----------------------------------------------------------------------
165  (
166  int aSize,
167  uint8 * aData
168  )
169 {
170  CString hexString;
171  int charsPerLine = ( ( mRenderWidth - 86 ) / 15 );
172  for( int i = 0; i < aSize; i++ )
173  {
174  //check screen size and format accordingly
175  if( i != 0 && i % charsPerLine == 0 )
176  hexString.AppendFormat( _T("\r\n+ x%03x\t\t"), i );
177 
178  //print the data
179  hexString.AppendFormat
180  (
181  _T(" %02x"),
182  aData[i]
183  );
184  }
185  hexString.Append( _T("\r\n") );
186  return hexString;
187 }
188 
189 //----------------------------------------------------------------------
192 //----------------------------------------------------------------------
193 void LogParser::init( const CString& aFilename )
194 {
195  mLogFilename = aFilename;
196  reset();
197  readLog();
198 }
199 
200 //----------------------------------------------------------------------
203 //----------------------------------------------------------------------
205 {
206  return mLogFilename;
207 }
CString formatMultiLineHex(int aSize, uint8 *aData)
Format bytes into a multi logLine hex dump format.
Definition: LogParser.cpp:165
STL namespace.
#define cnt_of_array(_a)
The number of elements in _a.
Definition: util_macros.h:90
#define FALSE
Definition: garmin_types.h:46
LogParser()
Constructor.
Definition: LogParser.cpp:23
#define TRUE
Definition: garmin_types.h:45
void reset()
Reset the log parser to initial state.
Definition: LogParser.cpp:130
BOOL readLog()
Reads from the log file starting from the end of the last read position and adds it to the display...
Definition: LogParser.cpp:44
void setRenderWidth(int aWidth)
Set the number of pixels available for rendering text.
Definition: LogParser.cpp:151
int getLineCount() const
Return the number of lines parsed so far.
Definition: LogParser.cpp:141
unsigned char uint8
8-bit unsigned integer
Definition: garmin_types.h:62
CString getFilename()
Get the path of the log file being parsed.
Definition: LogParser.cpp:204
void init(const CString &aFilename)
Initialize the LogParser to read a particular file.
Definition: LogParser.cpp:193
virtual ~LogParser()
Destructor.
Definition: LogParser.cpp:31