Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/srep is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_srep
0005 #define tools_srep
0006 
0007 #include <string>
0008 #include <vector>
0009 
0010 #include "forit"
0011 
0012 namespace tools {
0013 
0014 inline void replace(std::string& a_string,char a_old,char a_new){
0015   tools_sforit(a_string,it) {
0016     if((*it)==a_old) *it = a_new;
0017   }
0018 }
0019 
0020 inline bool replace(std::string& a_string,const std::string& a_old,const std::string& a_new){
0021   // return true : some replacement done.
0022   // return false : nothing replaced.
0023   if(a_old.empty()) return false;
0024   std::string snew;
0025   std::string::size_type lold = a_old.length();
0026   bool status = false;
0027   std::string stmp = a_string;
0028   while(true) {
0029     std::string::size_type pos = stmp.find(a_old);
0030     if(pos==std::string::npos){
0031       snew += stmp;
0032       break;
0033     } else {
0034       snew += stmp.substr(0,pos);
0035       snew += a_new;
0036       stmp = stmp.substr(pos+lold,stmp.length()-(pos+lold));
0037       status = true;
0038     }
0039   }
0040   a_string = snew;
0041   return status;
0042 }
0043 
0044 inline bool replace_(std::string& a_string,const std::string& a_old,const std::string& a_new) {
0045   return replace(a_string,a_old,a_new);
0046 }
0047 
0048 inline bool replace(std::vector<std::string>& a_strings,const std::string& a_old,const std::string& a_new){
0049   tools_vforit(std::string,a_strings,it) {
0050     if(!replace(*it,a_old,a_new)) return false;
0051   }
0052   return true;
0053 }
0054 
0055 inline void toxml(std::string& a_string){
0056   // > : &lt;
0057   // < : &gt;
0058   // & : &amp;
0059   // " : &quot;
0060   // ' : &apos;
0061   replace(a_string,"&","&amp;"); //must be first.
0062   replace(a_string,"<","&lt;");
0063   replace(a_string,">","&gt;");
0064   replace(a_string,"\"","&quot;");
0065   replace(a_string,"'","&apos;");
0066 }
0067 
0068 inline std::string to_xml(const std::string& a_string){
0069   std::string _s = a_string;
0070   toxml(_s);
0071   return _s;
0072 }
0073 
0074 inline void to_win(std::string& a_string) {
0075   replace(a_string,"/cygdrive/c","C:");  // CYGWIN.
0076   replace(a_string,"/mnt/c","C:");       // WSL.
0077   replace(a_string,'/','\\');
0078 }
0079 
0080 inline void to_win_python(std::string& a_string) {
0081   replace(a_string,"/cygdrive/c","c:");  // CYGWIN.
0082   replace(a_string,"/mnt/c","c:");       // WSL. (Python wants c: in lowercase).
0083   replace(a_string,"C:","c:");
0084   replace(a_string,'\\','/');
0085 }
0086 
0087 }
0088 
0089 #endif