File: vtkRISReader.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (276 lines) | stat: -rw-r--r-- 8,069 bytes parent folder | download | duplicates (3)
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation
// SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov

#include "vtkRISReader.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkStringArray.h"
#include "vtkTable.h"
#include "vtkVariant.h"
#include "vtksys/FStream.hxx"

#include <map>
#include <string>

VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkRISReader);

// Not all platforms support std::getline(istream&, std::string) so
// we have to provide our own
static istream& my_getline(istream& input, std::string& output, char delimiter = '\n');

//------------------------------------------------------------------------------

vtkRISReader::vtkRISReader()
  : FileName(nullptr)
  , Delimiter(nullptr)
  , MaxRecords(0)
{
  this->SetDelimiter(";");

  this->SetNumberOfInputPorts(0);
  this->SetNumberOfOutputPorts(1);
}

//------------------------------------------------------------------------------

vtkRISReader::~vtkRISReader()
{
  this->SetDelimiter(nullptr);
  this->SetFileName(nullptr);
}

//------------------------------------------------------------------------------

void vtkRISReader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "FileName: " << (this->FileName ? this->FileName : "(none)") << endl;
  os << indent << "Delimiter: " << (this->Delimiter ? this->Delimiter : "(none)") << endl;
  os << indent << "MaxRecords: " << this->MaxRecords << endl;
}

//------------------------------------------------------------------------------

