1 |
#include <iostream> |
#include <iostream> |
|
#include <fstream> |
|
2 |
#include <sstream> |
#include <sstream> |
3 |
#include <string> |
#include <string> |
4 |
#include <vector> |
#include <vector> |
5 |
|
|
6 |
|
#include <cstdio> |
7 |
|
|
8 |
#include <boost/algorithm/string.hpp> |
#include <boost/algorithm/string.hpp> |
9 |
#include <boost/filesystem.hpp> |
#include <boost/filesystem.hpp> |
18 |
string remove_comments(string in) { |
string remove_comments(string in) { |
19 |
bool isComment = false; |
bool isComment = false; |
20 |
ostringstream out; |
ostringstream out; |
21 |
for (int i=0; i<in.length() -1; i++) { |
for (unsigned 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 |
ifstream in( inputfile.c_str() ); |
|
94 |
ofstream out; |
FILE* in = fopen( inputfile.c_str(), "r" ); |
95 |
|
FILE* out = NULL; |
96 |
|
//ifstream in( inputfile.c_str() ); |
97 |
|
//ofstream out; |
98 |
|
|
99 |
ostringstream header; |
ostringstream header; |
100 |
|
|
101 |
if (!in.is_open() ) { |
if ( in == NULL ) { |
102 |
cout << "Could not open " << argv[1] << endl; |
cout << "Could not open " << argv[1] << endl; |
103 |
return 2; |
return 2; |
104 |
} |
} |
105 |
|
|
106 |
while ( in.good() ) { |
time_t start = time(NULL); |
107 |
in.getline(linebuf, BUFSIZE); |
|
108 |
|
const char* SEARCH = "CREATE DATABASE"; |
109 |
|
const int SEARCHLEN = strlen(SEARCH); |
110 |
|
|
111 |
string line(linebuf); |
while ( feof(in) == 0 && ferror(in) == 0 ) { |
112 |
|
fgets(linebuf, BUFSIZE, in); |
113 |
|
|
114 |
|
|
115 |
//if (line.substr(0, 15) == "CREATE DATABASE" ) { |
//if (line.substr(0, 15) == "CREATE DATABASE" ) { |
116 |
if ( boost::starts_with(line, "CREATE DATABASE") ) { |
//if ( boost::starts_with(line, "CREATE DATABASE") ) { |
117 |
|
if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) { |
118 |
|
|
119 |
if (out.is_open() ) { |
string line(linebuf); |
120 |
out.close(); |
|
121 |
|
if (out != NULL ) { |
122 |
|
fclose(out); |
123 |
} |
} |
124 |
|
|
125 |
boost::trim(line); |
boost::trim(line); |
126 |
string dbname = getLastWord(line); |
string dbname = get_db_name(line); |
127 |
cout << ">" << dbname << endl; |
cout << ">" << dbname << endl; |
128 |
|
|
129 |
|
|
134 |
|
|
135 |
bool did_exist = boost::filesystem::exists(filename); |
bool did_exist = boost::filesystem::exists(filename); |
136 |
|
|
137 |
out.open( filename.c_str(), ios::app ); |
//out.open( filename.c_str(), ios::app ); |
138 |
|
out = fopen64(filename.c_str(), "a"); |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
if (!out.is_open() ) { |
if ( out == NULL ) { |
143 |
cout << "could not create outfile " << filename << endl; |
cout << "could not create outfile " << filename << endl; |
144 |
return 3; |
return 3; |
145 |
} |
} |
146 |
|
|
147 |
if (!did_exist) { |
if (!did_exist) { |
148 |
out << header.str() << endl; //write preamble in new file |
fputs(header.str().c_str(), out); |
149 |
|
//out << header.str() << endl; //write preamble in new file |
150 |
} |
} |
151 |
|
|
152 |
} |
} |
153 |
|
|
154 |
|
|
155 |
if (out.is_open() ) { |
if (out != NULL ) { |
156 |
out << line << endl; |
fputs(linebuf, out); |
157 |
|
//fputs("\n", out); |
158 |
|
//out << linebuf << endl; |
159 |
} else { |
} else { |
160 |
header << line << endl; //collect preamble for later use |
header << linebuf; //collect preamble for later use |
161 |
} |
} |
162 |
|
|
163 |
} |
} |
164 |
|
|
165 |
|
|
166 |
if(out.is_open()) |
if (out != NULL) |
167 |
out.close(); |
fclose(out); |
168 |
|
|
169 |
|
// if (out.is_open()) |
170 |
|
// out.close(); |
171 |
|
|
172 |
|
time_t end = time(NULL); |
173 |
|
|
174 |
|
cout << "Elapsed " << (end-start) << " seconds" << endl; |
175 |
|
|
176 |
in.close(); |
fclose(in); |
177 |
} |
} |