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

Annotation of /misc/mysql_splitter/splitter.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20