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.

240 lines
4.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include "utlbuffer.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. //-----------------------------------------------------------------------------
  12. // Purpose: Advances until non-whitespace hit
  13. //-----------------------------------------------------------------------------
  14. ucs2 *AdvanceOverWhitespace(ucs2 *Start)
  15. {
  16. while (*Start != 0 && iswspace(*Start))
  17. {
  18. Start++;
  19. }
  20. return Start;
  21. }
  22. //-----------------------------------------------------------------------------
  23. // Purpose:
  24. //-----------------------------------------------------------------------------
  25. ucs2 *ReadUnicodeToken(ucs2 *start, ucs2 *token, int tokenBufferSize, bool &quoted)
  26. {
  27. // skip over any whitespace
  28. start = AdvanceOverWhitespace(start);
  29. quoted = false;
  30. *token = 0;
  31. if (!*start)
  32. {
  33. return start;
  34. }
  35. // check to see if it's a quoted string
  36. if (*start == '\"')
  37. {
  38. quoted = true;
  39. // copy out the string until we hit an end quote
  40. start++;
  41. int count = 0;
  42. while (*start && *start != '\"' && count < tokenBufferSize-1)
  43. {
  44. // check for special characters
  45. if (*start == '\\' && *(start+1) == 'n')
  46. {
  47. start++;
  48. *token = '\n';
  49. }
  50. else if (*start == '\\' && *(start+1) == '\"')
  51. {
  52. start++;
  53. *token = '\"';
  54. }
  55. else
  56. {
  57. *token = *start;
  58. }
  59. start++;
  60. token++;
  61. count++;
  62. }
  63. if (*start == '\"')
  64. {
  65. start++;
  66. }
  67. }
  68. else
  69. {
  70. // copy out the string until we hit a whitespace
  71. int count = 0;
  72. while (*start && !iswspace(*start) && count < tokenBufferSize-1)
  73. {
  74. // no checking for special characters if it's not a quoted string
  75. *token = *start;
  76. start++;
  77. token++;
  78. count++;
  79. }
  80. }
  81. *token = 0;
  82. return start;
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose: Same as above but no translation of \n
  86. //-----------------------------------------------------------------------------
  87. ucs2 *ReadUnicodeTokenNoSpecial(ucs2 *start, ucs2 *token, int tokenBufferSize, bool &quoted)
  88. {
  89. // skip over any whitespace
  90. start = AdvanceOverWhitespace(start);
  91. quoted = false;
  92. *token = 0;
  93. if (!*start)
  94. {
  95. return start;
  96. }
  97. // check to see if it's a quoted string
  98. if (*start == '\"')
  99. {
  100. quoted = true;
  101. // copy out the string until we hit an end quote
  102. start++;
  103. int count = 0;
  104. while (*start && *start != '\"' && count < tokenBufferSize-1)
  105. {
  106. // check for special characters
  107. /*
  108. if (*start == '\\' && *(start+1) == 'n')
  109. {
  110. start++;
  111. *token = '\n';
  112. }
  113. else
  114. */
  115. if (*start == '\\' && *(start+1) == '\"')
  116. {
  117. start++;
  118. *token = '\"';
  119. }
  120. else
  121. {
  122. *token = *start;
  123. }
  124. start++;
  125. token++;
  126. count++;
  127. }
  128. if (*start == '\"')
  129. {
  130. start++;
  131. }
  132. }
  133. else
  134. {
  135. // copy out the string until we hit a whitespace
  136. int count = 0;
  137. while (*start && !iswspace(*start) && count < tokenBufferSize-1)
  138. {
  139. // no checking for special characters if it's not a quoted string
  140. *token = *start;
  141. start++;
  142. token++;
  143. count++;
  144. }
  145. }
  146. *token = 0;
  147. return start;
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Purpose: Returns the first character after the next EOL characters
  151. //-----------------------------------------------------------------------------
  152. ucs2 *ReadToEndOfLine(ucs2 *start)
  153. {
  154. if (!*start)
  155. return start;
  156. while (*start)
  157. {
  158. if (*start == 0x0D || *start== 0x0A)
  159. break;
  160. start++;
  161. }
  162. while (*start == 0x0D || *start== 0x0A)
  163. start++;
  164. return start;
  165. }
  166. //-----------------------------------------------------------------------------
  167. // Purpose: file writing
  168. //-----------------------------------------------------------------------------
  169. void WriteUnicodeString(CUtlBuffer &buf, const wchar_t *string, bool addQuotes)
  170. {
  171. if (addQuotes)
  172. {
  173. buf.PutUnsignedShort('\"');
  174. }
  175. for (const wchar_t *ws = string; *ws != 0; ws++)
  176. {
  177. // handle special characters
  178. if (addQuotes && *ws == '\"')
  179. {
  180. buf.PutUnsignedShort('\\');
  181. }
  182. // write the character
  183. buf.PutUnsignedShort(*ws);
  184. }
  185. if (addQuotes)
  186. {
  187. buf.PutUnsignedShort('\"');
  188. }
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Purpose: file writing
  192. //-----------------------------------------------------------------------------
  193. void WriteAsciiStringAsUnicode(CUtlBuffer &buf, const char *string, bool addQuotes)
  194. {
  195. if (addQuotes)
  196. {
  197. buf.PutUnsignedShort('\"');
  198. }
  199. for (const char *sz = string; *sz != 0; sz++)
  200. {
  201. // handle special characters
  202. if (addQuotes && *sz == '\"')
  203. {
  204. buf.PutUnsignedShort('\\');
  205. }
  206. buf.PutUnsignedShort(*sz);
  207. }
  208. if (addQuotes)
  209. {
  210. buf.PutUnsignedShort('\"');
  211. }
  212. }