summaryrefslogtreecommitdiff
path: root/utils/tfstats/res2c/main.cpp
blob: 8d844cfee0786ab4e04d4cd39c2fcaaf96d44e27 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define NUM_PER_LINE 40

extern char* szHeaderFile;

void printUsage()
{
	printf("res2c <res file name> <c file name> <object name>\n");
}

char* id4filename(const char* filename)
{
	static char id[500];
	
	const char* read=filename;
	char *write=id;
	
	for (read;*read;read++)
	{
		//if first char
		if (read==filename)
		{
			if (isalpha(*read) || *read=='_')
				*write++=*read;
		}
		else if (isalnum(*read))
			*write++=*read;
	}
	*write++='s';
	*write++='r';
	*write++='c';
	*write++='\0';

	return id;
}
void main(int argc, const char* argv[])
{
	if (argc < 4)
	{
		printUsage();
		return;
	}
	char cppname[200];
	sprintf(cppname,"%s.cpp",argv[2]);
	char hname[200];
	sprintf(hname,"%s.h",argv[2]);
	
	FILE* f=fopen(argv[1],"rb");
	FILE* cppout=fopen(cppname,"at");
	FILE* hout=fopen(hname,"at");
	FILE* brheader=fopen("BinaryResource.h","wt");
	if (!brheader){printf("couldn't open %s to write\n","BinaryResource.h");exit(-1);}
	if (!f){printf("couldn't read %s\n",argv[1]);exit(-1);}
	if (!cppout){printf("couldn't open %s to write\n",argv[2]);exit(-1);}
	if (!hout){printf("couldn't open %s to write\n",argv[2]);exit(-1);}

	
	fprintf(brheader,szHeaderFile);
	fclose(brheader);
	
	
	fprintf(cppout,"\nunsigned char %s[]={\n",id4filename(argv[1]));
	
	int numLeft4Line=NUM_PER_LINE;
	
	unsigned char c;
	int result=fread(&c,sizeof(unsigned char),1,f);
	
	int numbytes=0;
	while (result)
	{
		//int longc=(*c)&0x000000ff;
		fprintf(cppout,"0x%02.2x,",c);
		numbytes++;
		if(--numLeft4Line==0)
		{
			numLeft4Line=NUM_PER_LINE;
			fprintf(cppout,"\n");
		}
		result=fread(&c,sizeof(unsigned char),1,f);
	}

	fprintf(cppout,"\n};\n\n");
	
	char* coloncolon=strstr(argv[3],"::");
	if (coloncolon!=NULL)
	{
		coloncolon+=2;
		fprintf(hout,"static CBinaryResource %s;\n",coloncolon);
		fprintf(cppout,"CBinaryResource %s(\"%s\",%li,%s);\n\n\n",argv[3],argv[1],numbytes,id4filename(argv[1]));
	}
	else
	{
		fprintf(hout,"//extern CBinaryResource g_%s;\n",argv[3]);
		fprintf(cppout,"CBinaryResource g_%s;\n",argv[3]);
	}
	fclose(cppout);
	fclose(hout);
	fclose(f);
}