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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Implements an interface for reading and writing heirarchical
// text files of key value pairs. The format of the file is as follows:
//
// chunkname0
// {
// "key0" "value0"
// "key1" "value1"
// ...
// "keyN" "valueN"
// chunkname1
// {
// "key0" "value0"
// "key1" "value1"
// ...
// "keyN" "valueN"
// }
// }
// ...
// chunknameN
// {
// "key0" "value0"
// "key1" "value1"
// ...
// "keyN" "valueN"
// }
//
// The chunk names are not necessarily unique, nor are the key names, unless the
// parsing application requires them to be.
//
// $NoKeywords: $
//=============================================================================//
#include <fcntl.h>
#ifdef _WIN32
#include <io.h>
#endif
#include <math.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "chunkfile.h"
#include "mathlib/vector.h"
#include "mathlib/vector4d.h"
#include "tier1/strtools.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CChunkHandlerMap::CChunkHandlerMap(void)
{
m_pHandlers = NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Destructor. Frees handler list.
//-----------------------------------------------------------------------------
CChunkHandlerMap::~CChunkHandlerMap(void)
{
ChunkHandlerInfoNode_t *pNode = m_pHandlers;
while (pNode != NULL)
{
ChunkHandlerInfoNode_t *pPrev = pNode;
pNode = pNode->pNext;
delete pPrev;
}
}
//-----------------------------------------------------------------------------
// Purpose: Adds a chunk handler to the handler list.
// Input : pszChunkName - Name of chunk to be handled.
// pfnHandler - Address of handler callback function.
// pData - Data to pass to the handler callback.
//-----------------------------------------------------------------------------
void CChunkHandlerMap::AddHandler(const char *pszChunkName, ChunkHandler_t pfnHandler, void *pData)
{
ChunkHandlerInfoNode_t *pNew = new ChunkHandlerInfoNode_t;
Q_strncpy(pNew->Handler.szChunkName, pszChunkName, sizeof( pNew->Handler.szChunkName ));
pNew->Handler.pfnHandler = pfnHandler;
pNew->Handler.pData = pData;
pNew->pNext = NULL;
if (m_pHandlers == NULL)
{
m_pHandlers = pNew;
}
else
{
ChunkHandlerInfoNode_t *pNode = m_pHandlers;
while (pNode->pNext != NULL)
{
pNode = pNode->pNext;
}
pNode->pNext = pNew;
}
}
//-----------------------------------------------------------------------------
// Purpose: Sets the callback for error handling within this chunk's scope.
// Input : pfnHandler -
// pData -
//-----------------------------------------------------------------------------
void CChunkHandlerMap::SetErrorHandler(ChunkErrorHandler_t pfnHandler, void *pData)
{
m_pfnErrorHandler = pfnHandler;
m_pErrorData = pData;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : ppData -
// Output : ChunkErrorHandler_t
//-----------------------------------------------------------------------------
ChunkErrorHandler_t CChunkHandlerMap::GetErrorHandler(void **ppData)
{
*ppData = m_pErrorData;
return(m_pfnErrorHandler);
}
//-----------------------------------------------------------------------------
// Purpose: Gets the handler for a given chunk name, if one has been set.
// Input : pszChunkName - Name of chunk.
// ppfnHandler - Receives the address of the callback function.
// ppData - Receives the context data for the given chunk.
// Output : Returns true if a handler was found, false if not.
//-----------------------------------------------------------------------------
ChunkHandler_t CChunkHandlerMap::GetHandler(const char *pszChunkName, void **ppData)
{
ChunkHandlerInfoNode_t *pNode = m_pHandlers;
while (pNode != NULL)
{
if (!stricmp(pNode->Handler.szChunkName, pszChunkName))
{
*ppData = pNode->Handler.pData;
return(pNode->Handler.pfnHandler);
}
pNode = pNode->pNext;
}
return(false);
}
//-----------------------------------------------------------------------------
// Purpose: Constructor. Initializes data members.
//-----------------------------------------------------------------------------
CChunkFile::CChunkFile(void)
{
m_hFile = NULL;
m_nCurrentDepth = 0;
m_szIndent[0] = '\0';
m_nHandlerStackDepth = 0;
m_DefaultChunkHandler = 0;
}
//-----------------------------------------------------------------------------
// Purpose: Destructor. Closes the file if it is currently open.
//-----------------------------------------------------------------------------
CChunkFile::~CChunkFile(void)
{
if (m_hFile != NULL)
{
fclose(m_hFile);
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pszChunkName -
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::BeginChunk(const char *pszChunkName)
{
//
// Write the chunk name and open curly.
//
char szBuf[MAX_KEYVALUE_LEN];
Q_snprintf(szBuf, sizeof( szBuf ), "%s\r\n%s{", pszChunkName, m_szIndent);
ChunkFileResult_t eResult = WriteLine(szBuf);
//
// Update the indentation depth.
//
if (eResult == ChunkFile_Ok)
{
m_nCurrentDepth++;
BuildIndentString(m_szIndent, m_nCurrentDepth);
}
return(eResult);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CChunkFile::BuildIndentString(char *pszDest, int nDepth)
{
if (nDepth >= 0)
{
for (int i = 0; i < nDepth; i++)
{
pszDest[i] = '\t';
}
pszDest[nDepth] = '\0';
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::Close(void)
{
if (m_hFile != NULL)
{
fclose(m_hFile);
m_hFile = NULL;
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::EndChunk(void)
{
if (m_nCurrentDepth > 0)
{
m_nCurrentDepth--;
BuildIndentString(m_szIndent, m_nCurrentDepth);
}
WriteLine("}");
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose: Returns a string explaining the last error that occurred.
//-----------------------------------------------------------------------------
const char *CChunkFile::GetErrorText(ChunkFileResult_t eResult)
{
static char szError[MAX_KEYVALUE_LEN];
switch (eResult)
{
case ChunkFile_UnexpectedEOF:
{
Q_strncpy(szError, "unexpected end of file", sizeof( szError ) );
break;
}
case ChunkFile_UnexpectedSymbol:
{
Q_snprintf(szError, sizeof( szError ), "unexpected symbol '%s'", m_szErrorToken);
break;
}
case ChunkFile_OpenFail:
{
Q_snprintf(szError, sizeof( szError ), "%s", strerror(errno)) ;
break;
}
case ChunkFile_StringTooLong:
{
Q_strncpy(szError, "unterminated string or string too long", sizeof( szError ) );
break;
}
default:
{
Q_snprintf(szError, sizeof( szError ), "error %d", eResult);
}
}
return(m_TokenReader.Error(szError));
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : eError -
//-----------------------------------------------------------------------------
void CChunkFile::HandleError(const char *szChunkName, ChunkFileResult_t eError)
{
// UNDONE: dispatch errors to the error handler.
// - keep track of current chunkname for reporting errors
// - use the last non-NULL handler that was pushed onto the stack?
// - need a return code to determine whether to abort parsing?
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::HandleChunk(const char *szChunkName)
{
// See if the default handler wants it?
if( m_DefaultChunkHandler )
{
ChunkFileResult_t testResult = m_DefaultChunkHandler( this, m_pDefaultChunkHandlerData, szChunkName );
if( testResult == ChunkFile_Ok )
{
return ChunkFile_Ok;
}
}
//
// If there is an active handler map...
//
if (m_nHandlerStackDepth > 0)
{
CChunkHandlerMap *pHandler = m_HandlerStack[m_nHandlerStackDepth - 1];
//
// If a chunk handler was found in the handler map...
//
void *pData;
ChunkHandler_t pfnHandler = pHandler->GetHandler(szChunkName, &pData);
if (pfnHandler != NULL)
{
// Dispatch this chunk to the handler.
ChunkFileResult_t eResult = pfnHandler(this, pData);
if (eResult != ChunkFile_Ok)
{
return(eResult);
}
}
else
{
//
// No handler for this chunk. Skip to the matching close curly brace.
//
int nDepth = 1;
ChunkFileResult_t eResult;
do
{
ChunkType_t eChunkType;
char szKey[MAX_KEYVALUE_LEN];
char szValue[MAX_KEYVALUE_LEN];
while ((eResult = ReadNext(szKey, szValue, sizeof(szValue), eChunkType)) == ChunkFile_Ok)
{
if (eChunkType == ChunkType_Chunk)
{
nDepth++;
}
}
if (eResult == ChunkFile_EndOfChunk)
{
eResult = ChunkFile_Ok;
nDepth--;
}
else if (eResult == ChunkFile_EOF)
{
return(ChunkFile_UnexpectedEOF);
}
} while ((nDepth) && (eResult == ChunkFile_Ok));
}
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose: Opens the chunk file for reading or writing.
// Input : pszFileName - Path of file to open.
// eMode - ChunkFile_Read or ChunkFile_Write.
// Output : Returns ChunkFile_Ok on success, ChunkFile_Fail on failure.
// UNDONE: boolean return value?
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::Open(const char *pszFileName, ChunkFileOpenMode_t eMode)
{
if (eMode == ChunkFile_Read)
{
// UNDONE: TokenReader encapsulates file - unify reading and writing to use the same file I/O.
// UNDONE: Support in-memory parsing.
if (m_TokenReader.Open(pszFileName))
{
m_nCurrentDepth = 0;
}
else
{
return(ChunkFile_OpenFail);
}
}
else if (eMode == ChunkFile_Write)
{
m_hFile = fopen(pszFileName, "wb");
if (m_hFile == NULL)
{
return(ChunkFile_OpenFail);
}
m_nCurrentDepth = 0;
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose: Removes the topmost set of chunk handlers.
//-----------------------------------------------------------------------------
void CChunkFile::PopHandlers(void)
{
if (m_nHandlerStackDepth > 0)
{
m_nHandlerStackDepth--;
}
}
void CChunkFile::SetDefaultChunkHandler( DefaultChunkHandler_t pHandler, void *pData )
{
m_DefaultChunkHandler = pHandler;
m_pDefaultChunkHandlerData = pData;}
//-----------------------------------------------------------------------------
// Purpose: Adds a set of chunk handlers to the top of the handler stack.
// Input : pHandlerMap - Object containing the list of chunk handlers.
//-----------------------------------------------------------------------------
void CChunkFile::PushHandlers(CChunkHandlerMap *pHandlerMap)
{
if (m_nHandlerStackDepth < MAX_INDENT_DEPTH)
{
m_HandlerStack[m_nHandlerStackDepth] = pHandlerMap;
m_nHandlerStackDepth++;
}
}
//-----------------------------------------------------------------------------
// Purpose: Reads the next term from the chunk file. The type of term read is
// returned in the eChunkType parameter.
// Input : szName - Name of key or chunk.
// szValue - If eChunkType is ChunkType_Key, contains the value of the key.
// nValueSize - Size of the buffer pointed to by szValue.
// eChunkType - ChunkType_Key or ChunkType_Chunk.
// Output : Returns ChunkFile_Ok on success, an error code if a parsing error occurs.
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::ReadNext(char *szName, char *szValue, int nValueSize, ChunkType_t &eChunkType)
{
// HACK: pass in buffer sizes?
trtoken_t eTokenType = m_TokenReader.NextToken(szName, MAX_KEYVALUE_LEN);
if (eTokenType != TOKENEOF)
{
switch (eTokenType)
{
case IDENT:
case STRING:
{
char szNext[MAX_KEYVALUE_LEN];
trtoken_t eNextTokenType;
//
// Read the next token to determine what we have.
//
eNextTokenType = m_TokenReader.NextToken(szNext, sizeof(szNext));
switch (eNextTokenType)
{
case OPERATOR:
{
if (!stricmp(szNext, "{"))
{
// Beginning of new chunk.
m_nCurrentDepth++;
eChunkType = ChunkType_Chunk;
szValue[0] = '\0';
return(ChunkFile_Ok);
}
else
{
// Unexpected symbol.
Q_strncpy(m_szErrorToken, szNext, sizeof( m_szErrorToken ) );
return(ChunkFile_UnexpectedSymbol);
}
}
case STRING:
case IDENT:
{
// Key value pair.
Q_strncpy(szValue, szNext, nValueSize );
eChunkType = ChunkType_Key;
return(ChunkFile_Ok);
}
case TOKENEOF:
{
// Unexpected end of file.
return(ChunkFile_UnexpectedEOF);
}
case TOKENSTRINGTOOLONG:
{
// String too long or unterminated string.
return ChunkFile_StringTooLong;
}
}
}
case OPERATOR:
{
if (!stricmp(szName, "}"))
{
// End of current chunk.
m_nCurrentDepth--;
return(ChunkFile_EndOfChunk);
}
else
{
// Unexpected symbol.
Q_strncpy(m_szErrorToken, szName, sizeof( m_szErrorToken ) );
return(ChunkFile_UnexpectedSymbol);
}
}
case TOKENSTRINGTOOLONG:
{
return ChunkFile_StringTooLong;
}
}
}
if (m_nCurrentDepth != 0)
{
// End of file while within the scope of a chunk.
return(ChunkFile_UnexpectedEOF);
}
return(ChunkFile_EOF);
}
//-----------------------------------------------------------------------------
// Purpose: Reads the current chunk and dispatches keys and sub-chunks to the
// appropriate handler callbacks.
// Input : pfnKeyHandler - Callback for any key values in this chunk.
// pData - Data to pass to the key value callback function.
// Output : Normally returns ChunkFile_Ok or ChunkFile_EOF. Otherwise, returns
// a ChunkFile_xxx error code.
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::ReadChunk(KeyHandler_t pfnKeyHandler, void *pData)
{
//
// Read the keys and sub-chunks.
//
ChunkFileResult_t eResult;
do
{
char szName[MAX_KEYVALUE_LEN];
char szValue[MAX_KEYVALUE_LEN];
ChunkType_t eChunkType;
eResult = ReadNext(szName, szValue, sizeof(szValue), eChunkType);
if (eResult == ChunkFile_Ok)
{
if (eChunkType == ChunkType_Chunk)
{
//
// Dispatch sub-chunks to the appropriate handler.
//
eResult = HandleChunk(szName);
}
else if ((eChunkType == ChunkType_Key) && (pfnKeyHandler != NULL))
{
//
// Dispatch keys to the key value handler.
//
eResult = pfnKeyHandler(szName, szValue, pData);
}
}
} while (eResult == ChunkFile_Ok);
//
// Cover up ChunkFile_EndOfChunk results because the caller doesn't want to see them.
//
if (eResult == ChunkFile_EndOfChunk)
{
eResult = ChunkFile_Ok;
}
//
// Dispatch errors to the handler.
//
if ((eResult != ChunkFile_Ok) && (eResult != ChunkFile_EOF))
{
//HandleError("chunkname", eResult);
}
return(eResult);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszValue -
// pbBool -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CChunkFile::ReadKeyValueBool(const char *pszValue, bool &bBool)
{
int nValue = atoi(pszValue);
if (nValue > 0)
{
bBool = true;
}
else
{
bBool = false;
}
return(true);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszValue -
// pfFloat -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CChunkFile::ReadKeyValueFloat(const char *pszValue, float &flFloat)
{
flFloat = (float)atof(pszValue);
return(true);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszValue -
// pnInt -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CChunkFile::ReadKeyValueInt(const char *pszValue, int &nInt)
{
nInt = atoi(pszValue);
return(true);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszKey -
// r -
// g -
// b -
// Output :
//-----------------------------------------------------------------------------
bool CChunkFile::ReadKeyValueColor(const char *pszValue, unsigned char &chRed, unsigned char &chGreen, unsigned char &chBlue)
{
if (pszValue != NULL)
{
int r = 0;
int g = 0;
int b = 0;
if (sscanf(pszValue, "%d %d %d", &r, &g, &b) == 3)
{
chRed = r;
chGreen = g;
chBlue = b;
return(true);
}
}
return(false);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszValue -
// pfPoint -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CChunkFile::ReadKeyValuePoint(const char *pszValue, Vector &Point)
{
if (pszValue != NULL)
{
return(sscanf(pszValue, "(%f %f %f)", &Point.x, &Point.y, &Point.z) == 3);
}
return(false);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszValue -
// pfVector -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CChunkFile::ReadKeyValueVector2(const char *pszValue, Vector2D &vec)
{
if (pszValue != NULL)
{
return ( sscanf( pszValue, "[%f %f]", &vec.x, &vec.y) == 2 );
}
return(false);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszValue -
// pfVector -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CChunkFile::ReadKeyValueVector3(const char *pszValue, Vector &vec)
{
if (pszValue != NULL)
{
return(sscanf(pszValue, "[%f %f %f]", &vec.x, &vec.y, &vec.z) == 3);
}
return(false);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszValue -
// pfVector -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CChunkFile::ReadKeyValueVector4(const char *pszValue, Vector4D &vec)
{
if( pszValue != NULL )
{
return(sscanf(pszValue, "[%f %f %f %f]", &vec[0], &vec[1], &vec[2], &vec[3]) == 4);
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszLine -
// Output :
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::WriteKeyValue(const char *pszKey, const char *pszValue)
{
if ((pszKey != NULL) && (pszValue != NULL))
{
char szTemp[MAX_KEYVALUE_LEN];
Q_snprintf(szTemp, sizeof( szTemp ), "\"%s\" \"%s\"", pszKey, pszValue);
return(WriteLine(szTemp));
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszKey -
// bValue -
// Output :
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::WriteKeyValueBool(const char *pszKey, bool bValue)
{
if (pszKey != NULL)
{
char szBuf[MAX_KEYVALUE_LEN];
Q_snprintf(szBuf, sizeof( szBuf ), "\"%s\" \"%d\"", pszKey, (int)bValue);
return(WriteLine(szBuf));
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszKey -
// nValue -
// Output :
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::WriteKeyValueInt(const char *pszKey, int nValue)
{
if (pszKey != NULL)
{
char szBuf[MAX_KEYVALUE_LEN];
Q_snprintf(szBuf, sizeof( szBuf ), "\"%s\" \"%d\"", pszKey, nValue);
return(WriteLine(szBuf));
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszKey -
// fValue -
// Output :
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::WriteKeyValueFloat(const char *pszKey, float fValue)
{
if (pszKey != NULL)
{
char szBuf[MAX_KEYVALUE_LEN];
Q_snprintf(szBuf, sizeof( szBuf ), "\"%s\" \"%g\"", pszKey, (double)fValue);
return(WriteLine(szBuf));
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszKey -
// r -
// g -
// b -
// Output :
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::WriteKeyValueColor(const char *pszKey, unsigned char r, unsigned char g, unsigned char b)
{
if (pszKey != NULL)
{
char szBuf[MAX_KEYVALUE_LEN];
Q_snprintf(szBuf, sizeof( szBuf ), "\"%s\" \"%d %d %d\"", pszKey, (int)r, (int)g, (int)b);
return(WriteLine(szBuf));
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszKey -
// fVector -
// Output :
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::WriteKeyValuePoint(const char *pszKey, const Vector &Point)
{
if (pszKey != NULL)
{
char szBuf[MAX_KEYVALUE_LEN];
Q_snprintf(szBuf, sizeof( szBuf ), "\"%s\" \"(%g %g %g)\"", pszKey, (double)Point[0], (double)Point[1], (double)Point[2]);
return(WriteLine(szBuf));
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::WriteKeyValueVector2(const char *pszKey, const Vector2D &vec)
{
if (pszKey != NULL)
{
char szBuf[MAX_KEYVALUE_LEN];
Q_snprintf( szBuf, sizeof( szBuf ), "\"%s\" \"[%g %g]\"", pszKey, (double)vec.x, (double)vec.y );
return(WriteLine(szBuf));
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::WriteKeyValueVector3(const char *pszKey, const Vector &vec)
{
if (pszKey != NULL)
{
char szBuf[MAX_KEYVALUE_LEN];
Q_snprintf(szBuf, sizeof( szBuf ), "\"%s\" \"[%g %g %g]\"", pszKey, (double)vec.x, (double)vec.y, (double)vec.z);
return(WriteLine(szBuf));
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszKey -
// fVector -
// Output :
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::WriteKeyValueVector4(const char *pszKey, const Vector4D &vec)
{
if (pszKey != NULL)
{
char szBuf[MAX_KEYVALUE_LEN];
Q_snprintf(szBuf, sizeof( szBuf ), "\"%s\" \"[%g %g %g %g]\"", pszKey, (double)vec.x, (double)vec.y, (double)vec.z, (double)vec.w);
return( WriteLine( szBuf ) );
}
return( ChunkFile_Ok );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pszLine -
// Output :
//-----------------------------------------------------------------------------
ChunkFileResult_t CChunkFile::WriteLine(const char *pszLine)
{
if (pszLine != NULL)
{
//
// Write the indentation string.
//
if (m_nCurrentDepth > 0)
{
int nWritten = fwrite(m_szIndent, 1, m_nCurrentDepth, m_hFile);
if (nWritten != m_nCurrentDepth)
{
return(ChunkFile_Fail);
}
}
//
// Write the string.
//
int nLen = strlen(pszLine);
int nWritten = fwrite(pszLine, 1, nLen, m_hFile);
if (nWritten != nLen)
{
return(ChunkFile_Fail);
}
//
// Write the linefeed.
//
if (fwrite("\r\n", 1, 2, m_hFile) != 2)
{
return(ChunkFile_Fail);
}
}
return(ChunkFile_Ok);
}
|