shader_to_h.cpp 1.74 KB
Newer Older
1
2
3
4
5
6
7
#include <iostream>
#include <fstream>
#include <sstream>

int main(int argc, char **argv)
{
	char buffer[512];
8
	for (int i=1; i< argc; ++i)
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
	{
		std::string filename(argv[i]);

		std::ifstream fs(filename.c_str(), std::ios::in);
		if (!fs.good())
		{
			std::cerr << "Unable to open file " << filename << std::endl;
			return 1;
		}

		size_t last_slash = filename.rfind('/');
		if (last_slash == std::string::npos)
		{
			last_slash = filename.rfind('\\');
			if (last_slash == std::string::npos)
				last_slash = 0;
			else
				++last_slash;
		}
		else
			++last_slash;
		
		std::string outName = filename.substr(last_slash,filename.size()-last_slash);
		
		std::stringstream ssi;
		std::stringstream sso;			
		
		std::ifstream fsi(outName.c_str(),std::ios::in);
		if (fsi.good())
		{
			while (!fsi.eof())
			{
				fsi.getline(buffer,512);
				if (!fsi.eof())
					ssi << buffer << std::endl ;
			}
			fsi.close();
		}
		
		
		// fist line
		fs.getline(buffer,512);				
		char *sub=buffer;
		while ((*sub=='/') || (*sub==' '))
			++sub;
		sso << "std::string "<<sub<< " =";

		// text of shader		
57
		unsigned int nbbl=0;
58
59
60
		while (!fs.eof())
		{
			fs.getline(buffer,512);
61
62
63
64
65
66
			//std::cout << buffer << std::endl;
			if (*buffer!=0)
			{
				for (unsigned int i=0; i<nbbl;++i)
					sso << std::endl <<"\"\\n\"";
				nbbl=0;
67
				sso << std::endl << "\"" << buffer <<"\\n\"";
68
69
70
			}
			else
				nbbl++;
71
		};
72
		sso << ";"<< std::endl<< std::endl;	
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
		
		std::string ssostr = sso.str();
		if (ssostr != ssi.str())
		{
			std::ofstream fso(outName.c_str(),std::ios::out);
			fso << ssostr;
			std::cout << "Shader_to_h: "<< outName << " copy"<< std::endl;
			fso.close();
		}
		else
			std::cout << "Shader_to_h: "<< outName << " ok"<< std::endl;
		
		fs.close();
		
	}
	
	
	return 0;
	
}