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.

138 lines
2.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=====================================================================================//
  6. #include "platform.h"
  7. #include "filesystem.h"
  8. #include "filesystem_helpers.h"
  9. #include "characterset.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. // wordbreak parsing set
  13. static characterset_t g_BreakSet, g_BreakSetIncludingColons;
  14. static void InitializeCharacterSets()
  15. {
  16. static bool s_CharacterSetInitialized = false;
  17. if (!s_CharacterSetInitialized)
  18. {
  19. CharacterSetBuild( &g_BreakSet, "{}()'" );
  20. CharacterSetBuild( &g_BreakSetIncludingColons, "{}()':" );
  21. s_CharacterSetInitialized = true;
  22. }
  23. }
  24. const char* ParseFile( const char* pFileBytes, char* pToken, bool* pWasQuoted, characterset_t *pCharSet )
  25. {
  26. if (pWasQuoted)
  27. *pWasQuoted = false;
  28. if (!pFileBytes)
  29. return 0;
  30. InitializeCharacterSets();
  31. // YWB: Ignore colons as token separators in COM_Parse
  32. static bool com_ignorecolons = false;
  33. characterset_t& breaks = pCharSet ? *pCharSet : (com_ignorecolons ? g_BreakSet : g_BreakSetIncludingColons);
  34. int c;
  35. int len = 0;
  36. pToken[0] = 0;
  37. // skip whitespace
  38. skipwhite:
  39. while ( (c = *pFileBytes) <= ' ')
  40. {
  41. if (c == 0)
  42. return 0; // end of file;
  43. pFileBytes++;
  44. }
  45. // skip // comments
  46. if (c=='/' && pFileBytes[1] == '/')
  47. {
  48. while (*pFileBytes && *pFileBytes != '\n')
  49. pFileBytes++;
  50. goto skipwhite;
  51. }
  52. // skip c-style comments
  53. if (c=='/' && pFileBytes[1] == '*' )
  54. {
  55. // Skip "/*"
  56. pFileBytes += 2;
  57. while ( *pFileBytes )
  58. {
  59. if ( *pFileBytes == '*' &&
  60. pFileBytes[1] == '/' )
  61. {
  62. pFileBytes += 2;
  63. break;
  64. }
  65. pFileBytes++;
  66. }
  67. goto skipwhite;
  68. }
  69. // handle quoted strings specially
  70. if (c == '\"')
  71. {
  72. if (pWasQuoted)
  73. *pWasQuoted = true;
  74. pFileBytes++;
  75. while (1)
  76. {
  77. c = *pFileBytes++;
  78. if (c=='\"' || !c)
  79. {
  80. pToken[len] = 0;
  81. return pFileBytes;
  82. }
  83. pToken[len] = c;
  84. len++;
  85. }
  86. }
  87. // parse single characters
  88. if ( IN_CHARACTERSET( breaks, c ) )
  89. {
  90. pToken[len] = c;
  91. len++;
  92. pToken[len] = 0;
  93. return pFileBytes+1;
  94. }
  95. // parse a regular word
  96. do
  97. {
  98. pToken[len] = c;
  99. pFileBytes++;
  100. len++;
  101. c = *pFileBytes;
  102. if ( IN_CHARACTERSET( breaks, c ) )
  103. break;
  104. } while (c>32);
  105. pToken[len] = 0;
  106. return pFileBytes;
  107. }
  108. char* ParseFile( char* pFileBytes, char* pToken, bool* pWasQuoted )
  109. {
  110. return (char*)ParseFile( (const char*)pFileBytes, pToken, pWasQuoted );
  111. }