rllib  1
rlhistoryreader.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlhistoryreader.cpp - description
3  -------------------
4  begin : Wed Dec 06 2006
5  copyright : (C) 2006 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rlhistoryreader.h"
17 #include "rlcutil.h"
18 #include <string.h>
19 
20 #define MAXBUF 256*256
21 
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 }
31 
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 }
40 
41 int rlHistoryReader::read(const char *csvName, rlTime *start, rlTime *end)
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 }
87 
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 }
109 
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 }
148 
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 }
171 
173 {
174  if(first_line == NULL) return "";
176  return current_line->line;
177 }
178 
180 {
181  if(current_line == NULL) return "";
183  if(current_line == NULL) return "";
184  return current_line->line;
185 }
186 
187 int rlHistoryReader::cat(const char *csvName, FILE *fout)
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 }
218 
219 //#define TESTING_HISTORYREADER1
220 #ifdef TESTING_HISTORYREADER1
221 int main()
222 {
223  const char *cptr;
224  rlTime start;
225  rlTime end;
226  start.getLocalTime();
227  start.hour = 0;
228  end.getLocalTime();
229 
230  rlHistoryReader reader;
231  reader.debug = 1;
232  reader.read("test", &start, &end);
233 
234  cptr = reader.firstLine();
235  while(*cptr != '\0')
236  {
237  printf("read=%s",cptr);
238  cptr = reader.nextLine();
239  }
240  return 0;
241 }
242 #endif
243 
244 //#define TESTING_HISTORYREADER2
245 #ifdef TESTING_HISTORYREADER2
246 int main()
247 {
248  rlHistoryReader reader;
249  reader.debug = 1;
250  reader.cat("test", stdout);
251  return 0;
252 }
253 #endif
void getLocalTime()
Definition: rltime.cpp:342
int hour
Definition: rltime.h:56
struct _rlHistoryReaderLine_ rlHistoryReaderLine
int read(const char *csvName, rlTime *start, rlTime *end)
virtual ~rlHistoryReader()
const char * nextLine()
rlHistoryReaderLine * current_line
const char * firstLine()
int main()
Definition: rlcorba.cpp:18
rlHistoryReaderLine * first_line
#define MAXBUF
int cat(const char *csvName, FILE *fout)
int pushLineToMemory(const char *line)
int getFileModificationTime(const char *filename)
Definition: rltime.cpp:392
Definition: rltime.h:25
void setTimeFromString(const char *time_string)
Definition: rltime.cpp:82
_rlHistoryReaderLine_ * next