// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 190. // // Program to make framework for a program by creating makefile // newclass.cpp, newclass.h, testnewclass.cpp // John Robinson // August 2003 // #include #include #include #include using namespace std; string makecontent("# \ Makefile for NAME class programs, created DATE\n\n\ all: testNAME\n\n\ testNAME: NAME.o testNAME.o\n\ \tg++ -o testNAME testNAME.o NAME.o\n\n\ testNAME.o: testNAME.cpp NAME.h\n\ \tg++ -c testNAME.cpp\n\ NAME.o: NAME.cpp NAME.h\n\ \tg++ -c NAME.cpp\n"); string headercontent("\ // Header for NAME class, created DATE\n\ \n#ifndef NAME_H\n#define NAME_H\n\n\ class NAME {\n\ public:\n\ \tNAME ();\n\ \t~NAME ();\n\ };\n\n\ \n#endif\n"); string implementationcontent("\ // Implementation for NAME class, created DATE\n\ \n#include \"NAME.h\"\n\n\ NAME::NAME() {\n\ }\n\n\ NAME::~NAME() {\n\ }\n"); string testcontent("\ // Test program for NAME class, created DATE\n\ \n#include \"NAME.h\"\n\n\ int main(int argc, char *argv[]) {\n\ \tNAME testNAME();\n\ \treturn 0;\n\ }\n"); void replaceall(string &st, const string oldsubstr, const string newsubstr) { string::size_type pos; string::size_type len = oldsubstr.length(); while ((pos = st.find(oldsubstr)) != string::npos) st.replace(pos,len,newsubstr); } using namespace std; int main(int argc, char *argv[]) { if (argc != 2) { cerr << "Usage: mkframework classname\n"; return -1; } time_t now = time(0); char c_str_now[80]; strftime(c_str_now,80,"%c",localtime(&now)); string strnow(c_str_now); // %c in strftime gives a vanilla date format string classname(argv[1]); // newclass string headername(classname); headername += ".h"; // newclass.h string implementationname(classname); implementationname += ".cpp"; // newclass.cpp string testname("test"); testname += classname + ".cpp"; // testnewclass.cpp ofstream makefile("Makefile"); if (!makefile) { cerr << "Can't create Makefile\n"; return -1; } ofstream headerfile(headername.c_str()); if (!headerfile) { cerr << "Can't create " << headername << endl; return -1; } ofstream implementationfile(implementationname.c_str()); if (!implementationfile) { cerr << "Can't create " << implementationname << endl; return -1; } ofstream testfile(testname.c_str()); if (!testfile) { cerr << "Can't create " << testname << endl; return -1; } replaceall(makecontent,"NAME",classname); replaceall(makecontent,"DATE",strnow); makefile << makecontent; makefile.close(); replaceall(headercontent,"NAME",classname); replaceall(headercontent,"DATE",strnow); headerfile << headercontent; headerfile.close(); replaceall(implementationcontent,"NAME",classname); replaceall(implementationcontent,"DATE",strnow); implementationfile << implementationcontent; implementationfile.close(); replaceall(testcontent,"NAME",classname); replaceall(testcontent,"DATE",strnow); testfile << testcontent; testfile.close(); }