--- misc/mysql_splitter/splitter.cpp 2011/04/10 20:37:28 1282 +++ misc/mysql_splitter/splitter.cpp 2011/05/12 19:38:56 1464 @@ -1,9 +1,10 @@ #include -#include +#include #include #include #include +#include #include #include @@ -15,17 +16,39 @@ const int BUFSIZE = 1024*1024; -string getLastWord(string input) { +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::erase_all(last, ";"); boost::trim(last); - - return last; } @@ -67,33 +90,42 @@ cout << "No file named " << inputfile << endl; return 1; } - - ifstream in( inputfile.c_str() ); - ofstream out; + + FILE* in = fopen64( inputfile.c_str(), "r" ); + FILE* out = NULL; + //ifstream in( inputfile.c_str() ); + //ofstream out; ostringstream header; - if (!in.is_open() ) { + if ( in == NULL ) { cout << "Could not open " << argv[1] << endl; + perror(""); return 2; - } - - while ( in.good() ) { - in.getline(linebuf, BUFSIZE); + } + + time_t start = time(NULL); + + const char* SEARCH = "CREATE DATABASE"; + const int SEARCHLEN = strlen(SEARCH); - string line(linebuf); + 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 ( boost::starts_with(line, "CREATE DATABASE") ) { + if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) { - if (out.is_open() ) { - out.close(); + string line(linebuf); + + if (out != NULL ) { + fclose(out); } boost::trim(line); - string dbname = getLastWord(line); + string dbname = get_db_name(line); cout << ">" << dbname << endl; @@ -104,34 +136,46 @@ bool did_exist = boost::filesystem::exists(filename); - out.open( filename.c_str(), ios::app ); + //out.open( filename.c_str(), ios::app ); + out = fopen64(filename.c_str(), "a"); - if (!out.is_open() ) { + if ( out == NULL ) { cout << "could not create outfile " << filename << endl; return 3; } if (!did_exist) { - out << header.str() << endl; //write preamble in new file + fputs(header.str().c_str(), out); + //out << header.str() << endl; //write preamble in new file } } - if (out.is_open() ) { - out << line << endl; + if (out != NULL ) { + fputs(linebuf, out); + //fputs("\n", out); + //out << linebuf << endl; } else { - header << line << endl; //collect preamble for later use + header << linebuf; //collect preamble for later use } } - if(out.is_open()) - out.close(); + if (out != NULL) + fclose(out); + +// if (out.is_open()) +// out.close(); + + time_t end = time(NULL); + + time_t elapsed = end-start; + cout << setfill('0') << "Elapsed time " << elapsed/60 << ":" << setw(2) << elapsed%60 << endl; - in.close(); + fclose(in); }