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> |
19 |
string remove_comments(string in) { |
string remove_comments(string in) { |
20 |
bool isComment = false; |
bool isComment = false; |
21 |
ostringstream out; |
ostringstream out; |
22 |
for (int i=0; i<in.length() -1; i++) { |
for (unsigned int i=0; i<in.length() -1; i++) { |
23 |
if (in.at(i) == '/' && in.at(i+1) == '*') |
if (in.at(i) == '/' && in.at(i+1) == '*') |
24 |
isComment = true; |
isComment = true; |
25 |
|
|
92 |
} |
} |
93 |
|
|
94 |
|
|
95 |
ifstream in( inputfile.c_str() ); |
FILE* in = fopen64( inputfile.c_str(), "r" ); |
96 |
ofstream out; |
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 |
time_t start = time(NULL); |
time_t start = time(NULL); |
109 |
|
|
110 |
while ( in.good() ) { |
const char* SEARCH = "CREATE DATABASE"; |
111 |
in.getline(linebuf, BUFSIZE); |
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); |
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); |
time_t end = time(NULL); |
175 |
|
|
|
cout << "Elapsed " << (end-start) << " seconds" << endl; |
|
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 |
} |
} |