File: vtkISIReader.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 (199 lines) | stat: -rw-r--r-- 5,729 bytes parent folder | download | duplicates (4)
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
// 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 "vtkISIReader.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(vtkISIReader);

// 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');

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

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

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

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

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

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

void vtkISIReader::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 vtkISIReader::RequestData(
  vtkInformation*, vtkInformationVector**, vtkInformationVector* outputVector)
{
  // Check that the filename has been specified
  if (!this->FileName)
  {
    vtkErrorMacro("vtkISIReader: You must specify a filename!");
    return 0;
  }

  // Open the file
  vtksys::ifstream file(this->FileName, ios::in | ios::binary);
  if (!file)
  {
    vtkErrorMacro(<< "vtkISIReader 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;

  // Get header information from the first two lines of the file ...
  std::string line_buffer;
  my_getline(file, line_buffer);
  if (line_buffer != "FN ISI Export Format")
  {
    vtkErrorMacro(<< "File " << this->FileName << " is not an ISI file");
    return 0;
  }

  my_getline(file, line_buffer);
  if (line_buffer != "VR 1.0")
  {
    vtkErrorMacro(<< "File " << this->FileName << " is not an ISI version 1.0 file");
    return 0;
  }

  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))
  {
    // 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() >= 2 ? line_buffer.substr(0, 2) : std::string();
      if (tag_type == "ER")
        break;
      if (tag_type == "EF")
        break;

      std::string tag_value = line_buffer.size() > 3 ? line_buffer.substr(3) : 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() >= 2 ? line_buffer.substr(0, 2) : std::string();
        if (next_tag_type != "  ")
          break;

        const std::string next_tag_value =
          line_buffer.size() > 3 ? line_buffer.substr(3) : std::string();

        tag_value += delimiter + next_tag_value;
      }

      // 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;
  }

  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