rllib  1
rlhistoryreader.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                        rlhistoryreader.cpp  -  description
00003                              -------------------
00004     begin                : Wed Dec 06 2006
00005     copyright            : (C) 2006 by Rainer Lehrig
00006     email                : lehrig@t-online.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as        *
00013  *   published by the Free Software Foundation                             *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #include "rlhistoryreader.h"
00017 #include "rlcutil.h"
00018 #include <string.h>
00019 
00020 #define MAXBUF 256*256
00021 
00022 rlHistoryReader::rlHistoryReader()
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 }
00031 
00032 rlHistoryReader::~rlHistoryReader()
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 }
00040 
00041 int rlHistoryReader::read(const char *csvName, rlTime *start, rlTime *end)
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 }
00087 
00088 int rlHistoryReader::clean()
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 }
00109 
00110 int rlHistoryReader::openFile()
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 }
00148 
00149 int rlHistoryReader::pushLineToMemory(const char *line)
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 }
00171 
00172 const char *rlHistoryReader::firstLine()
00173 {
00174   if(first_line == NULL) return "";
00175   current_line = first_line;
00176   return current_line->line;
00177 }
00178 
00179 const char *rlHistoryReader::nextLine()
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 }
00186 
00187 int rlHistoryReader::cat(const char *csvName, FILE *fout)
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 }
00218 
00219 //#define TESTING_HISTORYREADER1
00220 #ifdef TESTING_HISTORYREADER1
00221 int main()
00222 {
00223   const char *cptr;
00224   rlTime start;
00225   rlTime end;
00226   start.getLocalTime();
00227   start.hour = 0;
00228   end.getLocalTime();
00229 
00230   rlHistoryReader reader;
00231   reader.debug = 1;
00232   reader.read("test", &start, &end);
00233 
00234   cptr = reader.firstLine();
00235   while(*cptr != '\0')
00236   {
00237     printf("read=%s",cptr);
00238     cptr = reader.nextLine();
00239   }
00240   return 0;
00241 }
00242 #endif
00243 
00244 //#define TESTING_HISTORYREADER2
00245 #ifdef TESTING_HISTORYREADER2
00246 int main()
00247 {
00248   rlHistoryReader reader;
00249   reader.debug = 1;
00250   reader.cat("test", stdout);
00251   return 0;
00252 }
00253 #endif