inlib  1.2.0
Classes | Public Member Functions
inlib::upgrader Class Reference

List of all members.

Classes

class  upgrade_visitor

Public Member Functions

 upgrader (std::ostream &a_out, bool a_verbose, const std::string &a_upfile, const std::string &aFilter)
virtual ~upgrader ()
 upgrader (const upgrader &a_from)
upgraderoperator= (const upgrader &a_from)
bool verbose () const
bool upgrade (const std::string &a_string)
bool upgrade_file (const std::string &a_file)
bool upgrade_dir (const std::string &aPath)

Detailed Description

Definition at line 15 of file upgrader.


Constructor & Destructor Documentation

inlib::upgrader::upgrader ( std::ostream &  a_out,
bool  a_verbose,
const std::string &  a_upfile,
const std::string &  aFilter 
) [inline]

Definition at line 43 of file upgrader.

  :m_out(a_out)
  ,m_verbose(a_verbose)
  ,m_filter(aFilter)
  {
    if(m_verbose) {
      m_out << "upgrader::upgrader :"
           << " upgrade file is " << inlib::sout(a_upfile) 
           << std::endl;
    }
    std::string _a_upfile;
    file_name(a_upfile,_a_upfile);
    if(!inlib::file::read(_a_upfile,m_orders)) {
      m_out << "upgrader::upgrader :"
           << " can't read upgrade file." 
           << std::endl;
    }
  
    // include files :
    bool hasIncs = true;
    while(hasIncs) {
      hasIncs = false;
      std::vector<std::string> orders;
      unsigned int ordern = m_orders.size();
      for(unsigned int index=0;index<ordern;index++) {
        std::vector<std::string> wrds = words(m_orders[index],"\\n");
        if( (wrds.size()==2) && (wrds[0]=="include")) {
          const std::string& file = wrds[1];
          if(m_verbose) {
            m_out << "upgrader::upgrade_file :"
                 << " include " << inlib::sout(file) 
                 << std::endl;
          }
          std::vector<std::string> incs;
          std::string _file;
          file_name(file,_file);
          if(!inlib::file::read(_file,incs)) {
            m_out << "upgrader::upgrader :"
                 << " can't read include upgrade file " 
                 << inlib::sout(file) << "."
                 << std::endl;
            m_orders.clear();
            return;
          }
          unsigned int incn = incs.size();
          for(unsigned int inci=0;inci<incn;inci++) {
            hasIncs = true;
            orders.push_back(incs[inci]);
          }
        } else {
          orders.push_back(m_orders[index]);
        }
      }
      m_orders = orders;
    }
  }
virtual inlib::upgrader::~upgrader ( ) [inline, virtual]

Definition at line 100 of file upgrader.

{}
inlib::upgrader::upgrader ( const upgrader a_from) [inline]

Definition at line 103 of file upgrader.

  :m_out(a_from.m_out)
  ,m_verbose(a_from.m_verbose)
  ,m_orders(a_from.m_orders)
  ,m_filter(a_from.m_filter)
  {}

Member Function Documentation

upgrader& inlib::upgrader::operator= ( const upgrader a_from) [inline]

Definition at line 109 of file upgrader.

                                             {
    m_verbose = a_from.m_verbose;  
    m_orders = a_from.m_orders;
    m_filter = a_from.m_filter;
    return *this;
  }
bool inlib::upgrader::upgrade ( const std::string &  a_string) [inline]

Definition at line 118 of file upgrader.

                                          {
    if(!m_orders.size()) return true; //Nothing to do.
    bool value;
    if(!dir::is_a(a_string,value)) return false;
    if(value) return upgrade_dir(a_string);
    else return upgrade_file(a_string);
  }
bool inlib::upgrader::upgrade_dir ( const std::string &  aPath) [inline]

Definition at line 260 of file upgrader.

                                           {
    if(!m_orders.size()) return true; //Nothing to do.
    if(m_verbose) {
      m_out << "upgrader::upgrade_dir :"
          << " upgrade directory " << inlib::sout(aPath) << "..."
          << std::endl;
    }
    dir::tree tree(m_out);
    tree.set_path(aPath);
    if(!tree.build()) {
      m_out << "upgrader::upgrade_dir :"
            << "Can't get files of " << inlib::sout(aPath) << std::endl;
      return false;
    }
    upgrade_visitor visitor(*this);
    return tree.visit(visitor);
  }
bool inlib::upgrader::upgrade_file ( const std::string &  a_file) [inline]

Definition at line 126 of file upgrader.

                                             {
    if(m_verbose) {
      m_out << "upgrader::upgrade_file :"
          << " upgrade file " << inlib::sout(a_file) << "..."
          << std::endl;
    }
    if(m_filter.size()) {
      if(!match(a_file,m_filter)) return true;
    }
    unsigned int ordern = m_orders.size();
    for(unsigned int index=0;index<ordern;index++) {
      std::vector<std::string> wrds =
        words(m_orders[index],"\\n",true); //true = take empty.
      if(m_orders[index].size()<=0) {
      } else if( (m_orders[index].size()>=1) && (m_orders[index][0]=='#') ) {
  
      } else if( (wrds.size()==3) && (wrds[0]=="replace")) {
        if(m_verbose) {
          m_out << "upgrader::upgrade_file :"
              << " replace " << inlib::sout(wrds[1]) 
              << " by " << inlib::sout(wrds[2]) 
              << std::endl;
        }
        std::vector<std::string> changes;
        if(!replace(a_file,wrds[1],wrds[2],changes)) {
          m_out << "upgrader::upgrade_file :"
              << " upgrade of file " << inlib::sout(a_file) << " fails."
              << std::endl;
          return false;
        }
        unsigned int changen = changes.size();
        for(unsigned int count=0;count<changen;count+=2) {
          m_out << ">>>>>>>>>>>>> In file " << inlib::sout(a_file) 
              << ", line changed : " << std::endl;
          m_out << changes[count] << std::endl;
          m_out << changes[count+1] << std::endl;
        }
  
      } else if( (wrds.size()==4) && (wrds[0]=="replace_if")) {
        if(m_verbose) {
          m_out << "upgrader::upgrade_file :"
              << " if " << inlib::sout(wrds[1]) 
              << " replace " << inlib::sout(wrds[2]) 
              << " by " << inlib::sout(wrds[3]) 
              << std::endl;
        }
        std::vector<std::string> changes;
        if(!replace_if(a_file,wrds[1],wrds[2],wrds[3],changes)) {
          m_out << "upgrader::upgrade_file :"
              << " upgrade of file " << inlib::sout(a_file) << " fails."
              << std::endl;
          return false;
        }
        unsigned int changen = changes.size();
        for(unsigned int count=0;count<changen;count+=2) {
          m_out << ">>>>>>>>>>>>> In file " << inlib::sout(a_file) 
              << ", line changed : " << std::endl;
          m_out << changes[count] << std::endl;
          m_out << changes[count+1] << std::endl;
        }

      } else if( (wrds.size()==2) && (wrds[0]=="remove_line")) {
        if(m_verbose) {
          m_out << "upgrader::upgrade_file :"
              << " remove_line " << inlib::sout(wrds[1]) 
              << std::endl;
        }
        if(!remove_lines(a_file,wrds[1])) {
          m_out << "upgrader::upgrade_file :"
              << " upgrade of file " << inlib::sout(a_file) << " fails."
              << std::endl;
          return false;
        }

      } else if( (wrds.size()==2) && (wrds[0]=="remove_if")) {
        if(m_verbose) {
          m_out << "upgrader::upgrade_file :"
              << " remove_if " << inlib::sout(wrds[1]) 
              << std::endl;
        }
        if(!remove_if(a_file,wrds[1])) {
          m_out << "upgrader::upgrade_file :"
              << " upgrade of file " << inlib::sout(a_file) << " fails."
              << std::endl;
          return false;
        }

      } else if( (wrds.size()==3) && (wrds[0]=="if_rm_eol")) {
        if(m_verbose) {
          m_out << "upgrader::upgrade_file :"
              << " if_rm_eol " << inlib::sout(wrds[1]) 
              << std::endl;
        }
        std::vector<std::string> changes;
        if(!if_rm_eol(a_file,wrds[1],wrds[2],changes)) {
          m_out << "upgrader::upgrade_file :"
              << " upgrade of file " << inlib::sout(a_file) << " fails."
              << std::endl;
          return false;
        }
        unsigned int changen = changes.size();
        for(unsigned int count=0;count<changen;count+=2) {
          m_out << ">>>>>>>>>>>>> In file " << inlib::sout(a_file) 
              << ", line changed : " << std::endl;
          m_out << changes[count] << std::endl;
          m_out << changes[count+1] << std::endl;
        }

      } else if( (wrds.size()==3) && (wrds[0]=="warn")) {
        std::vector<std::string> found;
        if(!file::found(a_file,wrds[1],found)) {
          m_out << "upgrader::upgrade_file :"
              << " upgrade of file " << inlib::sout(a_file) << " fails."
              << std::endl;
          return false;
        }
        unsigned int foundn = found.size();
        for(unsigned int count=0;count<foundn;count++) {
          m_out << ">>>>>>>>>>>>> In file " << inlib::sout(a_file) 
              << ", have to upgrade by hand line :" 
              << std::endl;
          m_out << found[count] << std::endl;
          m_out << "Action being :" << std::endl;
          m_out << wrds[2] << std::endl;
        }
      } else {
        m_out << "upgrader::upgrade_file :"
            << " bad formed or unknown request " << inlib::sout(m_orders[index])
            << std::endl;
      }
    }
    return true;
  }
bool inlib::upgrader::verbose ( ) const [inline]

Definition at line 116 of file upgrader.

{return m_verbose;}

The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines