#include #include #include #include #include #include #include #include namespace po = boost::program_options; using namespace std; const int BUFSIZE = 1024*1024; string remove_comments(string in) { bool isComment = false; ostringstream out; for (unsigned int i=0; i words; words = boost::split(words, input, boost::is_any_of(" ") ); string last = words.back(); boost::trim(last); return last; } int main(int argc, char** argv) { po::options_description desc("Mysql dump file splitter:\nAllowed options"); desc.add_options() ("help", "produce help message") ("data", "ignore ceate tablestatements") ("input-file", po::value< string >(), "input file") ; po::positional_options_description p; p.add("input-file", -1); po::variables_map vm; po::store( po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); po::notify(vm); if (vm.count("help")) { cout << desc << "\n"; return 1; } if ( vm.count("input-file") == 0) { cout << desc << endl; return 1; } char linebuf[BUFSIZE]; string inputfile = vm["input-file"].as< string >(); if (! boost::filesystem::exists(inputfile) ) { cout << "No file named " << inputfile << endl; return 1; } FILE* in = fopen( inputfile.c_str(), "r" ); FILE* out = NULL; //ifstream in( inputfile.c_str() ); //ofstream out; ostringstream header; if ( in == NULL ) { cout << "Could not open " << argv[1] << endl; return 2; } time_t start = time(NULL); const char* SEARCH = "CREATE DATABASE"; const int SEARCHLEN = strlen(SEARCH); while ( feof(in) == 0 && ferror(in) == 0 ) { fgets(linebuf, BUFSIZE, in); //if (line.substr(0, 15) == "CREATE DATABASE" ) { //if ( boost::starts_with(line, "CREATE DATABASE") ) { if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) { string line(linebuf); if (out != NULL ) { fclose(out); } boost::trim(line); string dbname = get_db_name(line); cout << ">" << dbname << endl; ostringstream oss; oss << dbname << "_dump.sql"; string filename = oss.str(); bool did_exist = boost::filesystem::exists(filename); //out.open( filename.c_str(), ios::app ); out = fopen(filename.c_str(), "a"); if ( out == NULL ) { cout << "could not create outfile " << filename << endl; return 3; } if (!did_exist) { fputs(header.str().c_str(), out); //out << header.str() << endl; //write preamble in new file } } if (out != NULL ) { fputs(linebuf, out); fputs("\n", out); //out << linebuf << endl; } else { header << linebuf << endl; //collect preamble for later use } } if (out != NULL) fclose(out); // if (out.is_open()) // out.close(); time_t end = time(NULL); cout << "Elapsed " << (end-start) << " seconds" << endl; fclose(in); }