rllib  1
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
rlHistoryReader Class Reference

#include <rlhistoryreader.h>

Collaboration diagram for rlHistoryReader:
Collaboration graph
[legend]

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

rlHistoryReaderLinefirst_line
 
rlHistoryReaderLinecurrent_line
 
rlTime time
 
FILE * fin
 
int current_file
 
char * csv_name
 
char * csv_file_name
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ rlHistoryReader()

rlHistoryReader::rlHistoryReader ( )

Definition at line 22 of file rlhistoryreader.cpp.

23 {
24  debug = 0;
25  first_line = current_line = NULL;
26  fin = NULL;
27  current_file = -1;
28  csv_name = NULL;
29  csv_file_name = NULL;
30 }
rlHistoryReaderLine * current_line
rlHistoryReaderLine * first_line

◆ ~rlHistoryReader()

rlHistoryReader::~rlHistoryReader ( )
virtual

Definition at line 32 of file rlhistoryreader.cpp.

33 {
34  clean();
35  if(csv_name != NULL) delete [] csv_name;
36  csv_name = NULL;
37  if(csv_file_name != NULL) delete [] csv_file_name;
38  csv_file_name = NULL;
39 }

Member Function Documentation

◆ cat()

int rlHistoryReader::cat ( const char *  csvName,
FILE *  fout 
)

Definition at line 187 of file rlhistoryreader.cpp.

188 {
189  char *buf;
190  clean();
191  if(csv_name != NULL) delete [] csv_name;
192  csv_name = NULL;
193  if(csv_file_name != NULL) delete [] csv_file_name;
194  csv_file_name = NULL;
195 
196  first_line = current_line = NULL;
197  fin = NULL;
198  current_file = -1;
199  csv_name = new char[strlen(csvName)+1];
200  strcpy(csv_name,csvName);
201  csv_file_name = new char[strlen(csvName)+132];
202 
203  buf = new char[MAXBUF];
204  for(int i=0; i<10; i++)
205  {
206  openFile();
207  if(fin == NULL) break;
208  while(fgets(buf,MAXBUF-1,fin) != NULL)
209  {
210  fprintf(fout,"%s",buf);
211  }
212  fclose(fin);
213  fin = NULL;
214  }
215  delete [] buf;
216  return 0;
217 }
rlHistoryReaderLine * current_line
rlHistoryReaderLine * first_line
#define MAXBUF

◆ clean()

int rlHistoryReader::clean ( )

Definition at line 88 of file rlhistoryreader.cpp.

89 {
90  if(fin != NULL) fclose(fin);
91  fin = NULL;
92  if(first_line != NULL)
93  {
94  rlHistoryReaderLine *last_line;
96  while(current_line != NULL)
97  {
98  last_line = current_line;
100  if(last_line != NULL)
101  {
102  delete [] last_line->line;
103  delete last_line;
104  }
105  }
106  }
107  return 0;
108 }
rlHistoryReaderLine * current_line
rlHistoryReaderLine * first_line
_rlHistoryReaderLine_ * next

◆ firstLine()

const char * rlHistoryReader::firstLine ( )

Definition at line 172 of file rlhistoryreader.cpp.

173 {
174  if(first_line == NULL) return "";
176  return current_line->line;
177 }
rlHistoryReaderLine * current_line
rlHistoryReaderLine * first_line

◆ nextLine()

const char * rlHistoryReader::nextLine ( )

Definition at line 179 of file rlhistoryreader.cpp.

180 {
181  if(current_line == NULL) return "";
183  if(current_line == NULL) return "";
184  return current_line->line;
185 }
rlHistoryReaderLine * current_line
_rlHistoryReaderLine_ * next

◆ openFile()

int rlHistoryReader::openFile ( )
private

Definition at line 110 of file rlhistoryreader.cpp.

