1 |
#include <iostream> |
#include <iostream> |
2 |
#include <fstream> |
#include <iomanip> |
3 |
#include <sstream> |
#include <sstream> |
4 |
#include <string> |
#include <string> |
5 |
#include <vector> |
#include <vector> |
6 |
|
|
7 |
|
#include <cstdio> |
8 |
|
|
9 |
#include <boost/algorithm/string.hpp> |
#include <boost/algorithm/string.hpp> |
10 |
#include <boost/filesystem.hpp> |
#include <boost/filesystem.hpp> |
11 |
|
#include <boost/program_options.hpp> |
12 |
|
|
13 |
|
namespace po = boost::program_options; |
14 |
|
|
15 |
using namespace std; |
using namespace std; |
16 |
|
|
17 |
const int BUFSIZE = 1024*1024; |
const int BUFSIZE = 1024*1024; |
18 |
|
|
19 |
string getLastWord(string input) { |
string remove_comments(string in) { |
20 |
|
bool isComment = false; |
21 |
|
ostringstream out; |
22 |
|
for (unsigned int i=0; i<in.length() -1; i++) { |
23 |
|
if (in.at(i) == '/' && in.at(i+1) == '*') |
24 |
|
isComment = true; |
25 |
|
|
26 |
|
if (in.at(i) == '*' && in.at(i+1) == '/') { |
27 |
|
i++; |
28 |
|
isComment = false; |
29 |
|
continue; |
30 |
|
} |
31 |
|
|
32 |
|
if (isComment == false) |
33 |
|
out << in.at(i) ; |
34 |
|
} |
35 |
|
|
36 |
|
return out.str(); |
37 |
|
} |
38 |
|
|
39 |
|
string get_db_name(string input) { |
40 |
|
input = remove_comments(input); |
41 |
|
|
42 |
|
boost::erase_all(input,"`"); |
43 |
|
boost::erase_all(input, ";"); |
44 |
|
boost::trim(input); |
45 |
|
|
46 |
vector<string> words; |
vector<string> words; |
47 |
words = boost::split(words, input, boost::is_any_of(" ") ); |
words = boost::split(words, input, boost::is_any_of(" ") ); |
48 |
|
|
49 |
string last = words.back(); |
string last = words.back(); |
|
|
|
|
boost::erase_all(last, ";"); |
|
50 |
boost::trim(last); |
boost::trim(last); |
51 |
|
|
|
|
|
|
|
|
52 |
return last; |
return last; |
53 |
} |
} |
54 |
|
|
55 |
|
|
56 |
int main(int argc, char** argv) { |
int main(int argc, char** argv) { |
57 |
if (argc != 2) { |
|
58 |
cout << "Usage: splitter <file>" << endl; |
po::options_description desc("Mysql dump file splitter:\nAllowed options"); |
59 |
|
desc.add_options() |
60 |
|
("help", "produce help message") |
61 |
|
("data", "ignore ceate tablestatements") |
62 |
|
("input-file", po::value< string >(), "input file") |
63 |
|
; |
64 |
|
|
65 |
|
po::positional_options_description p; |
66 |
|
p.add("input-file", -1); |
67 |
|
|
68 |
|
|
69 |
|
po::variables_map vm; |
70 |
|
po::store( po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
71 |
|
po::notify(vm); |
72 |
|
|
73 |
|
if (vm.count("help")) { |
74 |
|
cout << desc << "\n"; |
75 |
|
return 1; |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
if ( vm.count("input-file") == 0) { |
81 |
|
cout << desc << endl; |
82 |
return 1; |
return 1; |
83 |
} |
} |
84 |
|
|
85 |
|
|
86 |
char linebuf[BUFSIZE]; |
char linebuf[BUFSIZE]; |
87 |
|
|
88 |
ifstream in(argv[1]); |
string inputfile = vm["input-file"].as< string >(); |
89 |
ofstream out; |
if (! boost::filesystem::exists(inputfile) ) { |
90 |
|
cout << "No file named " << inputfile << endl; |
91 |
|
return 1; |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
FILE* in = fopen64( inputfile.c_str(), "r" ); |
96 |
|
FILE* out = NULL; |
97 |
|
//ifstream in( inputfile.c_str() ); |
98 |
|
//ofstream out; |
99 |
|
|
100 |
ostringstream header; |
ostringstream header; |
101 |
|
|
102 |
if (!in.is_open() ) { |
if ( in == NULL ) { |
103 |
cout << "Could not open " << argv[1] << endl; |
cout << "Could not open " << argv[1] << endl; |
104 |
|
perror(""); |
105 |
return 2; |
return 2; |
106 |
} |
} |
107 |
|
|
108 |
while ( in.good() ) { |
time_t start = time(NULL); |
109 |
in.getline(linebuf, BUFSIZE); |
|
110 |
|
const char* SEARCH = "CREATE DATABASE"; |
111 |
|
const int SEARCHLEN = strlen(SEARCH); |
112 |
|
|
113 |
string line(linebuf); |
while ( feof(in) == 0 && ferror(in) == 0 ) { |
114 |
|
fgets(linebuf, BUFSIZE, in); |
115 |
|
|
116 |
|
|
117 |
//if (line.substr(0, 15) == "CREATE DATABASE" ) { |
//if (line.substr(0, 15) == "CREATE DATABASE" ) { |
118 |
if ( boost::starts_with(line, "CREATE DATABASE") ) { |
//if ( boost::starts_with(line, "CREATE DATABASE") ) { |
119 |
|
if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) { |
120 |
|
|
121 |
if (out.is_open() ) { |
string line(linebuf); |
122 |
out.close(); |
|
123 |
|
if (out != NULL ) { |
124 |
|
fclose(out); |
125 |
} |
} |
126 |
|
|
127 |
boost::trim(line); |
boost::trim(line); |
128 |
string dbname = getLastWord(line); |
string dbname = get_db_name(line); |
129 |
cout << ">" << dbname << endl; |
cout << ">" << dbname << endl; |
130 |
|
|
131 |
|
|
136 |
|
|
137 |
bool did_exist = boost::filesystem::exists(filename); |
bool did_exist = boost::filesystem::exists(filename); |
138 |
|
|
139 |
out.open( filename.c_str(), ios::app ); |
//out.open( filename.c_str(), ios::app ); |
140 |
|
out = fopen64(filename.c_str(), "a"); |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
if (!out.is_open() ) { |
if ( out == NULL ) { |
145 |
cout << "could not create outfile " << filename << endl; |
cout << "could not create outfile " << filename << endl; |
146 |
return 3; |
return 3; |
147 |
} |
} |
148 |
|
|
149 |
if (!did_exist) { |
if (!did_exist) { |
150 |
out << header.str() << endl; //write preamble in new file |
fputs(header.str().c_str(), out); |
151 |
|
//out << header.str() << endl; //write preamble in new file |
152 |
} |
} |
153 |
|
|
154 |
} |
} |
155 |
|
|
156 |
|
|
157 |
if (out.is_open() ) { |
if (out != NULL ) { |
158 |
out << line << endl; |
fputs(linebuf, out); |
159 |
|
//fputs("\n", out); |
160 |
|
//out << linebuf << endl; |
161 |
} else { |
} else { |
162 |
header << line << endl; //collect preamble for later use |
header << linebuf; //collect preamble for later use |
163 |
} |
} |
164 |
|
|
165 |
} |
} |
166 |
|
|
167 |
|
|
168 |
if(out.is_open()) |
if (out != NULL) |
169 |
out.close(); |
fclose(out); |
170 |
|
|
171 |
|
// if (out.is_open()) |
172 |
|
// out.close(); |
173 |
|
|
174 |
|
time_t end = time(NULL); |
175 |
|
|
176 |
|
|
177 |
|
time_t elapsed = end-start; |
178 |
|
cout << setfill('0') << "Elapsed time " << elapsed/60 << ":" << setw(2) << elapsed%60 << endl; |
179 |
|
|
180 |
in.close(); |
fclose(in); |
181 |
} |
} |