MC_string_helper.cpp

Go to the documentation of this file.
00001 
00002 #include <algorithm>
00003 #include <fstream>
00004 #include <cmath>
00005 
00006 #include <MC_string_helper.hpp>
00007 
00008 namespace mesh_conv
00009 {
00010   
00011 
00012   // helper to tokenize
00013   std::vector<std::string> MC_string_tokenizer::tokenize(const std::string& str,const std::string& delimiters)
00014   {
00015     std::vector<std::string> tokens;
00016     std::string::size_type delimPos = 0, tokenPos = 0, pos = 0;
00017     
00018     if(str.length()<1)  return tokens;
00019     while(1){
00020       delimPos = str.find_first_of(delimiters, pos);
00021       tokenPos = str.find_first_not_of(delimiters, pos);
00022       
00023       if(std::string::npos != delimPos){
00024         if(std::string::npos != tokenPos){
00025           if(tokenPos<delimPos){
00026             tokens.push_back(str.substr(pos,delimPos-pos));
00027           }else{
00028             tokens.push_back("");
00029           }
00030         }else{
00031           tokens.push_back("");
00032         }
00033         pos = delimPos+1;
00034       } else {
00035         if(std::string::npos != tokenPos){
00036           tokens.push_back(str.substr(pos));
00037         } else {
00038           tokens.push_back("");
00039         }
00040         break;
00041       }
00042     }
00043     return tokens;
00044   }
00045 
00046   std::string MC_string_converter::zero_padding(const std::string& input,const int& zero_number)
00047   {
00048       std::string current_string=input;
00049       int current_length=input.size();
00050       for(int k=0;k<zero_number-current_length;k++)
00051           current_string=std::string("0")+current_string;
00052       return current_string;
00053   }
00054   std::string MC_string_converter::to_lower(const std::string& input)
00055   {
00056       std::string res("");
00057       for(unsigned int k=0;k<input.length();++k)
00058           res+=std::tolower(input[k],std::cout.getloc ());
00059       return res;
00060   }
00061   std::string MC_string_converter::to_upper(const std::string& input)
00062   {
00063       std::string res("");
00064       for(unsigned int k=0;k<input.length();++k)
00065           res+=std::toupper(input[k],std::cout.getloc ());
00066       return res;
00067   }
00068 
00069   std::vector <std::string> MC_string_converter::delete_empty(const std::vector <std::string>& in)
00070   {
00071       std::vector <std::string> res;
00072 
00073       int N=in.size();
00074       for(int k=0;k<N;++k)
00075           if(in[k]!=" " && in[k]!="")
00076               res.push_back(in[k]);
00077       return res;
00078   }
00079 
00080   MC_file_helper MC_file_helper::copy(const std::string& input_filename,const std::string& output_filename)
00081   {
00082       std::ifstream input(input_filename.c_str(),std::ifstream::in);
00083       std::ofstream output(output_filename.c_str(),std::ifstream::out);
00084 
00085       if(input.is_open()==false)
00086       {std::cout<<"Error in MC_file_helper::copy("<<input_filename<<","<<output_filename<<"), the file "<<input_filename<<" cannot be opened"<<std::endl;exit(-1);}
00087       if(output.is_open()==false)
00088       {std::cout<<"Error in MC_file_helper::copy("<<input_filename<<","<<output_filename<<"), the file "<<output_filename<<" cannot be opened"<<std::endl;exit(-1);}
00089 
00090       char buffer[static_cast<int>(pow(2,16))];
00091       while(input.good())
00092       {
00093           input.getline(buffer,static_cast<int>(pow(2,16)));
00094           if(input.good())
00095               output<<std::string(buffer)+"\n";
00096       }
00097 
00098 
00099       input.close();
00100       output.close();
00101 
00102       return MC_file_helper();
00103   }
00104 
00105 
00106   std::pair<std::pair<int,int>,std::pair<std::string,std::string> > MC_string_converter::extract_number_part(const std::string& filename)
00107   {
00108     int N=filename.length();
00109 
00110     bool is_number=false;
00111     int last_position_number=-1;
00112     int first_position_number=-1;
00113     for(int k=N-1;k>=0;--k)
00114     {
00115         if(filename[k]>='0' && filename[k]<='9')
00116             is_number=true;
00117         else
00118             is_number=false;
00119         if(last_position_number==-1 && is_number==true)
00120             last_position_number=k;
00121         if(first_position_number==-1 && is_number==false && last_position_number!=-1)
00122             first_position_number=k;
00123     }
00124 
00125 
00126     std::string part_1=filename.substr(0,first_position_number+1);
00127     std::string part_2=filename.substr(last_position_number+1,filename.length());
00128 
00129 
00130     std::string number=filename.substr(first_position_number+1,last_position_number-first_position_number);
00131 
00132     int N_zero=-1;
00133     for(unsigned int k=0;k<number.length();++k)
00134     {
00135         if(number[k]!='0' && N_zero==-1)
00136             N_zero=k;
00137     }
00138     if(N_zero==-1)
00139         N_zero=number.length()-1;
00140 
00141     bool is_ok=true;
00142     int current_number=value_of<int>(number,&is_ok);
00143 
00144     if(is_ok==false)
00145         return std::make_pair(std::make_pair(-1,-1),std::make_pair(filename,std::string()));
00146 
00147     return std::make_pair(std::make_pair(current_number,N_zero),std::make_pair(part_1,part_2));
00148 
00149   }
00150 
00151   std::vector<std::string> MC_string_converter::load_filename_sequence(const std::string filename,const unsigned int& iteration)
00152   {
00153       //extract informations from the filename
00154       std::pair<std::pair<int,int>,std::pair<std::string,std::string> > info=MC_string_converter::extract_number_part(filename);
00155 
00156 
00157       std::vector<std::string> v_file;
00158 
00159       //std::cout<<info.first.first<<","<<info.first.second<<std::endl;
00160       if(info.first.second==-1)
00161       {
00162           v_file.push_back(filename);
00163           return v_file;
00164       }
00165 
00166       int zero_pad=info.first.second+(MC_string_converter::to_string(info.first.first).length());
00167 
00168       bool is_continue=true;
00169       std::ifstream file;
00170       int k=info.first.first;
00171       do
00172       {
00173           std::string number=MC_string_converter::zero_padding(MC_string_converter::to_string(k),zero_pad);
00174           std::string current_file=info.second.first+number+info.second.second;
00175 
00176 
00177 
00178           file.open(current_file.c_str());
00179           if(file.good()==false)
00180               is_continue=false;
00181           else
00182               v_file.push_back(current_file);
00183 
00184           //std::cout<<current_file<<" "<<is_continue<<std::endl;
00185           file.close();
00186           k+=iteration;
00187       }while(is_continue==true);
00188 
00189       return v_file;
00190 
00191   }
00192 
00193 
00194 }

Generated on Sun Apr 18 20:24:47 2010 by  doxygen 1.6.1