1 |
torben |
1231 |
#include <iostream> |
2 |
|
|
#include <fstream> |
3 |
|
|
#include <sstream> |
4 |
|
|
#include <string> |
5 |
|
|
#include <vector> |
6 |
|
|
|
7 |
torben |
1282 |
|
8 |
torben |
1231 |
#include <boost/algorithm/string.hpp> |
9 |
|
|
#include <boost/filesystem.hpp> |
10 |
torben |
1282 |
#include <boost/program_options.hpp> |
11 |
torben |
1231 |
|
12 |
torben |
1282 |
namespace po = boost::program_options; |
13 |
|
|
|
14 |
torben |
1231 |
using namespace std; |
15 |
|
|
|
16 |
|
|
const int BUFSIZE = 1024*1024; |
17 |
|
|
|
18 |
|
|
string getLastWord(string input) { |
19 |
|
|
vector<string> words; |
20 |
|
|
words = boost::split(words, input, boost::is_any_of(" ") ); |
21 |
|
|
|
22 |
|
|
string last = words.back(); |
23 |
|
|
|
24 |
|
|
boost::erase_all(last, ";"); |
25 |
|
|
boost::trim(last); |
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
return last; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
int main(int argc, char** argv) { |
34 |
torben |
1282 |
|
35 |
|
|
po::options_description desc("Mysql dump file splitter:\nAllowed options"); |
36 |
|
|
desc.add_options() |
37 |
|
|
("help", "produce help message") |
38 |
|
|
("data", "ignore ceate tablestatements") |
39 |
|
|
("input-file", po::value< string >(), "input file") |
40 |
|
|
; |
41 |
|
|
|
42 |
|
|
po::positional_options_description p; |
43 |
|
|
p.add("input-file", -1); |
44 |
|
|
|
45 |
|
|
|
46 |
|
|
po::variables_map vm; |
47 |
|
|
po::store( po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
48 |
|
|
po::notify(vm); |
49 |
|
|
|
50 |
|
|
if (vm.count("help")) { |
51 |
|
|
cout << desc << "\n"; |
52 |
|
|
return 1; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
if ( vm.count("input-file") == 0) { |
58 |
|
|
cout << desc << endl; |
59 |
torben |
1231 |
return 1; |
60 |
|
|
} |
61 |
|
|
|
62 |
torben |
1282 |
|
63 |
torben |
1231 |
char linebuf[BUFSIZE]; |
64 |
|
|
|
65 |
torben |
1282 |
string inputfile = vm["input-file"].as< string >(); |
66 |
|
|
if (! boost::filesystem::exists(inputfile) ) { |
67 |
|
|
cout << "No file named " << inputfile << endl; |
68 |
|
|
return 1; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
ifstream in( inputfile.c_str() ); |
73 |
torben |
1231 |
ofstream out; |
74 |
|
|
|
75 |
|
|
ostringstream header; |
76 |
|
|
|
77 |
|
|
if (!in.is_open() ) { |
78 |
|
|
cout << "Could not open " << argv[1] << endl; |
79 |
|
|
return 2; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
while ( in.good() ) { |
83 |
|
|
in.getline(linebuf, BUFSIZE); |
84 |
|
|
|
85 |
|
|
string line(linebuf); |
86 |
|
|
|
87 |
|
|
|
88 |
|
|
//if (line.substr(0, 15) == "CREATE DATABASE" ) { |
89 |
|
|
if ( boost::starts_with(line, "CREATE DATABASE") ) { |
90 |
|
|
|
91 |
|
|
if (out.is_open() ) { |
92 |
|
|
out.close(); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
boost::trim(line); |
96 |
|
|
string dbname = getLastWord(line); |
97 |
|
|
cout << ">" << dbname << endl; |
98 |
|
|
|
99 |
|
|
|
100 |
|
|
ostringstream oss; |
101 |
|
|
oss << dbname << "_dump.sql";
|
102 |
|
|
|
103 |
|
|
string filename = oss.str(); |
104 |
|
|
|
105 |
|
|
bool did_exist = boost::filesystem::exists(filename); |
106 |
|
|
|
107 |
|
|
out.open( filename.c_str(), ios::app ); |
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
if (!out.is_open() ) { |
112 |
|
|
cout << "could not create outfile " << filename << endl; |
113 |
|
|
return 3; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
if (!did_exist) { |
117 |
|
|
out << header.str() << endl; //write preamble in new file |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
|
123 |
|
|
if (out.is_open() ) { |
124 |
|
|
out << line << endl; |
125 |
|
|
} else { |
126 |
|
|
header << line << endl; //collect preamble for later use |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
|
132 |
|
|
if(out.is_open()) |
133 |
|
|
out.close(); |
134 |
|
|
|
135 |
|
|
|
136 |
|
|
in.close(); |
137 |
|
|
} |