inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/cloner
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_cloner
00005 #define inlib_cloner
00006 
00007 #include "sys/dir"
00008 #include "smanip"
00009 #include "file"
00010 
00011 namespace inlib {
00012 namespace cloner {
00013 
00014 class dump_visitor : public virtual dir::visitor {
00015 public:
00016   virtual bool directory(const std::string& a_string,bool&) {
00017     m_out << "dir : " << a_string << std::endl;
00018     return true;
00019   }
00020   virtual bool file(const std::string& a_string) {
00021     m_out << "file : " << a_string << std::endl;
00022     return true;
00023   }
00024 public:
00025   inline dump_visitor(std::ostream& a_out)
00026   :m_out(a_out){}
00027   virtual ~dump_visitor() {};
00028 public:
00029   inline dump_visitor(const dump_visitor& a_from)
00030   : dir::visitor(a_from),m_out(a_from.m_out){}
00031   inline dump_visitor& operator=(const dump_visitor&){
00032     return *this;
00033   }
00034 private:
00035   std::ostream& m_out;
00036 };
00037 
00038 class clone_visitor : public virtual dir::visitor {
00039 public:
00040   virtual bool directory(const std::string& a_dir,bool& aProcess) {
00041     aProcess = true;
00042     unsigned int l = m_dir.size();
00043     std::string to = m_To + a_dir.substr(l,a_dir.size()-l);
00044     replace_all(to);
00045     std::string p,n,s;
00046     path_name_suffix(to,p,n,s);
00047     if((n=="CVS")||(n==".svn")) {
00048       //m_out << "Do not create CVS directory " << inlib::sout(to) << "." 
00049       //      << std::endl;
00050       aProcess = false;
00051     } else {
00052       if(!dir::create(to)) {
00053         m_out << "Can't create dir " << inlib::sout(to) << "." << std::endl;
00054         m_failure = true;
00055         return false;
00056       } else {
00057         //m_out << "dir " << inlib::sout(to) << " created." << std::endl;
00058       }
00059     }
00060     return true;
00061   }
00062   virtual bool file(const std::string& aFile) {
00063     if(m_verbose) {
00064       m_out << "Lib_clone : treat " << inlib::sout(aFile) << "" << std::endl;
00065       //m_out << "Lib_clone :  m_dir " << inlib::sout(m_dir) << "" << std::endl;
00066       //m_out << "Lib_clone :  m_To " << inlib::sout(m_To) << "" << std::endl;
00067     }
00068     unsigned int l = m_dir.size();
00069     std::string newName = m_To + aFile.substr(l,aFile.size()-l);
00070     replace_all(newName);
00071     std::vector<std::string> text;
00072     if(!file::read(aFile,text)) {
00073       m_out << "Can't read " << inlib::sout(aFile) << std::endl;
00074       m_failure = true;
00075       return false;
00076     }
00077     FILE* file = ::fopen(newName.c_str(),"wb");
00078     if(!file) {
00079       m_out << "Can't create " << inlib::sout(newName) << std::endl;
00080       m_failure = true;
00081       return false;
00082     }
00083     unsigned int textn = text.size();
00084     for(unsigned int index=0;index<textn;index++) {
00085       std::string s = text[index];
00086       replace_all(s);
00087       replace(s,"LIB_CLONE_PWD",m_PWD);
00088       fprintf(file,"%s\n",s.c_str());
00089     }
00090     ::fclose(file);
00091     return true;
00092   }
00093 public:
00094   clone_visitor(std::ostream& a_out,
00095         const std::string& a_PWD,
00096         const std::string& a_dir,
00097         const std::string& aFrom,
00098         const std::string& aTo,
00099         bool aVerbose = false)
00100   :m_out(a_out)
00101   ,m_PWD(a_PWD)
00102   ,m_dir(a_dir)
00103   ,m_From(aFrom)
00104   ,m_To(aTo)
00105   ,m_verbose(aVerbose)
00106   ,m_failure(false){
00107     m_FROM = m_From;
00108     m_TO = m_To;
00109     touppercase(m_FROM);
00110     touppercase(m_TO);
00111     m_pack = m_From;
00112     m_to = m_To;
00113     tolowercase(m_pack);
00114     tolowercase(m_to);
00115   }
00116   virtual ~clone_visitor() {}
00117 private:
00118   clone_visitor(const clone_visitor& a_from)
00119   : dir::visitor(a_from),m_out(a_from.m_out){}
00120   clone_visitor& operator=(const clone_visitor&){return *this;}
00121 public:
00122   bool is_success() const { return !m_failure;}
00123 private:
00124   void replace_all(std::string& a_string) {
00125     replace(a_string,m_From,m_To);
00126     replace(a_string,m_FROM,m_TO);
00127     replace(a_string,m_pack,m_to);
00128   }
00129 private:
00130   std::ostream& m_out;
00131   std::string m_PWD;
00132   std::string m_dir;
00133   std::string m_From;
00134   std::string m_To;
00135   std::string m_FROM;
00136   std::string m_TO;
00137   std::string m_pack;
00138   std::string m_to;
00139   bool m_verbose;
00140   bool m_failure;
00141 };
00142 
00143 inline bool clone(
00144  const std::string& a_dir
00145 ,const std::string& a_from
00146 ,const std::string& a_to
00147 ,std::ostream& a_out
00148 ,bool a_verbose = false
00149 ,bool a_debug = false
00150 ){
00151   if(a_to.find(' ')!=std::string::npos) {
00152     a_out << "inlib::clone :"
00153           << " can't have spaces in new name (" << inlib::sout(a_to) << ")." 
00154           << std::endl;
00155     return false;
00156   }
00157 
00158   std::string spwd;
00159   if(!dir::pwd(spwd)) {
00160     a_out << "inlib::clone :"
00161           << " can't get current directory."
00162           << std::endl;
00163     return false;
00164   }
00165 
00166   dir::tree tree(a_out);
00167   tree.set_path(a_dir);
00168   if(!tree.build()) {
00169     a_out << "inlib::clone :"
00170           << " can't get files of " << inlib::sout(a_dir) 
00171           << "." << std::endl;
00172     return false;
00173   }
00174 
00175   if(a_debug) {
00176     dump_visitor visitor(a_out);
00177     tree.visit(visitor);
00178   }
00179 
00180   clone_visitor visitor(a_out,spwd,a_dir,a_from,a_to,a_verbose);
00181   tree.visit(visitor);
00182 
00183   return visitor.is_success();
00184 }
00185 
00186 }}
00187 
00188 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines