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.

243 lines
4.8 KiB

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