MercuryDPM  0.11
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SplitFiles.cpp
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
27 
28 #include <string>
29 #include <iomanip>
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <sys/stat.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 
37 
38 class CFile {
39 
40 public:
41 
43  CFile(std::string name) {
44  //set file names
45  data_filename.str("");
46  data_filename << name << ".data";
47  fstat_filename.str("");
48  fstat_filename << name << ".fstat";
49 
50  //open in-streams
51  data_file.open(data_filename.str().c_str(), std::fstream::in);
52  fstat_file.open(fstat_filename.str().c_str(), std::fstream::in);
53 
54  if (data_file.fail() || fstat_file.fail())
55  {
56  std::cerr << "ERROR: Input file " << data_filename.str() << " or " << fstat_filename.str() << " not found" << std::endl;
57  data_file.close();
58  fstat_file.close();
59  exit(-1);
60  } else {
61  std::cout << "Files opened: " << data_filename.str() << " and " << fstat_filename.str() << std::endl;
62  }
63  }
64 
66  ~CFile() {
67  data_file.close();
68  fstat_file.close();
69  std::cout << "Files closed: " << data_filename.str() << " and " << fstat_filename.str() << std::endl;
70  }
71 
72  bool copy(unsigned int stepsize, unsigned int counter) {
73  //return copy_last_time_step();
74  return copy_data(stepsize,counter) && copy_fstat(stepsize,counter);
75  }
76 
77  bool copy_data(unsigned int stepsize, unsigned int counter) {
78  unsigned int N;
79  std::string line;
80  std::stringstream output_filename;
81  std::fstream output_file;
82 
83  data_file >> N;
84  while (data_file.good()) {
85  //set output_filename
86  std::stringstream output_filename("");
87  output_filename << data_filename.str() << ".";
88  if (counter<1000) output_filename << "0";
89  if (counter<100) output_filename << "0";
90  if (counter<10) output_filename << "0";
91  output_filename << counter;
92  counter ++;
93  //std::cout << "Outputfile: " << output_filename.str() << std::endl;
94 
95  //open, write, close output file
96  output_file.open(output_filename.str().c_str(), std::fstream::out);
97  getline(data_file,line);
98  output_file << N << line << std::endl;
99  std::cout << N << line << std::endl;
100  for (unsigned int i=0; i<N; i++) {
101  getline(data_file,line);
102  output_file << line << std::endl;
103  }
104  output_file.close();
105  data_file >> N;
106 
107  //step over some timesteps
108  for(unsigned int j=1; j<stepsize; j++) {
109  for (unsigned int i=0; i<N+1; i++) getline(data_file,line);
110  data_file >> N;
111  }
112 
113  }
114  return true;
115  }
116 
117  bool copy_fstat(unsigned int stepsize, unsigned int counter) {
118  std::string line;
119  std::stringstream output_filename;
120  std::fstream output_file;
121 
122  getline(fstat_file,line);
123  while (fstat_file.good()) {
124  //set output_filename
125  output_filename.str("");
126  output_filename << fstat_filename.str() << ".";
127  if (counter<1000) output_filename << "0";
128  if (counter<100) output_filename << "0";
129  if (counter<10) output_filename << "0";
130  output_filename << counter;
131  counter ++;
132 
133  //open, write, close output file
134  output_file.open(output_filename.str().c_str(), std::fstream::out);
135  std::cout << line << std::endl;
136  output_file << line << std::endl;
137  getline(fstat_file,line);
138  output_file << line << std::endl;
139  getline(fstat_file,line);
140  output_file << line << std::endl;
141  getline(fstat_file,line);
142  while (line.c_str()[0] != '#'&&fstat_file.good()) {
143  getline(fstat_file,line);
144  output_file << line << std::endl;
145  }
146  output_file.close();
147 
148  //step over some timesteps
149  for(unsigned int j=1; j<stepsize; j++) {
150  getline(fstat_file,line);
151  getline(fstat_file,line);
152  getline(fstat_file,line);
153  while (line.c_str()[0] != '#'&&fstat_file.good()) {
154  getline(fstat_file,line);
155  }
156  }
157  }
158  return true;
159  }
160 
161 
162 private:
164  std::stringstream data_filename;
165  std::stringstream fstat_filename;
166 
168  std::fstream data_file;
169  std::fstream fstat_file;
170 };
171 
172 int main(int argc, char *argv[])
173 {
174  if (argc<2) {
175  std::cerr << "split_files problem_name [stepsize [initial_counter]]" << std::endl;
176  return -1;
177  }
178  std::string name(argv[1]);
179  std::cout << "Name: " << name << std::endl;
180 
181  unsigned int stepsize = 1;
182  if (argc>2) stepsize = atoi(argv[2]);
183 
184  //defines the initial counter
185  unsigned int counter = 0;
186  if (argc>3) counter = atoi(argv[3]);
187 
188  CFile files(name);
189  files.copy(stepsize,counter);
190  std::cout << "finished writing split files: " << name << std::endl;
191  return 0;
192 }
~CFile()
Destructor.
Definition: SplitFiles.cpp:66
std::fstream fstat_file
Definition: SplitFiles.cpp:169
bool copy(unsigned int stepsize, unsigned int counter)
Definition: SplitFiles.cpp:72
int main(int argc, char *argv[])
Definition: SplitFiles.cpp:172
bool copy()
takes data and fstat files and splits them into *.data.???? and *.fstat.???? files ...
bool copy_fstat(unsigned int stepsize, unsigned int counter)
Definition: SplitFiles.cpp:117
std::stringstream fstat_filename
Definition: SplitFiles.cpp:165
std::fstream data_file
Stream used for data files.
CFile(std::string name)
Constructor.
Definition: SplitFiles.cpp:43
bool copy_data(unsigned int stepsize, unsigned int counter)
Definition: SplitFiles.cpp:77
std::stringstream data_filename
These store the save file names,.