forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkOpenFOAMReader.cxx
8611 lines (7983 loc) · 260 KB
/
vtkOpenFOAMReader.cxx
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenFOAMReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// Thanks to Terry Jordan of SAIC at the National Energy
// Technology Laboratory who developed this class.
// Please address all comments to Terry Jordan ([email protected])
//
// Token-based FoamFile format lexer/parser,
// performance/stability/compatibility enhancements, gzipped file
// support, lagrangian field support, variable timestep support,
// builtin cell-to-point filter, pointField support, polyhedron
// decomposition support, OF 1.5 extended format support, multiregion
// support, old mesh format support, parallelization support for
// decomposed cases in conjunction with vtkPOpenFOAMReader, et. al. by
// Takuya Oshima of Niigata University, Japan ([email protected])
//
// * GUI Based selection of mesh regions and fields available in the case
// * Minor bug fixes / Strict memory allocation checks
// * Minor performance enhancements
// by Philippose Rajan ([email protected])
// Hijack the CRC routine of zlib to omit CRC check for gzipped files
// (on OSes other than Windows where the mechanism doesn't work due
// to pre-bound DLL symbols) if set to 1, or not (set to 0). Affects
// performance by about 3% - 4%.
#define VTK_FOAMFILE_OMIT_CRCCHECK 0
// The input/output buffer sizes for zlib in bytes.
#define VTK_FOAMFILE_INBUFSIZE (16384)
#define VTK_FOAMFILE_OUTBUFSIZE (131072)
#define VTK_FOAMFILE_INCLUDE_STACK_SIZE (10)
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#if VTK_FOAMFILE_OMIT_CRCCHECK
#define ZLIB_INTERNAL
#endif
// for possible future extension of linehead-aware directives
#define VTK_FOAMFILE_RECOGNIZE_LINEHEAD 0
#include "vtkOpenFOAMReader.h"
#include <vtkstd/vector>
#include "vtksys/DateStamp.h"
#include "vtksys/SystemTools.hxx"
#include <vtksys/ios/sstream>
#include "vtk_zlib.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkCharArray.h"
#include "vtkCollection.h"
#include "vtkConvexPointSet.h"
#include "vtkDataArraySelection.h"
#include "vtkDirectory.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkHexahedron.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolygon.h"
#include "vtkPyramid.h"
#include "vtkQuad.h"
#include "vtkSortDataArray.h"
#include "vtkStdString.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStringArray.h"
#include "vtkTetra.h"
#include "vtkTriangle.h"
#include "vtkUnstructuredGrid.h"
#include "vtkVertex.h"
#include "vtkWedge.h"
#if !(defined(_WIN32) && !defined(__CYGWIN__) || defined(__LIBCATAMOUNT__))
// for getpwnam() / getpwuid()
#include <sys/types.h>
#include <pwd.h>
// for getuid()
#include <unistd.h>
#endif
// for fabs()
#include <math.h>
// for isalnum() / isspace() / isdigit()
#include <ctype.h>
#if VTK_FOAMFILE_OMIT_CRCCHECK
uLong ZEXPORT crc32(uLong, const Bytef *, uInt)
{ return 0; }
#endif
vtkStandardNewMacro(vtkOpenFOAMReader);
// forward declarations
template <typename T> struct vtkFoamArrayVector
: public vtkstd::vector<T *>
{
private:
typedef vtkstd::vector<T *> Superclass;
public:
~vtkFoamArrayVector()
{
for(size_t arrayI = 0; arrayI < Superclass::size(); arrayI++)
{
if(Superclass::operator[](arrayI))
{
Superclass::operator[](arrayI)->Delete();
}
}
}
};
typedef vtkFoamArrayVector<vtkIntArray> vtkFoamIntArrayVector;
typedef vtkFoamArrayVector<vtkFloatArray> vtkFoamFloatArrayVector;
struct vtkFoamIntVectorVector;
struct vtkFoamError;
struct vtkFoamToken;
struct vtkFoamFileStack;
struct vtkFoamFile;
struct vtkFoamIOobject;
template <typename T> struct vtkFoamReadValue;
struct vtkFoamEntryValue;
struct vtkFoamEntry;
struct vtkFoamDict;
//-----------------------------------------------------------------------------
// class vtkOpenFOAMReaderPrivate
// the reader core of vtkOpenFOAMReader
class VTK_IO_EXPORT vtkOpenFOAMReaderPrivate : public vtkObject
{
public:
static vtkOpenFOAMReaderPrivate *New();
vtkTypeMacro(vtkOpenFOAMReaderPrivate, vtkObject);
vtkDoubleArray *GetTimeValues()
{return this->TimeValues;}
vtkGetMacro(TimeStep, int);
vtkSetMacro(TimeStep, int);
const vtkStdString &GetRegionName() const
{return this->RegionName;}
// gather timestep information
bool MakeInformationVector(const vtkStdString &, const vtkStdString &,
const vtkStdString &, vtkOpenFOAMReader *);
// read mesh/fields and create dataset
int RequestData(vtkMultiBlockDataSet *, bool, bool, bool);
void SetTimeValue(const double);
int MakeMetaDataAtTimeStep(vtkStringArray *, vtkStringArray *,
vtkStringArray *, const bool);
void SetupInformation(const vtkStdString &, const vtkStdString &,
const vtkStdString &, vtkOpenFOAMReaderPrivate *);
private:
struct vtkFoamBoundaryEntry
{
enum bt
{
PHYSICAL = 1, // patch, wall
PROCESSOR = 2, // processor
GEOMETRICAL = 0 // symmetryPlane, wedge, cyclic, empty, etc.
};
vtkStdString BoundaryName;
int NFaces, StartFace, AllBoundariesStartFace;
bool IsActive;
bt BoundaryType;
};
struct vtkFoamBoundaryDict : public vtkstd::vector<vtkFoamBoundaryEntry>
{
// we need to keep the path to time directory where the current mesh
// is read from, since boundaryDict may be accessed multiple times
// at a timestep for patch selections
vtkStdString TimeDir;
};
vtkOpenFOAMReader *Parent;
// case and region
vtkStdString CasePath;
vtkStdString RegionName;
vtkStdString ProcessorName;
// time information
vtkDoubleArray *TimeValues;
int TimeStep;
int TimeStepOld;
vtkStringArray *TimeNames;
int InternalMeshSelectionStatus;
int InternalMeshSelectionStatusOld;
// filenames / directories
vtkStringArray *VolFieldFiles;
vtkStringArray *PointFieldFiles;
vtkStringArray *LagrangianFieldFiles;
vtkStringArray *PolyMeshPointsDir;
vtkStringArray *PolyMeshFacesDir;
// for mesh construction
vtkIdType NumCells;
vtkIdType NumPoints;
vtkIntArray *FaceOwner;
// for cell-to-point interpolation
vtkPolyData *AllBoundaries;
vtkIntArray *AllBoundariesPointMap;
vtkIntArray *InternalPoints;
// for caching mesh
vtkUnstructuredGrid *InternalMesh;
vtkMultiBlockDataSet *BoundaryMesh;
vtkFoamIntArrayVector *BoundaryPointMap;
vtkFoamBoundaryDict BoundaryDict;
vtkMultiBlockDataSet *PointZoneMesh;
vtkMultiBlockDataSet *FaceZoneMesh;
vtkMultiBlockDataSet *CellZoneMesh;
// for polyhedra handling
int NumTotalAdditionalCells;
vtkIntArray *AdditionalCellIds;
vtkIntArray *NumAdditionalCells;
vtkFoamIntArrayVector *AdditionalCellPoints;
// constructor and destructor are kept private
vtkOpenFOAMReaderPrivate();
~vtkOpenFOAMReaderPrivate();
// not implemented.
vtkOpenFOAMReaderPrivate(const vtkOpenFOAMReaderPrivate &);
void operator=(const vtkOpenFOAMReaderPrivate &);
// clear mesh construction
void ClearInternalMeshes();
void ClearBoundaryMeshes();
void ClearMeshes();
vtkStdString RegionPath() const
{return (this->RegionName == "" ? "" : "/") + this->RegionName;}
vtkStdString TimePath(const int timeI) const
{return this->CasePath + this->TimeNames->GetValue(timeI);}
vtkStdString TimeRegionPath(const int timeI) const
{return this->TimePath(timeI) + this->RegionPath();}
vtkStdString CurrentTimePath() const
{return this->TimePath(this->TimeStep);}
vtkStdString CurrentTimeRegionPath() const
{return this->TimeRegionPath(this->TimeStep);}
vtkStdString CurrentTimeRegionMeshPath(vtkStringArray *dir) const
{return this->CasePath + dir->GetValue(this->TimeStep) + this->RegionPath()
+ "/polyMesh/";}
vtkStdString RegionPrefix() const
{return this->RegionName + (this->RegionName == "" ? "" : "/");}
// search time directories for mesh
void AppendMeshDirToArray(vtkStringArray *, const vtkStdString &, const int);
void PopulatePolyMeshDirArrays();
// search a time directory for field objects
void GetFieldNames(const vtkStdString &, const bool, vtkStringArray *,
vtkStringArray *);
void SortFieldFiles(vtkStringArray *, vtkStringArray *, vtkStringArray *);
void LocateLagrangianClouds(vtkStringArray *, const vtkStdString &);
// read controlDict
bool ListTimeDirectoriesByControlDict(vtkFoamDict *dict);
bool ListTimeDirectoriesByInstances();
// read mesh files
vtkFloatArray* ReadPointsFile();
vtkFoamIntVectorVector* ReadFacesFile (const vtkStdString &);
vtkFoamIntVectorVector* ReadOwnerNeighborFiles(const vtkStdString &,
vtkFoamIntVectorVector *);
bool CheckFacePoints(vtkFoamIntVectorVector *);
// create mesh
void InsertCellsToGrid(vtkUnstructuredGrid *, const vtkFoamIntVectorVector *,
const vtkFoamIntVectorVector *, vtkFloatArray *, vtkIdTypeArray *,
vtkIntArray *);
vtkUnstructuredGrid *MakeInternalMesh(const vtkFoamIntVectorVector *,
const vtkFoamIntVectorVector *, vtkFloatArray *);
void InsertFacesToGrid(vtkPolyData *, const vtkFoamIntVectorVector *, int,
int, vtkIntArray *, vtkIdList *, vtkIntArray *, const bool);
template <typename T1, typename T2> bool ExtendArray(T1 *, const int);
vtkMultiBlockDataSet* MakeBoundaryMesh(const vtkFoamIntVectorVector *,
vtkFloatArray *);
void SetBlockName(vtkMultiBlockDataSet *, unsigned int, const char *);
void TruncateFaceOwner();
// move additional points for decomposed cells
vtkPoints *MoveInternalMesh(vtkUnstructuredGrid *, vtkFloatArray *);
void MoveBoundaryMesh(vtkMultiBlockDataSet *, vtkFloatArray *);
// cell-to-point interpolator
void InterpolateCellToPoint(vtkFloatArray *, vtkFloatArray *, vtkPointSet *,
vtkIntArray *, const int);
// read and create cell/point fields
void ConstructDimensions(vtkStdString *, vtkFoamDict *);
bool ReadFieldFile(vtkFoamIOobject *, vtkFoamDict *, const vtkStdString &,
vtkDataArraySelection *);
vtkFloatArray *FillField(vtkFoamEntry *, int, vtkFoamIOobject *,
const vtkStdString &);
void GetVolFieldAtTimeStep(vtkUnstructuredGrid *, vtkMultiBlockDataSet *,
const vtkStdString &);
void GetPointFieldAtTimeStep(vtkUnstructuredGrid *, vtkMultiBlockDataSet *,
const vtkStdString &);
void AddArrayToFieldData(vtkDataSetAttributes *, vtkDataArray *,
const vtkStdString &);
// create lagrangian mesh/fields
vtkMultiBlockDataSet *MakeLagrangianMesh();
// create point/face/cell zones
vtkFoamDict *GatherBlocks(const char *, bool);
bool GetPointZoneMesh(vtkMultiBlockDataSet *, vtkPoints *);
bool GetFaceZoneMesh(vtkMultiBlockDataSet *, const vtkFoamIntVectorVector *,
vtkPoints *);
bool GetCellZoneMesh(vtkMultiBlockDataSet *, const vtkFoamIntVectorVector *,
const vtkFoamIntVectorVector *, vtkPoints *);
};
vtkStandardNewMacro(vtkOpenFOAMReaderPrivate);
//-----------------------------------------------------------------------------
// struct vtkFoamIntVectorVector
struct vtkFoamIntVectorVector
{
private:
vtkIntArray *Indices, *Body;
vtkFoamIntVectorVector();
public:
~vtkFoamIntVectorVector()
{
this->Indices->Delete();
this->Body->Delete();
}
vtkFoamIntVectorVector(const vtkFoamIntVectorVector &ivv) :
Indices(ivv.Indices), Body(ivv.Body)
{
this->Indices->Register(0); // vtkDataArrays do not have ShallowCopy
this->Body->Register(0);
}
vtkFoamIntVectorVector(const int nElements, const int bodyLength) :
Indices(vtkIntArray::New()), Body(vtkIntArray::New())
{
this->Indices->SetNumberOfValues(nElements + 1);
this->Body->SetNumberOfValues(bodyLength);
}
// GetSize() returns all allocated size (Size) while GetDataSize()
// returns actually used size (MaxId * nComponents)
int GetBodySize() const
{
return this->Body->GetSize();
}
// note that vtkIntArray::Resize() allocates (current size + new
// size) bytes if current size < new size
void ResizeBody(const int bodyLength)
{
this->Body->Resize(bodyLength);
}
int *SetIndex(const int i, const int bodyI)
{
return this->Body->GetPointer(*this->Indices->GetPointer(i) = bodyI);
}
void SetValue(const int bodyI, int value)
{
this->Body->SetValue(bodyI, value);
}
const int *operator[](const int i) const
{
return this->Body->GetPointer(this->Indices->GetValue(i));
}
int GetSize(const int i) const
{
return this->Indices->GetValue(i + 1) - this->Indices->GetValue(i);
}
int GetNumberOfElements() const
{
return this->Indices->GetNumberOfTuples() - 1;
}
vtkIntArray *GetIndices()
{
return this->Indices;
}
vtkIntArray *GetBody()
{
return this->Body;
}
};
//-----------------------------------------------------------------------------
// class vtkFoamError
// class for exception-carrying object
struct vtkFoamError : public vtkStdString
{
private:
typedef vtkStdString Superclass;
public:
vtkFoamError() :
vtkStdString()
{
}
vtkFoamError(const vtkFoamError& e) :
vtkStdString(e)
{
}
~vtkFoamError()
{
}
// a super-easy way to make use of operator<<()'s defined in
// vtksys_ios::ostringstream class
template <class T> vtkFoamError& operator<<(const T& t)
{
vtksys_ios::ostringstream os;
os << t;
this->Superclass::operator+=(os.str());
return *this;
}
};
//-----------------------------------------------------------------------------
// class vtkFoamToken
// token class which also works as container for list types
// - a word token is treated as a string token for simplicity
// - handles only atomic types. Handling of list types are left to the
// derived classes.
struct vtkFoamToken
{
public:
enum tokenType
{
// undefined type
UNDEFINED,
// atomic types
PUNCTUATION, LABEL, SCALAR, STRING, IDENTIFIER,
// vtkObject-derived list types
STRINGLIST, LABELLIST, SCALARLIST, VECTORLIST,
// original list types
LABELLISTLIST, ENTRYVALUELIST, EMPTYLIST, DICTIONARY,
// error state
TOKEN_ERROR
};
protected:
tokenType Type;
union
{
char Char;
int Int;
double Double;
vtkStdString* String;
vtkObjectBase *VtkObjectPtr;
// vtkObject-derived list types
vtkIntArray *LabelListPtr;
vtkFloatArray *ScalarListPtr, *VectorListPtr;
vtkStringArray *StringListPtr;
// original list types
vtkFoamIntVectorVector *LabelListListPtr;
vtkstd::vector<vtkFoamEntryValue*> *EntryValuePtrs;
vtkFoamDict *DictPtr;
};
void Clear()
{
if (this->Type == STRING || this->Type == IDENTIFIER)
{
delete this->String;
}
}
void AssignData(const vtkFoamToken& value)
{
switch (value.Type)
{
case PUNCTUATION:
this->Char = value.Char;
break;
case LABEL:
this->Int = value.Int;
break;
case SCALAR:
this->Double = value.Double;
break;
case STRING:
case IDENTIFIER:
this->String = new vtkStdString(*value.String);
break;
// required to suppress the 'enumeration value not handled' warning by
// g++ when compiled with -Wall
default:
break;
}
}
public:
vtkFoamToken() :
Type(UNDEFINED)
{
}
vtkFoamToken(const vtkFoamToken& value) :
Type(value.Type)
{
this->AssignData(value);
}
~vtkFoamToken()
{
this->Clear();
}
tokenType GetType() const
{
return this->Type;
}
template <typename T> bool Is() const;
template <typename T> T To() const;
#if defined(_MSC_VER)
// workaround for Win32-64ids-nmake70
VTK_TEMPLATE_SPECIALIZE bool Is<int>() const;
VTK_TEMPLATE_SPECIALIZE bool Is<float>() const;
VTK_TEMPLATE_SPECIALIZE bool Is<double>() const;
VTK_TEMPLATE_SPECIALIZE int To<int>() const;
VTK_TEMPLATE_SPECIALIZE float To<float>() const;
VTK_TEMPLATE_SPECIALIZE double To<double>() const;
#endif
// workaround for SunOS-CC5.6-dbg
int ToInt() const
{
return this->Int;
}
// workaround for SunOS-CC5.6-dbg
float ToFloat() const
{
return this->Type == LABEL ? this->Int : this->Double;
}
const vtkStdString ToString() const
{
return *this->String;
}
const vtkStdString ToIdentifier() const
{
return *this->String;
}
void SetBad()
{
this->Clear();
this->Type = TOKEN_ERROR;
}
void SetIdentifier(const vtkStdString& idString)
{
this->operator=(idString);
this->Type = IDENTIFIER;
}
void operator=(const char value)
{
this->Clear();
this->Type = PUNCTUATION;
this->Char = value;
}
void operator=(const int value)
{
this->Clear();
this->Type = LABEL;
this->Int = value;
}
void operator=(const double value)
{
this->Clear();
this->Type = SCALAR;
this->Double = value;
}
void operator=(const char *value)
{
this->Clear();
this->Type = STRING;
this->String = new vtkStdString(value);
}
void operator=(const vtkStdString& value)
{
this->Clear();
this->Type = STRING;
this->String = new vtkStdString(value);
}
void operator=(const vtkFoamToken& value)
{
this->Clear();
this->Type = value.Type;
this->AssignData(value);
}
bool operator==(const char value) const
{
return this->Type == PUNCTUATION && this->Char == value;
}
bool operator==(const int value) const
{
return this->Type == LABEL && this->Int == value;
}
bool operator==(const vtkStdString& value) const
{
return this->Type == STRING && *this->String == value;
}
bool operator!=(const vtkStdString& value) const
{
return this->Type != STRING || *this->String != value;
}
bool operator!=(const char value) const
{
return !this->operator==(value);
}
friend vtksys_ios::ostringstream& operator<<(vtksys_ios::ostringstream& str,
const vtkFoamToken& value)
{
switch (value.GetType())
{
case TOKEN_ERROR:
str << "badToken (an unexpected EOF?)";
break;
case PUNCTUATION:
str << value.Char;
break;
case LABEL:
str << value.Int;
break;
case SCALAR:
str << value.Double;
break;
case STRING:
case IDENTIFIER:
str << *value.String;
break;
// required to suppress the 'enumeration value not handled' warning by
// g++ when compiled with -Wall
default:
break;
}
return str;
}
};
VTK_TEMPLATE_SPECIALIZE inline bool vtkFoamToken::Is<int>() const
{
return this->Type == LABEL;
}
VTK_TEMPLATE_SPECIALIZE inline bool vtkFoamToken::Is<float>() const
{
return this->Type == LABEL || this->Type == SCALAR;
}
VTK_TEMPLATE_SPECIALIZE inline bool vtkFoamToken::Is<double>() const
{
return this->Type == SCALAR;
}
VTK_TEMPLATE_SPECIALIZE inline int vtkFoamToken::To<int>() const
{
return this->Int;
}
VTK_TEMPLATE_SPECIALIZE inline float vtkFoamToken::To<float>() const
{
return this->Type == LABEL ? this->Int : this->Double;
}
VTK_TEMPLATE_SPECIALIZE inline double vtkFoamToken::To<double>() const
{
return this->Type == LABEL ? this->Int : this->Double;
}
//-----------------------------------------------------------------------------
// class vtkFoamFileStack
// list of variables that have to be saved when a file is included.
struct vtkFoamFileStack
{
protected:
vtkStdString FileName;
FILE *File;
bool IsCompressed;
z_stream Z;
int ZStatus;
int LineNumber;
#if VTK_FOAMFILE_RECOGNIZE_LINEHEAD
bool WasNewline;
#endif
// buffer pointers. using raw pointers for performance reason.
unsigned char *Inbuf;
unsigned char *Outbuf;
unsigned char *BufPtr;
unsigned char *BufEndPtr;
vtkFoamFileStack() :
FileName(), File(NULL), IsCompressed(false), ZStatus(Z_OK), LineNumber(0),
#if VTK_FOAMFILE_RECOGNIZE_LINEHEAD
WasNewline(true),
#endif
Inbuf(NULL), Outbuf(NULL), BufPtr(NULL), BufEndPtr(NULL)
{
this->Z.zalloc = Z_NULL;
this->Z.zfree = Z_NULL;
this->Z.opaque = Z_NULL;
}
void Reset()
{
// this->FileName = "";
this->File = NULL;
this->IsCompressed = false;
// this->ZStatus = Z_OK;
this->Z.zalloc = Z_NULL;
this->Z.zfree = Z_NULL;
this->Z.opaque = Z_NULL;
// this->LineNumber = 0;
#if VTK_FOAMFILE_RECOGNIZE_LINEHEAD
this->WasNewline = true;
#endif
this->Inbuf = NULL;
this->Outbuf = NULL;
// this->BufPtr = NULL;
// this->BufEndPtr = NULL;
}
public:
const vtkStdString& GetFileName() const
{
return this->FileName;
}
int GetLineNumber() const
{
return this->LineNumber;
}
};
//-----------------------------------------------------------------------------
// class vtkFoamFile
// read and tokenize the input.
struct vtkFoamFile : public vtkFoamFileStack
{
private:
typedef vtkFoamFileStack Superclass;
public:
// #inputMode values
enum inputModes
{
INPUT_MODE_MERGE,
INPUT_MODE_OVERWRITE,
INPUT_MODE_PROTECT,
INPUT_MODE_WARN,
INPUT_MODE_ERROR
};
private:
bool Is13Positions;
inputModes InputMode;
// inclusion handling
vtkFoamFileStack *Stack[VTK_FOAMFILE_INCLUDE_STACK_SIZE];
int StackI;
vtkStdString CasePath;
// declare and define as private
vtkFoamFile();
bool InflateNext(unsigned char *buf, int requestSize);
int NextTokenHead();
// hacks to keep exception throwing / recursive codes out-of-line to make
// putBack(), getc() and readExpecting() inline expandable
void ThrowDuplicatedPutBackException();
void ThrowUnexpectedEOFException();
void ThrowUnexpectedNondigitCharExecption(const int c);
void ThrowUnexpectedTokenException(const char, const int c);
int ReadNext();
void PutBack(const int c)
{
if (--this->Superclass::BufPtr < this->Superclass::Outbuf)
{
this->ThrowDuplicatedPutBackException();
}
*this->Superclass::BufPtr = c;
}
// get a character
int Getc()
{
return this->Superclass::BufPtr == this->Superclass::BufEndPtr ? this->ReadNext()
: *this->Superclass::BufPtr++;
}
vtkFoamError StackString()
{
vtksys_ios::ostringstream os;
if (this->StackI > 0)
{
os << "\n included";
for (int stackI = this->StackI - 1; stackI >= 0; stackI--)
{
os << " from line " << this->Stack[stackI]->GetLineNumber() << " of "
<< this->Stack[stackI]->GetFileName() << "\n";
}
os << ": ";
}
return vtkFoamError() << os.str();
}
bool CloseIncludedFile()
{
if (this->StackI == 0)
{
return false;
}
this->Clear();
this->StackI--;
// use the default bitwise assignment operator
this->Superclass::operator=(*this->Stack[this->StackI]);
delete this->Stack[this->StackI];
return true;
}
void Clear()
{
if (this->Superclass::IsCompressed)
{
inflateEnd(&this->Superclass::Z);
}
delete [] this->Superclass::Inbuf;
delete [] this->Superclass::Outbuf;
this->Superclass::Inbuf = this->Superclass::Outbuf = NULL;
if (this->Superclass::File)
{
fclose(this->Superclass::File);
this->Superclass::File = NULL;
}
// don't reset the line number so that the last line number is
// retained after close
// lineNumber_ = 0;
}
//! Return file name (part beyond last /)
vtkStdString ExtractName(const vtkStdString& path) const
{
#if defined(_WIN32)
const vtkStdString pathFindSeparator = "/\\", pathSeparator = "\\";
#else
const vtkStdString pathFindSeparator = "/", pathSeparator = "/";
#endif
vtkStdString::size_type pos = path.find_last_of(pathFindSeparator);
if (pos == vtkStdString::npos)
{
// no slash
return path;
}
else if (pos+1 == path.size())
{
// final trailing slash
vtkStdString::size_type endPos = pos;
pos = path.find_last_of(pathFindSeparator, pos-1);
if (pos == vtkStdString::npos)
{
// no further slash
return path.substr(0, endPos);
}
else
{
return path.substr(pos + 1, endPos - pos - 1);
}
}
else
{
return path.substr(pos + 1, vtkStdString::npos);
}
}
//! Return directory path name (part before last /)
vtkStdString ExtractPath(const vtkStdString& path) const
{
#if defined(_WIN32)
const vtkStdString pathFindSeparator = "/\\", pathSeparator = "\\";
#else
const vtkStdString pathFindSeparator = "/", pathSeparator = "/";
#endif
const vtkStdString::size_type pos = path.find_last_of(pathFindSeparator);
return pos == vtkStdString::npos
? vtkStdString(".") + pathSeparator
: path.substr(0, pos + 1);
}
public:
vtkFoamFile(const vtkStdString& casePath) :
vtkFoamFileStack(), Is13Positions(false), InputMode(INPUT_MODE_ERROR),
StackI(0), CasePath(casePath)
{
}
~vtkFoamFile()
{
this->Close();
}
void SetIs13Positions(const bool is13Positions)
{
this->Is13Positions = is13Positions;
}
bool GetIs13Positions() const
{
return this->Is13Positions;
}
inputModes GetInputMode() const
{
return this->InputMode;
}
const vtkStdString GetCasePath() const
{
return this->CasePath;
}
const vtkStdString GetFilePath() const
{
return this->ExtractPath(this->FileName);
}
vtkStdString ExpandPath(const vtkStdString& pathIn,
const vtkStdString& defaultPath)
{
vtkStdString expandedPath;
bool isExpanded = false, wasPathSeparator = true;
const size_t nChars = pathIn.length();
for (size_t charI = 0; charI < nChars;)
{
char c = pathIn[charI];
switch (c)
{
case '$': // $-variable expansion
{
vtkStdString variable;
while (++charI < nChars && (isalnum(pathIn[charI]) || pathIn[charI]
== '_'))
{
variable += pathIn[charI];
}
if (variable == "FOAM_CASE") // discard path until the variable
{
expandedPath = this->CasePath;
wasPathSeparator = true;
isExpanded = true;
}
else if (variable == "FOAM_CASENAME")
{
// FOAM_CASENAME is the final directory name from CasePath
expandedPath += this->ExtractName(this->CasePath);
wasPathSeparator = false;
isExpanded = true;
}
else
{
const char *value = getenv(variable.c_str());
if (value != NULL)
{
expandedPath += value;
}
const vtkStdString::size_type len = expandedPath.length();
if (len > 0)
{
const char c2 = expandedPath[len - 1];
wasPathSeparator = (c2 == '/' || c2 == '\\');
}