Team Fortress 2 Source Code as on 22/4/2020
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.

129 lines
2.5 KiB

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