/[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 1461 by torben, Thu May 12 15:32:44 2011 UTC
# Line 1  Line 1 
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>
# Line 91  int main(int argc, char** argv) { Line 91  int main(int argc, char** argv) {
91          }          }
92    
93    
94          ifstream in( inputfile.c_str() );          FILE* in = fopen( inputfile.c_str(), "r" );
95          ofstream out;          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          }          }
# Line 106  int main(int argc, char** argv) { Line 108  int main(int argc, char** argv) {
108          const char* SEARCH = "CREATE DATABASE";          const char* SEARCH = "CREATE DATABASE";
109          const int SEARCHLEN = strlen(SEARCH);          const int SEARCHLEN = strlen(SEARCH);
110    
111          while ( in.good() ) {          while ( feof(in) == 0 && ferror(in) == 0 ) {
112                  in.getline(linebuf, BUFSIZE);                  fgets(linebuf, BUFSIZE, in);
113    
114    
115                  //if (line.substr(0, 15) == "CREATE DATABASE" ) {                  //if (line.substr(0, 15) == "CREATE DATABASE" ) {
# Line 115  int main(int argc, char** argv) { Line 117  int main(int argc, char** argv) {
117                  if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) {                  if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) {
118    
119              string line(linebuf);              string line(linebuf);
120                          if (out.is_open() ) {                          
121                                  out.close();                          if (out != NULL ) {
122                                    fclose(out);
123                          }                          }
124    
125                          boost::trim(line);                          boost::trim(line);
# Line 131  int main(int argc, char** argv) { Line 134  int main(int argc, char** argv) {
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  = fopen(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 << linebuf << endl;                          fputs(linebuf, out);
157                            fputs("\n", out);
158                            //out << linebuf << endl;
159                  } else {                  } else {
160                          header << linebuf << endl; //collect preamble for later use                          header << linebuf << endl; //collect preamble for later use
161                  }                  }
# Line 156  int main(int argc, char** argv) { Line 163  int main(int argc, char** argv) {
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);      time_t end = time(NULL);
173    
174      cout << "Elapsed " << (end-start) << " seconds" << endl;      cout << "Elapsed " << (end-start) << " seconds" << endl;
175    
176            fclose(in);
         in.close();  
177  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20