cmtkFileHeader.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 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: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkFileHeader_h_included_
00034 #define __cmtkFileHeader_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <System/cmtkMemory.h>
00039 
00040 namespace
00041 cmtk
00042 {
00043 
00046 
00049 class FileHeader
00050 {
00051 public:
00053   FileHeader( void *const header, const bool isBigEndian = true ) 
00054   {
00055     Header = static_cast<char*>( header );
00056     IsBigEndian = isBigEndian;
00057   }
00058   
00060   template<class T> T GetField( const size_t offset ) 
00061   {
00062     T result;
00063     memcpy( &result, Header+offset, sizeof( T ) );
00064 #ifdef WORDS_BIGENDIAN
00065     if ( ! IsBigEndian ) result = Memory::ByteSwap( result );
00066 #else
00067     if ( IsBigEndian ) result = Memory::ByteSwap( result );
00068 #endif
00069     return result;
00070   }
00071 
00073   template<class T> void StoreField( const size_t offset, T value ) 
00074   {
00075 #ifdef WORDS_BIGENDIAN
00076     if ( (sizeof(T) > 1) && ! IsBigEndian ) value = Memory::ByteSwap( value );
00077 #else
00078     if ( (sizeof(T) > 1) && IsBigEndian ) value = Memory::ByteSwap( value );
00079 #endif
00080     memcpy( Header+offset, &value, sizeof( T ) );
00081   }
00082   
00084   void StoreFieldString( const size_t offset, const char* value, const size_t maxlen = 0 ) 
00085   {
00086     if ( maxlen )
00087       strncpy( Header+offset, value, maxlen );
00088     else
00089       strcpy( Header+offset, value );
00090   }
00091   
00096   bool CompareFieldStringN( const size_t offset, const char* value, const size_t len ) const
00097   {
00098     return !memcmp( Header+offset, value, len );
00099   }
00100   
00102   template<class T> void GetArray
00103   ( T *const target, const size_t offset, const size_t length = 1 ) 
00104   {
00105     memcpy( target, Header+offset, length * sizeof( T ) );
00106 #ifdef WORDS_BIGENDIAN
00107     if ( ! IsBigEndian ) 
00108       {
00109       for ( size_t i = 0; i < length; ++i ) 
00110         target[i] = Memory::ByteSwap( target[i] );
00111       }
00112 #else
00113     if ( IsBigEndian ) 
00114       {
00115       for ( size_t i = 0; i < length; ++i ) 
00116         target[i] = Memory::ByteSwap( target[i] );
00117       }
00118 #endif
00119   }
00120   
00121 private:
00123   char *Header;
00124 
00126   bool IsBigEndian;
00127 };
00128 
00130 
00131 } // namespace cmtk
00132 
00133 #endif // #ifndef __cmtkFileHeader_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines