Exemple #1
0
  bool OptionsList::ReadFromStream(const Journalist& jnlst,
                                   std::istream& is)
  {
    jnlst.Printf(J_DETAILED, J_MAIN, "Start reading options from stream.\n");

    while (true) {
      std::string tag;
      std::string value;

      if (!readnexttoken(is, tag)) {
        // That's it - end of file reached.
        jnlst.Printf(J_DETAILED, J_MAIN,
                     "Finished reading options from file.\n");
        return true;
      }

      if (!readnexttoken(is, value)) {
        // Can't read value for a given tag
        jnlst.Printf(J_ERROR, J_MAIN,
                     "Error reading value for tag %s from file.\n",
                     tag.c_str());
        return false;
      }

      // Now add the value for the options list
      jnlst.Printf(J_DETAILED, J_MAIN,
                   "Adding option \"%s\" with value \"%s\" to OptionsList.\n",
                   tag.c_str(), value.c_str());

      if (IsValid(reg_options_)) {
        SmartPtr<const RegisteredOption> option = reg_options_->GetOption(tag);
        if (IsNull(option)) {
          std::string msg = "Read Option: ";
          msg += tag;
          msg += ". It is not a valid option. Check the list of available options.";
          THROW_EXCEPTION(OPTION_INVALID, msg);
        }

        if (option->Type() == OT_String) {
          bool result = SetStringValue(tag, value, false);
          ASSERT_EXCEPTION(result, OPTION_INVALID,
                           "Error setting string value read from option file.");
        }
        else if (option->Type() == OT_Number) {
          char* p_end;
          Number retval = strtod(value.c_str(), &p_end);
          if (*p_end!='\0' && !isspace(*p_end)) {
            std::string msg = "Option \"" + tag +
                              "\": Double value expected, but non-numeric option value \"" +
                              value + "\" found.\n";
            THROW_EXCEPTION(OPTION_INVALID, msg);
          }
          bool result = SetNumericValue(tag, retval, false);
          ASSERT_EXCEPTION(result, OPTION_INVALID,
                           "Error setting numeric value read from file.");
        }
        else if (option->Type() == OT_Integer) {
          char* p_end;
          Index retval = strtol(value.c_str(), &p_end, 10);
          if (*p_end!='\0' && !isspace(*p_end)) {
            std::string msg = "Option \"" + tag +
                              "\": Integer value expected, but non-integer option value \"" +
                              value + "\" found.\n";
            if (IsValid(jnlst_)) {
              option->OutputDescription(*jnlst_);
            }
            THROW_EXCEPTION(OPTION_INVALID, msg);
          }
          bool result = SetIntegerValue(tag, retval, false);
          ASSERT_EXCEPTION(result, OPTION_INVALID,
                           "Error setting integer value read from option file.");
        }
        else {
          DBG_ASSERT(false && "Option Type: Unknown");
        }
      }
      else {
        bool result = SetStringValue(tag, value, false);
        ASSERT_EXCEPTION(result, OPTION_INVALID,
                         "Error setting value read from option file.");
      }
    }
  }
Exemple #2
0
  bool OptionsList::ReadFromStream(const Journalist& jnlst,
                                   std::istream& is)
  {
    jnlst.Printf(J_DETAILED, J_MAIN, "Start reading options from stream.\n");

    while (true) {
      std::string tag;
      std::string value;

      if (!readnexttoken(is, tag)) {
        // That's it - end of file reached.
        jnlst.Printf(J_DETAILED, J_MAIN,
                     "Finished reading options from file.\n");
        return true;
      }

      if (!readnexttoken(is, value)) {
        // Can't read value for a given tag
        jnlst.Printf(J_ERROR, J_MAIN,
                     "Error reading value for tag %s from file.\n",
                     tag.c_str());
        return false;
      }

      // Now add the value for the options list
      jnlst.Printf(J_DETAILED, J_MAIN,
                   "Adding option \"%s\" with value \"%s\" to OptionsList.\n",
                   tag.c_str(), value.c_str());

      if (IsValid(reg_options_)) {
        SmartPtr<const RegisteredOption> option = reg_options_->GetOption(tag);
        if (IsNull(option)) {
          std::string msg = "Read Option: \"";
          msg += tag;
          msg += "\". It is not a valid option. Check the list of available options.";
          THROW_EXCEPTION(OPTION_INVALID, msg);
        }

        if (option->Type() == OT_String) {
          bool result = SetStringValue(tag, value, false);
          ASSERT_EXCEPTION(result, OPTION_INVALID,
                           "Error setting string value read from option file.");
        }
        else if (option->Type() == OT_Number) {
          // Some people like to use 'd' instead of 'e' in floating
          // point numbers.  Therefore, we change a 'd' to an 'e'
          char* buffer = new char[value.length()+1];
          strcpy(buffer, value.c_str());
          for (int i=0; i<(int)value.length(); ++i) {
            if (buffer[i]=='d' || buffer[i]=='D') {
              buffer[i] = 'e';
            }
          }
          char* p_end;
          Number retval = strtod(buffer, &p_end);
          if (*p_end!='\0' && !isspace(*p_end)) {
            delete [] buffer;
            std::string msg = "Option \"" + tag +
                              "\": Double value expected, but non-numeric option value \"" +
                              value + "\" found.\n";
            THROW_EXCEPTION(OPTION_INVALID, msg);
          }
          delete [] buffer;
          bool result = SetNumericValue(tag, retval, false);
          ASSERT_EXCEPTION(result, OPTION_INVALID,
                           "Error setting numeric value read from file.");
        }
        else if (option->Type() == OT_Integer) {
          char* p_end;
          size_t retval = strtol(value.c_str(), &p_end, 10);
          if (*p_end!='\0' && !isspace(*p_end)) {
            std::string msg = "Option \"" + tag +
                              "\": Integer value expected, but non-integer option value \"" +
                              value + "\" found.\n";
            if (IsValid(jnlst_)) {
              option->OutputDescription(*jnlst_);
            }
            THROW_EXCEPTION(OPTION_INVALID, msg);
          }
          bool result = SetIntegerValue(tag, static_cast<Index>(retval), false);
          ASSERT_EXCEPTION(result, OPTION_INVALID,
                           "Error setting integer value read from option file.");
        }
        else {
          DBG_ASSERT(false && "Option Type: Unknown");
        }
      }
      else {
        bool result = SetStringValue(tag, value, false);
        ASSERT_EXCEPTION(result, OPTION_INVALID,
                         "Error setting value read from option file.");
      }
    }
  }