|
rllib
1
|
#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.
{
debug = 0;
first_line = current_line = NULL;
fin = NULL;
current_file = -1;
csv_name = NULL;
csv_file_name = NULL;
}
| rlHistoryReader::~rlHistoryReader | ( | ) | [virtual] |
Definition at line 32 of file rlhistoryreader.cpp.
{
clean();
if(csv_name != NULL) delete [] csv_name;
csv_name = NULL;
if(csv_file_name != NULL) delete [] csv_file_name;
csv_file_name = NULL;
}
| int rlHistoryReader::cat | ( | const char * | csvName, |
| FILE * | fout | ||
| ) |
Definition at line 187 of file rlhistoryreader.cpp.
{
char *buf;
clean();
if(csv_name != NULL) delete [] csv_name;
csv_name = NULL;
if(csv_file_name != NULL) delete [] csv_file_name;
csv_file_name = NULL;
first_line = current_line = NULL;
fin = NULL;
current_file = -1;
csv_name = new char[strlen(csvName)+1];
strcpy(csv_name,csvName);
csv_file_name = new char[strlen(csvName)+132];
buf = new char[MAXBUF];
for(int i=0; i<10; i++)
{
openFile();
if(fin == NULL) break;
while(fgets(buf,MAXBUF-1,fin) != NULL)
{
fprintf(fout,"%s",buf);
}
fclose(fin);
fin = NULL;
}
delete [] buf;
return 0;
}
| int rlHistoryReader::clean | ( | ) |
Definition at line 88 of file rlhistoryreader.cpp.
{
if(fin != NULL) fclose(fin);
fin = NULL;
if(first_line != NULL)
{
rlHistoryReaderLine *last_line;
current_line = first_line;
while(current_line != NULL)
{
last_line = current_line;
current_line = current_line->next;
if(last_line != NULL)
{
delete [] last_line->line;
delete last_line;
}
}
}
return 0;
}
| const char * rlHistoryReader::firstLine | ( | ) |
Definition at line 172 of file rlhistoryreader.cpp.
{
if(first_line == NULL) return "";
current_line = first_line;
return current_line->line;
}
| const char * rlHistoryReader::nextLine | ( | ) |
Definition at line 179 of file rlhistoryreader.cpp.
{
if(current_line == NULL) return "";
current_line = current_line->next;
if(current_line == NULL) return "";
return current_line->line;
}
| int rlHistoryReader::openFile | ( | ) | [private] |
Definition at line 110 of file rlhistoryreader.cpp.
{
if(current_file == -1)
{
// find oldest file and open it for reading
int i_oldest = 0;
rlTime t,t_oldest;
t_oldest.getLocalTime(); // this must be newer that any file time
for(int i=0; i<10; i++)
{
sprintf(csv_file_name,"%s%d.csv",csv_name,i);
if(t.getFileModificationTime(csv_file_name) == 0)
{
if(debug) printf("try openFile=%s\n",csv_file_name);
if(t < t_oldest)
{
i_oldest = i;
t_oldest = t;
}
}
}
current_file = i_oldest;
sprintf(csv_file_name,"%s%d.csv",csv_name,i_oldest);
if(debug) printf("oldestFile=%s\n",csv_file_name);
fin = fopen(csv_file_name,"r");
if(debug && fin != NULL) printf("success openFile=%s\n",csv_file_name);
}
else
{
// open next file for reading
current_file++;
if(current_file >= 10) current_file = 0;
sprintf(csv_file_name,"%s%d.csv",csv_name,current_file);
fin = fopen(csv_file_name,"r");
if(debug && fin != NULL) printf("success openFile=%s\n",csv_file_name);
}
return 0;
}
| int rlHistoryReader::pushLineToMemory | ( | const char * | line | ) | [private] |
Definition at line 149 of file rlhistoryreader.cpp.
{
rlHistoryReaderLine *history_line;
// put line at 1 position
if(first_line == NULL)
{
first_line = new rlHistoryReaderLine;
first_line->line = new char[strlen(line)+1];
strcpy(first_line->line,line);
first_line->next = NULL;
}
else
{
history_line = first_line;
first_line = new rlHistoryReaderLine;
first_line->line = new char[strlen(line)+1];
strcpy(first_line->line,line);
first_line->next = history_line;
}
return 0;
}
Definition at line 41 of file rlhistoryreader.cpp.
{
char *buf;
clean();
if(csv_name != NULL) delete [] csv_name;
csv_name = NULL;
if(csv_file_name != NULL) delete [] csv_file_name;
csv_file_name = NULL;
first_line = current_line = NULL;
fin = NULL;
current_file = -1;
csv_name = new char[strlen(csvName)+1];
strcpy(csv_name,csvName);
csv_file_name = new char[strlen(csvName)+132];
buf = new char[MAXBUF];
for(int i=0; i<10; i++)
{
openFile();
if(debug) printf("reading=%s\n",csv_file_name);
if(fin == NULL) break;
while(fgets(buf,MAXBUF-1,fin) != NULL)
{
time.setTimeFromString(buf);
if(time < *start)
{
if(debug) printf("too old=%s",buf);
}
else if(time > *end)
{
if(debug) printf("too new=%s",buf);
break;
}
else
{
if(debug) printf("pushLineToMemory=%s",buf);
pushLineToMemory(buf);
}
}
fclose(fin);
fin = NULL;
}
delete [] buf;
return 0;
}
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.7.5.1