-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDefinedRanges.cpp
373 lines (332 loc) · 8.08 KB
/
DefinedRanges.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// DefinedRanges.cpp: implementation of the CDefinedRanges class.
//
// $Id$
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "phreeqci2.h"
#include "DefinedRanges.h"
#include "TreeCtrlIn.h"
#include "KeywordParser.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDefinedRanges::CDefinedRanges(const CTreeCtrlNode& rSimNode, bool bSkipSaves/* = false */, bool bSkipCopys/* = false */)
{
CTreeCtrlNode simNode(rSimNode);
ASSERT(simNode && simNode.GetLevel() == CTreeCtrlIn::SimLevel);
if (simNode == 0)
{
// Avoid crash in Release builds
TRACE0("WARNING: NULL simNode passed to CDefinedRanges\n");
return;
}
CTreeCtrlNode fileNode = simNode.GetParent();
ASSERT(fileNode && fileNode.GetLevel() == CTreeCtrlIn::FileLevel);
// foreach sim inclusive
Phreeqc p;
p.do_initialize();
for (CTreeCtrlNode sim = fileNode.GetChild(); sim != 0; sim = sim.GetNextSibling())
{
int no_soln_num_count = 0;
// foreach key
for (CTreeCtrlNode key = sim.GetChild(); key != 0; key = key.GetNextSibling())
{
int imageID = key.GetImageID();
CString strSolnSpread;
CTreeCtrlNode sib;
switch (imageID)
{
case CTreeCtrlIn::equilibrium_phasesImage :
break;
case CTreeCtrlIn::exchangeImage :
break;
case CTreeCtrlIn::gas_phaseImage :
break;
case CTreeCtrlIn::kineticsImage :
break;
case CTreeCtrlIn::mixImage :
break;
case CTreeCtrlIn::reactionImage :
break;
case CTreeCtrlIn::reaction_temperatureImage :
break;
case CTreeCtrlIn::solid_solutionsImage :
break;
case CTreeCtrlIn::solutionImage :
break;
case CTreeCtrlIn::surfaceImage :
break;
case CTreeCtrlIn::saveImage :
if (bSkipSaves) continue;
break;
case CTreeCtrlIn::solution_spreadImage :
break;
case CTreeCtrlIn::reaction_pressureImage :
break;
default:
// ignore all other keywords
continue;
break;
}
int n_user, n_user_end;
char *description = 0;
CString strKey = key.GetText();
// special case for SAVE
if (imageID == CTreeCtrlIn::saveImage)
{
ASSERT(strKey.GetLength() > (int)::strlen("SAVE"));
strKey = strKey.Mid(::strlen("SAVE"));
strKey.TrimLeft();
}
p.read_number_description(strKey.GetBuffer(strKey.GetLength() + 1),
&n_user, &n_user_end, &description);
p.PHRQ_free(description);
CTreeCtrlIn::ImageIndex imageIndex = static_cast<CTreeCtrlIn::ImageIndex>(imageID);
CKeyword::type nType = CTreeCtrlIn::GetKeywordType(imageIndex);
// special case for SAVE
if (imageID == CTreeCtrlIn::saveImage)
{
CString strSaved = strKey.SpanExcluding(_T(" \t"));
nType = CKeyword::GetKeywordType(strSaved);
ASSERT(nType != CKeyword::K_NOT_KEYWORD);
}
else if (imageID == CTreeCtrlIn::solution_spreadImage)
{
strSolnSpread = CKeywordParser::GetInverseTreeString(key.GetText()) + " \n";
for (CTreeCtrlNode sib = key.GetChild(); sib != 0; sib = sib.GetNextSibling())
{
strSolnSpread += CKeywordParser::GetInverseTreeString(sib.GetText()) + " \n";
}
no_soln_num_count += ReadSolutionSpread(strSolnSpread);
continue; // skip insert
}
(*this)[nType].insert(CRange(n_user, n_user_end));
}
if (no_soln_num_count > 0)
{
int max_soln = 0;
std::map< CKeyword::type, std::set<CRange> >::iterator iter = this->find(CKeyword::K_SOLUTION);
for (; iter != this->end(); ++iter)
{
std::set<CRange>::iterator si = iter->second.begin();
for (; si != iter->second.end(); ++si)
{
max_soln = max(max_soln, si->nMax);
}
}
(*this)[CKeyword::K_SOLUTION].insert(CRange(max_soln + 1, max_soln + no_soln_num_count));
}
//
// process previous sim for copies
//
CTreeCtrlNode prevSim = sim.GetPrevSibling();
if (!bSkipCopys && prevSim)
{
// foreach key
for (CTreeCtrlNode key = prevSim.GetChild(); key != 0; key = key.GetNextSibling())
{
if (key.GetImageID() != CTreeCtrlIn::copyImage) continue;
int n_user, n_user_end;
char *description;
CString strKey = key.GetText();
// strip off "COPY" keyword
ASSERT(strKey.GetLength() > (int)::strlen("COPY"));
strKey = strKey.Mid(::strlen("COPY"));
strKey.TrimLeft();
// read keyword
p.read_number_description(strKey.GetBuffer(strKey.GetLength() + 1),
&n_user, &n_user_end, &description);
ASSERT(n_user == n_user_end); // COPY does not accept a range of numbers for source index
ASSERT(description);
if (description)
{
CString strRange(_T("ignored_token "));
strRange += description;
p.PHRQ_free(description);
p.read_number_description(strRange.GetBuffer(strRange.GetLength() + 1),
&n_user, &n_user_end, &description);
p.PHRQ_free(description);
CKeyword::type nType = CKeyword::GetKeywordType(strKey.SpanExcluding(_T(" \t")));
ASSERT(nType != CKeyword::K_NOT_KEYWORD || strKey.SpanExcluding(_T(" \t")).CompareNoCase("cell") == 0);
if (nType != CKeyword::K_NOT_KEYWORD)
{
(*this)[nType].insert(CRange(n_user, n_user_end));
}
}
}
}
if (sim == simNode) break;
}
}
CDefinedRanges::~CDefinedRanges()
{
}
int CDefinedRanges::ReadSolutionSpread(CString& rStr)
{
try
{
PhreeqcI phreeqci(rStr);
return phreeqci.GetDefinedRanges(this);
}
catch (...)
{
CString strResource;
strResource.LoadString(IDS_EXCEPTION_ACCESS_VIOLATION);
::MessageBox(NULL, strResource, _T("Unhandled Exception"), MB_OK|MB_ICONERROR);
return 0;
}
}
//////////////////////////////////////////////////////////////////////
// CRange Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRange::CRange()
: nMin(-1), nMax(-1)
{
ASSERT(nMin <= nMax);
}
CRange::CRange(int nStart, int nEnd)
: nMin(nStart), nMax(nEnd)
{
ASSERT(nMin <= nMax);
}
CRange::CRange(LPCTSTR lpsz)
: nMin(-1), nMax(-1)
{
if (lpsz == 0 || _tcslen(lpsz) == 0)
return;
CString strRange(lpsz);
switch (strRange.Replace(_T("-"), _T(" ")))
{
case 0:
{
int min;
if (_stscanf(strRange, "%d", &min) == 1)
{
nMin = min;
nMax = min;
}
}
break;
case 1:
{
int min, max;
if (::_stscanf(strRange, "%d %d", &min, &max) == 2)
{
nMin = min;
nMax = max;
}
}
break;
default:
// Invalid range
break;
}
}
CRange::~CRange()
{
}
bool CRange::operator<(const CRange& rRange) const
{
if (this->nMin == rRange.nMin)
{
return (this->nMax < rRange.nMax);
}
else
{
return (this->nMin < rRange.nMin);
}
}
CRange CRange::Remove(int nVal)
{
if (IsEmpty() || !IsValid())
{
return CRange();
}
if (nMin == nMax)
{
if (nMin == nVal)
{
nMin = nMax = -1;
}
return CRange();
}
else
{
if (!Contains(nVal))
{
// nothing to do
return CRange();
}
else
{
if (nVal == nMax) // check endpoint
{
nMax--;
ASSERT(nMin <= nMax);
return CRange(); // return empty range
}
else
{
CRange range(*this);
if (nVal == nMin) // check beginpoint
{
range.nMin++;
nMin = nMax = -1; // set (*this) to empty
ASSERT(range.nMin <= range.nMax);
}
else
{
nMax = nVal - 1;
range.nMin = nVal + 1;
}
return range;
}
}
}
}
bool CRange::IsEmpty() const
{
return (nMin == -1 && nMax == -1);
}
bool CRange::IsValid() const
{
return (nMin != -1 && nMax != -1 && nMin <= nMax);
}
bool CRange::Contains(int nVal) const
{
return (IsValid() && (nMin <= nVal && nVal <= nMax));
}
CString CRange::toCString() const
{
CString strRange;
if (IsEmpty())
{
strRange = _T("Empty Range");
}
if (!IsValid())
{
strRange = _T("Invalid Range");
}
if (nMin != nMax)
{
strRange.Format(_T("%d-%d"), nMin, nMax);
}
else
{
strRange.Format(_T("%d"), nMin);
}
return strRange;
}
std::string CRange::toString() const
{
std::string sRange = (LPCTSTR)toCString();
return sRange;
}