/[projects]/misc/mysql_splitter/splitter.cpp
ViewVC logotype

Annotation of /misc/mysql_splitter/splitter.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1464 - (hide annotations) (download)
Thu May 12 19:38:56 2011 UTC (13 years ago) by torben
File size: 3489 byte(s)
also use fopen64 for source file and format the elapsed time a little
1 torben 1231 #include <iostream>
2 torben 1464 #include <iomanip>
3 torben 1231 #include <sstream>
4     #include <string>
5     #include <vector>
6    
7 torben 1461 #include <cstdio>
8 torben 1282
9 torben 1231 #include <boost/algorithm/string.hpp>
10     #include <boost/filesystem.hpp>
11 torben 1282 #include <boost/program_options.hpp>
12 torben 1231
13 torben 1282 namespace po = boost::program_options;
14    
15 torben 1231 using namespace std;
16    
17     const int BUFSIZE = 1024*1024;
18    
19 torben 1283 string remove_comments(string in) {
20     bool isComment = false;
21     ostringstream out;
22 torben 1460 for (unsigned int i=0; i<in.length() -1; i++) {
23 torben 1458 if (in.at(i) == '/' && in.at(i+1) == '*')
24 torben 1283 isComment = true;
25 torben 1458
26 torben 1283 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 torben 1284 string get_db_name(string input) {
40 torben 1283 input = remove_comments(input);
41    
42     boost::erase_all(input,"`");
43 torben 1284 boost::erase_all(input, ";");
44 torben 1283 boost::trim(input);
45 torben 1284
46 torben 1231 vector<string> words;
47     words = boost::split(words, input, boost::is_any_of(" ") );
48    
49     string last = words.back();
50     boost::trim(last);
51    
52     return last;
53     }
54    
55    
56     int main(int argc, char** argv) {
57 torben 1282
58     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 torben 1231 return 1;
83     }
84    
85 torben 1282
86 torben 1231 char linebuf[BUFSIZE];
87    
88 torben 1282 string inputfile = vm["input-file"].as< string >();
89     if (! boost::filesystem::exists(inputfile) ) {
90     cout << "No file named " << inputfile << endl;
91     return 1;
92     }
93    
94 torben 1458
95 torben 1464 FILE* in = fopen64( inputfile.c_str(), "r" );
96 torben 1461 FILE* out = NULL;
97     //ifstream in( inputfile.c_str() );
98     //ofstream out;
99 torben 1231
100     ostringstream header;
101    
102 torben 1461 if ( in == NULL ) {
103 torben 1231 cout << "Could not open " << argv[1] << endl;
104 torben 1464 perror("");
105 torben 1231 return 2;
106 torben 1458 }
107    
108 torben 1459 time_t start = time(NULL);
109    
110     const char* SEARCH = "CREATE DATABASE";
111     const int SEARCHLEN = strlen(SEARCH);
112 torben 1231
113 torben 1461 while ( feof(in) == 0 && ferror(in) == 0 ) {
114     fgets(linebuf, BUFSIZE, in);
115 torben 1231
116    
117     //if (line.substr(0, 15) == "CREATE DATABASE" ) {
118 torben 1459 //if ( boost::starts_with(line, "CREATE DATABASE") ) {
119     if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) {
120 torben 1231
121 torben 1459 string line(linebuf);
122 torben 1461
123     if (out != NULL ) {
124     fclose(out);
125 torben 1231 }
126    
127     boost::trim(line);
128 torben 1284 string dbname = get_db_name(line);
129 torben 1231 cout << ">" << dbname << endl;
130    
131    
132     ostringstream oss;
133     oss << dbname << "_dump.sql";
134    
135     string filename = oss.str();
136    
137     bool did_exist = boost::filesystem::exists(filename);
138    
139 torben 1461 //out.open( filename.c_str(), ios::app );
140 torben 1463 out = fopen64(filename.c_str(), "a");
141 torben 1231
142    
143    
144 torben 1461 if ( out == NULL ) {
145 torben 1231 cout << "could not create outfile " << filename << endl;
146     return 3;
147     }
148    
149     if (!did_exist) {
150 torben 1461 fputs(header.str().c_str(), out);
151     //out << header.str() << endl; //write preamble in new file
152 torben 1231 }
153    
154     }
155    
156    
157 torben 1461 if (out != NULL ) {
158     fputs(linebuf, out);
159 torben 1462 //fputs("\n", out);
160 torben 1461 //out << linebuf << endl;
161 torben 1231 } else {
162 torben 1462 header << linebuf; //collect preamble for later use
163 torben 1231 }
164    
165     }
166    
167    
168 torben 1461 if (out != NULL)
169     fclose(out);
170    
171     // if (out.is_open())
172     // out.close();
173 torben 1458
174     time_t end = time(NULL);
175    
176 torben 1231
177 torben 1464 time_t elapsed = end-start;
178     cout << setfill('0') << "Elapsed time " << elapsed/60 << ":" << setw(2) << elapsed%60 << endl;
179    
180 torben 1461 fclose(in);
181 torben 1231 }

  ViewVC Help
Powered by ViewVC 1.1.20