rllib  1
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
rlHistoryReader Class Reference

#include <rlhistoryreader.h>

Collaboration diagram for rlHistoryReader:
Collaboration graph
[legend]

List of all members.

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 ( )

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;
}

Member Function Documentation

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;
}
int rlHistoryReader::read ( const char *  csvName,
rlTime start,
rlTime end 
)

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;
}

Member Data Documentation

Definition at line 104 of file rlhistoryreader.h.

char* rlHistoryReader::csv_name [private]

Definition at line 104 of file rlhistoryreader.h.

Definition at line 103 of file rlhistoryreader.h.

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.

Definition at line 100 of file rlhistoryreader.h.

Definition at line 101 of file rlhistoryreader.h.


The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines