cmtkTypedStream.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2011 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2752 $
00026 //
00027 //  $LastChangedDate: 2011-01-17 11:33:31 -0800 (Mon, 17 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkTypedStream_h_included_
00034 #define __cmtkTypedStream_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Base/cmtkTypes.h>
00039 
00040 #include <stack>
00041 #include <stdio.h>
00042 
00043 #include <zlib.h>
00044 
00045 #ifndef NULL
00046 #define NULL 0
00047 #endif
00048 
00049 #include <string>
00050 
00051 namespace
00052 cmtk
00053 {
00054 
00057 
00061 
00063 typedef enum 
00064 {
00066   TYPEDSTREAM_READ,
00068   TYPEDSTREAM_WRITE,
00070   TYPEDSTREAM_WRITE_ZLIB,
00072   TYPEDSTREAM_APPEND
00073 } TypedStreamMode;
00074 
00076 typedef enum 
00077 {
00079   TYPEDSTREAM_ERROR,
00081   TYPEDSTREAM_OK
00082 } TypedStreamCondition;
00083 
00085 typedef enum 
00086 {
00088   TYPEDSTREAM_ERROR_NONE,
00090   TYPEDSTREAM_ERROR_UNKNOWN,
00094   TYPEDSTREAM_ERROR_SYSTEM,
00097   TYPEDSTREAM_ERROR_FORMAT,
00100   TYPEDSTREAM_ERROR_ARG,
00105   TYPEDSTREAM_ERROR_MODE,
00110   TYPEDSTREAM_ERROR_TYPE,
00115   TYPEDSTREAM_ERROR_LIMIT,
00118   TYPEDSTREAM_ERROR_LEVEL,
00123   TYPEDSTREAM_ERROR_INVALID,
00124   TYPEDSTREAM_ERROR_MAX
00125 } TypedStreamStatus;
00126 
00128 typedef enum 
00129 {
00131   TYPEDSTREAM_TYPE_INT,
00133   TYPEDSTREAM_TYPE_BOOL,
00135   TYPEDSTREAM_TYPE_BINARYBOOL,
00137   TYPEDSTREAM_TYPE_FLOAT,
00139   TYPEDSTREAM_TYPE_DOUBLE,
00141   TYPEDSTREAM_TYPE_STRING
00142 } TypedStreamType;
00143 
00145 typedef enum 
00146 {
00148   TYPEDSTREAM_EOF,
00150   TYPEDSTREAM_BEGIN,
00152   TYPEDSTREAM_END,
00154   TYPEDSTREAM_KEY,
00156   TYPEDSTREAM_VALUE,
00158   TYPEDSTREAM_COMMENT
00159 } TypedStreamToken;
00160 
00162 typedef enum 
00163 {
00165   TYPEDSTREAM_DEBUG_OFF,
00167   TYPEDSTREAM_DEBUG_ON
00168 } TypedStreamDebugFlag;
00169 
00171 #define TYPEDSTREAM_LIMIT_BUFFER        1024
00172 
00174 #define TYPEDSTREAM_FLUSH true
00175 
00177 #define TYPEDSTREAM_NOFLUSH false
00178 
00186 class TypedStream 
00187 {
00188 public:
00190   TypedStream();
00191 
00196   TypedStream( const char* filename, const TypedStreamMode mode );
00197 
00203   TypedStream( const char* dir, const char* archive, const TypedStreamMode mode );
00204 
00208   virtual ~TypedStream();
00209 
00212   void Open( const char* filename, const TypedStreamMode mode );
00213 
00216   void Open( const char* dir, const char* archive, const TypedStreamMode mode );
00217 
00220   void Close();
00221 
00230   TypedStreamCondition Seek( const char* section , 
00231                              const bool forward = false );
00232 
00237   TypedStreamCondition Rewind();
00238 
00242   int IsValid() 
00243   {
00244     return (this->File != NULL) || (this->GzFile != NULL); 
00245   }
00246 
00249   TypedStreamStatus GetStatus() const 
00250   { 
00251     return this->Status; 
00252   }
00253 
00261   TypedStreamCondition Begin( const char* section = NULL );
00262 
00270   TypedStreamCondition End( const bool flush = false );
00271 
00279   bool ReadBool( const char* key , 
00280                  const bool defaultValue = false ,
00281                  const bool forward = false );
00282 
00286   TypedStreamCondition ReadBoolArray( const char* key , 
00287                                       byte *const array , 
00288                                       const int size , 
00289                                       const bool forward = false );
00290   
00294   int ReadInt( const char* key , 
00295                const int defaultValue = 0 , 
00296                const bool forward = false );
00297 
00301   TypedStreamCondition ReadIntArray( const char* key , 
00302                                      int *const array , 
00303                                      const int size , 
00304                                      const bool forward = false );
00305   
00309   float ReadFloat( const char* key , 
00310                    const float defaultValue = 0 , 
00311                    const bool forward = false );
00312 
00316   TypedStreamCondition ReadFloatArray( const char* key , 
00317                                        float *const array , 
00318                                        const int size , 
00319                                        const bool forward = false );
00320 
00324   double ReadDouble( const char* key ,
00325                      const double defaultValue = 0 , 
00326                      const bool forward = false );
00327 
00331   TypedStreamCondition ReadDoubleArray( const char* key , 
00332                                         double *const array , 
00333                                         const int size , 
00334                                         const bool forward = false );
00335   
00343   Types::Coordinate ReadCoordinate( const char* key , 
00344                                     const Types::Coordinate defaultValue = 0 , 
00345                                     const bool forward = false ) 
00346   {
00347 #ifdef CMTK_COORDINATES_DOUBLE
00348     return this->ReadDouble( key, defaultValue, forward );
00349 #else
00350     return this->ReadFloat( key, defaultValue, forward );
00351 #endif
00352   }
00353   
00361   Types::DataItem ReadItem( const char* key , 
00362                             const Types::DataItem defaultValue = 0 , 
00363                             const bool forward = false ) 
00364   {
00365 #ifdef CMTK_DATA_DOUBLE
00366     return this->ReadDouble( key, defaultValue, forward );
00367 #else
00368     return this->ReadFloat( key, defaultValue, forward );
00369 #endif
00370   }
00371   
00379   TypedStreamCondition ReadCoordinateArray( const char* key , 
00380                                             Types::Coordinate *const array , 
00381                                             const int size , 
00382                                             const bool forward = false )
00383   {
00384 #ifdef CMTK_COORDINATES_DOUBLE
00385     return this->ReadDoubleArray( key, array, size, forward );
00386 #else
00387     return this->ReadFloatArray( key, array, size, forward );
00388 #endif
00389   }
00390 
00398   TypedStreamCondition ReadItemArray( const char* key , 
00399                                       Types::DataItem *const array , 
00400                                       const int size , 
00401                                       const bool forward = false ) 
00402   {
00403 #ifdef CMTK_DATA_DOUBLE
00404     return this->ReadDoubleArray( key, array, size, forward );
00405 #else
00406     return this->ReadFloatArray( key, array, size, forward );
00407 #endif
00408   }
00409 
00419   char* ReadString( const char* key , 
00420                     const char* defaultValue = NULL , 
00421                     const bool forward = false );
00422 
00424   TypedStreamCondition WriteBool( const char* key , 
00425                                   const bool value  );
00426 
00428   TypedStreamCondition WriteInt( const char* key , 
00429                                  const int value  );
00430 
00432   TypedStreamCondition WriteFloat( const char* key , 
00433                                    const float value  );
00434 
00436   TypedStreamCondition WriteDouble( const char* key , 
00437                                     const double value  );
00438 
00440   TypedStreamCondition WriteCoordinate( const char* key , 
00441                                         const Types::Coordinate value  ) 
00442   {
00443 #ifdef CMTK_COORDINATES_FLOAT
00444     return this->WriteFloat( key, value );
00445 #else
00446     return this->WriteDouble( key, value );
00447 #endif
00448   }
00449   
00451   TypedStreamCondition WriteItem( const char* key , 
00452                                   const Types::DataItem value  ) 
00453   {
00454 #ifdef CMTK_DATA_FLOAT
00455     return this->WriteFloat( key, value );
00456 #else
00457     return this->WriteDouble( key, value );
00458 #endif
00459   }
00460   
00462   TypedStreamCondition WriteString( const char* key , 
00463                                     const char* value  );
00464 
00466   TypedStreamCondition WriteString( const char* key , 
00467                                     const std::string& value  );
00468 
00470   TypedStreamCondition WriteComment( const char* fmt , ... );
00471 
00473   TypedStreamCondition WriteComment( const int argc , const char* argv[]  );
00474 
00476   TypedStreamCondition WriteComment( int argc , char* argv[]  );
00477 
00480   TypedStreamCondition WriteIntArray( const char* key , 
00481                                       const int* array , 
00482                                       const int size ,
00483                                       const int valuesPerLine = 10  );
00484 
00487   TypedStreamCondition WriteBoolArray( const char* key , 
00488                                        const byte* array , 
00489                                        const int size , 
00490                                        const int valuesPerLine = 10  );
00491 
00494   TypedStreamCondition WriteFloatArray( const char* key, 
00495                                         const float* array , 
00496                                         const int size ,
00497                                         const int valuesPerLine = 10  );
00498 
00501   TypedStreamCondition WriteDoubleArray( const char* key , 
00502                                          const double* array , 
00503                                          const int size ,
00504                                          const int valuesPerLine = 10  );
00505 
00513   TypedStreamCondition WriteCoordinateArray( const char* key, 
00514                                              const Types::Coordinate* array , 
00515                                              const int size ,
00516                                              const int valuesPerLine = 10  )
00517   { 
00518 #ifdef CMTK_COORDINATES_DOUBLE
00519     return this->WriteDoubleArray( key, array, size, valuesPerLine );
00520 #else
00521     return this->WriteFloatArray( key, array, size, valuesPerLine );
00522 #endif
00523   }
00524   
00532   TypedStreamCondition WriteItemArray( const char* key , 
00533                                        const Types::DataItem* array , 
00534                                        const int size , 
00535                                        const int valuesPerLine = 10  )
00536   { 
00537 #ifdef CMTK_DATA_DOUBLE
00538     return this->WriteDoubleArray( key, array, size, valuesPerLine );
00539 #else
00540     return this->WriteFloatArray( key, array, size, valuesPerLine );
00541 #endif
00542   }
00543 
00545   void SetDebugFlag( const TypedStreamDebugFlag debugFlag = TYPEDSTREAM_DEBUG_ON  )
00546   { 
00547     DebugFlag = debugFlag;
00548   }
00549   
00550 private:
00555   void InitInternals();
00556 
00558   FILE *File;
00559 
00561   gzFile GzFile;
00562 
00564   TypedStreamMode Mode;
00565 
00568   TypedStreamStatus Status;
00569 
00576   int PrecisionFloat;
00577 
00584   int PrecisionDouble;
00585 
00587   char Buffer[TYPEDSTREAM_LIMIT_BUFFER];
00588 
00590   char* BufferKey;
00591 
00593   char* BufferValue;
00594 
00599   std::stack<int> LevelStack;
00600 
00607   TypedStreamCondition GenericReadArray( const char* key , 
00608                                          const int type , 
00609                                          void *const array , 
00610                                          const int arraySize , 
00611                                          const bool forward = false  );
00612   
00614   TypedStreamToken ReadLineToken();
00615 
00623   static int StringCmp( const char* s1, const char* s2 );
00624 
00631   char* StringSplit( char* s1  ) const;
00632 
00634   mutable char* SplitPosition;
00635 
00637   static const char* GetTypedStreamIdent() 
00638   {
00639     return "! TYPEDSTREAM 1.1\n";
00640   }
00641   
00643   TypedStreamDebugFlag DebugFlag;
00644   
00646   void DebugOutput( const char* format , ... );
00647 };
00648 
00650 
00651 } // namespace cmtk
00652 
00654 
00655 #endif // #ifndef __cmtkTypedstream_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines