19 |
bool isComment = false; |
bool isComment = false; |
20 |
ostringstream out; |
ostringstream out; |
21 |
for (int i=0; i<in.length() -1; i++) { |
for (int i=0; i<in.length() -1; i++) { |
22 |
if (in.at(i) == '/' && in.at(i+1) == '*') |
if (in.at(i) == '/' && in.at(i+1) == '*') |
23 |
isComment = true; |
isComment = true; |
24 |
|
|
25 |
if (in.at(i) == '*' && in.at(i+1) == '/') { |
if (in.at(i) == '*' && in.at(i+1) == '/') { |
26 |
i++; |
i++; |
27 |
isComment = false; |
isComment = false; |
35 |
return out.str(); |
return out.str(); |
36 |
} |
} |
37 |
|
|
38 |
string getLastWord(string input) { |
string get_db_name(string input) { |
39 |
input = remove_comments(input); |
input = remove_comments(input); |
40 |
|
|
41 |
boost::erase_all(input,"`"); |
boost::erase_all(input,"`"); |
42 |
|
boost::erase_all(input, ";"); |
43 |
boost::trim(input); |
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 |
|
|
89 |
cout << "No file named " << inputfile << endl; |
cout << "No file named " << inputfile << endl; |
90 |
return 1; |
return 1; |
91 |
} |
} |
92 |
|
|
93 |
|
|
94 |
ifstream in( inputfile.c_str() ); |
ifstream in( inputfile.c_str() ); |
95 |
ofstream out; |
ofstream out; |
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 |
|
const char* SEARCH = "CREATE DATABASE"; |
107 |
|
const int SEARCHLEN = strlen(SEARCH); |
108 |
|
|
109 |
while ( in.good() ) { |
while ( in.good() ) { |
110 |
in.getline(linebuf, BUFSIZE); |
in.getline(linebuf, BUFSIZE); |
111 |
|
|
|
string line(linebuf); |
|
|
|
|
112 |
|
|
113 |
//if (line.substr(0, 15) == "CREATE DATABASE" ) { |
//if (line.substr(0, 15) == "CREATE DATABASE" ) { |
114 |
if ( boost::starts_with(line, "CREATE DATABASE") ) { |
//if ( boost::starts_with(line, "CREATE DATABASE") ) { |
115 |
|
if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) { |
116 |
|
|
117 |
|
string line(linebuf); |
118 |
if (out.is_open() ) { |
if (out.is_open() ) { |
119 |
out.close(); |
out.close(); |
120 |
} |
} |
121 |
|
|
122 |
boost::trim(line); |
boost::trim(line); |
123 |
string dbname = getLastWord(line); |
string dbname = get_db_name(line); |
124 |
cout << ">" << dbname << endl; |
cout << ">" << dbname << endl; |
125 |
|
|
126 |
|
|
148 |
|
|
149 |
|
|
150 |
if (out.is_open() ) { |
if (out.is_open() ) { |
151 |
out << line << endl; |
out << linebuf << endl; |
152 |
} else { |
} else { |
153 |
header << line << endl; //collect preamble for later use |
header << linebuf << endl; //collect preamble for later use |
154 |
} |
} |
155 |
|
|
156 |
} |
} |
157 |
|
|
158 |
|
|
159 |
if(out.is_open()) |
if(out.is_open()) |
160 |
out.close(); |
out.close(); |
161 |
|
|
162 |
|
time_t end = time(NULL); |
163 |
|
|
164 |
|
cout << "Elapsed " << (end-start) << " seconds" << endl; |
165 |
|
|
166 |
|
|
167 |
in.close(); |
in.close(); |