forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutlbuffer.h
1094 lines (905 loc) · 28.4 KB
/
utlbuffer.h
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
// Serialization/unserialization buffer
//=============================================================================//
#ifndef UTLBUFFER_H
#define UTLBUFFER_H
#ifdef _WIN32
#pragma once
#endif
#include "tier1/utlmemory.h"
#include "tier1/byteswap.h"
#include <stdarg.h>
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
struct characterset_t;
//-----------------------------------------------------------------------------
// Description of character conversions for string output
// Here's an example of how to use the macros to define a character conversion
// BEGIN_CHAR_CONVERSION( CStringConversion, '\\' )
// { '\n', "n" },
// { '\t', "t" }
// END_CHAR_CONVERSION( CStringConversion, '\\' )
//-----------------------------------------------------------------------------
class CUtlCharConversion
{
public:
struct ConversionArray_t
{
char m_nActualChar;
const char *m_pReplacementString;
};
CUtlCharConversion( char nEscapeChar, const char *pDelimiter, int nCount, ConversionArray_t *pArray );
char GetEscapeChar() const;
const char *GetDelimiter() const;
int GetDelimiterLength() const;
const char *GetConversionString( char c ) const;
int GetConversionLength( char c ) const;
int MaxConversionLength() const;
// Finds a conversion for the passed-in string, returns length
virtual char FindConversion( const char *pString, int *pLength );
protected:
struct ConversionInfo_t
{
int m_nLength;
const char *m_pReplacementString;
};
char m_nEscapeChar;
const char *m_pDelimiter;
int m_nDelimiterLength;
int m_nCount;
int m_nMaxConversionLength;
char m_pList[256];
ConversionInfo_t m_pReplacements[256];
};
#define BEGIN_CHAR_CONVERSION( _name, _delimiter, _escapeChar ) \
static CUtlCharConversion::ConversionArray_t s_pConversionArray ## _name[] = {
#define END_CHAR_CONVERSION( _name, _delimiter, _escapeChar ) \
}; \
CUtlCharConversion _name( _escapeChar, _delimiter, sizeof( s_pConversionArray ## _name ) / sizeof( CUtlCharConversion::ConversionArray_t ), s_pConversionArray ## _name );
#define BEGIN_CUSTOM_CHAR_CONVERSION( _className, _name, _delimiter, _escapeChar ) \
static CUtlCharConversion::ConversionArray_t s_pConversionArray ## _name[] = {
#define END_CUSTOM_CHAR_CONVERSION( _className, _name, _delimiter, _escapeChar ) \
}; \
_className _name( _escapeChar, _delimiter, sizeof( s_pConversionArray ## _name ) / sizeof( CUtlCharConversion::ConversionArray_t ), s_pConversionArray ## _name );
//-----------------------------------------------------------------------------
// Character conversions for C strings
//-----------------------------------------------------------------------------
CUtlCharConversion *GetCStringCharConversion();
//-----------------------------------------------------------------------------
// Character conversions for quoted strings, with no escape sequences
//-----------------------------------------------------------------------------
CUtlCharConversion *GetNoEscCharConversion();
//-----------------------------------------------------------------------------
// Macro to set overflow functions easily
//-----------------------------------------------------------------------------
#define SetUtlBufferOverflowFuncs( _get, _put ) \
SetOverflowFuncs( static_cast <UtlBufferOverflowFunc_t>( _get ), static_cast <UtlBufferOverflowFunc_t>( _put ) )
//-----------------------------------------------------------------------------
// Command parsing..
//-----------------------------------------------------------------------------
class CUtlBuffer
{
public:
enum SeekType_t
{
SEEK_HEAD = 0,
SEEK_CURRENT,
SEEK_TAIL
};
// flags
enum BufferFlags_t
{
TEXT_BUFFER = 0x1, // Describes how get + put work (as strings, or binary)
EXTERNAL_GROWABLE = 0x2, // This is used w/ external buffers and causes the utlbuf to switch to reallocatable memory if an overflow happens when Putting.
CONTAINS_CRLF = 0x4, // For text buffers only, does this contain \n or \n\r?
READ_ONLY = 0x8, // For external buffers; prevents null termination from happening.
AUTO_TABS_DISABLED = 0x10, // Used to disable/enable push/pop tabs
};
// Overflow functions when a get or put overflows
typedef bool (CUtlBuffer::*UtlBufferOverflowFunc_t)( int nSize );
// Constructors for growable + external buffers for serialization/unserialization
CUtlBuffer( int growSize = 0, int initSize = 0, int nFlags = 0 );
CUtlBuffer( const void* pBuffer, int size, int nFlags = 0 );
// This one isn't actually defined so that we catch contructors that are trying to pass a bool in as the third param.
CUtlBuffer( const void *pBuffer, int size, bool crap );
unsigned char GetFlags() const;
// NOTE: This will assert if you attempt to recast it in a way that
// is not compatible. The only valid conversion is binary-> text w/CRLF
void SetBufferType( bool bIsText, bool bContainsCRLF );
// Makes sure we've got at least this much memory
void EnsureCapacity( int num );
// Attaches the buffer to external memory....
void SetExternalBuffer( void* pMemory, int nSize, int nInitialPut, int nFlags = 0 );
bool IsExternallyAllocated() const;
// Takes ownership of the passed memory, including freeing it when this buffer is destroyed.
void AssumeMemory( void *pMemory, int nSize, int nInitialPut, int nFlags = 0 );
// copies data from another buffer
void CopyBuffer( const CUtlBuffer &buffer );
void CopyBuffer( const void *pubData, int cubData );
void Swap( CUtlBuffer &buf );
void Swap( CUtlMemory<uint8> &mem );
FORCEINLINE void ActivateByteSwappingIfBigEndian( void )
{
if ( IsX360() )
ActivateByteSwapping( true );
}
// Controls endian-ness of binary utlbufs - default matches the current platform
void ActivateByteSwapping( bool bActivate );
void SetBigEndian( bool bigEndian );
bool IsBigEndian( void );
// Resets the buffer; but doesn't free memory
void Clear();
// Clears out the buffer; frees memory
void Purge();
// Read stuff out.
// Binary mode: it'll just read the bits directly in, and characters will be
// read for strings until a null character is reached.
// Text mode: it'll parse the file, turning text #s into real numbers.
// GetString will read a string until a space is reached
char GetChar( );
unsigned char GetUnsignedChar( );
short GetShort( );
unsigned short GetUnsignedShort( );
int GetInt( );
int64 GetInt64( );
int GetIntHex( );
unsigned int GetUnsignedInt( );
float GetFloat( );
double GetDouble( );
template <size_t maxLenInChars> void GetString( char( &pString )[maxLenInChars] )
{
GetStringInternal( pString, maxLenInChars );
}
void GetStringManualCharCount( char *pString, size_t maxLenInChars )
{
GetStringInternal( pString, maxLenInChars );
}
void Get( void* pMem, int size );
void GetLine( char* pLine, int nMaxChars = 0 );
// Used for getting objects that have a byteswap datadesc defined
template <typename T> void GetObjects( T *dest, int count = 1 );
// This will get at least 1 byte and up to nSize bytes.
// It will return the number of bytes actually read.
int GetUpTo( void *pMem, int nSize );
// This version of GetString converts \" to \\ and " to \, etc.
// It also reads a " at the beginning and end of the string
void GetDelimitedString( CUtlCharConversion *pConv, char *pString, int nMaxChars = 0 );
char GetDelimitedChar( CUtlCharConversion *pConv );
// This will return the # of characters of the string about to be read out
// NOTE: The count will *include* the terminating 0!!
// In binary mode, it's the number of characters until the next 0
// In text mode, it's the number of characters until the next space.
int PeekStringLength();
// This version of PeekStringLength converts \" to \\ and " to \, etc.
// It also reads a " at the beginning and end of the string
// NOTE: The count will *include* the terminating 0!!
// In binary mode, it's the number of characters until the next 0
// In text mode, it's the number of characters between "s (checking for \")
// Specifying false for bActualSize will return the pre-translated number of characters
// including the delimiters and the escape characters. So, \n counts as 2 characters when bActualSize == false
// and only 1 character when bActualSize == true
int PeekDelimitedStringLength( CUtlCharConversion *pConv, bool bActualSize = true );
// Just like scanf, but doesn't work in binary mode
int Scanf( SCANF_FORMAT_STRING const char* pFmt, ... );
int VaScanf( const char* pFmt, va_list list );
// Eats white space, advances Get index
void EatWhiteSpace();
// Eats C++ style comments
bool EatCPPComment();
// (For text buffers only)
// Parse a token from the buffer:
// Grab all text that lies between a starting delimiter + ending delimiter
// (skipping whitespace that leads + trails both delimiters).
// If successful, the get index is advanced and the function returns true,
// otherwise the index is not advanced and the function returns false.
bool ParseToken( const char *pStartingDelim, const char *pEndingDelim, char* pString, int nMaxLen );
// Advance the get index until after the particular string is found
// Do not eat whitespace before starting. Return false if it failed
// String test is case-insensitive.
bool GetToken( const char *pToken );
// Parses the next token, given a set of character breaks to stop at
// Returns the length of the token parsed in bytes (-1 if none parsed)
int ParseToken( characterset_t *pBreaks, char *pTokenBuf, int nMaxLen, bool bParseComments = true );
// Write stuff in
// Binary mode: it'll just write the bits directly in, and strings will be
// written with a null terminating character
// Text mode: it'll convert the numbers to text versions
// PutString will not write a terminating character
void PutChar( char c );
void PutUnsignedChar( unsigned char uc );
void PutUint64( uint64 ub );
void PutInt16( int16 s16 );
void PutShort( short s );
void PutUnsignedShort( unsigned short us );
void PutInt( int i );
void PutInt64( int64 i );
void PutUnsignedInt( unsigned int u );
void PutFloat( float f );
void PutDouble( double d );
void PutString( const char* pString );
void Put( const void* pMem, int size );
// Used for putting objects that have a byteswap datadesc defined
template <typename T> void PutObjects( T *src, int count = 1 );
// This version of PutString converts \ to \\ and " to \", etc.
// It also places " at the beginning and end of the string
void PutDelimitedString( CUtlCharConversion *pConv, const char *pString );
void PutDelimitedChar( CUtlCharConversion *pConv, char c );
// Just like printf, writes a terminating zero in binary mode
void Printf( PRINTF_FORMAT_STRING const char* pFmt, ... ) FMTFUNCTION( 2, 3 );
void VaPrintf( const char* pFmt, va_list list );
// What am I writing (put)/reading (get)?
void* PeekPut( int offset = 0 );
const void* PeekGet( int offset = 0 ) const;
const void* PeekGet( int nMaxSize, int nOffset );
// Where am I writing (put)/reading (get)?
int TellPut( ) const;
int TellGet( ) const;
// What's the most I've ever written?
int TellMaxPut( ) const;
// How many bytes remain to be read?
// NOTE: This is not accurate for streaming text files; it overshoots
int GetBytesRemaining() const;
// Change where I'm writing (put)/reading (get)
void SeekPut( SeekType_t type, int offset );
void SeekGet( SeekType_t type, int offset );
// Buffer base
const void* Base() const;
void* Base();
// Returns the base as a const char*, only valid in text mode.
const char *String() const;
// memory allocation size, does *not* reflect size written or read,
// use TellPut or TellGet for that
int Size() const;
// Am I a text buffer?
bool IsText() const;
// Can I grow if I'm externally allocated?
bool IsGrowable() const;
// Am I valid? (overflow or underflow error), Once invalid it stays invalid
bool IsValid() const;
// Do I contain carriage return/linefeeds?
bool ContainsCRLF() const;
// Am I read-only
bool IsReadOnly() const;
// Converts a buffer from a CRLF buffer to a CR buffer (and back)
// Returns false if no conversion was necessary (and outBuf is left untouched)
// If the conversion occurs, outBuf will be cleared.
bool ConvertCRLF( CUtlBuffer &outBuf );
// Push/pop pretty-printing tabs
void PushTab();
void PopTab();
// Temporarily disables pretty print
void EnableTabs( bool bEnable );
protected:
// error flags
enum
{
PUT_OVERFLOW = 0x1,
GET_OVERFLOW = 0x2,
MAX_ERROR_FLAG = GET_OVERFLOW,
};
void SetOverflowFuncs( UtlBufferOverflowFunc_t getFunc, UtlBufferOverflowFunc_t putFunc );
bool OnPutOverflow( int nSize );
bool OnGetOverflow( int nSize );
protected:
// Checks if a get/put is ok
bool CheckPut( int size );
bool CheckGet( int size );
void AddNullTermination( );
// Methods to help with pretty-printing
bool WasLastCharacterCR();
void PutTabs();
// Help with delimited stuff
char GetDelimitedCharInternal( CUtlCharConversion *pConv );
void PutDelimitedCharInternal( CUtlCharConversion *pConv, char c );
// Default overflow funcs
bool PutOverflow( int nSize );
bool GetOverflow( int nSize );
// Does the next bytes of the buffer match a pattern?
bool PeekStringMatch( int nOffset, const char *pString, int nLen );
// Peek size of line to come, check memory bound
int PeekLineLength();
// How much whitespace should I skip?
int PeekWhiteSpace( int nOffset );
// Checks if a peek get is ok
bool CheckPeekGet( int nOffset, int nSize );
// Call this to peek arbitrarily long into memory. It doesn't fail unless
// it can't read *anything* new
bool CheckArbitraryPeekGet( int nOffset, int &nIncrement );
void GetStringInternal( char *pString, size_t maxLenInChars );
template <typename T> void GetType( T& dest, const char *pszFmt );
template <typename T> void GetTypeBin( T& dest );
template <typename T> void GetObject( T *src );
template <typename T> void PutType( T src, const char *pszFmt );
template <typename T> void PutTypeBin( T src );
template <typename T> void PutObject( T *src );
CUtlMemory<unsigned char> m_Memory;
int m_Get;
int m_Put;
unsigned char m_Error;
unsigned char m_Flags;
unsigned char m_Reserved;
#if defined( _X360 )
unsigned char pad;
#endif
int m_nTab;
int m_nMaxPut;
int m_nOffset;
UtlBufferOverflowFunc_t m_GetOverflowFunc;
UtlBufferOverflowFunc_t m_PutOverflowFunc;
CByteswap m_Byteswap;
};
// Stream style output operators for CUtlBuffer
inline CUtlBuffer &operator<<( CUtlBuffer &b, char v )
{
b.PutChar( v );
return b;
}
inline CUtlBuffer &operator<<( CUtlBuffer &b, unsigned char v )
{
b.PutUnsignedChar( v );
return b;
}
inline CUtlBuffer &operator<<( CUtlBuffer &b, short v )
{
b.PutShort( v );
return b;
}
inline CUtlBuffer &operator<<( CUtlBuffer &b, unsigned short v )
{
b.PutUnsignedShort( v );
return b;
}
inline CUtlBuffer &operator<<( CUtlBuffer &b, int v )
{
b.PutInt( v );
return b;
}
inline CUtlBuffer &operator<<( CUtlBuffer &b, unsigned int v )
{
b.PutUnsignedInt( v );
return b;
}
inline CUtlBuffer &operator<<( CUtlBuffer &b, float v )
{
b.PutFloat( v );
return b;
}
inline CUtlBuffer &operator<<( CUtlBuffer &b, double v )
{
b.PutDouble( v );
return b;
}
inline CUtlBuffer &operator<<( CUtlBuffer &b, const char *pv )
{
b.PutString( pv );
return b;
}
inline CUtlBuffer &operator<<( CUtlBuffer &b, const Vector &v )
{
b << v.x << " " << v.y << " " << v.z;
return b;
}
inline CUtlBuffer &operator<<( CUtlBuffer &b, const Vector2D &v )
{
b << v.x << " " << v.y;
return b;
}
class CUtlInplaceBuffer : public CUtlBuffer
{
public:
CUtlInplaceBuffer( int growSize = 0, int initSize = 0, int nFlags = 0 );
//
// Routines returning buffer-inplace-pointers
//
public:
//
// Upon success, determines the line length, fills out the pointer to the
// beginning of the line and the line length, advances the "get" pointer
// offset by the line length and returns "true".
//
// If end of file is reached or upon error returns "false".
//
// Note: the returned length of the line is at least one character because the
// trailing newline characters are also included as part of the line.
//
// Note: the pointer returned points into the local memory of this buffer, in
// case the buffer gets relocated or destroyed the pointer becomes invalid.
//
// e.g.: -------------
//
// char *pszLine;
// int nLineLen;
// while ( pUtlInplaceBuffer->InplaceGetLinePtr( &pszLine, &nLineLen ) )
// {
// ...
// }
//
// -------------
//
// @param ppszInBufferPtr on return points into this buffer at start of line
// @param pnLineLength on return holds num bytes accessible via (*ppszInBufferPtr)
//
// @returns true if line was successfully read
// false when EOF is reached or error occurs
//
bool InplaceGetLinePtr( /* out */ char **ppszInBufferPtr, /* out */ int *pnLineLength );
//
// Determines the line length, advances the "get" pointer offset by the line length,
// replaces the newline character with null-terminator and returns the initial pointer
// to now null-terminated line.
//
// If end of file is reached or upon error returns NULL.
//
// Note: the pointer returned points into the local memory of this buffer, in
// case the buffer gets relocated or destroyed the pointer becomes invalid.
//
// e.g.: -------------
//
// while ( char *pszLine = pUtlInplaceBuffer->InplaceGetLinePtr() )
// {
// ...
// }
//
// -------------
//
// @returns ptr-to-zero-terminated-line if line was successfully read and buffer modified
// NULL when EOF is reached or error occurs
//
char * InplaceGetLinePtr( void );
};
//-----------------------------------------------------------------------------
// Where am I reading?
//-----------------------------------------------------------------------------
inline int CUtlBuffer::TellGet( ) const
{
return m_Get;
}
//-----------------------------------------------------------------------------
// How many bytes remain to be read?
//-----------------------------------------------------------------------------
inline int CUtlBuffer::GetBytesRemaining() const
{
return m_nMaxPut - TellGet();
}
//-----------------------------------------------------------------------------
// What am I reading?
//-----------------------------------------------------------------------------
inline const void* CUtlBuffer::PeekGet( int offset ) const
{
return &m_Memory[ m_Get + offset - m_nOffset ];
}
//-----------------------------------------------------------------------------
// Unserialization
//-----------------------------------------------------------------------------
template <typename T>
inline void CUtlBuffer::GetObject( T *dest )
{
if ( CheckGet( sizeof(T) ) )
{
if ( !m_Byteswap.IsSwappingBytes() || ( sizeof( T ) == 1 ) )
{
*dest = *(T *)PeekGet();
}
else
{
m_Byteswap.SwapFieldsToTargetEndian<T>( dest, (T*)PeekGet() );
}
m_Get += sizeof(T);
}
else
{
Q_memset( dest, 0, sizeof(T) );
}
}
template <typename T>
inline void CUtlBuffer::GetObjects( T *dest, int count )
{
for ( int i = 0; i < count; ++i, ++dest )
{
GetObject<T>( dest );
}
}
template <typename T>
inline void CUtlBuffer::GetTypeBin( T &dest )
{
if ( CheckGet( sizeof(T) ) )
{
if ( !m_Byteswap.IsSwappingBytes() || ( sizeof( T ) == 1 ) )
{
dest = *(T *)PeekGet();
}
else
{
m_Byteswap.SwapBufferToTargetEndian<T>( &dest, (T*)PeekGet() );
}
m_Get += sizeof(T);
}
else
{
dest = 0;
}
}
template <>
inline void CUtlBuffer::GetTypeBin< float >( float &dest )
{
if ( CheckGet( sizeof( float ) ) )
{
uintptr_t pData = (uintptr_t)PeekGet();
if ( IsX360() && ( pData & 0x03 ) )
{
// handle unaligned read
((unsigned char*)&dest)[0] = ((unsigned char*)pData)[0];
((unsigned char*)&dest)[1] = ((unsigned char*)pData)[1];
((unsigned char*)&dest)[2] = ((unsigned char*)pData)[2];
((unsigned char*)&dest)[3] = ((unsigned char*)pData)[3];
}
else
{
// aligned read
dest = *(float *)pData;
}
if ( m_Byteswap.IsSwappingBytes() )
{
m_Byteswap.SwapBufferToTargetEndian< float >( &dest, &dest );
}
m_Get += sizeof( float );
}
else
{
dest = 0;
}
}
template <typename T>
inline void CUtlBuffer::GetType( T &dest, const char *pszFmt )
{
if (!IsText())
{
GetTypeBin( dest );
}
else
{
dest = 0;
Scanf( pszFmt, &dest );
}
}
inline char CUtlBuffer::GetChar( )
{
char c;
GetType( c, "%c" );
return c;
}
inline unsigned char CUtlBuffer::GetUnsignedChar( )
{
unsigned char c;
GetType( c, "%u" );
return c;
}
inline short CUtlBuffer::GetShort( )
{
short s;
GetType( s, "%d" );
return s;
}
inline unsigned short CUtlBuffer::GetUnsignedShort( )
{
unsigned short s;
GetType( s, "%u" );
return s;
}
inline int CUtlBuffer::GetInt( )
{
int i;
GetType( i, "%d" );
return i;
}
inline int64 CUtlBuffer::GetInt64( )
{
int64 i;
GetType( i, "%lld" );
return i;
}
inline int CUtlBuffer::GetIntHex( )
{
int i;
GetType( i, "%x" );
return i;
}
inline unsigned int CUtlBuffer::GetUnsignedInt( )
{
unsigned int u;
GetType( u, "%u" );
return u;
}
inline float CUtlBuffer::GetFloat( )
{
float f;
GetType( f, "%f" );
return f;
}
inline double CUtlBuffer::GetDouble( )
{
double d;
GetType( d, "%f" );
return d;
}
//-----------------------------------------------------------------------------
// Where am I writing?
//-----------------------------------------------------------------------------
inline unsigned char CUtlBuffer::GetFlags() const
{
return m_Flags;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
inline bool CUtlBuffer::IsExternallyAllocated() const
{
return m_Memory.IsExternallyAllocated();
}
//-----------------------------------------------------------------------------
// Where am I writing?
//-----------------------------------------------------------------------------
inline int CUtlBuffer::TellPut( ) const
{
return m_Put;
}
//-----------------------------------------------------------------------------
// What's the most I've ever written?
//-----------------------------------------------------------------------------
inline int CUtlBuffer::TellMaxPut( ) const
{
return m_nMaxPut;
}
//-----------------------------------------------------------------------------
// What am I reading?
//-----------------------------------------------------------------------------
inline void* CUtlBuffer::PeekPut( int offset )
{
return &m_Memory[m_Put + offset - m_nOffset];
}
//-----------------------------------------------------------------------------
// Various put methods
//-----------------------------------------------------------------------------
template <typename T>
inline void CUtlBuffer::PutObject( T *src )
{
if ( CheckPut( sizeof(T) ) )
{
if ( !m_Byteswap.IsSwappingBytes() || ( sizeof( T ) == 1 ) )
{
*(T *)PeekPut() = *src;
}
else
{
m_Byteswap.SwapFieldsToTargetEndian<T>( (T*)PeekPut(), src );
}
m_Put += sizeof(T);
AddNullTermination();
}
}
template <typename T>
inline void CUtlBuffer::PutObjects( T *src, int count )
{
for ( int i = 0; i < count; ++i, ++src )
{
PutObject<T>( src );
}
}
template <typename T>
inline void CUtlBuffer::PutTypeBin( T src )
{
if ( CheckPut( sizeof(T) ) )
{
if ( !m_Byteswap.IsSwappingBytes() || ( sizeof( T ) == 1 ) )
{
*(T *)PeekPut() = src;
}
else
{
m_Byteswap.SwapBufferToTargetEndian<T>( (T*)PeekPut(), &src );
}
m_Put += sizeof(T);
AddNullTermination();
}
}
template <typename T>
inline void CUtlBuffer::PutType( T src, const char *pszFmt )
{
if (!IsText())
{
PutTypeBin( src );
}
else
{
Printf( pszFmt, src );
}
}
//-----------------------------------------------------------------------------
// Methods to help with pretty-printing
//-----------------------------------------------------------------------------
inline bool CUtlBuffer::WasLastCharacterCR()
{
if ( !IsText() || (TellPut() == 0) )
return false;
return ( *( const char * )PeekPut( -1 ) == '\n' );
}
inline void CUtlBuffer::PutTabs()
{
int nTabCount = ( m_Flags & AUTO_TABS_DISABLED ) ? 0 : m_nTab;
for (int i = nTabCount; --i >= 0; )
{
PutTypeBin<char>( '\t' );
}
}
//-----------------------------------------------------------------------------
// Push/pop pretty-printing tabs
//-----------------------------------------------------------------------------
inline void CUtlBuffer::PushTab( )
{
++m_nTab;
}
inline void CUtlBuffer::PopTab()
{
if ( --m_nTab < 0 )
{
m_nTab = 0;
}
}
//-----------------------------------------------------------------------------
// Temporarily disables pretty print
//-----------------------------------------------------------------------------
inline void CUtlBuffer::EnableTabs( bool bEnable )
{
if ( bEnable )
{
m_Flags &= ~AUTO_TABS_DISABLED;
}
else
{
m_Flags |= AUTO_TABS_DISABLED;
}
}
inline void CUtlBuffer::PutChar( char c )
{
if ( WasLastCharacterCR() )
{
PutTabs();
}
PutTypeBin( c );
}
inline void CUtlBuffer::PutUnsignedChar( unsigned char c )
{
PutType( c, "%u" );
}
inline void CUtlBuffer::PutUint64( uint64 ub )
{
PutType( ub, "%llu" );
}
inline void CUtlBuffer::PutInt16( int16 s16 )
{
PutType( s16, "%d" );
}
inline void CUtlBuffer::PutShort( short s )
{
PutType( s, "%d" );
}
inline void CUtlBuffer::PutUnsignedShort( unsigned short s )
{
PutType( s, "%u" );
}
inline void CUtlBuffer::PutInt( int i )
{
PutType( i, "%d" );
}
inline void CUtlBuffer::PutInt64( int64 i )
{
PutType( i, "%llu" );
}
inline void CUtlBuffer::PutUnsignedInt( unsigned int u )
{
PutType( u, "%u" );
}
inline void CUtlBuffer::PutFloat( float f )
{
PutType( f, "%f" );
}
inline void CUtlBuffer::PutDouble( double d )
{
PutType( d, "%f" );
}
//-----------------------------------------------------------------------------
// Am I a text buffer?
//-----------------------------------------------------------------------------
inline bool CUtlBuffer::IsText() const
{
return (m_Flags & TEXT_BUFFER) != 0;
}
//-----------------------------------------------------------------------------
// Can I grow if I'm externally allocated?
//-----------------------------------------------------------------------------
inline bool CUtlBuffer::IsGrowable() const
{
return (m_Flags & EXTERNAL_GROWABLE) != 0;
}