int vtkRISReader::RequestData(
  vtkInformation*, vtkInformationVector**, vtkInformationVector* outputVector)
{
  // Check that the filename has been specified
  if (!this->FileName)
  {
    vtkErrorMacro("vtkRISReader: You must specify a filename!");
    return 0;
  }

  // Open the file
  vtksys::ifstream file(this->FileName, ios::in | ios::binary);
  if (!file)
  {
    vtkErrorMacro(<< "vtkRISReader could not open file " << this->FileName);
    return 0;
  }

  // Get the total size of the file ...
  file.seekg(0, ios::end);
  const int total_bytes = file.tellg();

  // Go back to the top of the file
  file.seekg(0, ios::beg);

  // Store the text data into a vtkTable
  vtkTable* table = vtkTable::GetData(outputVector);

  // Keep a mapping of column-name to column-index for quick lookups ...
  std::map<std::string, vtkIdType> columns;

  std::string line_buffer;

  const std::string delimiter(this->Delimiter ? this->Delimiter : "");
  int record_count = 0;

  // For each record in the file ...
  for (my_getline(file, line_buffer); file; my_getline(file, line_buffer))
  {
    // Skip empty lines ...
    if (line_buffer.empty())
      continue;

    // Stop if we exceed the maximum number of records ...
    if (this->MaxRecords && record_count >= this->MaxRecords)
      break;

    double progress =
      total_bytes ? static_cast<double>(file.tellg()) / static_cast<double>(total_bytes) : 0.5;

    this->InvokeEvent(vtkCommand::ProgressEvent, &progress);

    // Add a new row to the table for the record ...
    table->InsertNextBlankRow();

    // For each field in the record ...
    for (; file;)
    {
      const std::string tag_type = line_buffer.size() >= 6 && line_buffer[2] == ' ' &&
          line_buffer[3] == ' ' && line_buffer[4] == '-' && line_buffer[5] == ' '
        ? line_buffer.substr(0, 2)
        : std::string();

      if (tag_type == "ER")
        break;

      std::string tag_value = line_buffer.size() > 6 ? line_buffer.substr(6) : std::string();

      // For each line in the field ...
      for (my_getline(file, line_buffer); file; my_getline(file, line_buffer))
      {
        const std::string next_tag_type = line_buffer.size() >= 6 && line_buffer[2] == ' ' &&
            line_buffer[3] == ' ' && line_buffer[4] == '-' && line_buffer[5] == ' '
          ? line_buffer.substr(0, 2)
          : std::string();

        if (next_tag_type == tag_type)
        {
          const std::string next_tag_value =
            line_buffer.size() > 6 ? line_buffer.substr(6) : std::string();
          tag_value += delimiter + next_tag_value;
        }
        else if (next_tag_type.empty())
        {
          tag_value += line_buffer;
        }
        else
        {
          break;
        }
      }

      // If necessary, add a new column to the table to store this value ...
      if (!columns.count(tag_type))
      {
        vtkStringArray* const new_column = vtkStringArray::New();
        new_column->SetName(tag_type.c_str());
        new_column->SetNumberOfTuples(record_count + 1);
        columns[tag_type] = table->GetNumberOfColumns();
        table->AddColumn(new_column);
        new_column->Delete();
      }

      // Set the table value ...
      table->SetValue(record_count, columns[tag_type], tag_value.c_str());
    }

    // Keep track of the current record count ...
    ++record_count;
  }
  /*
    // Loop through every line in the file ...
    std::string tag;
    std::string tag_type;
    std::string tag_value;
    bool explicit_tag;
    for(my_getline(file, line_buffer); file; my_getline(file, line_buffer))
      {
      if(this->MaxRecords && record_count >= this->MaxRecords)
        break;

      // Skip empty lines ...
      if(line_buffer.empty())
        continue;

      double progress = total_bytes
        ? static_cast<double>(file.tellg()) / static_cast<double>(total_bytes)
        : 0.5;

      this->InvokeEvent(vtkCommand::ProgressEvent, &progress);

      // Try to extract the tag, tag type, and tag value ...
      if(line_buffer.size() >= 6 && line_buffer[2] == ' ' && line_buffer[3] == ' ' && line_buffer[4]
    == '-' && line_buffer[5] == ' ')
        {
        explicit_tag = true;
        tag_type = line_buffer.substr(0, 2);
        tag_value = line_buffer.substr(6);
        }
      else
        {
        explicit_tag = false;
        tag_value = line_buffer;
        }

      // If this is the end of a record ...
      if(tag_type == "ER")
        {
        ++record_count;
        continue;
        }

      // If necessary, add a new row to the table to store this value ...
      while(table->GetNumberOfRows() < record_count + 1)
        table->InsertNextBlankRow();

      // If necessary, add a new column to the table to store this value ...
      if(!columns.count(tag_type))
        {
        vtkStringArray* const new_column = vtkStringArray::New();
        new_column->SetName(tag_type.c_str());
        new_column->SetNumberOfTuples(record_count + 1);
        columns[tag_type] = table->GetNumberOfColumns();
        table->AddColumn(new_column);
        new_column->Delete();
        }

      // Set the table value ...
      std::string old_value = table->GetValue(record_count, columns[tag_type]).ToString();
      if(old_value.empty())
        {
        table->SetValue(record_count, columns[tag_type], tag_value);
        }
      else
        {
        if(explicit_tag)
          {
          table->SetValue(record_count, columns[tag_type], (old_value + delimiter +
    tag_value).c_str());
          }
        else
          {
          table->SetValue(record_count, columns[tag_type], (old_value + " " + tag_value).c_str());
          }
        }
      }
  */

  return 1;
}

//------------------------------------------------------------------------------
static istream& my_getline(istream& input, std::string& output, char delimiter)
{
  output = "";

  unsigned int numCharactersRead = 0;
  int nextValue = 0;

  while ((nextValue = input.get()) != EOF && numCharactersRead < output.max_size())
  {
    ++numCharactersRead;

    char downcast = static_cast<char>(nextValue);
    if (downcast == delimiter || (delimiter == '\n' && downcast == '\r'))
    {
      if (delimiter == '\n' && downcast == '\r' && input.peek() == '\n')
      {
        input.get();
      }
      return input;
    }
    else
    {
      output += downcast;
    }
  }

  return input;
}
VTK_ABI_NAMESPACE_END