summaryrefslogtreecommitdiff
path: root/utils/entcount/entcount.cpp
blob: 6982bed46f35cc8cb5b3bb4abfa191e6c8a7f564 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: searches through all bsp files in the current directory parsing out entity details
//
//=============================================================================//

#include <stdio.h>
#include <string.h>
#include <io.h>
#include <malloc.h>

#define max(x,y) ( ((x) > (y)) ? (x) : (y) )

void SetSearchWord( const char *searchWord );
char *FindSearchWord( char *buffer, char *bufend );
char *ParseToken( char *data, char *newToken );

void ClearTable( void );
void ClearUsageCountTable( void );
void AddToTable( const char *name );
void PrintOutTable( void );

void ParseFGD( char *buffer, char *bufend, const char *searchKey );

const char *g_UsageString = "usage:  entcount [-fgd <fgdfile>] [-nofgd] [-permap] [-onlyent <entname>] [-files <searchmask>]\n";


int main( int argc, char *argv[] )
{
	if ( argc < 2 )
	{
		printf( g_UsageString );
		return 0;
	}

	bool printPerMap = false;
	const char *filterEnt = NULL;
	const char *fgdFile = NULL;
	const char *fileMask = "*.bsp";

	// parse the arguments
	for ( int count = 1; count < argc; count++ )
	{
		if ( !stricmp( argv[count], "-permap" ) )
		{
			printPerMap = true;
		}
		else if ( !stricmp( argv[count], "-onlyent" ) )
		{
			count++;
			if ( count < argc )
			{
				filterEnt = argv[count];
			}
		}
		else if ( !stricmp( argv[count], "-fgd" ) )
		{
			count++;
			if ( count < argc )
			{
				fgdFile = argv[count];
			}
		}
		else if ( !stricmp( argv[count], "-files" ) )
		{
			count++;
			if ( count < argc )
			{
				fileMask = argv[count];
			}
		}
		else if ( !stricmp( argv[count], "-nofgd" ) )
		{
		}
		else
		{
			printf( "error: unknown parameter \"%s\"\n", argv[count] );
			printf( g_UsageString );
			return 1;
		}
	}

	// clear the entity accumulator table
	ClearTable();

	// open and parse the FGD, unless the -nofgd flag is specified
	if ( fgdFile && !filterEnt && !printPerMap )
	{
		FILE *f = fopen( fgdFile, "rb" );
		if ( !f )
		{
			printf( "error: could not open file %s\n", fgdFile );
			return 2;
		}

		int filelen;
		fseek( f, 0, SEEK_END );
		filelen = ftell( f );
		fseek( f, 0, SEEK_SET );

		// allocate and load into memory
		char *buffer = (char*)malloc( filelen );
		char *bufend = buffer + filelen;
		fread( buffer, filelen, 1, f );
		fclose( f );

		// search for all @pointclass, then @solidclass
		ParseFGD( buffer, bufend, "PointClass" );
		ParseFGD( buffer, bufend, "SolidClass" );

		// reset the usage counts to 0
		ClearUsageCountTable();	

		free( buffer );
	}

	// parse through all the bsp files
	_finddata_t fileinfo;
	int FHandle = _findfirst( fileMask, &fileinfo );
	
	if ( FHandle == -1 )
	{
		printf( "error: no files found in current directory\n" );
		return 1;
	}

	SetSearchWord( "\"classname\"" );

	do {
		// open the file
		FILE *f = fopen( fileinfo.name, "rb" );
		if ( !f )
		{
			printf( "error: couldn't open file %s\n", fileinfo.name );
			return 2;
		}

		// calculate file length
		int filelen;
		fseek( f, 0, SEEK_END );
		filelen = ftell( f );
		fseek( f, 0, SEEK_SET );

		// allocate and load into memory
		char *buffer = (char*)malloc( filelen );
		char *bufpos = buffer + strlen( "\"classname\"" ) - 1;
		char *bufend = buffer + filelen;
		fread( buffer, filelen, 1, f );
		fclose( f );

		bool entFound = false;

		while ( 1 )
		{
			bufpos = FindSearchWord( bufpos, bufend );
			if ( !bufpos )
				break;

			// find the next word
			static char Token[256];
			ParseToken( bufpos, Token );

			// add the word to the list, filtering if necessary
			if ( !filterEnt || !stricmp(filterEnt, Token) )
			{
				AddToTable( Token );
				entFound = true;
			}

			bufpos += strlen( Token );
		}

		free( buffer );

		// print the bsp name, if an ent is found, or we are not filtering for ents
		if ( entFound || !filterEnt )
			printf( "%s\n", fileinfo.name );

		if ( printPerMap )
		{
			PrintOutTable();
			ClearUsageCountTable();
		}

	} while ( _findnext(FHandle, &fileinfo) == 0 );

	PrintOutTable();

	return 0;
}

void ParseFGD( char *buffer, char *bufend, const char *searchKey )
{
	char *bufpos = buffer + strlen( searchKey ) - 1;
	SetSearchWord( searchKey );

	while ( 1 )
	{
		bufpos = FindSearchWord( bufpos, bufend );
		if ( !bufpos )
			break;

		// search for the corresponding '='
		while ( *bufpos != '=' )
			bufpos++;
		bufpos++;
		
		// find the classname
		static char Token[256];
		ParseToken( bufpos, Token );

		AddToTable( Token );

		bufpos += strlen( Token );
	}
}


/////////// entity table stuff //////////////
const int MAX_ENTS = 2000;
int NumEnts = 0;
char *EntNames[ MAX_ENTS ];
int EntUsage[ MAX_ENTS ];

void ClearTable( void )
{
	memset( EntNames, 0, sizeof(EntNames) );
	memset( EntUsage, 0, sizeof(EntUsage) );
}

void ClearUsageCountTable( void )
{
	memset( EntUsage, 0, sizeof(EntUsage) );
}

void AddToTable( const char *name )
{
	// search for it in the table
	for ( int i = 0; i < NumEnts;  i++ )
	{
		if ( EntNames[i] && !strcmp(EntNames[i], name) )
		{
			// it's already in the table
			// increment the usage count
			EntUsage[i] += 1;
			return;
		}
	}

	// append to the table
	EntNames[NumEnts] = (char*)malloc( strlen(name) + 1 );
	strcpy( EntNames[NumEnts], name );
	EntUsage[NumEnts] = 1;

	NumEnts++;
}

void PrintOutTable( void )
{
	while ( 1 )
	{
		// find the highest item
		int highestUsage = -1;
		int highestEnt = 0;

		for ( int i = 0; i < NumEnts; i++ )
		{
			if ( EntNames[i] && highestUsage < EntUsage[i] )
			{
				highestUsage = EntUsage[i];
				highestEnt = i;
			}
		}

		// check for no more ents
		if ( highestUsage == -1 )
			return;
		
		// display usage stats of item
		printf( " %5d  %s\n", highestUsage, EntNames[highestEnt] );

		// remove item from list
		free( EntNames[highestEnt] );
		EntNames[highestEnt] = NULL;
	}
}



////////// string search stuff ////////////

static unsigned char JumpTable[256];
static int SearchWordLen = 0;
static const char *SearchWord;

void SetSearchWord( const char *Word )
{
	SearchWord = Word;
	SearchWordLen = strlen( SearchWord );

	// build the jump table
	
	// initialize all values to jump the length of the string
	memset( JumpTable, SearchWordLen, sizeof(JumpTable) );

	// set the amount the searcher can jump forward, depending on the character
	for ( int i = 0; i < SearchWordLen; i++ )
	{
		JumpTable[ (unsigned char)SearchWord[i] ] = max( SearchWordLen - i - 1, 1 );
	}
}

char *FindSearchWord( char *buffer, char *bufend )
{

/*
	for ( int i = SearchWordLen-1; i >= 0; i-- )
	{
		if ( *buffer != SearchWord[i] )
		{
			buffer += ( JumpTable[ (unsigned char)(*(buffer + i - SearchWordLen + 1)) ] - 1 );

			// no more buffer to search
			if ( buffer >= bufend )
				return NULL;

			// reset search counter
			i = SearchWordLen;
		}
		else
		{
			// it's a match, move backwards to search
			buffer--;
		}
	}
*/

	while ( 1 )
	{
		if ( strnicmp(buffer - SearchWordLen, SearchWord, SearchWordLen) )
		{
			// strings not equal, jump ahead
			buffer += JumpTable[ (unsigned char)*buffer ];

			if ( buffer >= bufend )
				return NULL;
		}
		else
		{
			break;
		}
	}

	// we have a match!

	// return a pointer just past the found key
	return buffer;
}



/*
==============
ParseToken

Parse a token out of a string
outputs the parsed token into newToken
==============
*/
char *ParseToken( char *data, char *newToken )
{
	int             c;
	int             len;
	
	len = 0;
	newToken[0] = 0;
	
	if (!data)
		return NULL;
		
// skip whitespace
skipwhite:
	while ( (c = *data) <= ' ')
	{
		if (c == 0)
			return NULL;                    // end of file;
		data++;
	}
	
// skip // comments
	if (c=='/' && data[1] == '/')
	{
		while (*data && *data != '\n')
			data++;
		goto skipwhite;
	}
	

// handle quoted strings specially
	if (c == '\"')
	{
		data++;
		while ( len < 256 )
		{
			c = *data++;
			if (c=='\"' || !c)
			{
				newToken[len] = 0;
				return data;
			}
			newToken[len] = c;
			len++;
		}

		if ( len >= 256 )
		{
			len--;
			newToken[len] = 0;
		}
	}

// parse single characters
	if ( c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' )
	{
		newToken[len] = c;
		len++;
		newToken[len] = 0;
		return data+1;
	}

// parse a regular word
	do
	{
		newToken[len] = c;
		data++;
		len++;
		c = *data;
		if ( c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' )
			break;

		if ( len >= 256 )
		{
			len--;
			newToken[len] = 0;
			break;
		}

	} while (c>32);
	
	newToken[len] = 0;
	return data;
}