Exemplo n.º 1
0
/**
 * \brief Function to parse textual or file parameters
 * \param IN opt A structure containing the set of submitted options
 * \param OUT paramsStr a string containing all of parameters
 * \param IN paramOptName the name of the option for a single parameter
 * \param IN paramsVector a vector of parameters
 * \return true if all parameters are syntaxicaly valid
 */
int
vishnu::validateParameters(const boost::shared_ptr<Options> & opt,
                           std::string & paramsStr,
                           const std::string & paramOptName,
                           const ListStrings & paramsVector){

  if( opt->count(paramOptName) ){
    paramsStr = opt->get< std::string >(paramOptName) ;
  }

  // Append other parameters in paramStr
  for(ListStrings::const_iterator it = paramsVector.begin(); it != paramsVector.end() ; it++) {
    paramsStr += " " + *it ;
  }

  //Now check the syntax of parameters and set them suitable for VISHNU
  ListStrings paramsVecBuffer ;
  boost::trim(paramsStr) ; boost::split(paramsVecBuffer, paramsStr, boost::is_any_of(" "), boost::token_compress_on);

  paramsStr = "" ; // Reinitialization for outpout
  for(ListStrings::iterator it = paramsVecBuffer.begin(); it != paramsVecBuffer.end() ; it++) {

    size_t pos = (*it).find("=") ;
    if( pos == 0 || pos == std::string::npos || pos == (*it).size() - 1 ){
      std::cerr << "Uncompleted definition for the parameter : '" << *it << "'"<< std::endl ;
      return CLI_ERROR_INVALID_PARAMETER;
    }

    std::string paramName = (*it).substr(0, pos) ; // Keep the parameter name in upper case
    std::string paramValue = (*it).substr(pos+1, std::string::npos) ;

    // Check whether the parameter is duplicate
    if( paramsStr.size() != 0) {
      size_t start = 0 ;
      while( pos = paramsStr.find(paramName+"=", start), pos != std::string::npos ){
        if( pos == 0 || paramsStr[pos-1] == char(' ') ) {
          std::cerr << "Duplicate parameter : '" << paramName << "'"<< std::endl ;
          return CLI_ERROR_INVALID_PARAMETER ;
        }
        start = pos + paramName.size() ;
      }
      paramsStr += " " ;
    }
    // Append the parameter
    paramsStr += paramName + "=" + paramValue ;
  }
  return 0 ;
}
Exemplo n.º 2
0
/**
 * \brief function to set parameters appearing in a script
 * \param scriptContent The string to modify
 * \param \param params a list of parameters in the form of PARAM1=value1  PARAM2=value2 ...
 */
void
vishnu::setParams(std::string& scriptContent,
                  const std::string & params) {
  std::string paramName;
  std::string paramValue;
  size_t pos;
  ListStrings paramsVec;

  std::string& refParams = const_cast<std::string&>(params);
  boost::trim(refParams);
  boost::split(paramsVec, refParams, boost::is_any_of(" "));
  for (ListStrings::iterator it = paramsVec.begin(); it != paramsVec.end(); ++it) {
    pos = it->find("=");
    if (pos != std::string::npos) {
      paramName = it->substr(0, pos);
      paramValue = it->substr(pos+1, std::string::npos);
      replaceAllOccurences(scriptContent, "$" + paramName, paramValue);
      replaceAllOccurences(scriptContent, "${" + paramName + "}", paramValue);
    }
  }
}