HG-MD  1
STD_save.h
Go to the documentation of this file.
00001 #ifndef STD_SAVE_H
00002 #define STD_SAVE_H
00003 
00004 #include<string>
00005 #include<vector>
00006 #include<sstream>
00007 #include<fstream>
00008 #include<iostream>
00009 #include<stdlib.h>
00010 
00011 #ifdef HIGH_PRECISION
00012         typedef long double Mdouble;
00013 #else 
00014         typedef double Mdouble;
00015 #endif
00016 
00017 using namespace std;
00018 
00023 class STD_save {
00024 
00025 public:
00026 
00028 STD_save(){
00029         constructor();
00030         #ifdef CONSTUCTOR_OUTPUT
00031                         cerr << "STD_save() finished"<<endl;
00032         #endif  
00033 }
00034 
00036 STD_save(STD_save& other) {
00037         constructor();
00038         counter = other.get_counter();
00039         problem_name.str(other.get_name());
00040         set_options_fstat(other.get_options_fstat());
00041         set_options_data(other.get_options_data());
00042         set_options_ene(other.get_options_ene());
00043         set_options_stat(other.get_options_stat());
00044         set_options_restart(other.get_options_restart());
00045         #ifdef CONSTUCTOR_OUTPUT
00046                         cerr << "STD_save(STD_save& other) finished"<<endl;
00047         #endif  
00048 };
00049 
00050 void constructor(){
00051         counter=0;
00052         set_options_restart(1);
00053         set_options_fstat(1);
00054         set_options_ene(1);
00055         set_options_data(1);
00056         set_options_stat(1);
00057         file_counter = 0;
00058         step_size = 1;
00059         data_file.precision(13);
00060         fstat_file.precision(13);
00061         ene_file.precision(13);
00062         stat_file.precision(5);
00063         stat_file.setf(ios::left);
00064 }
00065 
00066 
00068 void inc_counter_in_file();
00069 
00071 int read_run_num_from_file();
00072 
00074 void set_counter_from_file();
00075 
00077 void save_info_to_disk();
00078 
00080 void set_counter(int new_counter);
00081 
00083 int get_counter();
00084 
00086 virtual void info(){}
00087 
00089 bool FileExists(std::string strFilename);
00090 
00091 void auto_number(){     set_counter_from_file(); inc_counter_in_file();}
00092 
00094 std::vector<int> get_numbers(int size_x, int size_y);
00095 
00097 int launch_new(const char* name, bool quick=false);
00098 
00100 void set_name(const char* name){problem_name.str(""); problem_name << name;}
00102 string get_name(){return problem_name.str();}
00103 
00105 fstream& get_data_file(){return data_file;}     
00107 fstream& get_stat_file(){return stat_file;}     
00109 fstream& get_fstat_file(){return fstat_file;}   
00111 fstream& get_ene_file(){return ene_file;}
00112 
00113 void set_fstat_filename(string filename){fstat_filename.str(""); fstat_filename << filename;}
00114 void set_data_filename(string filename){data_filename.str(""); data_filename << filename;}
00115 void set_stat_filename(string filename){stat_filename.str(""); stat_filename << filename;}
00116 void set_ene_filename(string filename){ene_filename.str(""); ene_filename << filename;}
00117 
00118 void set_fstat_filename(){fstat_filename.str(""); fstat_filename << problem_name.str().c_str()  << ".fstat";}
00119 void set_data_filename(){data_filename.str(""); data_filename << problem_name.str().c_str()  << ".data";}
00120 void set_stat_filename(){stat_filename.str(""); stat_filename << problem_name.str().c_str()  << ".stat";}
00121 void set_ene_filename(){ene_filename.str(""); ene_filename << problem_name.str().c_str()  << ".ene";}
00122 
00123 string get_fstat_filename(){return fstat_filename.str();}
00124 string get_data_filename(){return data_filename.str();}
00125 string get_stat_filename(){return stat_filename.str();}
00126 string get_ene_filename(){return ene_filename.str();}
00127 
00128 
00129 void set_step_size(unsigned int new_){step_size= new_;} 
00130 unsigned int get_step_size(){return step_size;} 
00131 
00133 void set_options_fstat(unsigned int new_){options_fstat = new_;}
00134 unsigned int get_options_fstat(void){return options_fstat;}
00135 
00136 void set_options_data(unsigned int new_){options_data = new_;}
00137 unsigned int get_options_data(void){return options_data;}
00138 
00139 void set_options_stat(unsigned int new_){options_stat = new_;}
00140 unsigned int get_options_stat(void){return options_stat;}
00141 
00142 void set_options_restart(unsigned int new_){options_restart = new_;}
00143 unsigned int get_options_restart(void){return options_restart;}
00144 
00145 void set_options_ene(unsigned int new_){options_ene = new_;}
00146 unsigned int get_options_ene(void){return options_ene;}
00147 
00148 
00149 bool open_file (fstream& file, string filename, unsigned int options, fstream::openmode mode) 
00150 {
00151         if (options==1) {
00152                 //cout << "open " << filename << endl;
00153                 file.open( filename.c_str(), mode);
00154                 if (file.fail()) return false;
00155         }
00156         return true;
00157 }
00158 
00159 bool open_fstat_file (fstream::openmode mode = fstream::out) {
00160         return open_file (fstat_file, fstat_filename.str(), get_options_fstat(), mode);
00161 }
00162 bool open_data_file (fstream::openmode mode = fstream::out) {
00163         return open_file (data_file, data_filename.str(), get_options_data(), mode);
00164 }
00165 bool open_stat_file (fstream::openmode mode = fstream::out) {
00166         return open_file (stat_file, stat_filename.str(), get_options_stat(), mode);
00167 }
00168 bool open_ene_file (fstream::openmode mode = fstream::out) {
00169         return open_file (ene_file, ene_filename.str(), get_options_ene(), mode);
00170 }
00171 
00173 bool open_counted_file(fstream& file, string filenameNoCount, fstream::openmode mode){
00174         //create filename
00175         stringstream filename;
00176         filename << filenameNoCount << ".";
00177         if (file_counter<1000) filename << "0";
00178         if (file_counter<100) filename << "0";
00179         if (file_counter<10) filename << "0";
00180         filename << file_counter;
00181         //close old file
00182         if (file.is_open()) file.close();
00183         //open new file
00184         file.open(filename.str().c_str(), mode);
00185         if (!file.is_open()) {cerr << "Error in opening " << filename.str() << endl; return false;}
00186         //else cout << "open(" << mode << ") " << filename.str() << endl;
00187         //increase counter
00188         file_counter += step_size;
00189         return true;
00190 }
00191 
00192 bool increase_counter_fstat(fstream::openmode mode){
00193         file_counter -= step_size;
00194         return open_counted_file(fstat_file, fstat_filename.str(), mode);
00195 }
00196 bool increase_counter_data(fstream::openmode mode){
00197         return open_counted_file(data_file, data_filename.str(), mode);
00198 }
00199 bool increase_counter_stat(fstream::openmode mode){
00200         file_counter -= step_size;
00201         return open_counted_file(stat_file, stat_filename.str(), mode);
00202 }
00203 bool increase_counter_ene(fstream::openmode mode){
00204         file_counter -= step_size;
00205         return open_counted_file(ene_file, ene_filename.str(), mode);
00206 }
00207 
00208 void set_file_counter(int new_) {file_counter = new_;}
00209 int get_file_counter() {return file_counter;}
00210 
00211 private:
00213 int counter;
00214 
00215 protected:
00217 stringstream problem_name;
00218 
00221 stringstream data_filename;
00222 stringstream stat_filename;
00223 stringstream fstat_filename;
00224 stringstream ene_filename;
00225 
00227 fstream data_file;
00228 fstream stat_file;
00229 fstream fstat_file;
00230 fstream ene_file;
00231 
00232 
00237 unsigned int options_fstat; 
00238 unsigned int options_data; 
00239 unsigned int options_stat; 
00240 unsigned int options_ene; 
00241 unsigned int options_restart; 
00242 
00244 unsigned int file_counter;
00245 unsigned int step_size;
00246 };
00247 
00248 
00249 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines