All pastes #73665 Raw Edit

Miscellany

public cpp v1 · immutable
#73665 ·published 2006-06-28 03:11 UTC
rendered paste body
#include <fstream>#include <iostream>#include <vector>#include <string>//#define DEBUG_Husing std::ifstream;using std::ofstream;using std::cout;using std::vector;using std::endl;using std::string;int main(int argc, char argv[]){#ifdef _DEBUG    char* filename = "test.c";#else    if( argc < 2)    {        std::cerr << "Usage:\n" <<        "generator.exe file.c\n";        exit(1);    }    char* filename = argv[1];#endif    ifstream File;    File.open(filename);    if(!File.is_open())    {        std::cerr << "Unable to open file";        exit(1);    }    /*  Create printf string */    string spf; // printf("1",2); define 2    string chardef, source; // define 1    string line;    string previousline;    while (File.peek() != EOF)    {        previousline = line;        File >> line;        bool newline = false;        for(unsigned int i = 0; i < line.size(); i++)        {            string::size_type pos = i;            string c = line.substr(pos,1);            newline = false;            if(c == "\\")            {                spf = spf + ",x5c";                chardef = chardef + "%c";                source += "\\";            }            else if(c == "\"")            {                spf = spf + ",x22";                chardef = chardef +  "%c";                source += "\"";            }            else if(c == " ")            {                chardef = chardef + " ";            }            else            {                chardef = chardef + c;                source += c;            }            if(i == line.size()-1)            {                chardef = chardef + " ";                source += " ";            }        }        if(File.peek() == '\n')        {            chardef += "%c";            spf += ",xD";            source += '\n';        }    }    string outPrint;    outPrint = "printf(" + spf + "," + chardef + ");";    cout << outPrint << endl << chardef << endl << source << endl;    for( string::size_type pos = 0; (pos = chardef.find(" return 0", pos)) != std::string::npos; pos++)    {        chardef.insert(pos + outPrint.length(), outPrint);    }    for( string::size_type pos = 0; (pos = source.find(" return 0", pos)) != std::string::npos; pos++)    {        source.insert( pos + outPrint.length(), outPrint);    }    /*    cout << chardef << endl << endl;    cout << "char *s = \"" << "char* s = %c" << chardef << "%c;" << "\"; \n" << source << endl;    */    File.close();    /* Output to file */    ofstream oFile;    oFile.open(filename, ofstream::trunc);     if(!oFile.is_open())    {        std::cerr << "Unable to open file";        exit(1);    }    oFile << "char *s = \"" << "char* s = %c" << chardef << "%c;" << "\"; \n" << source << endl;    oFile.close();    return 0;}