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
|
/*
//
// Copyright 2016 Google, Inc.
//
// Copyright 1997-2009 Torsten Rohlfing
//
// Copyright 2004-2013 SRI International
//
// This file is part of the Computational Morphometry Toolkit.
//
// http://www.nitrc.org/projects/cmtk/
//
// The Computational Morphometry Toolkit is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// The Computational Morphometry Toolkit is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with the Computational Morphometry Toolkit. If not, see
// <http://www.gnu.org/licenses/>.
//
// $Revision: 5436 $
//
// $LastChangedDate: 2018-12-10 19:01:20 -0800 (Mon, 10 Dec 2018) $
//
// $LastChangedBy: torstenrohlfing $
//
*/
#ifndef __cmtkTypedStreamOutput_h_included_
#define __cmtkTypedStreamOutput_h_included_
#include <cmtkconfig.h>
#include <Base/cmtkTypes.h>
#include <IO/cmtkTypedStream.h>
#include <stack>
#include <stdio.h>
#include <zlib.h>
#ifndef NULL
#define NULL 0
#endif
#include <string>
namespace
cmtk
{
/** \addtogroup IO */
//@{
/** Class for writing "typedstream" archives.
*/
class TypedStreamOutput : public TypedStream
{
public:
/// This class.
typedef TypedStreamOutput Self;
/// Base class.
typedef TypedStream Superclass;
/// Access modes for archives.
typedef enum
{
/// Currently unset.
MODE_UNSET,
/// Write-only access.
MODE_WRITE,
/// Write-only access piped through zlib/gzip compression.
MODE_WRITE_ZLIB,
/// Open existing archive and append to it.
MODE_APPEND
} Mode;
/// Default constructor.
TypedStreamOutput() : TypedStream(), m_Mode( MODE_UNSET ) {}
/** Open constructor.
*\param filename Name of the archive to open.
*\param mode Access mode, ie. read-only, write-only, etc.
*/
TypedStreamOutput( const std::string& filename, const Self::Mode mode );
/** Open constructor for separate path and archive names.
*\param dir Directory to open archive in.
*\param archive Name of the archive to open.
*\param mode Access mode, ie. read-only, write-only, etc.
*/
TypedStreamOutput( const std::string& dir, const std::string& archive, const Self::Mode mode );
/** Destructor.
* Close() is called to close a possibly open archive.
*/
virtual ~TypedStreamOutput();
/** Open another archive without constructing a new object.
*/
void Open( const std::string& filename, const Self::Mode mode );
/** Open another archive in explicit directory.
*/
void Open( const std::string& dir, const std::string& archive, const Self::Mode mode );
/** Close an open archive.
*/
void Close();
/** Begin a section.
* This function will start a new section and increase the indentation level by one.
*\param section Name of the new section.
*\return Error condition.
*/
Self::Condition Begin( const std::string& section );
/** End a section.
* In the open archive, this function will close the last section and
* decrease the nesting level by one.
*\param flush If this flag is set, the output file buffer will be flushed
* after closing the section.
*\return Error condition.
*/
Self::Condition End( const bool flush = false );
/// Write a boolean value to an open archive.
Self::Condition WriteBool( const char* key /*!< The name of the field under which to write this value in the archive.*/,
const bool value /*!< Value to write to the archive under the given key. */ );
/// Write an integer value to an open archive.
Self::Condition WriteInt( const char* key /*!< The name of the field under which to write this value in the archive.*/,
const int value /*!< Value to write to the archive under the given key. */ );
/// Write a float value to an open archive.
Self::Condition WriteFloat( const char* key /*!< The name of the field under which to write this value in the archive.*/,
const float value /*!< Value to write to the archive under the given key. */ );
/// Write a double precision float value to an open archive.
Self::Condition WriteDouble( const char* key /*!< The name of the field under which to write this value in the archive.*/,
const double value /*!< Value to write to the archive under the given key. */ );
/// Write an Types::Coordinate value to an open archive.
Self::Condition WriteCoordinate( const char* key /*!< The name of the field under which to write this value in the archive.*/,
const Types::Coordinate value /*!< Value to write to the archive under the given key. */ )
{
#ifdef CMTK_COORDINATES_FLOAT
return this->WriteFloat( key, value );
#else
return this->WriteDouble( key, value );
#endif
}
/// Write an Types::DataItem value to an open archive.
Self::Condition WriteItem( const char* key /*!< The name of the field under which to write this value in the archive.*/,
const Types::DataItem value /*!< Value to write to the archive under the given key. */ )
{
#ifdef CMTK_DATA_FLOAT
return this->WriteFloat( key, value );
#else
return this->WriteDouble( key, value );
#endif
}
/// Write a string to an open archive.
Self::Condition WriteString( const char* key /*!< The name of the field under which to write this string in the archive.*/,
const char* value /*!< String to write to the archive under the given key. */ );
/// Write a string to an open archive.
Self::Condition WriteString( const char* key /*!< The name of the field under which to write this string in the archive.*/,
const std::string& value /*!< String to write to the archive under the given key. */ );
/** Write array of integer values to an open archive.
*/
Self::Condition WriteIntArray( const char* key /*!< The name of the field under which to write this array in the archive.*/,
const int* array /*!< Pointer to the array to be written.*/,
const int size /*!< Number of values in the array. This is the number of values written to the archive. */,
const int valuesPerLine = 10 /*!< Optional number of values per line of text written to the archive. This improves readability of the resulting archive as a text. */ );
Self::Condition WriteIntArray( const char* key /*!< The name of the field under which to write this array in the archive.*/,
const long long int* array /*!< Pointer to the array to be written.*/,
const int size /*!< Number of values in the array. This is the number of values written to the archive. */,
const int valuesPerLine = 10 /*!< Optional number of values per line of text written to the archive. This improves readability of the resulting archive as a text. */ );
/** Write array of binay encoded boole values to an open archive.
*/
Self::Condition WriteBoolArray( const char* key /*!< The name of the field under which to write this array in the archive.*/,
const byte* array /*!< Pointer to the array to be written.*/,
const int size /*!< Number of values in the array. This is the number of values written to the archive. */,
const int valuesPerLine = 10 /*!< Optional number of values per line of text written to the archive. This improves readability of the resulting archive as a text. */ );
/** Write array of single-precision values to an open archive.
*/
Self::Condition WriteFloatArray( const char* key/*!< The name of the field under which to write this array in the archive.*/,
const float* array /*!< Pointer to the array to be written.*/,
const int size /*!< Number of values in the array. This is the number of values written to the archive. */,
const int valuesPerLine = 10 /*!< Optional number of values per line of text written to the archive. This improves readability of the resulting archive as a text. */ );
/** Write array of double-precision values to an open archive.
*/
Self::Condition WriteDoubleArray( const char* key /*!< The name of the field under which to write this array in the archive.*/,
const double* array /*!< Pointer to the array to be written.*/,
const int size /*!< Number of values in the array. This is the number of values written to the archive. */,
const int valuesPerLine = 10 /*!< Optional number of values per line of text written to the archive. This improves readability of the resulting archive as a text. */ );
/** Write array of double- or single precision values to an open archive.
* Whether double- or single-precision data is written depends on the
* definition of the CMTK_COORDINATES_DOUBLE preprocessor symbol. This function
* is thus guaranteed to always match the Types::Coordinate type.
*\see CMTK_COORDINATES_DOUBLE
*\see Types::Coordinate
*/
Self::Condition WriteCoordinateArray( const char* key/*!< The name of the field under which to write this array in the archive.*/,
const Types::Coordinate* array /*!< Pointer to the array to be written.*/,
const int size /*!< Number of values in the array. This is the number of values written to the archive. */,
const int valuesPerLine = 10 /*!< Optional number of values per line of text written to the archive. This improves readability of the resulting archive as a text. */ )
{
#ifdef CMTK_COORDINATES_DOUBLE
return this->WriteDoubleArray( key, array, size, valuesPerLine );
#else
return this->WriteFloatArray( key, array, size, valuesPerLine );
#endif
}
/** Write array of double- or single precision values to an open archive.
* Whether double- or single-precision data is written depends on the
* definition of the CMTK_DATA_DOUBLE preprocessor symbol. This function
* is thus guaranteed to always match the Types::DataItem type.
*\see CMTK_DATA_DOUBLE
*\see Types::DataItem
*/
Self::Condition WriteItemArray( const char* key /*!< The name of the field under which to write this array in the archive.*/,
const Types::DataItem* array /*!< Pointer to the array to be written.*/,
const int size /*!< Number of values in the array. This is the number of values written to the archive. */,
const int valuesPerLine = 10 /*!< Optional number of values per line of text written to the archive. This improves readability of the resulting archive as a text. */ )
{
#ifdef CMTK_DATA_DOUBLE
return this->WriteDoubleArray( key, array, size, valuesPerLine );
#else
return this->WriteFloatArray( key, array, size, valuesPerLine );
#endif
}
private:
/// Mode the current archive was opened with.
Self::Mode m_Mode;
};
//@}
} // namespace cmtk
//@}
#endif // #ifndef __cmtkTypedStreamOutput_h_included_
|