Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
5.3 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef CHUNKFILE_H
  8. #define CHUNKFILE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <stdio.h>
  13. #include "tier2/tokenreader.h"
  14. #define MAX_INDENT_DEPTH 80
  15. #define MAX_KEYVALUE_LEN 1024
  16. class CChunkFile;
  17. class Vector2D;
  18. class Vector;
  19. class Vector4D;
  20. //
  21. // Modes for Open.
  22. //
  23. enum ChunkFileOpenMode_t
  24. {
  25. ChunkFile_Read = 0,
  26. ChunkFile_Write,
  27. };
  28. //
  29. // Return codes.
  30. //
  31. enum ChunkFileResult_t
  32. {
  33. ChunkFile_Ok = 0,
  34. ChunkFile_Fail,
  35. ChunkFile_OpenFail,
  36. ChunkFile_EndOfChunk,
  37. ChunkFile_EOF,
  38. ChunkFile_UnexpectedEOF,
  39. ChunkFile_UnexpectedSymbol,
  40. ChunkFile_OutOfMemory,
  41. ChunkFile_StringTooLong,
  42. ChunkFile_NotHandled
  43. };
  44. enum ChunkType_t
  45. {
  46. ChunkType_Key = 0,
  47. ChunkType_Chunk,
  48. };
  49. typedef ChunkFileResult_t (*DefaultChunkHandler_t)(CChunkFile *pFile, void *pData, char const *pChunkName);
  50. typedef ChunkFileResult_t (*ChunkHandler_t)(CChunkFile *pFile, void *pData);
  51. typedef ChunkFileResult_t (*KeyHandler_t)(const char *szKey, const char *szValue, void *pData);
  52. typedef bool (*ChunkErrorHandler_t)(CChunkFile *pFile, const char *szChunkName, void *pData);
  53. struct ChunkHandlerInfo_t
  54. {
  55. char szChunkName[80];
  56. ChunkHandler_t pfnHandler;
  57. void *pData;
  58. };
  59. struct ChunkHandlerInfoNode_t
  60. {
  61. ChunkHandlerInfo_t Handler;
  62. struct ChunkHandlerInfoNode_t *pNext;
  63. };
  64. //
  65. // Consider handling chunks with handler objects instead of callbacks.
  66. //
  67. //class CChunkHandler
  68. //{
  69. // virtual ChunkFileResult_t HandleChunk(const char *szName);
  70. // virtual ChunkFileResult_t HandleKey(const char *szKey, const char *szValue);
  71. // virtual bool HandleError(const char *szChunkName, ChunkFileResult_t eError);
  72. //};
  73. class CChunkHandlerMap
  74. {
  75. public:
  76. CChunkHandlerMap(void);
  77. ~CChunkHandlerMap(void);
  78. void AddHandler(const char *pszChunkName, ChunkHandler_t pfnHandler, void *pData);
  79. ChunkHandler_t GetHandler(const char *pszChunkName, void **pData);
  80. void SetErrorHandler(ChunkErrorHandler_t pfnHandler, void *pData);
  81. ChunkErrorHandler_t GetErrorHandler(void **pData);
  82. protected:
  83. ChunkHandlerInfoNode_t *m_pHandlers;
  84. ChunkErrorHandler_t m_pfnErrorHandler;
  85. void *m_pErrorData;
  86. };
  87. class CChunkFile
  88. {
  89. public:
  90. CChunkFile(void);
  91. ~CChunkFile(void);
  92. ChunkFileResult_t Open(const char *pszFileName, ChunkFileOpenMode_t eMode);
  93. ChunkFileResult_t Close(void);
  94. const char *GetErrorText(ChunkFileResult_t eResult);
  95. //
  96. // Functions for writing chunk files.
  97. //
  98. ChunkFileResult_t BeginChunk(const char *pszChunkName);
  99. ChunkFileResult_t EndChunk(void);
  100. ChunkFileResult_t WriteKeyValue(const char *pszKey, const char *pszValue);
  101. ChunkFileResult_t WriteKeyValueBool(const char *pszKey, bool bValue);
  102. ChunkFileResult_t WriteKeyValueColor(const char *pszKey, unsigned char r, unsigned char g, unsigned char b);
  103. ChunkFileResult_t WriteKeyValueFloat(const char *pszKey, float fValue);
  104. ChunkFileResult_t WriteKeyValueInt(const char *pszKey, int nValue);
  105. ChunkFileResult_t WriteKeyValuePoint(const char *pszKey, const Vector &Point);
  106. ChunkFileResult_t WriteKeyValueVector2(const char *pszKey, const Vector2D &vec);
  107. ChunkFileResult_t WriteKeyValueVector3(const char *pszKey, const Vector &vec);
  108. ChunkFileResult_t WriteKeyValueVector4( const char *pszKey, const Vector4D &vec);
  109. ChunkFileResult_t WriteLine(const char *pszLine);
  110. //
  111. // Functions for reading chunk files.
  112. //
  113. ChunkFileResult_t ReadChunk(KeyHandler_t pfnKeyHandler = NULL, void *pData = NULL);
  114. ChunkFileResult_t ReadNext(char *szKey, char *szValue, int nValueSize, ChunkType_t &eChunkType);
  115. ChunkFileResult_t HandleChunk(const char *szChunkName);
  116. void HandleError(const char *szChunkName, ChunkFileResult_t eError);
  117. // These functions should more really be named Parsexxx and possibly moved elsewhere.
  118. static bool ReadKeyValueBool(const char *pszValue, bool &bBool);
  119. static bool ReadKeyValueColor(const char *pszValue, unsigned char &chRed, unsigned char &chGreen, unsigned char &chBlue);
  120. static bool ReadKeyValueInt(const char *pszValue, int &nInt);
  121. static bool ReadKeyValueFloat(const char *pszValue, float &flFloat);
  122. static bool ReadKeyValuePoint(const char *pszValue, Vector &Point);
  123. static bool ReadKeyValueVector2(const char *pszValue, Vector2D &vec);
  124. static bool ReadKeyValueVector3(const char *pszValue, Vector &vec);
  125. static bool ReadKeyValueVector4( const char *pszValue, Vector4D &vec);
  126. // The default chunk handler gets called before any other chunk handlers.
  127. //
  128. // If the handler returns ChunkFile_Ok, then it goes into the chunk.
  129. // If the handler returns ChunkFile_NotHandled, then the chunk is
  130. // passed to the regular handlers.
  131. //
  132. // If you pass NULL in here, then it disables the default chunk handler.
  133. void SetDefaultChunkHandler( DefaultChunkHandler_t pHandler, void *pData );
  134. void PushHandlers(CChunkHandlerMap *pHandlerMap);
  135. void PopHandlers(void);
  136. protected:
  137. void BuildIndentString(char *pszDest, int nDepth);
  138. TokenReader m_TokenReader;
  139. FILE *m_hFile;
  140. char m_szErrorToken[80];
  141. char m_szIndent[MAX_INDENT_DEPTH];
  142. int m_nCurrentDepth;
  143. // See SetDefaultChunkHandler..
  144. DefaultChunkHandler_t m_DefaultChunkHandler;
  145. void *m_pDefaultChunkHandlerData;
  146. CChunkHandlerMap *m_HandlerStack[MAX_INDENT_DEPTH];
  147. int m_nHandlerStackDepth;
  148. };
  149. #endif // CHUNKFILE_H