1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkEnSightMasterServerReader.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkEnSightMasterServerReader.h"
16 
17 #include "vtkInformation.h"
18 #include "vtkInformationVector.h"
19 #include "vtkObjectFactory.h"
20 
21 #include <string>
22 
23 //----------------------------------------------------------------------------
24 vtkStandardNewMacro(vtkEnSightMasterServerReader);
25 
vtkEnSightMasterServerReaderStartsWith(const char * str1,const char * str2)26 static int vtkEnSightMasterServerReaderStartsWith(const char* str1, const char* str2)
27 {
28   if ( !str1 || !str2 || strlen(str1) < strlen(str2) )
29   {
30     return 0;
31   }
32   return !strncmp(str1, str2, strlen(str2));
33 }
34 
35 //----------------------------------------------------------------------------
vtkEnSightMasterServerReader()36 vtkEnSightMasterServerReader::vtkEnSightMasterServerReader()
37 {
38   this->PieceCaseFileName = nullptr;
39   this->MaxNumberOfPieces = 0;
40   this->CurrentPiece      = -1;
41 }
42 
43 //----------------------------------------------------------------------------
~vtkEnSightMasterServerReader()44 vtkEnSightMasterServerReader::~vtkEnSightMasterServerReader()
45 {
46   this->SetPieceCaseFileName(nullptr);
47 }
48 
49 //----------------------------------------------------------------------------
RequestData(vtkInformation * request,vtkInformationVector ** inputVector,vtkInformationVector * outputVector)50 int vtkEnSightMasterServerReader::RequestData(
51   vtkInformation *request,
52   vtkInformationVector **inputVector,
53   vtkInformationVector *outputVector)
54 {
55   if ( !this->MaxNumberOfPieces )
56   {
57     vtkErrorMacro("No pieces to read");
58     return 0;
59   }
60 
61   if ( this->CurrentPiece < 0 ||
62        this->CurrentPiece >= this->MaxNumberOfPieces )
63   {
64     vtkErrorMacro("Current piece has to be set before reading the file");
65     return 0;
66   }
67   if ( this->DetermineFileName(this->CurrentPiece) != VTK_OK )
68   {
69     vtkErrorMacro("Cannot update piece: " << this->CurrentPiece);
70     return 0;
71   }
72   if ( !this->Reader )
73   {
74     this->Reader = vtkGenericEnSightReader::New();
75   }
76   this->Reader->SetCaseFileName(this->PieceCaseFileName);
77   if ( !this->Reader->GetFilePath() )
78   {
79     this->Reader->SetFilePath( this->GetFilePath() );
80   }
81   return this->Superclass::RequestData(request, inputVector, outputVector);
82 }
83 
84 //----------------------------------------------------------------------------
RequestInformation(vtkInformation * vtkNotUsed (request),vtkInformationVector ** vtkNotUsed (inputVector),vtkInformationVector * vtkNotUsed (outputVector))85 int vtkEnSightMasterServerReader::RequestInformation(
86   vtkInformation *vtkNotUsed(request),
87   vtkInformationVector **vtkNotUsed(inputVector),
88   vtkInformationVector *vtkNotUsed(outputVector))
89 {
90   if ( this->DetermineFileName(-1) != VTK_OK )
91   {
92     vtkErrorMacro("Problem parsing the case file");
93     return 0;
94   }
95   return 1;
96 }
97 
98 //----------------------------------------------------------------------------
DetermineFileName(int piece)99 int vtkEnSightMasterServerReader::DetermineFileName(int piece)
100 {
101   if (!this->CaseFileName)
102   {
103     vtkErrorMacro("A case file name must be specified.");
104     return VTK_ERROR;
105   }
106   std::string sfilename;
107   if (this->FilePath)
108   {
109     sfilename = this->FilePath;
110     if (sfilename.at(sfilename.length()-1) != '/')
111     {
112       sfilename += "/";
113     }
114     sfilename += this->CaseFileName;
115     vtkDebugMacro("full path to case file: " << sfilename.c_str());
116   }
117   else
118   {
119     sfilename = this->CaseFileName;
120   }
121 
122   this->IS = new ifstream(sfilename.c_str(), ios::in);
123   if (this->IS->fail())
124   {
125     vtkErrorMacro("Unable to open file: " << sfilename.c_str());
126     delete this->IS;
127     this->IS = nullptr;
128     return 0;
129   }
130 
131   char result[1024];
132 
133   int servers       = 0;
134   int numberservers = 0;
135   int currentserver = 0;
136 
137   while ( this->ReadNextDataLine(result) )
138   {
139     if ( strcmp(result, "FORMAT") == 0 )
140     {
141       // Format
142     }
143     else if ( strcmp(result, "SERVERS") == 0 )
144     {
145       servers = 1;
146     }
147     else if ( servers &&
148               vtkEnSightMasterServerReaderStartsWith(result, "number of servers:") )
149     {
150       sscanf(result, "number of servers: %i", &numberservers);
151       if ( !numberservers )
152       {
153         vtkErrorMacro("The case file is corrupted");
154         break;
155       }
156     }
157     else if ( servers &&
158               vtkEnSightMasterServerReaderStartsWith(result, "casefile:") )
159     {
160       if ( currentserver == piece )
161       {
162         char filename[VTK_MAXPATH] = "";
163         sscanf(result, "casefile: %s", filename);
164         if ( filename[0] == 0 )
165         {
166           vtkErrorMacro("Problem parsing file name from: " << result);
167           return VTK_ERROR;
168         }
169         this->SetPieceCaseFileName(filename);
170         break;
171       }
172       currentserver ++;
173     }
174   }
175   if ( piece == -1 && currentserver != numberservers )
176   {
177     //cout << "Number of servers (" << numberservers
178     // << ") is not equal to the actual number of servers ("
179     // << currentserver << ")" << endl;
180     return VTK_ERROR;
181   }
182 
183   this->MaxNumberOfPieces = numberservers;
184   delete this->IS;
185   this->IS = nullptr;
186   return VTK_OK;
187 }
188 
189 //----------------------------------------------------------------------------
CanReadFile(const char * fname)190 int vtkEnSightMasterServerReader::CanReadFile(const char* fname)
191 {
192   // We may have to read quite a few lines of the file to do this test
193   // for real.  Just check the extension.
194   size_t len = strlen(fname);
195   if((len >= 4) && (strcmp(fname+len-4, ".sos") == 0))
196   {
197     return 1;
198   }
199   else if((len >= 5) && (strcmp(fname+len-5, ".case") == 0))
200   {
201     return 1;
202   }
203   return 0;
204 }
205 
206 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)207 void vtkEnSightMasterServerReader::PrintSelf(ostream& os, vtkIndent indent)
208 {
209   this->Superclass::PrintSelf(os,indent);
210   os << indent << "Current piece: " << this->CurrentPiece << endl;
211   os << indent << "Piece Case File name: "
212      << (this->PieceCaseFileName?this->PieceCaseFileName:"<none>") << endl;
213   os << indent << "Maximum numbe of pieces: " << this->MaxNumberOfPieces
214      << endl;
215 }
216