-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcelIterator.cpp
More file actions
218 lines (198 loc) · 5.88 KB
/
ExcelIterator.cpp
File metadata and controls
218 lines (198 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <Foundation/StdLogger.h>
#include <Foundation/Memory.h>
#include "ExcelIterator.h"
#include "Excel.h"
namespace Excel
{
using RowIterator = Foundation::Iterator<CellRow>;
using RowIteratorPtr = std::shared_ptr<RowIterator>;
struct CellRowIterator::Impl
{
FileIteratorPtr fileIter;
Book currentBook;
Sheet::Iterator sheetIter;
std::string sheetPattern;
std::string cellPattern;
std::string namePattern;
RowIteratorPtr rowIter;
Impl(FileIteratorPtr xlFileSource, const std::string& sheetPattern_)
: fileIter(std::move(xlFileSource))
, sheetPattern(sheetPattern_)
{}
Foundation::LoggerPtr log = Foundation::GetLogger("CellRowIterator");
};
CellRowIterator::CellRowIterator(FileIteratorPtr xlFileSource, const std::string& sheetPattern)
: m_impl(std::make_shared<Impl>(std::move(xlFileSource), sheetPattern))
{
}
void CellRowIterator::PutCellPattern(const std::string& cells)
{
LOG4CXX_DEBUG(m_impl->log, "PutCellPattern: " << cells);
m_impl->cellPattern = cells;
}
void CellRowIterator::PutNamePattern(const std::string& names)
{
LOG4CXX_DEBUG(m_impl->log, "PutNamePattern: " << names);
m_impl->namePattern = names;
}
/// Is this iterator beyond the end or before the start?
bool CellRowIterator::Off() const { return m_item.empty(); }
/// Move to the first item
void CellRowIterator::Start()
{
LOG4CXX_TRACE(m_impl->log, "Start:");
m_item.clear();
m_impl->fileIter->Start();
StartBook();
}
/// Move to the next item. Precondition: !Off()
void CellRowIterator::Forth()
{
m_item.clear();
m_impl->rowIter->Forth();
if (!m_impl->rowIter->Off())
SetItem();
else if (m_impl->namePattern.empty())
{
m_impl->sheetIter.Forth();
if (!StartSheet())
{
m_impl->fileIter->Forth();
StartBook();
}
}
else
{
m_impl->fileIter->Forth();
StartBook();
}
}
/// Load the first sheet in the current file.
bool CellRowIterator::StartBook()
{
LOG4CXX_TRACE(m_impl->log, "StartBook: ");
while (!m_impl->fileIter->Off())
{
m_impl->currentBook.Unload();
LOG4CXX_DEBUG(m_impl->log, "StartBook: " << m_impl->fileIter->Item() << " heapUsed " << HeapUsed());
if (m_impl->currentBook.Load(m_impl->fileIter->Item()))
{
if (!m_impl->namePattern.empty())
{
LOG4CXX_DEBUG(m_impl->log, "StartBook: " << " name " << m_impl->namePattern);
auto name = m_impl->currentBook.GetName(m_impl->namePattern);
if (name.IsValid())
{
m_impl->rowIter = std::make_shared<Range::Iterator>(name);
if (SetItem())
return true;
}
else
{
LOG4CXX_WARN(m_impl->log, "Failed to find " << m_impl->namePattern << " in " << m_impl->fileIter->Item());
}
}
else
{
m_impl->sheetIter.Start(m_impl->currentBook, m_impl->sheetPattern);
if (StartSheet())
return true;
}
}
else
{
LOG4CXX_WARN(m_impl->log, "Failed to load " << m_impl->fileIter->Item());
}
m_impl->fileIter->Forth();
}
return false;
}
/// Set m_impl->rowIter to a sheet row
bool CellRowIterator::StartSheet()
{
LOG4CXX_TRACE(m_impl->log, "StartSheet: ");
while (!m_impl->sheetIter.Off())
{
LOG4CXX_DEBUG(m_impl->log, "StartSheet: " << m_impl->sheetIter.Item().GetName());
m_impl->rowIter = std::make_shared<Rows::Iterator>(m_impl->sheetIter.Item());
if (!m_impl->rowIter->Off() && SetItem())
return true;
m_impl->sheetIter.Forth();
}
return false;
}
/// Set m_item. Precondition: !m_impl->rowIter->Off()
bool CellRowIterator::SetItem()
{
m_item = m_impl->rowIter->Item();
LOG4CXX_TRACE(m_impl->log, "At: " << m_impl->rowIter->Item());
return !m_item.empty();
}
struct SheetIterator::Impl
{
FileIteratorPtr fileIter;
Book currentBook;
Sheet::Iterator sheetIter;
Foundation::LoggerPtr log;
Impl(FileIteratorPtr xlFileSource)
: fileIter(std::move(xlFileSource))
, log(Foundation::GetLogger("SheetIterator"))
{}
};
SheetIterator::SheetIterator(FileIteratorPtr xlFileSource)
: m_impl(std::make_shared<Impl>(std::move(xlFileSource)))
{
}
/// Is this iterator beyond the end or before the start?
bool SheetIterator::Off() const { return m_item.empty(); }
/// Move to the first item
void SheetIterator::Start()
{
m_impl->fileIter->Start();
StartBook();
}
/// Move to the next item. Precondition: !Off()
void SheetIterator::Forth()
{
m_impl->sheetIter.Forth();
if (m_impl->sheetIter.Off())
{
m_impl->fileIter->Forth();
StartBook();
}
else
SetItem();
}
/// Get the first sheet in the current file.
void SheetIterator::StartBook()
{
m_item.clear();
while (!m_impl->fileIter->Off())
{
m_impl->currentBook.Unload();
LOG4CXX_DEBUG(m_impl->log, "StartBook: " << m_impl->fileIter->Item());
if (m_impl->currentBook.Load(m_impl->fileIter->Item()))
{
m_impl->sheetIter.Start(m_impl->currentBook);
if (!m_impl->sheetIter.Off())
{
m_item.push_back(m_impl->fileIter->Item().string());
m_item.push_back(m_impl->sheetIter.Item().GetName());
break;
}
}
else
{
LOG4CXX_WARN(m_impl->log, "Failed to load " << m_impl->fileIter->Item());
}
m_impl->fileIter->Forth();
}
}
/// Set m_item. Precondition: !m_impl->sheetIter.Off()
void SheetIterator::SetItem()
{
auto name = m_impl->sheetIter.Item().GetName();
LOG4CXX_DEBUG(m_impl->log, "At: " << name);
m_item.back() = name;
}
} // namespace Excel