1#ifndef DATA_FILE_H
2#define DATA_FILE_H
3
4/*
5 * Legal Notice
6 *
7 * This document and associated source code (the "Work") is a part of a
8 * benchmark specification maintained by the TPC.
9 *
10 * The TPC reserves all right, title, and interest to the Work as provided
11 * under U.S. and international laws, including without limitation all patent
12 * and trademark rights therein.
13 *
14 * No Warranty
15 *
16 * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
17 * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
18 * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
19 * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
20 * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
21 * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
22 * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
23 * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
24 * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
25 * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
26 * WITH REGARD TO THE WORK.
27 * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
28 * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
29 * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
30 * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
31 * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
32 * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
33 * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
34 * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
35 *
36 * Contributors
37 * - Doug Johnson
38 */
39
40#include <deque>
41#include <sstream>
42#include <string>
43#include <vector>
44#include <stdexcept>
45
46#include "ITextSplitter.h"
47#include "ShrinkToFit.h"
48
49namespace TPCE {
50//
51// Description:
52// A template class for converting a series of text records into a binary
53// in-memory structure for quick easy access.
54//
55// Exception Safety:
56// The Basic guarantee is provided.
57//
58// Copy Behavior:
59// Copying is allowed.
60//
61template <class RecordType> class DataFile {
62private:
63 typedef std::vector<RecordType> Records; // For convenience and readability
64 Records records;
65
66public:
67 // Leverage the size type of our underlying storage container but
68 // insulate clients from the implementation particulars by creating
69 // our own type.
70 typedef typename Records::size_type size_type;
71
72 explicit DataFile(ITextSplitter &splitter) {
73 // eof only returns true after trying to read the end, so
74 // "prime the pump" by doing an initial read.
75 std::deque<std::string> fields = splitter.getNextRecord();
76
77 // Process each record.
78 while (!splitter.eof()) {
79 if (1 == fields.size() && "" == fields[0]) {
80 // We found a blank line so skip it and move on.
81 fields = splitter.getNextRecord();
82 continue;
83 }
84
85 // Add the record.
86 records.push_back(RecordType(fields));
87
88 // Move on to the next record.
89 fields = splitter.getNextRecord();
90 }
91
92 // Now that everything has been loaded tighten up our storage.
93 shrink_to_fit<Records>(records);
94 // records.shrink_to_fit(); // C++11
95 }
96
97 //
98 // Default copies and destructor are ok.
99 //
100 // ~DataFile();
101 // DataFile(const DataFile&);
102 // DataFile& operator=(const DataFile&);
103 //
104
105 size_type size() const {
106 return records.size();
107 }
108
109 // Provide un-checked const access to the records.
110 const RecordType &operator[](size_type idx) const {
111 return records[idx];
112 }
113
114 // Provide range-checked const access to the records.
115 const RecordType &at(size_type idx) const {
116 return records.at(idx);
117 }
118};
119
120} // namespace TPCE
121#endif // DATA_FILE_H
122