111 {
112  if(current_file == -1)
113  {
114  // find oldest file and open it for reading
115  int i_oldest = 0;
116  rlTime t,t_oldest;
117  t_oldest.getLocalTime(); // this must be newer that any file time
118  for(int i=0; i<10; i++)
119  {
120  sprintf(csv_file_name,"%s%d.csv",csv_name,i);
122  {
123  if(debug) printf("try openFile=%s\n",csv_file_name);
124  if(t < t_oldest)
125  {
126  i_oldest = i;
127  t_oldest = t;
128  }
129  }
130  }
131  current_file = i_oldest;
132  sprintf(csv_file_name,"%s%d.csv",csv_name,i_oldest);
133  if(debug) printf("oldestFile=%s\n",csv_file_name);
134  fin = fopen(csv_file_name,"r");
135  if(debug && fin != NULL) printf("success openFile=%s\n",csv_file_name);
136  }
137  else
138  {
139  // open next file for reading
140  current_file++;
141  if(current_file >= 10) current_file = 0;
142  sprintf(csv_file_name,"%s%d.csv",csv_name,current_file);
143  fin = fopen(csv_file_name,"r");
144  if(debug && fin != NULL) printf("success openFile=%s\n",csv_file_name);
145  }
146  return 0;
147 }
void getLocalTime()
Definition: rltime.cpp:342
int getFileModificationTime(const char *filename)
Definition: rltime.cpp:392
Definition: rltime.h:25

◆ pushLineToMemory()

int rlHistoryReader::pushLineToMemory ( const char *  line)
private

Definition at line 149 of file rlhistoryreader.cpp.

150 {
151  rlHistoryReaderLine *history_line;
152 
153  // put line at 1 position
154  if(first_line == NULL)
155  {
157  first_line->line = new char[strlen(line)+1];
158  strcpy(first_line->line,line);
159  first_line->next = NULL;
160  }
161  else
162  {
163  history_line = first_line;
165  first_line->line = new char[strlen(line)+1];
166  strcpy(first_line->line,line);
167  first_line->next = history_line;
168  }
169  return 0;
170 }
struct _rlHistoryReaderLine_ rlHistoryReaderLine
rlHistoryReaderLine * first_line
_rlHistoryReaderLine_ * next

◆ read()

int rlHistoryReader::read ( const char *  csvName,
rlTime start,
rlTime end 
)

Definition at line 41 of file rlhistoryreader.cpp.

42 {
43  char *buf;
44  clean();
45  if(csv_name != NULL) delete [] csv_name;
46  csv_name = NULL;
47  if(csv_file_name != NULL) delete [] csv_file_name;
48  csv_file_name = NULL;
49 
50  first_line = current_line = NULL;
51  fin = NULL;
52  current_file = -1;
53  csv_name = new char[strlen(csvName)+1];
54  strcpy(csv_name,csvName);
55  csv_file_name = new char[strlen(csvName)+132];
56 
57  buf = new char[MAXBUF];
58  for(int i=0; i<10; i++)
59  {
60  openFile();
61  if(debug) printf("reading=%s\n",csv_file_name);
62  if(fin == NULL) break;
63  while(fgets(buf,MAXBUF-1,fin) != NULL)
64  {
66  if(time < *start)
67  {
68  if(debug) printf("too old=%s",buf);
69  }
70  else if(time > *end)
71  {
72  if(debug) printf("too new=%s",buf);
73  break;
74  }
75  else
76  {
77  if(debug) printf("pushLineToMemory=%s",buf);
78  pushLineToMemory(buf);
79  }
80  }
81  fclose(fin);
82  fin = NULL;
83  }
84  delete [] buf;
85  return 0;
86 }
rlHistoryReaderLine * current_line
rlHistoryReaderLine * first_line
#define MAXBUF
int pushLineToMemory(const char *line)
void setTimeFromString(const char *time_string)
Definition: rltime.cpp:82

Member Data Documentation

◆ csv_file_name

char * rlHistoryReader::csv_file_name
private

Definition at line 104 of file rlhistoryreader.h.

◆ csv_name

char* rlHistoryReader::csv_name
private

Definition at line 104 of file rlhistoryreader.h.

◆ current_file

int rlHistoryReader::current_file
private

Definition at line 103 of file rlhistoryreader.h.

◆ current_line

rlHistoryReaderLine * rlHistoryReader::current_line
private

Definition at line 100 of file rlhistoryreader.h.

◆ debug

int rlHistoryReader::debug

Definition at line 96 of file rlhistoryreader.h.

◆ fin

FILE* rlHistoryReader::fin
private

Definition at line 102 of file rlhistoryreader.h.

◆ first_line

rlHistoryReaderLine* rlHistoryReader::first_line
private

Definition at line 100 of file rlhistoryreader.h.

◆ time

rlTime rlHistoryReader::time
private

Definition at line 101 of file rlhistoryreader.h.


The documentation for this class was generated from the following files: