1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkWriter.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 "vtkWriter.h"
16 
17 #include "vtkCommand.h"
18 #include "vtkDataObject.h"
19 #include "vtkDemandDrivenPipeline.h"
20 #include "vtkErrorCode.h"
21 #include "vtkInformation.h"
22 #include "vtkInformationVector.h"
23 
24 #include <vtksys/ios/sstream>
25 
26 
27 // Construct with no start and end write methods or arguments.
vtkWriter()28 vtkWriter::vtkWriter()
29 {
30   this->SetNumberOfInputPorts(1);
31   this->SetNumberOfOutputPorts(0);
32 }
33 
~vtkWriter()34 vtkWriter::~vtkWriter()
35 {
36 }
37 
SetInputData(vtkDataObject * input)38 void vtkWriter::SetInputData(vtkDataObject *input)
39 {
40   this->SetInputData(0, input);
41 }
42 
SetInputData(int index,vtkDataObject * input)43 void vtkWriter::SetInputData(int index, vtkDataObject *input)
44 {
45   this->SetInputDataInternal(index, input);
46 }
47 
GetInput()48 vtkDataObject *vtkWriter::GetInput()
49 {
50   return this->GetInput(0);
51 }
52 
GetInput(int port)53 vtkDataObject *vtkWriter::GetInput(int port)
54 {
55   if (this->GetNumberOfInputConnections(port) < 1)
56     {
57     return NULL;
58     }
59   return this->GetExecutive()->GetInputData(port, 0);
60 }
61 
62 
63 // Write data to output. Method executes subclasses WriteData() method, as
64 // well as StartMethod() and EndMethod() methods.
Write()65 int vtkWriter::Write()
66 {
67   // Make sure we have input.
68   if (this->GetNumberOfInputConnections(0) < 1)
69     {
70     vtkErrorMacro("No input provided!");
71     return 0;
72     }
73 
74   // always write even if the data hasn't changed
75   this->Modified();
76   this->UpdateWholeExtent();
77   return 1;
78 }
79 
ProcessRequest(vtkInformation * request,vtkInformationVector ** inputVector,vtkInformationVector * outputVector)80 int vtkWriter::ProcessRequest(vtkInformation *request,
81                               vtkInformationVector **inputVector,
82                               vtkInformationVector *outputVector)
83 {
84   // generate the data
85   if(request->Has(vtkDemandDrivenPipeline::REQUEST_DATA()))
86     {
87     return this->RequestData(request, inputVector, outputVector);
88     }
89 
90   return this->Superclass::ProcessRequest(request, inputVector, outputVector);
91 }
92 
RequestData(vtkInformation *,vtkInformationVector **,vtkInformationVector *)93 int vtkWriter::RequestData(
94   vtkInformation *,
95   vtkInformationVector **,
96   vtkInformationVector *)
97 {
98   this->SetErrorCode(vtkErrorCode::NoError);
99 
100   vtkDataObject *input = this->GetInput();
101   int idx;
102 
103   // make sure input is available
104   if ( !input )
105     {
106     vtkErrorMacro(<< "No input!");
107     return 0;
108     }
109 
110   for (idx = 0; idx < this->GetNumberOfInputPorts(); ++idx)
111     {
112     if (this->GetInputExecutive(idx, 0) != NULL)
113       {
114       this->GetInputExecutive(idx, 0)->Update();
115       }
116     }
117 
118   unsigned long lastUpdateTime =  this->GetInput(0)->GetUpdateTime();
119   for (idx = 1; idx < this->GetNumberOfInputPorts(); ++idx)
120     {
121     if (this->GetInput(idx))
122       {
123       unsigned long updateTime = this->GetInput(idx)->GetUpdateTime();
124       if ( updateTime > lastUpdateTime )
125         {
126         lastUpdateTime = updateTime;
127         }
128       }
129     }
130 
131   if (lastUpdateTime < this->WriteTime && this->GetMTime() < this->WriteTime)
132     {
133     // we are up to date
134     return 1;
135     }
136 
137   this->InvokeEvent(vtkCommand::StartEvent,NULL);
138   this->WriteData();
139   this->InvokeEvent(vtkCommand::EndEvent,NULL);
140 
141   this->WriteTime.Modified();
142 
143   return 1;
144 }
145 
PrintSelf(ostream & os,vtkIndent indent)146 void vtkWriter::PrintSelf(ostream& os, vtkIndent indent)
147 {
148   this->Superclass::PrintSelf(os,indent);
149 
150 }
151 
EncodeString(char * resname,const char * name,bool doublePercent)152 void vtkWriter::EncodeString(char* resname, const char* name, bool doublePercent)
153 {
154   if ( !name || !resname )
155     {
156     return;
157     }
158   int cc = 0;
159   vtksys_ios::ostringstream str;
160 
161   char buffer[10];
162 
163   while( name[cc] )
164     {
165     // Encode spaces and %'s (and most non-printable ascii characters)
166     // The reader does not support spaces in strings.
167     if ( name[cc] < 33  || name[cc] > 126 ||
168          name[cc] == '\"' || name[cc] == '%' )
169       {
170       sprintf(buffer, "%02X", static_cast<unsigned char>(name[cc]));
171       if (doublePercent)
172         {
173         str << "%%";
174         }
175       else
176         {
177         str << "%";
178         }
179       str << buffer;
180       }
181     else
182       {
183       str << name[cc];
184       }
185     cc++;
186     }
187   strcpy(resname, str.str().c_str());
188 }
189 
EncodeWriteString(ostream * out,const char * name,bool doublePercent)190 void vtkWriter::EncodeWriteString(ostream* out, const char* name, bool doublePercent)
191 {
192   if (!name)
193     {
194     return;
195     }
196   int cc = 0;
197 
198   char buffer[10];
199 
200   while( name[cc] )
201     {
202     // Encode spaces and %'s (and most non-printable ascii characters)
203     // The reader does not support spaces in strings.
204     if ( name[cc] < 33  || name[cc] > 126 ||
205          name[cc] == '\"' || name[cc] == '%' )
206       {
207       sprintf(buffer, "%02X", static_cast<unsigned char>(name[cc]));
208       if (doublePercent)
209         {
210         *out << "%%";
211         }
212       else
213         {
214         *out << "%";
215         }
216       *out << buffer;
217       }
218     else
219       {
220       *out << name[cc];
221       }
222     cc++;
223     }
224 }
225 
226 
227