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

Diff of /misc/mysql_splitter/splitter.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1460 by torben, Wed May 11 19:19:07 2011 UTC revision 1464 by torben, Thu May 12 19:38:56 2011 UTC
# Line 1  Line 1 
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>
# Line 91  int main(int argc, char** argv) { Line 92  int main(int argc, char** argv) {
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    
# Line 106  int main(int argc, char** argv) { Line 110  int main(int argc, char** argv) {
110          const char* SEARCH = "CREATE DATABASE";          const char* SEARCH = "CREATE DATABASE";
111          const int SEARCHLEN = strlen(SEARCH);          const int SEARCHLEN = strlen(SEARCH);
112    
113          while ( in.good() ) {          while ( feof(in) == 0 && ferror(in) == 0 ) {
114                  in.getline(linebuf, BUFSIZE);                  fgets(linebuf, BUFSIZE, in);
115    
116    
117                  //if (line.substr(0, 15) == "CREATE DATABASE" ) {                  //if (line.substr(0, 15) == "CREATE DATABASE" ) {
# Line 115  int main(int argc, char** argv) { Line 119  int main(int argc, char** argv) {
119                  if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) {                  if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) {
120    
121              string line(linebuf);              string line(linebuf);
122                          if (out.is_open() ) {                          
123                                  out.close();                          if (out != NULL ) {
124                                    fclose(out);
125                          }                          }
126    
127                          boost::trim(line);                          boost::trim(line);
# Line 131  int main(int argc, char** argv) { Line 136  int main(int argc, char** argv) {
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 << linebuf << endl;                          fputs(linebuf, out);
159                            //fputs("\n", out);
160                            //out << linebuf << endl;
161                  } else {                  } else {
162                          header << linebuf << 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  }  }

Legend:
Removed from v.1460  
changed lines
  Added in v.1464

  ViewVC Help
Powered by ViewVC 1.1.20