Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef tools_srep 4 #ifndef tools_srep 5 #define tools_srep 5 #define tools_srep 6 6 7 #include <string> 7 #include <string> 8 #include <vector> 8 #include <vector> 9 #include <utility> << 10 9 11 #include "forit" 10 #include "forit" 12 11 13 namespace tools { 12 namespace tools { 14 13 15 inline void replace(std::string& a_string,char 14 inline void replace(std::string& a_string,char a_old,char a_new){ 16 tools_sforit(a_string,it) { 15 tools_sforit(a_string,it) { 17 if((*it)==a_old) *it = a_new; 16 if((*it)==a_old) *it = a_new; 18 } 17 } 19 } 18 } 20 19 21 inline bool replace(std::string& a_string,cons 20 inline bool replace(std::string& a_string,const std::string& a_old,const std::string& a_new){ 22 // return true : some replacement done. 21 // return true : some replacement done. 23 // return false : nothing replaced. 22 // return false : nothing replaced. 24 if(a_old.empty()) return false; 23 if(a_old.empty()) return false; 25 std::string snew; 24 std::string snew; 26 std::string::size_type lold = a_old.length() 25 std::string::size_type lold = a_old.length(); 27 bool status = false; 26 bool status = false; 28 std::string stmp = a_string; 27 std::string stmp = a_string; 29 while(true) { 28 while(true) { 30 std::string::size_type pos = stmp.find(a_o 29 std::string::size_type pos = stmp.find(a_old); 31 if(pos==std::string::npos){ 30 if(pos==std::string::npos){ 32 snew += stmp; 31 snew += stmp; 33 break; 32 break; 34 } else { 33 } else { 35 snew += stmp.substr(0,pos); 34 snew += stmp.substr(0,pos); 36 snew += a_new; 35 snew += a_new; 37 stmp = stmp.substr(pos+lold,stmp.length( 36 stmp = stmp.substr(pos+lold,stmp.length()-(pos+lold)); 38 status = true; 37 status = true; 39 } 38 } 40 } 39 } 41 a_string = std::move(snew); << 40 a_string = snew; 42 return status; 41 return status; 43 } 42 } 44 43 45 inline bool replace_(std::string& a_string,con 44 inline bool replace_(std::string& a_string,const std::string& a_old,const std::string& a_new) { 46 return replace(a_string,a_old,a_new); 45 return replace(a_string,a_old,a_new); 47 } 46 } 48 47 49 inline bool replace(std::vector<std::string>& 48 inline bool replace(std::vector<std::string>& a_strings,const std::string& a_old,const std::string& a_new){ 50 tools_vforit(std::string,a_strings,it) { 49 tools_vforit(std::string,a_strings,it) { 51 if(!replace(*it,a_old,a_new)) return false 50 if(!replace(*it,a_old,a_new)) return false; 52 } 51 } 53 return true; 52 return true; 54 } 53 } 55 54 56 inline void toxml(std::string& a_string){ 55 inline void toxml(std::string& a_string){ 57 // > : < 56 // > : < 58 // < : > 57 // < : > 59 // & : & 58 // & : & 60 // " : " 59 // " : " 61 // ' : ' 60 // ' : ' 62 replace(a_string,"&","&"); //must be fir 61 replace(a_string,"&","&"); //must be first. 63 replace(a_string,"<","<"); 62 replace(a_string,"<","<"); 64 replace(a_string,">",">"); 63 replace(a_string,">",">"); 65 replace(a_string,"\"","""); 64 replace(a_string,"\"","""); 66 replace(a_string,"'","'"); 65 replace(a_string,"'","'"); 67 } 66 } 68 67 69 inline std::string to_xml(const std::string& a 68 inline std::string to_xml(const std::string& a_string){ 70 std::string _s = a_string; 69 std::string _s = a_string; 71 toxml(_s); 70 toxml(_s); 72 return _s; 71 return _s; 73 } 72 } 74 73 75 inline void to_win(std::string& a_string) { 74 inline void to_win(std::string& a_string) { 76 replace(a_string,"/cygdrive/c","C:"); // CY 75 replace(a_string,"/cygdrive/c","C:"); // CYGWIN. 77 replace(a_string,"/mnt/c","C:"); // WS 76 replace(a_string,"/mnt/c","C:"); // WSL. 78 replace(a_string,'/','\\'); 77 replace(a_string,'/','\\'); 79 } 78 } 80 79 81 inline void to_win_python(std::string& a_strin 80 inline void to_win_python(std::string& a_string) { 82 replace(a_string,"/cygdrive/c","c:"); // CY 81 replace(a_string,"/cygdrive/c","c:"); // CYGWIN. 83 replace(a_string,"/mnt/c","c:"); // WS 82 replace(a_string,"/mnt/c","c:"); // WSL. (Python wants c: in lowercase). 84 replace(a_string,"C:","c:"); 83 replace(a_string,"C:","c:"); 85 replace(a_string,'\\','/'); 84 replace(a_string,'\\','/'); 86 } 85 } 87 86 88 } 87 } 89 88 90 #endif 89 #endif