示例#1
0
文件: Stream.cpp 项目: 0xPIT/Sming
// as above but a given skipChar is ignored
// this allows format characters (typically commas) in values to be ignored
long Stream::parseInt(char skipChar)
{
  boolean isNegative = false;
  long value = 0;
  int c;
  
  c = peekNextDigit();
  // ignore non numeric leading characters
  if(c < 0)
  return 0; // zero returned if timeout
  
  do{
    if(c == skipChar)
    ; // ignore this charactor
    else if(c == '-')
    isNegative = true;
    else if(c >= '0' && c <= '9')        // is c a digit?
    value = value * 10 + c - '0';
    read();  // consume the character we got with peek
    c = timedPeek();
  }
  while( (c >= '0' && c <= '9') || c == skipChar );
  
  if(isNegative)
  value = -value;
  return value;
}
// as parseInt but returns a floating point value
float DeviceConnection::parseFloat(){
  bool isNegative = false;
  bool isFraction = false;
  long value = 0;
  char c;
  float fraction = 1.0;

  c = peekNextDigit();
    // ignore non numeric leading characters
  if(c < 0)
    return 0; // zero returned if timeout

  do{
    if(c == '-')
      isNegative = true;
    else if (c == '.')
      isFraction = true;
    else if(c >= '0' && c <= '9')  {      // is c a digit?
      value = value * 10 + c - '0';
      if(isFraction)
         fraction *= 0.1;
    }
    read();  // consume the character we got with peek
    c = peek();
  }
  while( (c >= '0' && c <= '9')  || c == '.');

  if(isNegative)
    value = -value;
  if(isFraction)
    return value * fraction;
  else
    return value;
}
// returns the first valid (long) integer value from the current position.
// initial characters that are not digits (or the minus sign) are skipped
// function is terminated by the first character that is not a digit.
long DeviceConnection::parseInt()
{
  bool isNegative = false;
  long value = 0;
  int c;

  c = peekNextDigit();
  // ignore non numeric leading characters
  if(c < 0)
    return 0; // zero returned if timeout

  do{
    if(c == '-')
      isNegative = true;
    else if(c >= '0' && c <= '9')        // is c a digit?
      value = value * 10 + c - '0';
    read();  // consume the character we got with peek
    c = peek();
  }
  while( (c >= '0' && c <= '9'));

  if(isNegative)
    value = -value;
  return value;
}
unsigned long SUIStream::parseIntHex(char skipChar)
{
	uint32_t value = 0;
	int c;

	c = peekNextDigit(true);
	// ignore non numeric leading characters
	if (c < 0)
		return 0; // zero returned if timeout

	do {
		if (c == skipChar)
			; // ignore this character
		else if (c >= '0' && c <= '9') // is c a digit?
			value = (value * 16) + c - '0';
		else if (c >= 'A' && c <= 'F')
			value = (value * 16) + ((c - 'A') + 10);
		else if (c >= 'a' && c <= 'f')
			value = (value * 16) + ((c - 'a') + 10);

		read(); // consume the character we got with peek
		c = timedPeek();
	} while ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || c == skipChar);


	return value;

}
示例#5
0
文件: Stream.cpp 项目: 141141/ESP31B
// as above but the given skipChar is ignored
// this allows format characters (typically commas) in values to be ignored
float ICACHE_FLASH_ATTR Stream::parseFloat(char skipChar) {
    boolean isNegative = false;
    boolean isFraction = false;
    long value = 0;
    int c;
    float fraction = 1.0;

    c = peekNextDigit();
    // ignore non numeric leading characters
    if(c < 0)
        return 0; // zero returned if timeout

    do {
        if(c == skipChar)
            ; // ignore
        else if(c == '-')
            isNegative = true;
        else if(c == '.')
            isFraction = true;
        else if(c >= '0' && c <= '9') {      // is c a digit?
            value = value * 10 + c - '0';
            if(isFraction)
                fraction *= 0.1;
        }
        read();  // consume the character we got with peek
        c = timedPeek();
    } while((c >= '0' && c <= '9') || c == '.' || c == skipChar);

    if(isNegative)
        value = -value;
    if(isFraction)
        return value * fraction;
    else
        return value;
}
unsigned long SUIStream::parseULong(char skipChar)
{
	uint32_t value = 0;
	int c;

	c = peekNextDigit();
	// ignore non numeric leading characters
	if (c < 0)
		return 0; // zero returned if timeout

	do {
		if (c == skipChar)
			; // ignore this character
		else if (c >= '0' && c <= '9') // is c a digit?
			value = value * 10 + c - '0';
		read(); // consume the character we got with peek
		c = timedPeek();
	} while ((c >= '0' && c <= '9') || c == skipChar);


	return value;

}