versadac  1
versadac - Scalable Recorder Firmware
am_csvstream.h
1 /*****************************************************************************
2 FILE : am_csvstream.h
3 VERSION : $Id: am_csvstream.h 4938 2006-10-10 14:20:18Z martinto $
4 AUTHOR : Sandra Herring
5 SYSTEM : GNU C++ for Power PC
6 DESCRIPTION : Utilities for outputting spreadsheet text
7 *****************************************************************************/
8 
9 #if !defined(__AM_CSVSTREAM_H)
10 #define __AM_CSVSTREAM_H
11 
12 #if !defined(__AM_HISTORYPVCONF_H)
13 #include "am_historypvconf.h"
14 #endif
15 
16 #if !defined(__LINKLIST_H)
17 #include "linklist.h"
18 #endif
19 
20 #include <stdio.h>
21 #include "stdtypes.h"
22 
23 extern "C"
24 {
25 #include "unicodestringhandler.h"
26 #include "utils.h"
27 #include "globals_enums.h"
28 }
29 
30 #define EXCEL_DAYS_TO_1970 25569.0 /* number of days from 1900 to 1970, according to Excel/Lotus.
31  One greater than actual number as included non-leap day 29/2/1900 */
32 
33 #define AM_CSV_FNAME_EXTN ".csv"
34 #define AM_CSV_FNAME_EXTN_L 4
35 
36 
37 class AM_TimeFormatter;
38 
39 typedef struct
40 {
41  uint8 timeFormat;
42  bool bInclValues;
43  bool bInclMessages;
44  bool bInclHeaderInfo;
45  bool bInclColumnHeadings;
46  bool bUseTabDelimiter;
48 
49 typedef struct
50 {
51  AM_CsvOptions options;
52  char decimalChars[2];
53  char delimiter;
54  AM_TimeFormatter * pTimeFormatter;
55 } AM_CsvSetup;
56 
57 
58 class AM_HistoryHeader;
59 class AM_HistoryXORPV;
60 class AM_Archiver;
61 
63 {
64  public :
65  static const sint32 WRITE_OK;
66 
67 /*------------------------------------------------------------------------------
68 FUNCTION : AM_CsvStream constructor
69 ARGUMENTS : CSV configuration settings
70 ------------------------------------------------------------------------------*/
71  AM_CsvStream( AM_CsvSetup * pSetup,
72  const char * serverName,
73  const char * userName,
74  const char * password,
75  const char * dirName,
76  uint32 maxFileSize,
77  AM_Archiver * pArchiver );
78 
79 /*------------------------------------------------------------------------------
80 FUNCTION : AM_CsvStream::open
81 DESCRIPTION : service to close current output and start a new CSV file for the next batch
82 ARGUMENTS : headerInfo Details to include in filename and spreadsheet titles.
83  pBatchName Batch name to include in working file name.
84  startTime Start time to include in working file name.
85  retFileName Exit parameter for working name of output file.
86  pRetFileNameEndTime Exit parameter for where end timestamp goes in filename.
87 RETURN : Number of bytes written if opened and header output ok, else
88  OUTPUT_ERR or OUTPUT_FULL_ERR.
89 NOTES :
90 ------------------------------------------------------------------------------*/
91  sint32 open( AM_HistoryHeader & headerInfo,
92  const char * pBatchName,
93  time_t startTime,
94  char * retFileName,
95  char ** pRetFileNameEndTime );
96 
97 /*------------------------------------------------------------------------------
98 FUNCTION : AM_CsvStream::close
99 DESCRIPTION : service to close this stream
100 ARGUMENTS : fileName Name of output file just completed.
101  pFileNameEndTime Where to insert actual end time in filename.
102  endTime Timestamp to rename file with after closing.
103  historyFileTime Timestamp to put on output files
104 RETURN : None.
105 NOTES :
106 ------------------------------------------------------------------------------*/
107  void close( const char * fileName,
108  char * pFileNameEndTime,
109  time_t endTime,
110  uint32 historyFileTime );
111 
112 /*---------------------------------------------------------------------------
113 FUNCTION : getDecimalChars
114 DESCRIPTION : Returns the locale-dependent decimal point string to use in embedded float text.
115 ARGUMENTS : None.
116 RETURN : string to use.
117 NOTES :
118 ---------------------------------------------------------------------------*/
119  const char * getDecimalChars();
120 
121 /*---------------------------------------------------------------------------
122 FUNCTION : getPvStatusText
123 DESCRIPTION : Returns translated text for a specific PV status.
124 ARGUMENTS : Status code.
125 RETURN : Translated string.
126 NOTES :
127 ---------------------------------------------------------------------------*/
128  char * getPvStatusText( uint8 status );
129 
130 /*---------------------------------------------------------------------------
131 FUNCTION : getTimeFormat
132 DESCRIPTION : Returns the time output format.
133 ARGUMENTS : None.
134 RETURN : Format (one of ENUM_CSVTimeFormat).
135 NOTES :
136 ---------------------------------------------------------------------------*/
137  uint8 getTimeFormat();
138 
139 /*---------------------------------------------------------------------------
140 FUNCTION : getServerName
141 DESCRIPTION : Returns the remote server name.
142 ARGUMENTS : None.
143 RETURN : Server name.
144 NOTES :
145 ---------------------------------------------------------------------------*/
146  const char * getServerName();
147 
148 /*---------------------------------------------------------------------------
149 FUNCTION : getUserName
150 DESCRIPTION : Returns the user name for remote login.
151 ARGUMENTS : None.
152 RETURN : Login user name.
153 NOTES :
154 ---------------------------------------------------------------------------*/
155  const char * getUserName();
156 
157 /*---------------------------------------------------------------------------
158 FUNCTION : getPassword
159 DESCRIPTION : Returns the password for remote login.
160 ARGUMENTS : None.
161 RETURN : Login password.
162 NOTES :
163 ---------------------------------------------------------------------------*/
164  const char * getPassword();
165 
166 /*---------------------------------------------------------------------------
167 FUNCTION : getDirName
168 DESCRIPTION : Returns the output directory path.
169 ARGUMENTS : None.
170 RETURN : Absolute directory path.
171 NOTES :
172 ---------------------------------------------------------------------------*/
173  const char * getDirName();
174 
175 /*---------------------------------------------------------------------------
176 FUNCTION : getMaxFileSize
177 DESCRIPTION : Returns the output maximum file size.
178 ARGUMENTS : None.
179 RETURN : Maximum file size.
180 NOTES :
181 ---------------------------------------------------------------------------*/
182  uint32 getMaxFileSize();
183 
184 /*------------------------------------------------------------------------------
185 FUNCTION : AM_CsvStream getFp
186 DESCRIPTION : service to return output file handle.
187 ARGUMENTS : None.
188 RETURN : file handle.
189 NOTES :
190 ------------------------------------------------------------------------------*/
191  FILE * getFp();
192 
193 /*------------------------------------------------------------------------------
194 FUNCTION : AM_CsvStream writeHeaderInfo
195 DESCRIPTION : Outputs file header information
196 ARGUMENTS : History file information
197  Instrument serial number.
198 RETURN : Number of characters written
199 NOTES :
200 ------------------------------------------------------------------------------*/
201  sint32 writeHeaderInfo( AM_HistoryHeader & info,
202  const char * pInstrSerNo );
203 
204 /*------------------------------------------------------------------------------
205 FUNCTION : AM_CsvStream writeColumnHeadings
206 DESCRIPTION : Outputs PV column headings
207 ARGUMENTS : List of point configurations
208 RETURN : Number of characters written
209 NOTES :
210 ------------------------------------------------------------------------------*/
211  sint32 writeColumnHeadings( LinkedList<AM_HistoryPVConfig> &pointConfigs );
212 
213 /*------------------------------------------------------------------------------
214 FUNCTION : AM_CsvStream writeValues
215 DESCRIPTION : Outputs a row of sample data
216 ARGUMENTS : Timestamp, data list and associated PV configurations.
217 RETURN : Number of characters written
218 NOTES :
219 ------------------------------------------------------------------------------*/
220  sint32 writeValues( time_t utcTimeStamp,
221  uint16 millisecs,
222  AM_HistoryXORPV * pSamples,
223  LinkedList<AM_HistoryPVConfig> &pointConfigs );
224 
225 /*------------------------------------------------------------------------------
226 FUNCTION : AM_CsvStream writeMessage
227 DESCRIPTION : Outputs a row with text message
228 ARGUMENTS : Record timestamp and text to output
229 RETURN : Number of characters written
230 NOTES :
231 ------------------------------------------------------------------------------*/
232  sint32 writeMessage( time_t utcTimeStamp,
233  uint16 millisecs,
234  const char * pText );
235 
236 /*---------------------------------------------------------------------------
237 FUNCTION : getTranslatedItemText
238 DESCRIPTION : Returns translated text for a specific item in an enumeration list
239 ARGUMENTS : #define for enumeration list
240  index number of item in enumeration list
241 RETURN : Translated string.
242 NOTES :
243 ---------------------------------------------------------------------------*/
244  char * getTranslatedItemText( uint8 enumeration_id, uint8 enumeration_no );
245 
246 /*---------------------------------------------------------------------------
247 FUNCTION : getTranslatedGlobalsItemText
248 DESCRIPTION : Returns translated text for a specific item in the globals enumeration list
249 ARGUMENTS : #define for enumeration list
250  index number of item in enumeration list
251 RETURN : Translated string.
252 NOTES :
253 ---------------------------------------------------------------------------*/
254  char * getTranslatedGlobalsItemText( int8 enumeration_id, uint8 enumeration_no );
255 
256  private :
257  void determineSeparators();
258 
259  char * fileName( const char * groupName,
260  const char * pBatchName,
261  time_t batchStartTime,
262  time_t batchStopTime,
263  char * retFileName,
264  char ** pRetFileNameEndTime );
265 
266  char * fileNameTime( time_t timestamp,
267  char * pRetFileNameTime );
268 
269  sint32 writeTimestamp( time_t utcTimeStamp,
270  uint16 millisecs );
271 
272  static const char * fileNameNoTime;
273 
274  FILE * m_Fp;
275  AM_CsvSetup * m_pSetup;
276  AM_Archiver * m_pArchiver;
277  const char * m_ServerName;
278  const char * m_UserName;
279  const char * m_Password;
280  const char * m_DirName;
281  uint32 m_MaxFileSize;
282  int m_Socket;
283  int m_Fd;
284  string_100 m_tmpStr;
285 
286 }; // class AM_CsvStream
287 
288 
289 // inline services for class AM_CsvStream
290 
291 /*---------------------------------------------------------------------------
292 FUNCTION : getDecimalChars
293 DESCRIPTION : Returns the locale-dependent decimal point string to use in embedded float text.
294 ARGUMENTS : None.
295 RETURN : string to use.
296 NOTES :
297 ---------------------------------------------------------------------------*/
298 inline const char * AM_CsvStream::getDecimalChars()
299 {
300  return m_pSetup->decimalChars;
301 }
302 
303 /*---------------------------------------------------------------------------
304 FUNCTION : getPvStatusText
305 DESCRIPTION : Returns translated text for a specific PV status.
306 ARGUMENTS : Status code.
307 RETURN : Translated string.
308 NOTES :
309 ---------------------------------------------------------------------------*/
310 inline char * AM_CsvStream::getPvStatusText( uint8 status )
311 {
312  return getTranslatedGlobalsItemText(GLOBALS_G_CHANNEL_STATUS_E_ER, status);
313 }
314 
315 /*---------------------------------------------------------------------------
316 FUNCTION : getTimeFormat
317 DESCRIPTION : Returns the time output format.
318 ARGUMENTS : None.
319 RETURN : Format (one of ENUM_CSVTimeFormat).
320 NOTES :
321 ---------------------------------------------------------------------------*/
322 inline uint8 AM_CsvStream::getTimeFormat()
323 {
324  return m_pSetup->options.timeFormat;
325 }
326 
327 /*---------------------------------------------------------------------------
328 FUNCTION : getServerName
329 DESCRIPTION : Returns the remote server name.
330 ARGUMENTS : None.
331 RETURN : Server name.
332 NOTES :
333 ---------------------------------------------------------------------------*/
334 inline const char * AM_CsvStream::getServerName()
335 {
336  return m_ServerName;
337 }
338 
339 /*---------------------------------------------------------------------------
340 FUNCTION : getUserName
341 DESCRIPTION : Returns the user name for remote login.
342 ARGUMENTS : None.
343 RETURN : Login user name.
344 NOTES :
345 ---------------------------------------------------------------------------*/
346 inline const char * AM_CsvStream::getUserName()
347 {
348  return m_UserName;
349 }
350 
351 /*---------------------------------------------------------------------------
352 FUNCTION : getPassword
353 DESCRIPTION : Returns the password for remote login.
354 ARGUMENTS : None.
355 RETURN : Login password.
356 NOTES :
357 ---------------------------------------------------------------------------*/
358 inline const char * AM_CsvStream::getPassword()
359 {
360  return m_Password;
361 }
362 
363 /*---------------------------------------------------------------------------
364 FUNCTION : getDirName
365 DESCRIPTION : Returns the output directory path.
366 ARGUMENTS : None.
367 RETURN : Absolute directory path.
368 NOTES :
369 ---------------------------------------------------------------------------*/
370 inline const char * AM_CsvStream::getDirName()
371 {
372  return m_DirName;
373 }
374 
375 /*---------------------------------------------------------------------------
376 FUNCTION : getMaxFileSize
377 DESCRIPTION : Returns the output maximum file size.
378 ARGUMENTS : None.
379 RETURN : Maximum file size.
380 NOTES :
381 ---------------------------------------------------------------------------*/
382 inline uint32 AM_CsvStream::getMaxFileSize()
383 {
384  return m_MaxFileSize;
385 }
386 
387 /*------------------------------------------------------------------------------
388 FUNCTION : AM_CsvStream getFp
389 DESCRIPTION : service to return output file handle.
390 ARGUMENTS : None.
391 RETURN : file handle.
392 NOTES :
393 ------------------------------------------------------------------------------*/
394 inline FILE * AM_CsvStream::getFp()
395 {
396  return m_Fp;
397 }
398 
399 /*---------------------------------------------------------------------------
400 FUNCTION : getTranslatedItemText
401 DESCRIPTION : Returns translated text for a specific item in an enumeration list
402 ARGUMENTS : #define for enumeration list
403  index number of item in enumeration list
404 RETURN : Translated string.
405 NOTES :
406 ---------------------------------------------------------------------------*/
407 inline char * AM_CsvStream::getTranslatedItemText( uint8 enumeration_id, uint8 enumeration_no )
408 {
409  unicode_p t = PA_Get_Enum_String(enumeration_id, enumeration_no);
410 
411  return unicode_toAscii((char*)m_tmpStr, t);
412 }
413 
414 /*---------------------------------------------------------------------------
415 FUNCTION : getTranslatedGlobalsItemText
416 DESCRIPTION : Returns translated text for a specific item in the globals enumeration list
417 ARGUMENTS : #define for enumeration list
418  index number of item in enumeration list
419 RETURN : Translated string.
420 NOTES :
421 ---------------------------------------------------------------------------*/
422 inline char * AM_CsvStream::getTranslatedGlobalsItemText( int8 enumeration_id, uint8 enumeration_no )
423 {
424  unicode_p t = PA_Get_Globals_Enum_String(enumeration_id, enumeration_no);
425 
426  return unicode_toAscii((char*)m_tmpStr, t);
427 }
428 
429 #endif
430 
Definition: am_historyxorpv.h:16
Definition: am_csvstream.h:49
Definition: am_archiver.h:90
Definition: am_csvstream.h:62
Definition: am_historyhdr.h:28
Definition: am_csvstream.h:39
Definition: am_timeformatter.h:21