forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkRTXMLPolyDataReader.cxx
226 lines (196 loc) · 6.18 KB
/
vtkRTXMLPolyDataReader.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkRTXMLPolyDataReader.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.
=========================================================================*/
#include "vtkRTXMLPolyDataReader.h"
#include "vtkObjectFactory.h"
#include "vtkDirectory.h"
#include <vtkstd/vector>
#include <vtkstd/string>
vtkStandardNewMacro(vtkRTXMLPolyDataReader);
class vtkRTXMLPolyDataReaderInternals
{
public:
vtkstd::vector<vtkstd::string> AvailableDataFileList;
vtkstd::vector<vtkstd::string> ProcessedFileList;
};
//----------------------------------------------------------------------------
vtkRTXMLPolyDataReader::vtkRTXMLPolyDataReader():vtkXMLPolyDataReader()
{
this->Internal = new vtkRTXMLPolyDataReaderInternals;
this->DataLocation = NULL;
}
//----------------------------------------------------------------------------
vtkRTXMLPolyDataReader::~vtkRTXMLPolyDataReader()
{
delete this->Internal;
this->SetDataLocation(0);
}
void vtkRTXMLPolyDataReader::SetLocation(const char* dataLocation)
{
this->SetDataLocation(dataLocation);
this->ResetReader();
}
//----------------------------------------------------------------------------
void vtkRTXMLPolyDataReader::UpdateToNextFile()
{
if (this->Internal->AvailableDataFileList.size() > 0)
{
// set the Reader to read the new available data file
char* fullname = const_cast<char*> (
this->Internal->AvailableDataFileList[0].c_str()
);
this->SetFileName(fullname);
this->Internal->ProcessedFileList.push_back(
this->Internal->AvailableDataFileList[0]
);
this->Internal->AvailableDataFileList.erase(
this->Internal->AvailableDataFileList.begin()
);
this->Update();
this->Modified();
}
}
//----------------------------------------------------------------------------
const char* vtkRTXMLPolyDataReader::GetNextFileName()
{
if (this->Internal->AvailableDataFileList.size() > 0)
{
return this->Internal->AvailableDataFileList[0].c_str();
}
else
{
return NULL;
}
}
//----------------------------------------------------------------------------
int vtkRTXMLPolyDataReader::NewDataAvailable()
{
// TODO: data concurrency issue
// what if data is available partly on the file system
// i.e. a data writer is writing the file, but not done yet
// enforce the writer to obtain a "flock" is too restrictive, is it?
// no data directory is specified, use the current dir
if (!this->DataLocation)
{
this->InitializeToCurrentDir();
return VTK_ERROR;
}
// now the reader should be initialized already
if (this->Internal->AvailableDataFileList.size() > 0)
{
return VTK_OK;
}
vtkDirectory *dataDir = vtkDirectory::New();
dataDir->Open(this->DataLocation);
// now check if there are new files available
// and place them into the AvailableDataFileList
int current = dataDir->GetNumberOfFiles();
int processed = static_cast<int>(this->Internal->ProcessedFileList.size());
if (current > processed)
{
for (int i=0; i<current; i++)
{
char* file = this->GetDataFileFullPathName(dataDir->GetFile(i));
if ( ! IsProcessed(file) )
{
this->Internal->AvailableDataFileList.push_back(file);
}
else
{
delete [] file;
}
}
dataDir->Delete();
return VTK_OK;
}
else
{
dataDir->Delete();
return VTK_ERROR;
}
}
// Description:
// Internal method to return the fullpath name of a file, which
// is the concatenation of "this->DataLocation" and "name"
// caller has to free the memory of returned fullpath name.
//----------------------------------------------------------------------
char* vtkRTXMLPolyDataReader::GetDataFileFullPathName(const char* name)
{
char* fullpath;
int n = static_cast<int>(strlen(this->DataLocation));
int m = static_cast<int>(strlen(name));
fullpath = new char[n+m+2];
strcpy(fullpath,this->DataLocation);
#if defined (_WIN32) // WINDOW style path
if (fullpath[n-1] != '/' && fullpath[n-1] != '\\')
{
#if !defined(__CYGWIN__)
fullpath[n++] = '\\';
#else
fullpath[n++] = '/';
#endif
}
#else // POSIX style path
if (fullpath[n-1] != '/')
{
fullpath[n++] = '/';
}
#endif //
strcpy(&fullpath[n],name);
return fullpath;
}
void vtkRTXMLPolyDataReader::InitializeToCurrentDir()
{
// TODO: need to add support for other platform like windows
this->SetLocation("./");
}
//--------------------------------------------------------------------------
int vtkRTXMLPolyDataReader::IsProcessed(const char* fname)
{
int size = static_cast<int>(this->Internal->ProcessedFileList.size());
for (int i=0 ; i<size ; i++)
{
const char* aFile = this->Internal->ProcessedFileList[i].c_str();
if ( strcmp(fname,aFile) == 0 )
{
return 1;
}
}
return 0;
}
//----------------------------------------------------------------------------
void vtkRTXMLPolyDataReader::ResetReader()
{
// assume the DataLocation is set at this point
// clean up the two vectors first
this->Internal->ProcessedFileList.clear();
this->Internal->AvailableDataFileList.clear();
vtkDirectory *dataDir = vtkDirectory::New();
dataDir->Open(this->DataLocation);
for (int i=0; i< dataDir->GetNumberOfFiles();i++)
{
this->Internal->ProcessedFileList.push_back(
this->GetDataFileFullPathName(dataDir->GetFile(i))
);
}
// initialize with an empty filename if filename is not set
if (!this->GetFileName())
{
this->SetFileName("");
}
dataDir->Delete();
}
//----------------------------------------------------------------------------
void vtkRTXMLPolyDataReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "DataLocation: "
<< (this->DataLocation? this->DataLocation:"(none)") << "\n";
}