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
| #include <string>
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
std::string escapeLine( std::string orig )
{
string retme;
for (unsigned int i=0; i<orig.size(); i++)
{
switch (orig[i])
{
case '\\\':
retme +="\\\\\";
break;
case '"':
retme +="\\\"";
break;
case '\
': // Strip out the final linefeed.
break;
default:
retme += orig[i];
}
}
retme +="\\\
"; // Add an escaped linefeed to the escaped string.
return retme;
}
int main( int argc, char ** argv )
{
string filenamein, filenameout;
if ( argc > 1 )
filenamein = argv[ 1 ];
else
{
// Not enough arguments
fprintf( stderr,"Usage: %s <file to convert.mel> [ <output file name.mel> ]\
", argv[0] );
exit( -1 );
}
if ( argc > 2 )
filenameout = argv[ 2 ];
else
{
string new_ending ="_mel.h";
filenameout = filenamein;
std::string::size_type pos;
pos = filenameout.find(".mel" );
if (pos == std::string::npos)
filenameout += new_ending;
else
filenameout.replace( pos, new_ending.size(), new_ending );
}
printf("Converting "%s" to "%s"\
", filenamein.c_str(), filenameout.c_str() );
ifstream filein( filenamein.c_str(), ios::in );
ofstream fileout( filenameout.c_str(), ios::out );
if (!filein.good())
{
fprintf( stderr,"Unable to open input file %s\
", filenamein.c_str() );
exit( -2 );
}
if (!fileout.good())
{
fprintf( stderr,"Unable to open output file %s\
", filenameout.c_str() );
exit( -3 );
}
// Write the file.
fileout <<"tempstr =";
while( filein.good() )
{
string buff;
if ( getline( filein, buff ) )
{
fileout <<""" << escapeLine( buff ) <<""" << endl;
}
}
fileout <<";" << endl;
filein.close();
fileout.close();
return 0;
} |