00001 #include "text2src.h"
00002
00008 #include <iostream>
00009 #include <iomanip>
00010 using namespace std;
00011
00012
00013
00014 void text2source(ostream &os,istream &is)
00015 {
00016 bool firstLine = true;
00017 bool empty = true;
00018
00019 while (1)
00020 {
00021 const int bufferSize = 1024;
00022 char buffer[bufferSize];
00023
00024 if (!is.getline(buffer,bufferSize))
00025 break;
00026
00027 empty = false;
00028
00029 if (firstLine)
00030 firstLine = false;
00031 else
00032 os << "\\n\"" << '\\' << endl;
00033
00034 char output[bufferSize*2];
00035 int p1 = 0,p2 = 0;
00036
00037 while (buffer[p1]!='\0')
00038 {
00039 switch (buffer[p1])
00040 {
00041 case '\"':
00042 case '\\':
00043 output[p2++] = '\\';
00044 break;
00045 }
00046
00047 output[p2++] = buffer[p1++];
00048 }
00049
00050 output[p2] = '\0';
00051
00052 os << '\"' << output;
00053 }
00054
00055 if (!empty)
00056 os << "\\n\";" << endl;
00057 }
00058
00059
00060
00061 void binary2source(ostream &os,istream &is)
00062 {
00063 os.setf(ios::hex,ios::basefield);
00064
00065 os << '{' << endl;
00066
00067 bool begin = true;
00068 while (is.good() && !is.eof())
00069 {
00070 unsigned char buffer[16];
00071 is.read((char *) buffer,16);
00072
00073 int size = is.gcount();
00074 if (size>0 && !begin)
00075 {
00076 os << ',';
00077 os << endl;
00078 }
00079
00080 begin = false;
00081
00082 for (int i=0; i<size;i++)
00083 {
00084 os << "0x" << setw(2) << setfill('0') << (unsigned int)buffer[i];
00085 if (i<size-1)
00086 os << ',';
00087 }
00088
00089 }
00090
00091 os << endl << "};" << endl;
00092 }
00093