Пример #1
0
bool ReadValue(AnyValue& value,std::istream& in,const std::string& delims)
{
  EatWhitespace(in);
  if(!in) {
    printf("ReadValue: hit end of file\n");
    return false;
  }
  if(in.peek() == '"') {
    //beginning of string
    std::string str;
    if(!InputQuotedString(in,str)) {
      printf("ReadValue: unable to read quoted string\n");
      return false;
    }
    value = str;
    return true;
  }
  else if(in.peek() == '\'') {
    //character: TODO: translate escapes properly
    in.get();
    char c=in.get();
    value = c;
    char end=in.get();
    if(end != '\'') {
      printf("ReadValue: character not delimited properly\n");
      return false;
    }
    return true;
  }
  else {
    std::string str;
    if(delims.empty())
      in >> str;
    else {
      while(in && delims.find(in.peek()) == std::string::npos && !isspace(in.peek())) {
	str += in.get();
      }
    }
    if(str.empty()) {
      printf("ReadValue: read an empty string\n");
      return false;
    }
    if(IsValidInteger(str.c_str())) {
      int val;
      std::stringstream ss(str);
      ss>>val;
      value = val;
      return true;
    }
Пример #2
0
//
// See help text within for an explanation of the expected parameters.
//
void CGalileoCmdLine::ParseParam(const char *pszParam, BOOL bFlag, BOOL bLast)
{
	static int param_count = 0;
	static char last_switch = 0;
	char temp_switch;

	param_count++;

	if (m_bFail) {
		// If command line parsing has failed before, don't
		// try to interpret the command line further.
		return;
	}

	if (bFlag && strlen(pszParam) != 1) {
		Fail("Exactly one letter must follow a switch character.  " "Switch characters are \"/\" and \"-\".");
		return;
	}

	if (pszParam[0] == '?') {
		// get Syntax display string from resource file
		CString version;
		CString syntax;

		version.Format(IDS_VERSION_OUTPUT, (LPCTSTR) theApp.GetVersionString(TRUE));
		VERIFY(syntax.LoadString(IDS_CMDLINE_SYNTAX));
		AfxMessageBox(version + "\n\n" + syntax);

		m_bFail = TRUE;
		return;
	}

	if (last_switch) {
		temp_switch = last_switch;
		last_switch = 0;

		// Previous switch expects another parameter.
		switch (temp_switch) {
			// Expecting the config file.
		case 'C':
			if (!m_sConfigFile.IsEmpty())	// has it already been set?
			{
				Fail("Config file parameter was specified more than once.");
			} else if (IsValidFilename(pszParam)) {
				if (VerifyReadable(pszParam))
					m_sConfigFile = pszParam;
				else
					m_bFail = TRUE;
			} else {
				Fail("C switch should be followed by the name of a configuration file.");
			}
			return;
			// Expecting the result file.
		case 'R':
			if (!m_sResultFile.IsEmpty())	// has it already been set?
			{
				Fail("Result file parameter was specified more than once.");
			} else if (IsValidFilename(pszParam)) {
				if (VerifyWritable(pszParam))
					m_sResultFile = pszParam;
				else
					m_bFail = TRUE;
			} else {
				Fail("R switch should be followed by the name of the desired result file.");
			}
			return;
			// Expecting the timeout value.
		case 'T':
			if (m_iTimeout >= 0)	// has it already been set?
			{
				Fail("Timeout parameter was specified more than once.");
			} else if (IsValidInteger(pszParam)) {
				m_iTimeout = atoi(pszParam);
			} else {
				Fail("T switch should be followed by an integer timeout value.");
			}
			return;
			// Expecting the port number
		case 'P':
			if (m_iLoginportnumber > 0)	// has it already been set?
			{
				Fail("Login port number parameter was specified more than once.");
			} else if (IsValidInteger(pszParam)) {
				m_iLoginportnumber = atoi(pszParam);
				if (m_iLoginportnumber < 1 || m_iLoginportnumber > 65535)
					Fail("P switch should be followed by a valid port value (1-65535).");
			} else {
				Fail("P switch should be followed by a valid port value (1-65535).");
			}
			return;
			// Check if need to show Bigmeter automatically
		case 'M':
			if (m_bShowBigmeter == TRUE)	// has it already been set?
			{
				Fail("Show Bigmeter parameter was specified more than once.");
			} else {
				int i = atoi(pszParam);

				if (i == 1)
					m_bShowBigmeter = TRUE;
			}
			return;
		default:
			{
				char tmpary[2] = { last_switch, 0 };
				Fail("Unrecognized switch: " + (CString) tmpary + ".");
			}
			return;
		}
	}

	if (bFlag) {
		m_bSwitches = TRUE;
		last_switch = toupper(pszParam[0]);

		//////////////////////////////////////////////////////////////////////
		// This is an example of how to allow switches that have meaning on
		// their own, without any additional parameters.
		//
		//      if ( last_switch == 'V' )       // spit out version number and exit
		//      {
		//              // Set BOOL member indicating that this switch was specified
		//              // Make sure it's initialized in the constructor
		//              m_bVersion = TRUE;
		//
		//              last_switch = 0;        // don't look for more parameters related to this switch
		//              return;                         // don't allow it to reach the bLast checking
		//      }
		//////////////////////////////////////////////////////////////////////

		if (bLast) {
			Fail("An additional parameter was expected after the last switch.");
			return;
		}

		return;
	}
	// If switches haven't been used (so far)...
	if (!m_bSwitches) {
		switch (param_count) {
			// Expecting the config file.
		case 1:
			if (IsValidFilename(pszParam)) {
				if (VerifyReadable(pszParam))
					m_sConfigFile = pszParam;
				else
					m_bFail = TRUE;
			} else {
				Fail("First parameter should be the name of a valid config file.");
			}
			return;
			// Expecting the result file.
		case 2:
			if (IsValidFilename(pszParam)) {
				if (VerifyWritable(pszParam))
					m_sResultFile = pszParam;
				else
					m_bFail = TRUE;
			} else {
				Fail("Second parameter should be the name of the result file.");
			}
			return;
			// Expecting the timeout value.
		case 3:
			if (IsValidInteger(pszParam)) {
				m_iTimeout = atoi(pszParam);
			} else {
				Fail("Third parameter should be an integer timeout value.");
			}
			return;
		default:
			Fail("Too many parameters.");
			return;
		}
	}

	Fail("Didn't know what to do with this parameter:\n"
	     + (CString) pszParam + "\nPlease report this as an Iometer bug.");
}