1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkArrayDataWriter.h
5 
6 -------------------------------------------------------------------------
7   Copyright 2008 Sandia Corporation.
8   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9   the U.S. Government retains certain rights in this software.
10 -------------------------------------------------------------------------
11 
12   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
13   All rights reserved.
14   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
15 
16      This software is distributed WITHOUT ANY WARRANTY; without even
17      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18      PURPOSE.  See the above copyright notice for more information.
19 
20 =========================================================================*/
21 
22 /**
23  * @class   vtkArrayDataWriter
24  * @brief   Serialize vtkArrayData to a file or stream.
25  *
26  *
27  * vtkArrayDataWriter serializes vtkArrayData using a text-based
28  * format that is human-readable and easily parsed (default option).  The
29  * WriteBinary array option can be used to serialize the vtkArrayData
30  * using a binary format that is optimized for rapid throughput.
31  *
32  * vtkArrayDataWriter can be used in two distinct ways: first, it can be used as a
33  * normal pipeline filter, which writes its inputs to a file.  Alternatively, static
34  * methods are provided for writing vtkArrayData instances to files or arbitrary c++
35  * streams.
36  *
37  * Inputs:
38  *   Input port 0: (required) vtkArrayData object.
39  *
40  * Output Format:
41  *   See http://www.kitware.com/InfovisWiki/index.php/N-Way_Array_File_Formats for
42  *   details on how vtkArrayDataWriter encodes data.
43  *
44  * @sa
45  * vtkArrayDataReader
46  *
47  * @par Thanks:
48  * Developed by Timothy M. Shead (tshead@sandia.gov) at Sandia National Laboratories.
49  */
50 
51 #ifndef vtkArrayDataWriter_h
52 #define vtkArrayDataWriter_h
53 
54 #include "vtkIOCoreModule.h" // For export macro
55 #include "vtkStdString.h"    // For string API
56 #include "vtkWriter.h"
57 
58 class vtkArrayData;
59 
60 class VTKIOCORE_EXPORT vtkArrayDataWriter : public vtkWriter
61 {
62 public:
63   static vtkArrayDataWriter* New();
64   vtkTypeMacro(vtkArrayDataWriter, vtkWriter);
65   void PrintSelf(ostream& os, vtkIndent indent) override;
66 
67   ///@{
68   /**
69    * Get / set the filename where data will be stored (when used as a filter).
70    */
71   vtkSetFilePathMacro(FileName);
72   vtkGetFilePathMacro(FileName);
73   ///@}
74 
75   ///@{
76   /**
77    * Get / set whether data will be written in binary format (when used as a filter).
78    */
79   vtkSetMacro(Binary, vtkTypeBool);
80   vtkGetMacro(Binary, vtkTypeBool);
81   vtkBooleanMacro(Binary, vtkTypeBool);
82   ///@}
83 
84   /**
85    * The output string. This is only set when WriteToOutputString is set.
86    */
GetOutputString()87   virtual vtkStdString GetOutputString() { return this->OutputString; }
88 
89   ///@{
90   /**
91    * Whether to output to a string instead of to a file, which is the default.
92    */
93   vtkSetMacro(WriteToOutputString, bool);
94   vtkGetMacro(WriteToOutputString, bool);
95   vtkBooleanMacro(WriteToOutputString, bool);
96   ///@}
97 
98   int Write() override; // This is necessary to get Write() wrapped for scripting languages.
99 
100   /**
101    * Writes input port 0 data to a file, using an arbitrary filename and binary flag.
102    */
103   bool Write(const vtkStdString& FileName, bool WriteBinary = false);
104 
105   /**
106    * Write an arbitrary array to a file, without using the pipeline.
107    */
108   static bool Write(vtkArrayData* array, const vtkStdString& file_name, bool WriteBinary = false);
109 
110   /**
111    * Write input port 0 data to an arbitrary stream.  Note: streams should always be opened in
112    * binary mode, to prevent problems reading files on Windows.
113    */
114   bool Write(ostream& stream, bool WriteBinary = false);
115 
116   /**
117    * Write arbitrary data to a stream without using the pipeline.  Note: streams should always
118    * be opened in binary mode, to prevent problems reading files on Windows.
119    */
120   static bool Write(vtkArrayData* array, ostream& stream, bool WriteBinary = false);
121 
122   /**
123    * Write input port 0 data to a string. Note that the WriteBinary argument is not
124    * optional in order to not clash with the inherited Write() method.
125    */
126   vtkStdString Write(bool WriteBinary);
127 
128   /**
129    * Write arbitrary data to a string without using the pipeline.
130    */
131   static vtkStdString Write(vtkArrayData* array, bool WriteBinary = false);
132 
133 protected:
134   vtkArrayDataWriter();
135   ~vtkArrayDataWriter() override;
136 
137   int FillInputPortInformation(int port, vtkInformation* info) override;
138   void WriteData() override;
139 
140   char* FileName;
141   vtkTypeBool Binary;
142   bool WriteToOutputString;
143   vtkStdString OutputString;
144 
145 private:
146   vtkArrayDataWriter(const vtkArrayDataWriter&) = delete;
147   void operator=(const vtkArrayDataWriter&) = delete;
148 };
149 
150 #endif
151