1/*
2 * Legal Notice
3 *
4 * This document and associated source code (the "Work") is a part of a
5 * benchmark specification maintained by the TPC.
6 *
7 * The TPC reserves all right, title, and interest to the Work as provided
8 * under U.S. and international laws, including without limitation all patent
9 * and trademark rights therein.
10 *
11 * No Warranty
12 *
13 * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
14 * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
15 * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
16 * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
17 * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
18 * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
19 * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
20 * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
21 * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
22 * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
23 * WITH REGARD TO THE WORK.
24 * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
25 * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
26 * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
27 * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
28 * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
29 * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
30 * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
31 * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * Contributors
34 * - Doug Johnson
35 */
36
37/*
38 * Class representing a flat file loader.
39 */
40#ifndef FLAT_FILE_LOADER_H
41#define FLAT_FILE_LOADER_H
42
43#include <cstdio>
44#include "BaseLoader.h"
45#include "unusedflag.h"
46
47using namespace std;
48
49namespace TPCE {
50
51// EGen Formatting Defaults
52#ifndef DATETIME_FORMAT
53#define DATETIME_FORMAT 12 // YYYY-MM-DD HH:MM:SS.mmm
54#endif
55
56#ifndef TIME_FORMAT
57#define TIME_FORMAT 01 // hh:mm:ss
58#endif
59
60#ifndef DATE_FORMAT
61#define DATE_FORMAT 10 // YYYY-MM-DD
62#endif
63
64#ifndef BOOLEAN_TRUE
65#define BOOLEAN_TRUE "1"
66#endif
67
68#ifndef BOOLEAN_FALSE
69#define BOOLEAN_FALSE "0"
70#endif
71
72#ifndef BUFFER_SIZE
73#define BUFFER_SIZE 0
74#endif
75
76#ifndef FILE_OPEN_MODE_OVERWRITE
77#ifdef WIN32
78#define FILE_OPEN_MODE_OVERWRITE "w+"
79#else
80#define FILE_OPEN_MODE_OVERWRITE "w"
81#endif
82#endif
83
84#ifndef FILE_OPEN_MODE_APPEND
85#ifdef WIN32
86#define FILE_OPEN_MODE_APPEND "a+"
87#else
88#define FILE_OPEN_MODE_APPEND "a"
89#endif
90#endif
91
92// EGen Formatting
93const int FlatFileDateTimeFormat = DATETIME_FORMAT;
94const int FlatFileTimeFormat = TIME_FORMAT;
95const int FlatFileDateFormat = DATE_FORMAT;
96const char *const FlatFileBoolTrue = BOOLEAN_TRUE;
97const char *const FlatFileBoolFalse = BOOLEAN_FALSE;
98
99// EGen Buffering
100const int FlatFileBufferSize = BUFFER_SIZE;
101
102// EGen File Open Modes
103const char *const FlatFileOpenModeOverwrite = FILE_OPEN_MODE_OVERWRITE;
104const char *const FlatFileOpenModeAppend = FILE_OPEN_MODE_APPEND;
105
106// Overwrite vs. append functionality for output flat files.
107enum FlatFileOutputModes { FLAT_FILE_OUTPUT_APPEND = 0, FLAT_FILE_OUTPUT_OVERWRITE };
108
109/*
110 * FlatLoader class.
111 */
112template <typename T> class CFlatFileLoader : public CBaseLoader<T> {
113protected:
114 FILE *hOutFile;
115
116public:
117 CFlatFileLoader(char *szFileName, FlatFileOutputModes FlatFileOutputMode);
118 ~CFlatFileLoader(void);
119
120 // virtual void WriteNextRecord(const T* next_record UNUSED) {};
121 // virtual void WriteNextRecord(const T& next_record UNUSED) {};
122 void FinishLoad(); // finish load
123};
124
125/*
126 * The constructor.
127 */
128template <typename T> CFlatFileLoader<T>::CFlatFileLoader(char *szFileName, FlatFileOutputModes flatFileOutputMode) {
129 if (FLAT_FILE_OUTPUT_APPEND == flatFileOutputMode) {
130 hOutFile = fopen(szFileName, FlatFileOpenModeAppend);
131 } else if (FLAT_FILE_OUTPUT_OVERWRITE == flatFileOutputMode) {
132 hOutFile = fopen(szFileName, FlatFileOpenModeOverwrite);
133 }
134
135 if (!hOutFile) {
136 throw CSystemErr(CSystemErr::eCreateFile, "CFlatFileLoader<T>::CFlatFileLoader");
137 }
138
139 if (FlatFileBufferSize > 0) {
140 if (setvbuf(hOutFile, NULL, _IOFBF, FlatFileBufferSize)) {
141 throw CSystemErr(CSystemErr::eCreateFile, "CFlatFileLoader<T>::CFlatFileLoader");
142 }
143 }
144}
145
146/*
147 * Destructor.
148 */
149template <typename T> CFlatFileLoader<T>::~CFlatFileLoader() {
150 fclose(hOutFile);
151}
152
153/*
154 * Commit sent rows. This needs to be called after the last row has been
155 * sent and before the object is destructed. Otherwise all rows will be
156 * discarded.
157 */
158template <typename T> void CFlatFileLoader<T>::FinishLoad() {
159 fflush(hOutFile);
160}
161
162} // namespace TPCE
163
164#endif // FLAT_FILE_LOADER_H
165