목차

include/AK/IBytes.h

Go to the documentation of this file.
00001 
00002 //
00003 // Copyright (c) 2006 Audiokinetic Inc. / All Rights Reserved
00004 //
00006 
00009 
00010 #ifndef _AK_IBYTES_H
00011 #define _AK_IBYTES_H
00012 
00013 #include <wchar.h>
00014 #include <AK/Tools/Common/AkPlatformFuncs.h> 
00015 
00016 namespace AK
00017 {
00020     class IReadBytes
00021     {
00022     public:
00025 
00026 
00029         virtual bool ReadBytes( 
00030             void * in_pData,        
00031             long in_cBytes,         
00032             long & out_cRead        
00033             ) = 0;
00034 
00036 
00039 
00040 
00044         template<class T>
00045         bool Read( 
00046             T & out_data )  
00047         {
00048             long cRead;
00049             return ReadBytes( &out_data, sizeof( T ), cRead );
00050         }
00051 
00056         template<class T>
00057         T Read()
00058         {
00059             T value;
00060 
00061             long cRead;
00062             ReadBytes( &value, sizeof( T ), cRead );
00063 
00064             return value;
00065         }
00066 
00069         bool ReadString( 
00070             wchar_t * out_pszString,    
00071             long in_nMax )          
00072         {
00073             long cChars;
00074             if ( !Read<long>( cChars ) ) 
00075                 return false;
00076 
00077             bool bRet = true;
00078 
00079             if ( cChars > 0 )
00080             {
00081                 long cRead;
00082 
00083                 if ( cChars < in_nMax )
00084                 {
00085                     ReadBytes( out_pszString, cChars * sizeof( wchar_t ), cRead );
00086                     out_pszString[ cChars ] = 0;
00087 
00088                     bRet = cRead == (long)( cChars * sizeof( wchar_t ) );
00089                 }
00090                 else
00091                 {
00092                     ReadBytes( out_pszString, in_nMax * sizeof( wchar_t ), cRead );
00093                     out_pszString[ in_nMax - 1 ] = 0;
00094 
00095                     bRet = cRead == (long)( cChars * sizeof( wchar_t ) );
00096 
00097                     if ( bRet )
00098                     {
00099                         // Read extra characters in temp buffer.
00100                         long cRemaining = cChars - in_nMax;
00101 
00102                         wchar_t * pTemp = new wchar_t[ cRemaining ];
00103 
00104                         ReadBytes( pTemp, cRemaining * sizeof( wchar_t ), cRead );
00105 
00106                         bRet = cRemaining == (long)( cChars * sizeof( wchar_t ) );
00107 
00108                         delete [] pTemp;
00109                     }
00110                 }
00111             }
00112             else
00113             {
00114                 out_pszString[ 0 ] = 0;
00115             }
00116 
00117             return bRet;
00118         }
00120     };
00121 
00124     class IWriteBytes
00125     {
00126     public:
00129 
00130 
00133         virtual bool WriteBytes( 
00134             const void * in_pData,  
00135             AkInt32 in_cBytes,      
00136             AkInt32 & out_cWritten      
00137             ) = 0;
00138 
00140 
00143 
00144 
00148         template<class T>
00149         bool Write( 
00150             const T & in_data )     
00151         {
00152             AkInt32 cWritten;
00153             return WriteBytes( &in_data, sizeof( T ), cWritten );
00154         }
00155 
00158         bool WriteString( 
00159             const wchar_t * in_pszString )  
00160         {
00161             if ( in_pszString != NULL )
00162             {
00163                 size_t cChars = wcslen( in_pszString );
00164                 if ( !Write<AkUInt32>( (AkUInt32) cChars ) )
00165                     return false;
00166 
00167                 AkInt32 cWritten = 0;
00168                 AkInt32 cToWrite = (AkInt32)( cChars * sizeof( wchar_t ) );
00169 
00170                 if ( cChars > 0 )
00171                 {
00172                     WriteBytes( in_pszString, cToWrite, cWritten );
00173                 }
00174 
00175                 return cWritten == cToWrite;
00176             }
00177             return Write<AkUInt32>( 0 );
00178         }
00179 
00182         bool WriteString( 
00183             const char * in_pszString ) 
00184         {
00185             if ( in_pszString != NULL )
00186             {
00187                 size_t cChars = strlen( in_pszString );
00188                 if ( !Write<AkUInt32>( (AkUInt32) cChars ) )
00189                     return false;
00190 
00191                 AkInt32 cWritten = 0;
00192 
00193                 if ( cChars > 0 )
00194                 {
00195                     WriteBytes( in_pszString, (AkInt32) cChars, cWritten );
00196                 }
00197 
00198                 return cWritten == (AkInt32) cChars;
00199             }
00200             return Write<AkUInt32>( 0 );
00201         }
00203     };
00204 
00207     class IWriteBuffer : public IWriteBytes
00208     {
00209     public:
00212 
00213 
00216         virtual AkInt32 Count() const = 0;
00217 
00220         virtual AkUInt8 * Bytes() const = 0;
00221 
00223         virtual void SetCount( AkInt32 in_cBytes ) = 0;
00224 
00227         virtual bool Reserve( AkInt32 in_cBytes ) = 0;
00228 
00230         virtual void Clear() = 0;
00231 
00233         virtual AkUInt8 * Detach() = 0;
00234 
00236     };
00237 }
00238 
00239 #endif // _AK_IBYTES_H