4 |
#include <string> |
#include <string> |
5 |
#include <vector> |
#include <vector> |
6 |
|
|
7 |
|
|
8 |
#include <boost/algorithm/string.hpp> |
#include <boost/algorithm/string.hpp> |
9 |
#include <boost/filesystem.hpp> |
#include <boost/filesystem.hpp> |
10 |
|
#include <boost/program_options.hpp> |
11 |
|
|
12 |
|
namespace po = boost::program_options; |
13 |
|
|
14 |
using namespace std; |
using namespace std; |
15 |
|
|
16 |
const int BUFSIZE = 1024*1024; |
const int BUFSIZE = 1024*1024; |
17 |
|
|
18 |
string getLastWord(string input) { |
string remove_comments(string in) { |
19 |
|
bool isComment = false; |
20 |
|
ostringstream out; |
21 |
|
for (int i=0; i<in.length() -1; i++) { |
22 |
|
if (in.at(i) == '/' && in.at(i+1) == '*') |
23 |
|
isComment = true; |
24 |
|
|
25 |
|
if (in.at(i) == '*' && in.at(i+1) == '/') { |
26 |
|
i++; |
27 |
|
isComment = false; |
28 |
|
continue; |
29 |
|
} |
30 |
|
|
31 |
|
if (isComment == false) |
32 |
|
out << in.at(i) ; |
33 |
|
} |
34 |
|
|
35 |
|
return out.str(); |
36 |
|
} |
37 |
|
|
38 |
|
string get_db_name(string input) { |
39 |
|
input = remove_comments(input); |
40 |
|
|
41 |
|
boost::erase_all(input,"`"); |
42 |
|
boost::erase_all(input, ";"); |
43 |
|
boost::trim(input); |
44 |
|
|
45 |
vector<string> words; |
vector<string> words; |
46 |
words = boost::split(words, input, boost::is_any_of(" ") ); |
words = boost::split(words, input, boost::is_any_of(" ") ); |
47 |
|
|
48 |
string last = words.back(); |
string last = words.back(); |
|
|
|
|
boost::erase_all(last, ";"); |
|
49 |
boost::trim(last); |
boost::trim(last); |
50 |
|
|
|
|
|
|
|
|
51 |
return last; |
return last; |
52 |
} |
} |
53 |
|
|
54 |
|
|
55 |
int main(int argc, char** argv) { |
int main(int argc, char** argv) { |
56 |
if (argc != 2) { |
|
57 |
cout << "Usage: splitter <file>" << endl; |
po::options_description desc("Mysql dump file splitter:\nAllowed options"); |
58 |
|
desc.add_options() |
59 |
|
("help", "produce help message") |
60 |
|
("data", "ignore ceate tablestatements") |
61 |
|
("input-file", po::value< string >(), "input file") |
62 |
|
; |
63 |
|
|
64 |
|
po::positional_options_description p; |
65 |
|
p.add("input-file", -1); |
66 |
|
|
67 |
|
|
68 |
|
po::variables_map vm; |
69 |
|
po::store( po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
70 |
|
po::notify(vm); |
71 |
|
|
72 |
|
if (vm.count("help")) { |
73 |
|
cout << desc << "\n"; |
74 |
|
return 1; |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
if ( vm.count("input-file") == 0) { |
80 |
|
cout << desc << endl; |
81 |
return 1; |
return 1; |
82 |
} |
} |
83 |
|
|
84 |
|
|
85 |
char linebuf[BUFSIZE]; |
char linebuf[BUFSIZE]; |
86 |
|
|
87 |
ifstream in(argv[1]); |
string inputfile = vm["input-file"].as< string >(); |
88 |
|
if (! boost::filesystem::exists(inputfile) ) { |
89 |
|
cout << "No file named " << inputfile << endl; |
90 |
|
return 1; |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
ifstream in( inputfile.c_str() ); |
95 |
ofstream out; |
ofstream out; |
96 |
|
|
97 |
ostringstream header; |
ostringstream header; |
99 |
if (!in.is_open() ) { |
if (!in.is_open() ) { |
100 |
cout << "Could not open " << argv[1] << endl; |
cout << "Could not open " << argv[1] << endl; |
101 |
return 2; |
return 2; |
102 |
} |
} |
103 |
|
|
104 |
|
time_t start = time(NULL); |
105 |
|
|
106 |
while ( in.good() ) { |
while ( in.good() ) { |
107 |
in.getline(linebuf, BUFSIZE); |
in.getline(linebuf, BUFSIZE); |
117 |
} |
} |
118 |
|
|
119 |
boost::trim(line); |
boost::trim(line); |
120 |
string dbname = getLastWord(line); |
string dbname = get_db_name(line); |
121 |
cout << ">" << dbname << endl; |
cout << ">" << dbname << endl; |
122 |
|
|
123 |
|
|
154 |
|
|
155 |
|
|
156 |
if(out.is_open()) |
if(out.is_open()) |
157 |
out.close(); |
out.close(); |
158 |
|
|
159 |
|
time_t end = time(NULL); |
160 |
|
|
161 |
|
cout << "Elapsed " << (end-start) << " seconds" << endl; |
162 |
|
|
163 |
|
|
164 |
in.close(); |
in.close(); |