#include <rlhistoryreader.h>

Public Member Functions | |
| rlHistoryReader () | |
| virtual | ~rlHistoryReader () |
| int | read (const char *csvName, rlTime *start, rlTime *end) |
| const char * | firstLine () |
| const char * | nextLine () |
| int | clean () |
| int | cat (const char *csvName, FILE *fout) |
Public Attributes | |
| int | debug |
Private Member Functions | |
| int | openFile () |
| int | pushLineToMemory (const char *line) |
Private Attributes | |
| rlHistoryReaderLine * | first_line |
| rlHistoryReaderLine * | current_line |
| rlTime | time |
| FILE * | fin |
| int | current_file |
| char * | csv_name |
| char * | csv_file_name |
This class reads tab separated text including time stamp from 10 csv files Use it together with rlHistoryLogger.
Example usage within pvbrowser slot function:
include "rlhistoryreader.h"
typedef struct // (todo: define your data structure here)
{
rlHistoryReader hist;
}
DATA;...snip...
static int slotButtonEvent(PARAM *p, int id, DATA *d)
{
if(p == NULL || id == 0 || d == NULL) return -1;
if(id == buttonShowPlot)
{
rlTime tStart, tDiff, tEnd;
tStart.year = 2009;
tStart.month = 8;
tStart.day = 29;
tStart.hour = 0;tDiff.hour = 24;
tEnd = tStart + tDiff;
printf("start=%s diff=%s end=%s\n",tStart.getTimeString(), tDiff.getTimeString(), tEnd.getTimeString()); if(d->hist.read("test", &tStart, &tEnd) < 0)
{
printf("could not read test csv file\n");
return 0;
}
const char *line = d->hist.firstLine();
while(*line != '\0')
{
printf("line=%s", line);
const char *values = strchr(line,'');
if(values != NULL)
{
float val1, val2;
sscanf(values,"\t%f\t%f", &val1, &val2);
printf("val1=%f val2=%f\n", val1, val2);
// fill your buffer for plotting here
}
line = d->hist.nextLine();
}
// display your xy-graphic from the filled buffer here
}
return 0;
}
Definition at line 86 of file rlhistoryreader.h.
| rlHistoryReader::rlHistoryReader | ( | ) |
Definition at line 22 of file rlhistoryreader.cpp.
00023 { 00024 debug = 0; 00025 first_line = current_line = NULL; 00026 fin = NULL; 00027 current_file = -1; 00028 csv_name = NULL; 00029 csv_file_name = NULL; 00030 }
| rlHistoryReader::~rlHistoryReader | ( | ) | [virtual] |
Definition at line 32 of file rlhistoryreader.cpp.
00033 { 00034 clean(); 00035 if(csv_name != NULL) delete [] csv_name; 00036 csv_name = NULL; 00037 if(csv_file_name != NULL) delete [] csv_file_name; 00038 csv_file_name = NULL; 00039 }
| int rlHistoryReader::cat | ( | const char * | csvName, | |
| FILE * | fout | |||
| ) |
Definition at line 187 of file rlhistoryreader.cpp.
00188 { 00189 char *buf; 00190 clean(); 00191 if(csv_name != NULL) delete [] csv_name; 00192 csv_name = NULL; 00193 if(csv_file_name != NULL) delete [] csv_file_name; 00194 csv_file_name = NULL; 00195 00196 first_line = current_line = NULL; 00197 fin = NULL; 00198 current_file = -1; 00199 csv_name = new char[strlen(csvName)+1]; 00200 strcpy(csv_name,csvName); 00201 csv_file_name = new char[strlen(csvName)+132]; 00202 00203 buf = new char[MAXBUF]; 00204 for(int i=0; i<10; i++) 00205 { 00206 openFile(); 00207 if(fin == NULL) break; 00208 while(fgets(buf,MAXBUF-1,fin) != NULL) 00209 { 00210 fprintf(fout,"%s",buf); 00211 } 00212 fclose(fin); 00213 fin = NULL; 00214 } 00215 delete [] buf; 00216 return 0; 00217 }
| int rlHistoryReader::clean | ( | ) |
Definition at line 88 of file rlhistoryreader.cpp.
00089 { 00090 if(fin != NULL) fclose(fin); 00091 fin = NULL; 00092 if(first_line != NULL) 00093 { 00094 rlHistoryReaderLine *last_line; 00095 current_line = first_line; 00096 while(current_line != NULL) 00097 { 00098 last_line = current_line; 00099 current_line = current_line->next; 00100 if(last_line != NULL) 00101 { 00102 delete [] last_line->line; 00103 delete last_line; 00104 } 00105 } 00106 } 00107 return 0; 00108 }
| const char * rlHistoryReader::firstLine | ( | ) |
Definition at line 172 of file rlhistoryreader.cpp.
00173 { 00174 if(first_line == NULL) return ""; 00175 current_line = first_line; 00176 return current_line->line; 00177 }
| const char * rlHistoryReader::nextLine | ( | ) |
Definition at line 179 of file rlhistoryreader.cpp.
00180 { 00181 if(current_line == NULL) return ""; 00182 current_line = current_line->next; 00183 if(current_line == NULL) return ""; 00184 return current_line->line; 00185 }
| int rlHistoryReader::openFile | ( | ) | [private] |
Definition at line 110 of file rlhistoryreader.cpp.
00111 { 00112 if(current_file == -1) 00113 { 00114 // find oldest file and open it for reading 00115 int i_oldest = 0; 00116 rlTime t,t_oldest; 00117 t_oldest.getLocalTime(); // this must be newer that any file time 00118 for(int i=0; i<10; i++) 00119 { 00120 sprintf(csv_file_name,"%s%d.csv",csv_name,i); 00121 if(t.getFileModificationTime(csv_file_name) == 0) 00122 { 00123 if(debug) printf("try openFile=%s\n",csv_file_name); 00124 if(t < t_oldest) 00125 { 00126 i_oldest = i; 00127 t_oldest = t; 00128 } 00129 } 00130 } 00131 current_file = i_oldest; 00132 sprintf(csv_file_name,"%s%d.csv",csv_name,i_oldest); 00133 if(debug) printf("oldestFile=%s\n",csv_file_name); 00134 fin = fopen(csv_file_name,"r"); 00135 if(debug && fin != NULL) printf("success openFile=%s\n",csv_file_name); 00136 } 00137 else 00138 { 00139 // open next file for reading 00140 current_file++; 00141 if(current_file >= 10) current_file = 0; 00142 sprintf(csv_file_name,"%s%d.csv",csv_name,current_file); 00143 fin = fopen(csv_file_name,"r"); 00144 if(debug && fin != NULL) printf("success openFile=%s\n",csv_file_name); 00145 } 00146 return 0; 00147 }
| int rlHistoryReader::pushLineToMemory | ( | const char * | line | ) | [private] |
Definition at line 149 of file rlhistoryreader.cpp.
00150 { 00151 rlHistoryReaderLine *history_line; 00152 00153 // put line at 1 position 00154 if(first_line == NULL) 00155 { 00156 first_line = new rlHistoryReaderLine; 00157 first_line->line = new char[strlen(line)+1]; 00158 strcpy(first_line->line,line); 00159 first_line->next = NULL; 00160 } 00161 else 00162 { 00163 history_line = first_line; 00164 first_line = new rlHistoryReaderLine; 00165 first_line->line = new char[strlen(line)+1]; 00166 strcpy(first_line->line,line); 00167 first_line->next = history_line; 00168 } 00169 return 0; 00170 }
Definition at line 41 of file rlhistoryreader.cpp.
00042 { 00043 char *buf; 00044 clean(); 00045 if(csv_name != NULL) delete [] csv_name; 00046 csv_name = NULL; 00047 if(csv_file_name != NULL) delete [] csv_file_name; 00048 csv_file_name = NULL; 00049 00050 first_line = current_line = NULL; 00051 fin = NULL; 00052 current_file = -1; 00053 csv_name = new char[strlen(csvName)+1]; 00054 strcpy(csv_name,csvName); 00055 csv_file_name = new char[strlen(csvName)+132]; 00056 00057 buf = new char[MAXBUF]; 00058 for(int i=0; i<10; i++) 00059 { 00060 openFile(); 00061 if(debug) printf("reading=%s\n",csv_file_name); 00062 if(fin == NULL) break; 00063 while(fgets(buf,MAXBUF-1,fin) != NULL) 00064 { 00065 time.setTimeFromString(buf); 00066 if(time < *start) 00067 { 00068 if(debug) printf("too old=%s",buf); 00069 } 00070 else if(time > *end) 00071 { 00072 if(debug) printf("too new=%s",buf); 00073 break; 00074 } 00075 else 00076 { 00077 if(debug) printf("pushLineToMemory=%s",buf); 00078 pushLineToMemory(buf); 00079 } 00080 } 00081 fclose(fin); 00082 fin = NULL; 00083 } 00084 delete [] buf; 00085 return 0; 00086 }
char * rlHistoryReader::csv_file_name [private] |
Definition at line 104 of file rlhistoryreader.h.
char* rlHistoryReader::csv_name [private] |
Definition at line 104 of file rlhistoryreader.h.
int rlHistoryReader::current_file [private] |
Definition at line 103 of file rlhistoryreader.h.
rlHistoryReaderLine * rlHistoryReader::current_line [private] |
Definition at line 100 of file rlhistoryreader.h.
Definition at line 96 of file rlhistoryreader.h.
FILE* rlHistoryReader::fin [private] |
Definition at line 102 of file rlhistoryreader.h.
rlHistoryReaderLine* rlHistoryReader::first_line [private] |
Definition at line 100 of file rlhistoryreader.h.
rlTime rlHistoryReader::time [private] |
Definition at line 101 of file rlhistoryreader.h.
1.6.3