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.

423 lines
7.8 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 "mdlcheck_util.h"
  12. #include "tier0/dbg.h"
  13. #include "utlvector.h"
  14. extern bool uselogfile;
  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 ( uselogfile )
  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. //-----------------------------------------------------------------------------
  60. // Purpose:
  61. // Input : depth -
  62. // *fmt -
  63. // ... -
  64. //-----------------------------------------------------------------------------
  65. CUtlVector< char * > printQueue;
  66. void vprint_queued( int depth, const char *fmt, ... )
  67. {
  68. char string[ 8192 ];
  69. strnset( string, ' ', depth * 2 );
  70. va_list va;
  71. va_start( va, fmt );
  72. vsprintf( &string[depth * 2], fmt, va );
  73. va_end( va );
  74. printQueue.AddToTail( strdup( string ) );
  75. }
  76. void dump_print_queue( )
  77. {
  78. FILE *fp = NULL;
  79. if ( uselogfile )
  80. {
  81. fp = fopen( "log.txt", "ab" );
  82. }
  83. for (int i = 0; i < printQueue.Count(); i++)
  84. {
  85. ::printf( "%s", printQueue[i] );
  86. OutputDebugString( printQueue[i] );
  87. if ( fp )
  88. {
  89. char *p = printQueue[i];
  90. while ( *p )
  91. {
  92. if ( *p == '\n' )
  93. {
  94. fputc( '\r', fp );
  95. }
  96. fputc( *p, fp );
  97. p++;
  98. }
  99. }
  100. }
  101. if (fp)
  102. {
  103. fclose( fp );
  104. }
  105. printQueue.RemoveAll();
  106. }
  107. bool com_ignorecolons = false; // YWB: Ignore colons as token separators in COM_Parse
  108. bool com_ignoreinlinecomment = false;
  109. static bool s_com_token_unget = false;
  110. char com_token[1024];
  111. int linesprocessed = 0;
  112. //-----------------------------------------------------------------------------
  113. // Purpose:
  114. //-----------------------------------------------------------------------------
  115. void CC_UngetToken( void )
  116. {
  117. s_com_token_unget = true;
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose:
  121. // Input : ch -
  122. // Output : Returns true on success, false on failure.
  123. //-----------------------------------------------------------------------------
  124. bool CC_IsBreakChar( char ch )
  125. {
  126. bool brk = false;
  127. switch ( ch )
  128. {
  129. case '{':
  130. case '}':
  131. case ')':
  132. case '(':
  133. case '[':
  134. case ']':
  135. // case '\'':
  136. // case '/':
  137. case ',':
  138. case ';':
  139. case '<':
  140. case '>':
  141. brk = true;
  142. break;
  143. case ':':
  144. if ( !com_ignorecolons )
  145. brk = true;
  146. break;
  147. default:
  148. break;
  149. }
  150. return brk;
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Purpose:
  154. // Input : *data -
  155. // Output : char
  156. //-----------------------------------------------------------------------------
  157. char *CC_ParseToken(char *data)
  158. {
  159. int c;
  160. int len;
  161. if ( s_com_token_unget )
  162. {
  163. s_com_token_unget = false;
  164. return data;
  165. }
  166. len = 0;
  167. com_token[0] = 0;
  168. if (!data)
  169. return NULL;
  170. // skip whitespace
  171. skipwhite:
  172. while ( (c = *data) <= ' ')
  173. {
  174. if (c == 0)
  175. return NULL; // end of file;
  176. if ( c== '\n' )
  177. {
  178. linesprocessed++;
  179. }
  180. data++;
  181. }
  182. // skip // comments
  183. if ( !com_ignoreinlinecomment )
  184. {
  185. if (c=='/' && data[1] == '/')
  186. {
  187. while (*data && *data != '\n')
  188. data++;
  189. goto skipwhite;
  190. }
  191. }
  192. if ( c == '/' && data[1] == '*' )
  193. {
  194. while (data[0] && data[1] && !( data[0] == '*' && data[1] == '/' ) )
  195. {
  196. if ( *data == '\n' )
  197. {
  198. linesprocessed++;
  199. }
  200. data++;
  201. }
  202. if ( data[0] == '*' && data[1] == '/' )
  203. {
  204. data+=2;
  205. }
  206. goto skipwhite;
  207. }
  208. // handle quoted strings specially
  209. bool isLstring = data[0] == 'L' && (data[1] == '\"' );
  210. if ( isLstring )
  211. {
  212. com_token[len++] = (char)c;
  213. return data+1;
  214. }
  215. if ( c == '\"' )
  216. {
  217. data++;
  218. bool bEscapeSequence = false;
  219. while (1)
  220. {
  221. Assert( len < 1024 );
  222. c = *data++;
  223. if ( (c=='\"' && !bEscapeSequence) || !c && len < sizeof( com_token ) - 1 )
  224. {
  225. com_token[len] = 0;
  226. return data;
  227. }
  228. bEscapeSequence = ( c == '\\' );
  229. com_token[len] = (char)c;
  230. len++;
  231. }
  232. }
  233. // parse single characters
  234. if ( CC_IsBreakChar( (char)c ) )
  235. {
  236. Assert( len < 1024 );
  237. com_token[len] = (char)c;
  238. len++;
  239. com_token[len] = 0;
  240. return data+1;
  241. }
  242. // parse a regular word
  243. do
  244. {
  245. Assert( len < 1024 );
  246. com_token[len] = (char)c;
  247. data++;
  248. len++;
  249. c = *data;
  250. if ( CC_IsBreakChar( (char)c ) )
  251. break;
  252. } while (c>32 && len < sizeof( com_token ) - 1);
  253. com_token[len] = 0;
  254. return data;
  255. }
  256. //-----------------------------------------------------------------------------
  257. // Purpose:
  258. // Input : *name -
  259. // *len -
  260. // Output : unsigned char
  261. //-----------------------------------------------------------------------------
  262. unsigned char *COM_LoadFile( const char *name, int *len)
  263. {
  264. FILE *fp;
  265. fp = fopen( name, "rb" );
  266. if ( !fp )
  267. {
  268. *len = 0;
  269. return NULL;
  270. }
  271. fseek( fp, 0, SEEK_END );
  272. *len = ftell( fp );
  273. fseek( fp, 0, SEEK_SET );
  274. unsigned char *buffer = new unsigned char[ *len + 1 ];
  275. fread( buffer, *len, 1, fp );
  276. fclose( fp );
  277. buffer[ *len ] = 0;
  278. return buffer;
  279. }
  280. //-----------------------------------------------------------------------------
  281. // Purpose:
  282. // Input : *buffer -
  283. //-----------------------------------------------------------------------------
  284. void COM_FreeFile( unsigned char *buffer )
  285. {
  286. delete[] buffer;
  287. }
  288. //-----------------------------------------------------------------------------
  289. // Purpose:
  290. // Input : *dir -
  291. // Output : Returns true on success, false on failure.
  292. //-----------------------------------------------------------------------------
  293. bool COM_DirectoryExists( const char *dir )
  294. {
  295. if ( !_access( dir, 0 ) )
  296. return true;
  297. return false;
  298. }
  299. //-----------------------------------------------------------------------------
  300. // Purpose:
  301. // Input : *input -
  302. // Output : char
  303. //-----------------------------------------------------------------------------
  304. char *CC_ParseUntilEndOfLine( char *input )
  305. {
  306. while (*input && *input != '\n')
  307. input++;
  308. return input;
  309. }
  310. //-----------------------------------------------------------------------------
  311. // Purpose:
  312. // Input : *input -
  313. // *ch -
  314. // *breakchar -
  315. // Output : char
  316. //-----------------------------------------------------------------------------
  317. char *CC_RawParseChar( char *input, const char *ch, char *breakchar )
  318. {
  319. bool done = false;
  320. int listlen = strlen( ch );
  321. do
  322. {
  323. input = CC_ParseToken( input );
  324. if ( strlen( com_token ) <= 0 )
  325. break;
  326. if ( strlen( com_token ) == 1 )
  327. {
  328. for ( int i = 0; i < listlen; i++ )
  329. {
  330. if ( com_token[ 0 ] == ch[ i ] )
  331. {
  332. *breakchar = ch [ i ];
  333. done = true;
  334. break;
  335. }
  336. }
  337. }
  338. } while ( !done );
  339. return input;
  340. }
  341. //-----------------------------------------------------------------------------
  342. // Purpose:
  343. // Input : *input -
  344. // *pairing -
  345. // Output : char
  346. //-----------------------------------------------------------------------------
  347. char *CC_DiscardUntilMatchingCharIncludingNesting( char *input, const char *pairing )
  348. {
  349. int nestcount = 1;
  350. do
  351. {
  352. input = CC_ParseToken( input );
  353. if ( strlen( com_token ) <= 0 )
  354. break;
  355. if ( strlen( com_token ) == 1 )
  356. {
  357. if ( com_token[ 0 ] == pairing[ 0 ] )
  358. {
  359. nestcount++;
  360. }
  361. else if ( com_token[ 0 ] == pairing[ 1 ] )
  362. {
  363. nestcount--;
  364. }
  365. }
  366. } while ( nestcount != 0 );
  367. return input;
  368. }