#include #include #include #include #include #include #include using namespace std; const int BUFSIZE = 1024*1024; string getLastWord(string input) { vector words; words = boost::split(words, input, boost::is_any_of(" ") ); string last = words.back(); boost::erase_all(last, ";"); boost::trim(last); return last; } int main(int argc, char** argv) { if (argc != 2) { cout << "Usage: splitter " << endl; return 1; } char linebuf[BUFSIZE]; ifstream in(argv[1]); ofstream out; ostringstream header; if (!in.is_open() ) { cout << "Could not open " << argv[1] << endl; return 2; } while ( in.good() ) { in.getline(linebuf, BUFSIZE); string line(linebuf); //if (line.substr(0, 15) == "CREATE DATABASE" ) { if ( boost::starts_with(line, "CREATE DATABASE") ) { if (out.is_open() ) { out.close(); } boost::trim(line); string dbname = getLastWord(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 ); if (!out.is_open() ) { cout << "could not create outfile " << filename << endl; return 3; } if (!did_exist) { out << header.str() << endl; //write preamble in new file } } if (out.is_open() ) { out << line << endl; } else { header << line << endl; //collect preamble for later use } } if(out.is_open()) out.close(); in.close(); }