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.

70 lines
1.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. =================//
  2. //
  3. // Read JSON-formatted data into KeyValues
  4. //
  5. //=============================================================================//
  6. #ifndef KEYVALUESJSON_H
  7. #define KEYVALUESJSON_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "keyvalues.h"
  12. class CUtlBuffer;
  13. /// JSON parser. Use this class when you need to customize the parsing.
  14. class KeyValuesJSONParser
  15. {
  16. public:
  17. KeyValuesJSONParser( const CUtlBuffer &buf );
  18. KeyValuesJSONParser( const char *pszText, int cbSize = -1 );
  19. ~KeyValuesJSONParser();
  20. /// Parse the whole string. If there's a problem, returns NULL and sets m_nLine,m_szErrMsg with more info.
  21. KeyValues *ParseFile();
  22. /// Error message is returned here, if there is one.
  23. char m_szErrMsg[ 1024 ];
  24. /// Line number of current token during parsing, or of the error, of pasring fails
  25. int m_nLine;
  26. private:
  27. bool ParseObject( KeyValues *pObject );
  28. bool ParseArray( KeyValues *pArray );
  29. bool ParseValue( KeyValues *pValue );
  30. void Init( const char *pszText, int cbSize );
  31. const char *m_cur;
  32. const char *m_end;
  33. enum
  34. {
  35. kToken_Err = -2, // An error has been discovered, don't parse anything else
  36. kToken_EOF = -1,
  37. kToken_String = 1,
  38. kToken_NumberInt = 2,
  39. kToken_NumberFloat = 3,
  40. kToken_True = 4,
  41. kToken_False = 5,
  42. kToken_Null = 6,
  43. };
  44. int m_eToken;
  45. CUtlVectorFixedGrowable<char,1024> m_vecTokenChars;
  46. void NextToken();
  47. void ParseNumberToken();
  48. void ParseStringToken();
  49. const char *GetTokenDebugText();
  50. };
  51. #ifdef _DEBUG
  52. extern void TestKeyValuesJSONParser();
  53. #endif
  54. #endif // KEYVALUESJSON_H