00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <iostream.h>
00019 #include <fstream.h>
00020 #include <unistd.h>
00021 #include <errno.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024
00025 #include "vuString.h"
00026
00027 using namespace std;
00028
00029 void usage(const char* exe);
00030 bool readnumber(const char* str, int& number);
00031
00032 int main(int argc, char ** argv)
00033 {
00034 if(argc != 4) {
00035 usage(argv[0]);
00036 return -1;
00037 }
00038
00039 vuString fileName = argv[1];
00040 vuString outfileName = argv[2];
00041 vuString varName = argv[3];
00042
00043 ifstream is(fileName, ios::binary);
00044 if(!is) {
00045 cout << "error opening " << fileName << " for reading." << endl;
00046 return -1;
00047 }
00048
00049 ofstream os(outfileName);
00050 if(!os) {
00051 cout << "error opening " << outfileName << " for writing." << endl;
00052 return -1;
00053 }
00054
00055 is.seekg(0, ios::end);
00056 int size = is.tellg();
00057 is.seekg(0, ios::beg);
00058 if (size <= 0) {
00059 cout << "input file " << fileName << " has zero size." << endl;
00060 return -1;
00061 }
00062 cout << "reading " << size << " bytes from " << fileName << " ..." << endl;
00063
00064 os << "//Header for file " << fileName << " written in variable "
00065 << varName << endl << endl;
00066 os << "int " << varName << "_size = " << size << ";" << endl;
00067 os << "char " << varName << "["<<size<<"] = { " << endl;
00068
00069 char buffer[1024];
00070 is.read(buffer, 1024);
00071 int blen = is.gcount();
00072 int rest = size;
00073 os.flags(ios::showbase|ios::hex);
00074 os << (unsigned int)((unsigned char)buffer[0]);
00075 rest--;
00076 int pos = 1;
00077 while(rest) {
00078 if(pos == blen) {
00079 is.read(buffer, 1024);
00080 blen = is.gcount();
00081 pos = 0;
00082 }
00083 os << ", ";
00084 if(!(pos%8)) os << endl;
00085 os << hex << (unsigned int)((unsigned char)buffer[pos]);
00086 pos++;
00087 rest--;
00088 }
00089 os << "};" << endl;
00090 os.close();
00091 cout << "done." << endl;
00092 return 0;
00093 }
00094
00095 void usage(const char* exe)
00096 {
00097 const char *file = strrchr(exe,'/');
00098 if(!file)
00099 {
00100 file = strrchr(exe,'\\');
00101 if(file) file++;
00102 else file = exe;
00103 } else file++;
00104
00105 cout << "usage: " << file << " inputfile outputfile.h varname" << endl << endl;
00106 cout << "Creates a header with character array and size information from binary file." << endl;
00107 cout << "inputfile name of input file (some binary file, eg. png)"<< endl;
00108 cout << "outputfile name of output .h file"<<endl;
00109 cout << "varname name of char array in header"<<endl;
00110 }