Exemplo n.º 1
0
void PropertyList::set(const std::string& arg)
{
  // extract:   variable_name:type=value   or   variable_name:array[type]=value1,value2
 boost::regex expression(  "([[:word:]]+)(\\:([[:word:]]+)(\\[([[:word:]]+)\\])?=(.*))?"  );
 boost::match_results<std::string::const_iterator> what;

 std::string name;
 std::string type;
 std::string subtype; // in case of array<type>
 std::string value;

 if (regex_search(arg,what,expression))
 {
   name=what[1];
   type=what[3];
   subtype=what[5];
   value=what[6];
   // CFinfo << name << ":" << type << (subtype.empty() ? std::string() : std::string("["+subtype+"]"))  << "=" << value << CFendl;

   if      (type == "bool")
     store[name]=from_str<bool>(value);
   else if (type == "unsigned")
     store[name]=from_str<Uint>(value);
   else if (type == "integer")
     store[name]=from_str<int>(value);
   else if (type == "real")
     store[name]=from_str<Real>(value);
   else if (type == "string")
     store[name]=value;
   else if (type == "uri")
     store[name]=from_str<URI>(value);
   else if (type == "array")
   {
     std::vector<std::string> array;

     // the strings could have comma's inside, brackets, etc...
     Uint in_brackets(0);
     std::string::iterator first = value.begin();
     std::string::iterator it = first;
     for ( ; it!=value.end(); ++it)
     {
       if (*it == '(') // opening bracket
         ++in_brackets;
       else if (*it == ')') // closing bracket
         --in_brackets;
       else if (*it == ',' && in_brackets == 0)
       {
         array.push_back(std::string(first,it));
         boost::algorithm::trim(array.back());
         first = it+1;
       }
     }
     array.push_back(std::string(first,it));
     boost::algorithm::trim(array.back());

     if (subtype == "bool")
     {
       std::vector<bool> vec; vec.reserve(array.size());
       boost_foreach(const std::string& str_val,array)
           vec.push_back(from_str<bool>(str_val));
       store[name]=vec;
     }
     else if (subtype == "unsigned")
Exemplo n.º 2
0
void OptionList::fill_from_vector( const std::vector<std::string> & args )
{
  // extract:   variable_name:type=value   or   variable_name:array[type]=value1,value2
  boost::regex expression(  "([[:word:]]+)(\\:([[:word:]]+)(\\[([[:word:]]+)\\])?=(.*))?"  );
  boost::match_results<std::string::const_iterator> what;

  boost_foreach (const std::string& arg, args)
  {

    std::string name;
    std::string type;
    std::string subtype; // in case of array<type>
    std::string value;

    if (regex_search(arg,what,expression))
    {
      name=what[1];
      type=what[3];
      subtype=what[5];
      value=what[6];

      //CFinfo << name << ":" << type << (subtype.empty() ? std::string() : std::string("["+subtype+"]"))  << "=" << value << CFendl;

      if      (type == "bool")
        set_option_to_list< OptionT<bool> >(name, from_str<bool>( value ), *this );
      else if (type == "unsigned")
        set_option_to_list< OptionT<Uint> >(name, from_str<Uint>( value ), *this );
      else if (type == "integer")
        set_option_to_list< OptionT<int> >(name, from_str<int>( value ), *this );
      else if (type == "real")
        set_option_to_list< OptionT<Real> >(name, from_str<Real>( value ), *this );
      else if (type == "string")
        set_option_to_list< OptionT<std::string> >(name, value, *this );
      else if (type == "uri")
        set_option_to_list< OptionURI >(name, from_str<URI>( value ), *this );
      else if (type == "array")
      {
        std::vector<std::string> array;

        // the strings could have comma's inside, brackets, etc...
        Uint in_brackets(0);
        std::string::iterator first = value.begin();
        std::string::iterator it = first;
        for ( ; it!=value.end(); ++it)
        {
          if (*it == '(') // opening bracket
            ++in_brackets;
          else if (*it == ')') // closing bracket
            --in_brackets;
          else if (*it == ',' && in_brackets == 0)
          {
            array.push_back(std::string(first,it));
            boost::algorithm::trim(array.back());
            first = it+1;
          }
        }
        array.push_back(std::string(first,it));
        boost::algorithm::trim(array.back());

        if (subtype == "bool")
          set_array_to_list<bool>(name, array, *this);
        else if (subtype == "unsigned")
          set_array_to_list<Uint>(name, array, *this);
        else if (subtype == "integer")
          set_array_to_list<int>(name, array, *this);
        else if (subtype == "real")
          set_array_to_list<Real>(name, array, *this);
        else if (subtype == "string")
          set_option_to_list< OptionArrayT<std::string> >(name, array, *this);
        else if (subtype == "uri")
          set_array_to_list<URI>(name, array, *this);

      }
      else
        throw ParsingFailed(FromHere(), "The type ["+type+"] of passed argument [" + arg + "] is invalid.\n"+
          "Format should be:\n"
          " -  for simple types:  variable_name:type=value\n"
          " -  for array types:   variable_name:array[type]=value1,value2\n"
          "  with possible type: [bool,unsigned,integer,real,string,uri]");
    }
    else
      throw ParsingFailed(FromHere(), "Could not parse [" + arg + "].\n"+
         "Format should be:\n"
         " -  for simple types:  variable_name:type=value\n"
         " -  for array types:   variable_name:array[type]=value1,value2\n"
         "  with possible type: [bool,unsigned,integer,real,string,uri]");
  }