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.

362 lines
6.7 KiB

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