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.

368 lines
6.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include <io.h>
  9. #include <stdio.h>
  10. #include <windows.h>
  11. #include "classcheck_util.h"
  12. #include "icodeprocessor.h"
  13. #include "tier0/dbg.h"
  14. //-----------------------------------------------------------------------------
  15. // Purpose:
  16. // Input : depth -
  17. // *fmt -
  18. // ... -
  19. //-----------------------------------------------------------------------------
  20. void vprint( int depth, const char *fmt, ... )
  21. {
  22. char string[ 8192 ];
  23. va_list va;
  24. va_start( va, fmt );
  25. vsprintf( string, fmt, va );
  26. va_end( va );
  27. FILE *fp = NULL;
  28. if ( processor->GetLogFile() )
  29. {
  30. fp = fopen( "log.txt", "ab" );
  31. }
  32. while ( depth-- > 0 )
  33. {
  34. printf( " " );
  35. OutputDebugString( " " );
  36. if ( fp )
  37. {
  38. fprintf( fp, " " );
  39. }
  40. }
  41. ::printf( "%s", string );
  42. OutputDebugString( string );
  43. if ( fp )
  44. {
  45. char *p = string;
  46. while ( *p )
  47. {
  48. if ( *p == '\n' )
  49. {
  50. fputc( '\r', fp );
  51. }
  52. fputc( *p, fp );
  53. p++;
  54. }
  55. fclose( fp );
  56. }
  57. }
  58. bool com_ignorecolons = false; // YWB: Ignore colons as token separators in COM_Parse
  59. bool com_ignoreinlinecomment = false;
  60. static bool s_com_token_unget = false;
  61. char com_token[1024];
  62. int linesprocessed = 0;
  63. //-----------------------------------------------------------------------------
  64. // Purpose:
  65. //-----------------------------------------------------------------------------
  66. void CC_UngetToken( void )
  67. {
  68. s_com_token_unget = true;
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose:
  72. // Input : ch -
  73. // Output : Returns true on success, false on failure.
  74. //-----------------------------------------------------------------------------
  75. bool CC_IsBreakChar( char ch )
  76. {
  77. bool brk = false;
  78. switch ( ch )
  79. {
  80. case '{':
  81. case '}':
  82. case ')':
  83. case '(':
  84. case '[':
  85. case ']':
  86. case '\'':
  87. case '/':
  88. case ',':
  89. case ';':
  90. case '<':
  91. case '>':
  92. brk = true;
  93. break;
  94. case ':':
  95. if ( !com_ignorecolons )
  96. brk = true;
  97. break;
  98. default:
  99. break;
  100. }
  101. return brk;
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Purpose:
  105. // Input : *data -
  106. // Output : char
  107. //-----------------------------------------------------------------------------
  108. char *CC_ParseToken(char *data)
  109. {
  110. int c;
  111. int len;
  112. if ( s_com_token_unget )
  113. {
  114. s_com_token_unget = false;
  115. return data;
  116. }
  117. len = 0;
  118. com_token[0] = 0;
  119. if (!data)
  120. return NULL;
  121. // skip whitespace
  122. skipwhite:
  123. while ( (c = *data) <= ' ')
  124. {
  125. if (c == 0)
  126. return NULL; // end of file;
  127. if ( c== '\n' )
  128. {
  129. linesprocessed++;
  130. }
  131. data++;
  132. }
  133. // skip // comments
  134. if ( !com_ignoreinlinecomment )
  135. {
  136. if (c=='/' && data[1] == '/')
  137. {
  138. while (*data && *data != '\n')
  139. data++;
  140. goto skipwhite;
  141. }
  142. }
  143. if ( c == '/' && data[1] == '*' )
  144. {
  145. while (data[0] && data[1] && !( data[0] == '*' && data[1] == '/' ) )
  146. {
  147. if ( *data == '\n' )
  148. {
  149. linesprocessed++;
  150. }
  151. data++;
  152. }
  153. if ( data[0] == '*' && data[1] == '/' )
  154. {
  155. data+=2;
  156. }
  157. goto skipwhite;
  158. }
  159. // handle quoted strings specially
  160. bool isLstring = data[0] == 'L' && (data[1] == '\"' );
  161. if ( isLstring )
  162. {
  163. com_token[len++] = (char)c;
  164. return data+1;
  165. }
  166. if ( c == '\"' )
  167. {
  168. data++;
  169. bool bEscapeSequence = false;
  170. while (1)
  171. {
  172. Assert( len < 1024 );
  173. if ( len >= 1024 )
  174. {
  175. com_token[ len -1 ] = 0;
  176. return data;
  177. }
  178. c = *data++;
  179. if ( (c=='\"' && !bEscapeSequence) || !c && len < sizeof( com_token ) - 1 )
  180. {
  181. com_token[len] = 0;
  182. return data;
  183. }
  184. bEscapeSequence = ( c == '\\' );
  185. com_token[len] = (char)c;
  186. len++;
  187. }
  188. }
  189. // parse single characters
  190. if ( CC_IsBreakChar( (char)c ) )
  191. {
  192. Assert( len < 1024 );
  193. com_token[len] = (char)c;
  194. len++;
  195. com_token[len] = 0;
  196. return data+1;
  197. }
  198. // parse a regular word
  199. do
  200. {
  201. Assert( len < 1024 );
  202. com_token[len] = (char)c;
  203. data++;
  204. len++;
  205. c = *data;
  206. if ( CC_IsBreakChar( (char)c ) )
  207. break;
  208. } while (c>32 && len < sizeof( com_token ) - 1);
  209. com_token[len] = 0;
  210. return data;
  211. }
  212. //-----------------------------------------------------------------------------
  213. // Purpose:
  214. // Input : *name -
  215. // *len -
  216. // Output : unsigned char
  217. //-----------------------------------------------------------------------------
  218. unsigned char *COM_LoadFile( const char *name, int *len)
  219. {
  220. FILE *fp;
  221. fp = fopen( name, "rb" );
  222. if ( !fp )
  223. {
  224. *len = 0;
  225. return NULL;
  226. }
  227. fseek( fp, 0, SEEK_END );
  228. *len = ftell( fp );
  229. fseek( fp, 0, SEEK_SET );
  230. unsigned char *buffer = new unsigned char[ *len + 1 ];
  231. fread( buffer, *len, 1, fp );
  232. fclose( fp );
  233. buffer[ *len ] = 0;
  234. return buffer;
  235. }
  236. //-----------------------------------------------------------------------------
  237. // Purpose:
  238. // Input : *buffer -
  239. //-----------------------------------------------------------------------------
  240. void COM_FreeFile( unsigned char *buffer )
  241. {
  242. delete[] buffer;
  243. }
  244. //-----------------------------------------------------------------------------
  245. // Purpose:
  246. // Input : *dir -
  247. // Output : Returns true on success, false on failure.
  248. //-----------------------------------------------------------------------------
  249. bool COM_DirectoryExists( const char *dir )
  250. {
  251. if ( !_access( dir, 0 ) )
  252. return true;
  253. return false;
  254. }
  255. //-----------------------------------------------------------------------------
  256. // Purpose:
  257. // Input : *input -
  258. // Output : char
  259. //-----------------------------------------------------------------------------
  260. char *CC_ParseUntilEndOfLine( char *input )
  261. {
  262. while (*input && *input != '\n')
  263. input++;
  264. return input;
  265. }
  266. //-----------------------------------------------------------------------------
  267. // Purpose:
  268. // Input : *input -
  269. // *ch -
  270. // *breakchar -
  271. // Output : char
  272. //-----------------------------------------------------------------------------
  273. char *CC_RawParseChar( char *input, const char *ch, char *breakchar )
  274. {
  275. bool done = false;
  276. int listlen = strlen( ch );
  277. do
  278. {
  279. input = CC_ParseToken( input );
  280. if ( strlen( com_token ) <= 0 )
  281. break;
  282. if ( strlen( com_token ) == 1 )
  283. {
  284. for ( int i = 0; i < listlen; i++ )
  285. {
  286. if ( com_token[ 0 ] == ch[ i ] )
  287. {
  288. *breakchar = ch [ i ];
  289. done = true;
  290. break;
  291. }
  292. }
  293. }
  294. } while ( !done );
  295. return input;
  296. }
  297. //-----------------------------------------------------------------------------
  298. // Purpose:
  299. // Input : *input -
  300. // *pairing -
  301. // Output : char
  302. //-----------------------------------------------------------------------------
  303. char *CC_DiscardUntilMatchingCharIncludingNesting( char *input, const char *pairing )
  304. {
  305. int nestcount = 1;
  306. do
  307. {
  308. input = CC_ParseToken( input );
  309. if ( strlen( com_token ) <= 0 )
  310. break;
  311. if ( strlen( com_token ) == 1 )
  312. {
  313. if ( com_token[ 0 ] == pairing[ 0 ] )
  314. {
  315. nestcount++;
  316. }
  317. else if ( com_token[ 0 ] == pairing[ 1 ] )
  318. {
  319. nestcount--;
  320. }
  321. }
  322. } while ( nestcount != 0 );
  323. return input;
  324. }