Exemplo n.º 1
0
Arquivo: proc.c Projeto: ds2dev/gcc
/* Parse a textual list of cpus and store the result inside a cpuList object.
 * Input format is the following:
 * - comma-separated list of items (no spaces)
 * - each item is either a single decimal number (cpu index), or a range made
 *   of two numbers separated by a single dash (-). Ranges are inclusive.
 * Examples:
 *           0
 *           2,4-127,128-143
 *           0-1
 */
static void
cpuList_parse (cpuList* list, const char* line, int line_len)
{
    const char* p = line;
    const char* end = p + line_len;
    const char* q;

    /* NOTE: the input line coming from sysfs typically contains a
    * trailing newline, so take care of it in the code below
     */
    while (p < end && *p != '\n')
    {
        int val, start_value, end_value;

        /* Find the end of current item, and put it into 'q' */
        q = memchr(p, ',', end-p);
        if (q == NULL) {
            q = end;
        }

        /* Get first value */
        p = parse_decimal(p, q, &start_value);
        if (p == NULL)
            goto BAD_FORMAT;

        end_value = start_value;

        /* If we're not at the end of the item, expect a dash and
         * and integer; extract end value.
         */
        if (p < q && *p == '-') {
            p = parse_decimal(p+1, q, &end_value);
            if (p == NULL)
                goto BAD_FORMAT;
        }

        /* Set CPU list */
        for (val = start_value; val <= end_value; val++) {
            list->mask++;
        }

        /* Jump to next item */
        p = q;
        if (p < end)
            p++;
    }

BAD_FORMAT:
    ;
}
Exemplo n.º 2
0
double strtod(const char *str, char **endptr)
{
	char **curend = (char**)(&str); /* In case endptr == NULL. */
	const char *orig = str;
	double sign = 1;

	while (isspace(*str)) ++str; /* Skip whitespace. */

	if		(*str == '+') ++str;
	else if (*str == '-') ++str, sign = -1;

	double result = check_nan_inf(str, curend);

	if (result != 0) return result * sign;

	if (strcicmp("0X", str) == 0 && isxdigit(*(str+2)))
		result = parse_hex_decimal(str+2, curend);
	else
		result = parse_decimal(str, curend);

	/* If no valid conversion, do not skip whitespace. */
	if (*curend == orig) *curend = (char*)orig;

	if (endptr != NULL) *endptr = *curend;

	if (isinf(result)) errno = ERANGE, result = HUGE_VAL;

	return result * sign;
}
Exemplo n.º 3
0
void iterator::parse_operator( void )
{
	_type = TOK_OPERATOR;
	while ( utf::is_pattern_syntax( _c ) && special.find( _c ) == special.end() )
		next_utf();

	// Deal with '.' starting a number
	if ( utf::is_number( _c ) )
	{
		if ( _value == U"." )
			parse_decimal();
		else if ( _value.back() == '.' )
		{
			_next.insert( 0, 1, _c );
			_c = '.';
			_value.pop_back();
		}
	}
}
Exemplo n.º 4
0
static void out_vformat(Out& o, const char* format, va_list args) {
    int nn = 0;

    for (;;) {
        int mm;
        int padZero = 0;
        int padLeft = 0;
        char sign = '\0';
        int width = -1;
        int prec  = -1;
        size_t bytelen = sizeof(int);
        int slen;
        char buffer[32];  /* temporary buffer used to format numbers */

        char  c;

        /* first, find all characters that are not 0 or '%' */
        /* then send them to the output directly */
        mm = nn;
        do {
            c = format[mm];
            if (c == '\0' || c == '%')
                break;
            mm++;
        } while (1);

        if (mm > nn) {
            o.Send(format+nn, mm-nn);
            nn = mm;
        }

        /* is this it ? then exit */
        if (c == '\0')
            break;

        /* nope, we are at a '%' modifier */
        nn++;  // skip it

        /* parse flags */
        for (;;) {
            c = format[nn++];
            if (c == '\0') {  /* single trailing '%' ? */
                c = '%';
                o.Send(&c, 1);
                return;
            }
            else if (c == '0') {
                padZero = 1;
                continue;
            }
            else if (c == '-') {
                padLeft = 1;
                continue;
            }
            else if (c == ' ' || c == '+') {
                sign = c;
                continue;
            }
            break;
        }

        /* parse field width */
        if ((c >= '0' && c <= '9')) {
            nn --;
            width = (int)parse_decimal(format, &nn);
            c = format[nn++];
        }

        /* parse precision */
        if (c == '.') {
            prec = (int)parse_decimal(format, &nn);
            c = format[nn++];
        }

        /* length modifier */
        switch (c) {
        case 'h':
            bytelen = sizeof(short);
            if (format[nn] == 'h') {
                bytelen = sizeof(char);
                nn += 1;
            }
            c = format[nn++];
            break;
        case 'l':
            bytelen = sizeof(long);
            if (format[nn] == 'l') {
                bytelen = sizeof(long long);
                nn += 1;
            }
            c = format[nn++];
            break;
        case 'z':
            bytelen = sizeof(size_t);
            c = format[nn++];
            break;
        case 't':
            bytelen = sizeof(ptrdiff_t);
            c = format[nn++];
            break;
        default:
            ;
        }

        /* conversion specifier */
        const char* str = buffer;
        if (c == 's') {
            /* string */
            str = va_arg(args, const char*);
            if (str == NULL) {
                str = "(null)";
            }
        } else if (c == 'c') {
Exemplo n.º 5
0
// Processes a just-completed term
// Returns true if new sentence has just passed checksum test and is validated
bool TinyGPS::term_complete()
{
  if (_is_checksum_term)
  {
    byte checksum = 16 * from_hex(_term[0]) + from_hex(_term[1]);
    if (checksum == _parity)
    {
      if (_gps_data_good)
      {
#ifndef _GPS_NO_STATS
        ++_good_sentences;
#endif
        _last_time_fix = _new_time_fix;
        _last_position_fix = _new_position_fix;

        switch(_sentence_type)
        {
        case _GPS_SENTENCE_GPRMC:
          _time      = _new_time;
          _date      = _new_date;
          _latitude  = _new_latitude;
          _longitude = _new_longitude;
          _speed     = _new_speed;
          _course    = _new_course;
          _rmc_ready = true;
          break;
        case _GPS_SENTENCE_GPGGA:
          _altitude  = _new_altitude;
          _time      = _new_time;
          _latitude  = _new_latitude;
          _longitude = _new_longitude;
          _gga_ready = true;
          _hdop      = _new_hdop;
          _sat_count = _new_sat_count;
        case _GPS_SENTENCE_GPGSV:
          _gsv_ready = true;
          break;
        }

        return true;
      }
    }

#ifndef _GPS_NO_STATS
    else
      ++_failed_checksum;
#endif
    return false;
  }

  // the first term determines the sentence type
  if (_term_number == 0)
  {
    if (!gpsstrcmp(_term, _GPRMC_TERM))
      _sentence_type = _GPS_SENTENCE_GPRMC;
    else if (!gpsstrcmp(_term, _GPGGA_TERM))
      _sentence_type = _GPS_SENTENCE_GPGGA;
    else if (!gpsstrcmp(_term, _GPGSV_TERM))
      _sentence_type = _GPS_SENTENCE_GPGSV;
    else
      _sentence_type = _GPS_SENTENCE_OTHER;
    return false;
  }

  if (_sentence_type != _GPS_SENTENCE_OTHER && _term[0])
  switch((_sentence_type == _GPS_SENTENCE_GPGGA ? 200 : 100) + _term_number)
  {
    case 101: // Time in both sentences
    case 201:
      _new_time = parse_decimal();
      _new_time_fix = millis();
      break;
    case 102: // GPRMC validity
      _gps_data_good = _term[0] == 'A';
      break;
    case 103: // Latitude
    case 202:
      _new_latitude = parse_degrees();
      _new_position_fix = millis();
      break;
    case 104: // N/S
    case 203:
      if (_term[0] == 'S')
        _new_latitude = -_new_latitude;
      break;
    case 105: // Longitude
    case 204:
      _new_longitude = parse_degrees();
      break;
    case 106: // E/W
    case 205:
      if (_term[0] == 'W')
        _new_longitude = -_new_longitude;
      break;
    case 107: // Speed (GPRMC)
      _new_speed = parse_decimal();
      break;
    case 108: // Course (GPRMC)
      _new_course = parse_decimal();
      break;
    case 109: // Date (GPRMC)
      _new_date = gpsatol(_term);
      break;
    case 206: // Fix data (GPGGA)
      _gps_data_good = _term[0] > '0';
      break;
    case 207: // Number of satelites tracked (GPGGA)
      _new_sat_count = parse_decimal();
      break;
    case 208: // Horizontal Dilution of Position (GPGGA)
      _new_hdop = parse_decimal();
      break;
    case 209: // Altitude (GPGGA)
      _new_altitude = parse_decimal();
      break;
  } /* switch */

  return false;
}
Exemplo n.º 6
0
// Processes a just-completed term
// Returns true if new sentence has just passed checksum test and is validated
bool TinyGPS::term_complete()
{
  if (_is_checksum_term)
  {
    byte checksum = 16 * from_hex(_term[0]) + from_hex(_term[1]);
    if (checksum == _parity)
    {
      if (_gps_data_good)
      {
#ifndef _GPS_NO_STATS
        ++_good_sentences;
#endif
        _last_time_fix = _new_time_fix;
        _last_position_fix = _new_position_fix;

        switch(_sentence_type)
        {
        case _GPS_SENTENCE_GPRMC:
          _time      = _new_time;
          _date      = _new_date;
          _latitude  = _new_latitude;
          _longitude = _new_longitude;
          _speed     = _new_speed;
          _course    = _new_course;
          break;
        case _GPS_SENTENCE_GPGGA:
          _altitude  = _new_altitude;
          _time      = _new_time;
          _latitude  = _new_latitude;
          _longitude = _new_longitude;
          _numsats   = _new_numsats;
          _hdop      = _new_hdop;
          break;
        }

        return true;
      }
    }

#ifndef _GPS_NO_STATS
    else
      ++_failed_checksum;
#endif
    return false;
  }

  // the first term determines the sentence type
  if (_term_number == 0)
  {
    if (!gpsstrcmp(_term, _GPRMC_TERM))
      _sentence_type = _GPS_SENTENCE_GPRMC;
    else if (!gpsstrcmp(_term, _GPGGA_TERM))
      _sentence_type = _GPS_SENTENCE_GPGGA;
    else
      _sentence_type = _GPS_SENTENCE_OTHER;
    return false;
  }

  if (_sentence_type != _GPS_SENTENCE_OTHER && _term[0])
    switch(COMBINE(_sentence_type, _term_number))
  {
    case COMBINE(_GPS_SENTENCE_GPRMC, 1): // Time in both sentences
    case COMBINE(_GPS_SENTENCE_GPGGA, 1):
      _new_time = parse_decimal();
      _new_time_fix = millis();
      break;
    case COMBINE(_GPS_SENTENCE_GPRMC, 2): // GPRMC validity
      _gps_data_good = _term[0] == 'A';
      break;
    case COMBINE(_GPS_SENTENCE_GPRMC, 3): // Latitude
    case COMBINE(_GPS_SENTENCE_GPGGA, 2):
      _new_latitude = parse_degrees();
      _new_position_fix = millis();
      break;
    case COMBINE(_GPS_SENTENCE_GPRMC, 4): // N/S
    case COMBINE(_GPS_SENTENCE_GPGGA, 3):
      if (_term[0] == 'S')
        _new_latitude = -_new_latitude;
      break;
    case COMBINE(_GPS_SENTENCE_GPRMC, 5): // Longitude
    case COMBINE(_GPS_SENTENCE_GPGGA, 4):
      _new_longitude = parse_degrees();
      break;
    case COMBINE(_GPS_SENTENCE_GPRMC, 6): // E/W
    case COMBINE(_GPS_SENTENCE_GPGGA, 5):
      if (_term[0] == 'W')
        _new_longitude = -_new_longitude;
      break;
    case COMBINE(_GPS_SENTENCE_GPRMC, 7): // Speed (GPRMC)
      _new_speed = parse_decimal();
      break;
    case COMBINE(_GPS_SENTENCE_GPRMC, 8): // Course (GPRMC)
      _new_course = parse_decimal();
      break;
    case COMBINE(_GPS_SENTENCE_GPRMC, 9): // Date (GPRMC)
      _new_date = gpsatol(_term);
      break;
    case COMBINE(_GPS_SENTENCE_GPGGA, 6): // Fix data (GPGGA)
      _gps_data_good = _term[0] > '0';
      break;
    case COMBINE(_GPS_SENTENCE_GPGGA, 7): // Satellites used (GPGGA)
      _new_numsats = (unsigned char)atoi(_term);
      break;
    case COMBINE(_GPS_SENTENCE_GPGGA, 8): // HDOP
      _new_hdop = parse_decimal();
      break;
    case COMBINE(_GPS_SENTENCE_GPGGA, 9): // Altitude (GPGGA)
      _new_altitude = parse_decimal();
      break;
  }

  return false;
}
Exemplo n.º 7
0
Arquivo: json.c Projeto: AmesianX/wine
/* ECMA-262 5.1 Edition    15.12.1.2 */
static HRESULT parse_json_value(json_parse_ctx_t *ctx, jsval_t *r)
{
    HRESULT hres;

    switch(skip_spaces(ctx)) {

    /* JSONNullLiteral */
    case 'n':
        if(!is_keyword(ctx, nullW))
            break;
        *r = jsval_null();
        return S_OK;

    /* JSONBooleanLiteral */
    case 't':
        if(!is_keyword(ctx, trueW))
            break;
        *r = jsval_bool(TRUE);
        return S_OK;
    case 'f':
        if(!is_keyword(ctx, falseW))
            break;
        *r = jsval_bool(FALSE);
        return S_OK;

    /* JSONObject */
    case '{': {
        WCHAR *prop_name;
        jsdisp_t *obj;
        jsval_t val;

        hres = create_object(ctx->ctx, NULL, &obj);
        if(FAILED(hres))
            return hres;

        ctx->ptr++;
        if(skip_spaces(ctx) == '}') {
            ctx->ptr++;
            *r = jsval_obj(obj);
            return S_OK;
        }

        while(1) {
            if(*ctx->ptr != '"')
                break;
            hres = parse_json_string(ctx, &prop_name);
            if(FAILED(hres))
                break;

            if(skip_spaces(ctx) != ':') {
                FIXME("missing ':'\n");
                heap_free(prop_name);
                break;
            }

            ctx->ptr++;
            hres = parse_json_value(ctx, &val);
            if(SUCCEEDED(hres)) {
                hres = jsdisp_propput_name(obj, prop_name, val);
                jsval_release(val);
            }
            heap_free(prop_name);
            if(FAILED(hres))
                break;

            if(skip_spaces(ctx) == '}') {
                ctx->ptr++;
                *r = jsval_obj(obj);
                return S_OK;
            }

            if(*ctx->ptr++ != ',') {
                FIXME("expected ','\n");
                break;
            }
            skip_spaces(ctx);
        }

        jsdisp_release(obj);
        break;
    }

    /* JSONString */
    case '"': {
        WCHAR *string;
        jsstr_t *str;

        hres = parse_json_string(ctx, &string);
        if(FAILED(hres))
            return hres;

        /* FIXME: avoid reallocation */
        str = jsstr_alloc(string);
        heap_free(string);
        if(!str)
            return E_OUTOFMEMORY;

        *r = jsval_string(str);
        return S_OK;
    }

    /* JSONArray */
    case '[': {
        jsdisp_t *array;
        unsigned i = 0;
        jsval_t val;

        hres = create_array(ctx->ctx, 0, &array);
        if(FAILED(hres))
            return hres;

        ctx->ptr++;
        if(skip_spaces(ctx) == ']') {
            ctx->ptr++;
            *r = jsval_obj(array);
            return S_OK;
        }

        while(1) {
            hres = parse_json_value(ctx, &val);
            if(FAILED(hres))
                break;

            hres = jsdisp_propput_idx(array, i, val);
            jsval_release(val);
            if(FAILED(hres))
                break;

            if(skip_spaces(ctx) == ']') {
                ctx->ptr++;
                *r = jsval_obj(array);
                return S_OK;
            }

            if(*ctx->ptr != ',') {
                FIXME("expected ','\n");
                break;
            }

            ctx->ptr++;
            i++;
        }

        jsdisp_release(array);
        break;
    }

    /* JSONNumber */
    default: {
        int sign = 1;
        double n;

        if(*ctx->ptr == '-') {
            sign = -1;
            ctx->ptr++;
            skip_spaces(ctx);
        }

        if(!isdigitW(*ctx->ptr))
            break;

        if(*ctx->ptr == '0') {
            ctx->ptr++;
            n = 0;
            if(is_identifier_char(*ctx->ptr))
                break;
        }else {
            hres = parse_decimal(&ctx->ptr, ctx->end, &n);
            if(FAILED(hres))
                return hres;
        }

        *r = jsval_number(sign*n);
        return S_OK;
    }
    }

    FIXME("Syntax error at %s\n", debugstr_w(ctx->ptr));
    return E_FAIL;
}
Exemplo n.º 8
0
void iterator::parse_number( void )
{
	_ival = utf::integer_value( _c );
	if ( _ival == 0 )
	{
		next_utf();
		// Number with different bases
		switch ( _c )
		{
			case 'b':
			case 'B':
				// Binary number
				next_utf();
				while ( utf::is_number_decimal( _c ) )
				{
					uint64_t v = utf::integer_value( _c );
					if ( v > 1 )
						throw_runtime( "expected binary digit" );
					_ival = ( _ival << 1 ) + v;
					next_utf();
				}
				_type = TOK_INTEGER;
				break;

			case 'c':
			case 'C':
				// Octal number
				next_utf();
				while ( utf::is_number_decimal( _c ) )
				{
					uint64_t v = utf::integer_value( _c );
					if ( v > 7 )
						throw_runtime( "expected octal digit" );
					_ival = ( _ival << 3 ) + v;
					next_utf();
				}
				_type = TOK_INTEGER;
				break;

			case 'x':
			case 'X':
				// Hexadecimal number
				next_utf();
				while ( utf::is_hex_digit( _c ) )
				{
					uint64_t v = utf::integer_value( _c );
					_ival = ( _ival << 4 ) + v;
					next_utf();
				}
				_type = TOK_INTEGER;
				break;

			default:
				if ( utf::is_number( _c ) )
					// Unknown base...
					throw_runtime( "0 should be followed by a base letter" );
				else
					// else it's just a 0 by itself, which is okay... continue as a decimal
					parse_decimal();
				break;
		}
	}
	else if ( _ival > 0 )
		parse_decimal();
}
Exemplo n.º 9
0
// Processes a just-completed term
// Returns true if new sentence has just passed checksum test and is validated
bool Gps::term_complete()
{
  //Console.println("compl");
  if (_is_checksum_term)
  {
    byte checksum = (byte)(16 * from_hex(_term[0]) + from_hex(_term[1]));
    //Console.print(checksum);
    //Console.print(",");
    //Console.println(_parity);
    if (checksum == _parity)
    {
      //if (1==1)
      if (_gps_data_good)
      {
#ifndef _GPS_NO_STATS
        ++_good_sentences;
#endif
        _last_time_fix = _new_time_fix;
        _last_position_fix = _new_position_fix;

        switch (_sentence_type)
        {
          case _GPS_SENTENCE_GPRMC:
            _time = _new_time;
            _date = _new_date;
            _latitude = _new_latitude;
            _longitude = _new_longitude;
            _speed = _new_speed;
            _course = _new_course;
            break;

          case _GPS_SENTENCE_GPGGA:
            _altitude = _new_altitude;
            _time = _new_time;
            _latitude = _new_latitude;
            _longitude = _new_longitude;
            _numsats = _new_numsats;
            _hdop = _new_hdop;
            break;

          default:
            break;
        }

        return true;
      }
    }

#ifndef _GPS_NO_STATS
    else
    {
      ++_failed_checksum;
    }
#endif
    return false;
  }

  // the first term determines the sentence type
  if (_term_number == 0)
  {
    if (!gpsstrcmp(_term, _GPRMC_TERM))
    {
      _sentence_type = _GPS_SENTENCE_GPRMC;
    }
    else if (!gpsstrcmp(_term, _GPGGA_TERM))
    {
      _sentence_type = _GPS_SENTENCE_GPGGA;
    }
    else
    {
      _sentence_type = _GPS_SENTENCE_OTHER;
    }
    return false;
  }

  if (_sentence_type != _GPS_SENTENCE_OTHER && _term[0])
  {
    switch (COMBINE(_sentence_type, _term_number))
    {
      case COMBINE(_GPS_SENTENCE_GPRMC, 1): // Time in both sentences
      case COMBINE(_GPS_SENTENCE_GPGGA, 1):
        _new_time = parse_decimal();
        _new_time_fix = millis();
        break;

      case COMBINE(_GPS_SENTENCE_GPRMC, 2): // GPRMC validity
        _gps_data_good = _term[0] == 'A';
        break;

      case COMBINE(_GPS_SENTENCE_GPRMC, 3): // Latitude
      case COMBINE(_GPS_SENTENCE_GPGGA, 2):
        _new_latitude = parse_degrees();
        _new_position_fix = millis();
        break;

      case COMBINE(_GPS_SENTENCE_GPRMC, 4): // N/S
      case COMBINE(_GPS_SENTENCE_GPGGA, 3):
        if (_term[0] == 'S')
          _new_latitude = -_new_latitude;
        break;

      case COMBINE(_GPS_SENTENCE_GPRMC, 5): // Longitude
      case COMBINE(_GPS_SENTENCE_GPGGA, 4):
        _new_longitude = parse_degrees();
        break;

      case COMBINE(_GPS_SENTENCE_GPRMC, 6): // E/W
      case COMBINE(_GPS_SENTENCE_GPGGA, 5):
        if (_term[0] == 'W')
          _new_longitude = -_new_longitude;
        break;

      case COMBINE(_GPS_SENTENCE_GPRMC, 7): // Speed (GPRMC)
        _new_speed = parse_decimal();
        break;

      case COMBINE(_GPS_SENTENCE_GPRMC, 8): // Course (GPRMC)
        _new_course = parse_decimal();
        break;

      case COMBINE(_GPS_SENTENCE_GPRMC, 9): // Date (GPRMC)
        _new_date = gpsatol(_term);
        break;

      case COMBINE(_GPS_SENTENCE_GPGGA, 6): // Fix data (GPGGA) ; 0=invalid, 1=GPS fix, 2=DGPS fix, 6=estimation
        _gps_data_good = _term[0] > '0';
        break;

      case COMBINE(_GPS_SENTENCE_GPGGA, 7): // NN-Satellites used (GPGGA): 00-12
        _new_numsats = (unsigned char)atoi(_term);
        break;

      case COMBINE(_GPS_SENTENCE_GPGGA, 8): // D.D - HDOP (GPGGA) - horizontal deviation
        _new_hdop = parse_decimal();
        break;

      case COMBINE(_GPS_SENTENCE_GPGGA, 9): // H.H - Altitude (GPGGA)
        _new_altitude = parse_decimal();
        break;

      default:
        break;
    }
  }
  return false;
}
Exemplo n.º 10
0
// Processes a just-completed term
// Returns true if new sentence has just passed checksum test and is validated
bool ParserNMEA::term_complete()
{
	if (_is_checksum_term)
	{
		byte checksum = 16 * from_hex(_term[0]) + from_hex(_term[1]);
		if (checksum == _parity)
		{
			if (_gps_data_good)
			{
				#ifndef _GPS_NO_STATS
				++_good_sentences;
				#endif
				_last_time_fix = _new_time_fix;
				_last_position_fix = _new_position_fix;

				switch(_sentence_type)
				{
					case _GPS_SENTENCE_GPRMC:
					_time      = _new_time;
					_latitude  = _new_latitude;
					_longitude = _new_longitude;
					_speed     = _new_speed;
					break;
					case _GPS_SENTENCE_GPVTG:
					_speed     = _new_speed;
					break;
					case _GPS_SENTENCE_GPZDA:
				    _time      = _new_time;
					_day      = _new_day;
					_month    = _new_month;
					_year     = _new_year;
					break;
					case _GPS_SENTENCE_GPGGA:
					_time      = _new_time;
					_latitude  = _new_latitude;
					_longitude = _new_longitude;
					_hdop      = _new_hdop;
					break;
				}

				return true;
			}
		}

		#ifndef _GPS_NO_STATS
		else
		++_failed_checksum;
		#endif
		return false;
	}

	// the first term determines the sentence type
	if (_term_number == 0)
	{
		if (!gpsstrcmp(_term, _GPZDA_TERM))
		_sentence_type = _GPS_SENTENCE_GPZDA;
		else if (!gpsstrcmp(_term, _GPGGA_TERM))
		_sentence_type = _GPS_SENTENCE_GPGGA;
		else if (!gpsstrcmp(_term, _GPVTG_TERM))
		_sentence_type = _GPS_SENTENCE_GPVTG;
		else
		_sentence_type = _GPS_SENTENCE_OTHER;
		return false;
	}

	if (_sentence_type != _GPS_SENTENCE_OTHER && _term[0])
	switch(COMBINE(_sentence_type, _term_number))
	{
		case COMBINE(_GPS_SENTENCE_GPZDA, 1): // Time in ZDA
		_new_time = parse_decimal();
		_new_time_fix = millis();
		break;
		case COMBINE(_GPS_SENTENCE_GPRMC, 2): // GPRMC validity
		_gps_data_good = _term[0] == 'A';
		break;
		case COMBINE(_GPS_SENTENCE_GPRMC, 3): // Latitude
	    _new_latitude = parse_degrees();
		_new_position_fix = millis();
		break;
		case COMBINE(_GPS_SENTENCE_GPGGA, 2):
		_new_latitude = parse_degrees();
		_new_position_fix = millis();
		break;
		case COMBINE(_GPS_SENTENCE_GPRMC, 4): // N/S
		if (_term[0] == 'S')
		_new_latitude = -_new_latitude;
		break;
		case COMBINE(_GPS_SENTENCE_GPGGA, 3):
		if (_term[0] == 'S')
		_new_latitude = -_new_latitude;
		break;
		case COMBINE(_GPS_SENTENCE_GPGGA, 4):
		_new_longitude = parse_degrees();
		break;
		case COMBINE(_GPS_SENTENCE_GPGGA, 5):
		if (_term[0] == 'W')
		_new_longitude = -_new_longitude;
		break;
		case COMBINE(_GPS_SENTENCE_GPVTG, 5): // Speed (GPVTG)
		_new_speed = parse_decimal();
		break;
		case COMBINE(_GPS_SENTENCE_GPRMC, 8): // Course (GPRMC)
		_new_course = parse_decimal();
		break;
		case COMBINE(_GPS_SENTENCE_GPZDA, 2): // Date/day (GPZDA)
		_new_day = (unsigned char)atoi(_term);
		break;
		case COMBINE(_GPS_SENTENCE_GPZDA, 3): // Date/month (GPZDA)
		_new_month = (unsigned char)atoi(_term);
		break;
		case COMBINE(_GPS_SENTENCE_GPZDA, 4): // Date/year (GPZDA)
		 _new_year = parse_decimal() / 100;
		break;
		case COMBINE(_GPS_SENTENCE_GPGGA, 6): // Fix data (GPGGA)
		_gps_data_good = _term[0] > '0';
		break;
		case COMBINE(_GPS_SENTENCE_GPGGA, 8): // HDOP
		_new_hdop = parse_decimal();
		break;
	}

	return false;
}
Exemplo n.º 11
0
// Processes a just-completed term
// Returns true if new sentence has just passed checksum test and is validated
bool Gps::term_complete()
{
  if (_is_checksum_term)
  {
    unsigned char checksum = 16 * from_hex(_term[0]) + from_hex(_term[1]);
    if (checksum == _parity)
    {
      if (_gps_fixed)
      {
        _last_time_fix = _new_time_fix;
        _last_position_fix = _new_position_fix;
        
        switch(_sentence_type)
        {
          case _GPS_SENTENCE_GPRMC:
            strcpy(_rmc_time, _new_time);
            strcpy(_date,     _new_date);
            strcpy(_lat,      _new_lat);
            strcpy(_lon,      _new_lon);
            strcpy(_speed,    _new_speed);
            strcpy(_course,   _new_course);
            break;
          case _GPS_SENTENCE_GPGGA:
            strcpy(_gga_time, _new_time);
            strcpy(_lat,      _new_lat);
            strcpy(_lon,      _new_lon);
            strcpy(_altitude, _new_altitude);
            break;
        }
        
        // Return a valid object only when we've got two rmc and gga
        // messages with the same timestamp
        if (! strcmp(_gga_time, _rmc_time))
          return true;
      }
    }
    return false;
  }
  
  // the first term determines the sentence type
  if (_term_number == 0)
  {
    if (!strcmp(_term, _GPRMC_TERM))
      _sentence_type = _GPS_SENTENCE_GPRMC;
    else if (!strcmp(_term, _GPGGA_TERM))
      _sentence_type = _GPS_SENTENCE_GPGGA;
    else
      _sentence_type = _GPS_SENTENCE_OTHER;
    return false;
  }
  
  if (_sentence_type != _GPS_SENTENCE_OTHER && _term[0])
    switch((_sentence_type == _GPS_SENTENCE_GPGGA ? 200 : 100) + _term_number)
  {
    case 101: // Time in both sentences
    case 201:
      strncpy(_new_time, _term, 6);
      _new_time[6] = 'h';
      _new_time[7] = '\0';
      _new_time_fix = millis();
      break;
    case 102: // GPRMC validity ('A'=fixed, 'V'=no fix yet)
      _gps_fixed = _term[0] == 'A';
      break;
    case 103: // Latitude
    case 202:
      strncpy(_new_lat, _term, 7);  // APRS format: 3020.12N (DD, MM.MM, Hemisphere)
      _new_lat[7] = '\0';
      _new_position_fix = millis();
      break;
    case 104: // N/S
    case 203:
      _new_lat[7] = _term[0];
      _new_lat[8] = '\0';
      break;
    case 105: // Longitude
    case 204:
      strncpy(_new_lon, _term, 8);  // APRS format: 00143.13W (DD, MM.MM, Hemisphere)
      _new_lon[8] = '\0';
      break;
    case 106: // E/W
    case 205:
      _new_lon[8] = _term[0];
      _new_lon[9] = '\0';
      break;
    case 107: // Speed (GPRMC)
      // TODO: This is highly dependant on the venus 634 flpx GPS, where course/speed
      // is already left-padded with zeros. 
      strncpy(_new_speed, _term, 3);
      _new_speed[3] = '\0';
      break;
    case 108: // Course (GPRMC)
      strncpy(_new_course, _term, 3);
      _new_course[3] = '\0';
      break;
    case 109: // Date (GPRMC)
      strncpy(_new_date, _term, 6);
      _new_date[6] = '\0';
      break;
    case 206: // Fix data (GPGGA)
      _gps_fixed = _term[0] > '0';
      break;
    case 209: // Altitude (GPGGA)
      long altitude = parse_decimal();  // altitude in cm
      // 10000 ft = 3048 m
      // x ft = altitude mt --> x = 100 * altitude (in cm) / 3048
      altitude = (altitude * 25) / 762;  // APRS needs feet
      left_pad(_new_altitude, altitude, 6);
      _new_altitude[6] = '\0';
      break;
  }
  
  return false;
}
Exemplo n.º 12
0
// Processes a just-completed term
// Returns true if new sentence has just passed checksum test and is validated
bool TinyGPS::term_complete()
{
  if (_is_checksum_term)
  {
    byte checksum = 16 * from_hex(_term[0]) + from_hex(_term[1]);
    if (checksum == _parity)
    {
        _last_time_fix = _new_time_fix;
        _last_position_fix = _new_position_fix;

        switch(_sentence_type)
        {
          case _GPS_SENTENCE_PUBX:
            _gps_fix_quality = _new_gps_fix_quality;
            _altitude = _new_altitude;
            _time = _new_time;
            _latitude  = _new_latitude;
            _longitude = _new_longitude;
            _sats = _new_sats;
            _course = _new_course;
            _vspeed = _new_vspeed;
            _speed = _new_speed;
            break;
        }
        return true;
    }
    return false;
  }

  // the first term determines the sentence type
  if (_term_number == 0)
  {
    if (!gpsstrcmp(_term, _PUBX_TERM))
      _sentence_type = _GPS_SENTENCE_PUBX;
    else
      _sentence_type = _GPS_SENTENCE_OTHER;
    return false;
  }

  if (_sentence_type != _GPS_SENTENCE_OTHER && _term[0]) {
    switch(400 + _term_number)
    {
      // Time
      case 402:
        _new_time = parse_decimal();
        _new_time_fix = millis();
        break;
      // Latitude
      case 403:
        _new_latitude = parse_degrees();
        _new_position_fix = millis();
        break;
      // N/S
      case 404:
        if (_term[0] == 'S')
          _new_latitude = -_new_latitude;
        break;
      // Longitude
      case 405:
        _new_longitude = parse_degrees();
        break;
      // E/W
      case 406:
        if (_term[0] == 'W')
          _new_longitude = -_new_longitude;
        break;
      // Speed (in km/h)
      case 411:
        _new_speed = parse_decimal();
        break;
      // Course
      case 412:
        _new_course = parse_decimal();
        break;
      // Altitude
      case 407:
        _new_altitude = parse_decimal();
        break;
      // Sat num
      case 418:
        _new_sats = parse_decimal()/100;
        break;
      // fix quality
      case 408:
        if (_term[0] == 'G' || _term[0] == 'D') {
          _gps_data_good = true;
          if (_term[1] == '2')
            _new_gps_fix_quality = 2;
          else if (_term[1] == '3')
            _new_gps_fix_quality = 3;
        } else {
          _new_gps_fix_quality = 1;
          _gps_data_good = false;
        }
        break;
      case 413:
        _new_vspeed = parse_decimal() * -1; // positive=downwards
        break;
    }
  }
  return false;
}
Exemplo n.º 13
0
static int next_token(parser_ctx_t *ctx, void *lval)
{
    do {
        if(!skip_spaces(ctx))
            return tEOF;
    }while(skip_comment(ctx) || skip_html_comment(ctx));

    if(ctx->implicit_nl_semicolon) {
        if(ctx->nl)
            return ';';
        ctx->implicit_nl_semicolon = FALSE;
    }

    if(isalphaW(*ctx->ptr)) {
        int ret = check_keywords(ctx, lval);
        if(ret)
            return ret;

        return parse_identifier(ctx, lval);
    }

    if(isdigitW(*ctx->ptr)) {
        double n;

        if(!parse_numeric_literal(ctx, &n))
            return -1;

        *(literal_t**)lval = new_double_literal(ctx, n);
        return tNumericLiteral;
    }

    switch(*ctx->ptr) {
    case '{':
    case '(':
    case ')':
    case '[':
    case ']':
    case ';':
    case ',':
    case '~':
    case '?':
        return *ctx->ptr++;

    case '}':
        *(const WCHAR**)lval = ctx->ptr++;
        return '}';

    case '.':
        if(ctx->ptr+1 < ctx->end && isdigitW(ctx->ptr[1])) {
            double n;
            HRESULT hres;
            hres = parse_decimal(&ctx->ptr, ctx->end, &n);
            if(FAILED(hres)) {
                lex_error(ctx, hres);
                return -1;
            }
            *(literal_t**)lval = new_double_literal(ctx, n);
            return tNumericLiteral;
        }
        ctx->ptr++;
        return '.';

    case '<':
        if(++ctx->ptr == ctx->end) {
            *(int*)lval = EXPR_LESS;
            return tRelOper;
        }

        switch(*ctx->ptr) {
        case '=':  /* <= */
            ctx->ptr++;
            *(int*)lval = EXPR_LESSEQ;
            return tRelOper;
        case '<':  /* << */
            if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
                ctx->ptr++;
                *(int*)lval = EXPR_ASSIGNLSHIFT;
                return tAssignOper;
            }
            *(int*)lval = EXPR_LSHIFT;
            return tShiftOper;
        default: /* < */
            *(int*)lval = EXPR_LESS;
            return tRelOper;
        }

    case '>':
        if(++ctx->ptr == ctx->end) { /* > */
            *(int*)lval = EXPR_GREATER;
            return tRelOper;
        }

        switch(*ctx->ptr) {
        case '=':  /* >= */
            ctx->ptr++;
            *(int*)lval = EXPR_GREATEREQ;
            return tRelOper;
        case '>':  /* >> */
            if(++ctx->ptr < ctx->end) {
                if(*ctx->ptr == '=') {  /* >>= */
                    ctx->ptr++;
                    *(int*)lval = EXPR_ASSIGNRSHIFT;
                    return tAssignOper;
                }
                if(*ctx->ptr == '>') {  /* >>> */
                    if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* >>>= */
                        ctx->ptr++;
                        *(int*)lval = EXPR_ASSIGNRRSHIFT;
                        return tAssignOper;
                    }
                    *(int*)lval = EXPR_RRSHIFT;
                    return tRelOper;
                }
            }
            *(int*)lval = EXPR_RSHIFT;
            return tShiftOper;
        default:
            *(int*)lval = EXPR_GREATER;
            return tRelOper;
        }

    case '+':
        ctx->ptr++;
        if(ctx->ptr < ctx->end) {
            switch(*ctx->ptr) {
            case '+':  /* ++ */
                ctx->ptr++;
                return tINC;
            case '=':  /* += */
                ctx->ptr++;
                *(int*)lval = EXPR_ASSIGNADD;
                return tAssignOper;
            }
        }
        return '+';

    case '-':
        ctx->ptr++;
        if(ctx->ptr < ctx->end) {
            switch(*ctx->ptr) {
            case '-':  /* -- or --> */
                ctx->ptr++;
                if(ctx->is_html && ctx->nl && ctx->ptr < ctx->end && *ctx->ptr == '>') {
                    ctx->ptr++;
                    return tHTMLCOMMENT;
                }
                return tDEC;
            case '=':  /* -= */
                ctx->ptr++;
                *(int*)lval = EXPR_ASSIGNSUB;
                return tAssignOper;
            }
        }
        return '-';

    case '*':
        if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
            ctx->ptr++;
            *(int*)lval = EXPR_ASSIGNMUL;
            return tAssignOper;
        }
        return '*';

    case '%':
        if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
            ctx->ptr++;
            *(int*)lval = EXPR_ASSIGNMOD;
            return tAssignOper;
        }
        return '%';

    case '&':
        if(++ctx->ptr < ctx->end) {
            switch(*ctx->ptr) {
            case '=':  /* &= */
                ctx->ptr++;
                *(int*)lval = EXPR_ASSIGNAND;
                return tAssignOper;
            case '&':  /* && */
                ctx->ptr++;
                return tANDAND;
            }
        }
        return '&';

    case '|':
        if(++ctx->ptr < ctx->end) {
            switch(*ctx->ptr) {
            case '=':  /* |= */
                ctx->ptr++;
                *(int*)lval = EXPR_ASSIGNOR;
                return tAssignOper;
            case '|':  /* || */
                ctx->ptr++;
                return tOROR;
            }
        }
        return '|';

    case '^':
        if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* ^= */
            ctx->ptr++;
            *(int*)lval = EXPR_ASSIGNXOR;
            return tAssignOper;
        }
        return '^';

    case '!':
        if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* != */
            if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* !== */
                ctx->ptr++;
                *(int*)lval = EXPR_NOTEQEQ;
                return tEqOper;
            }
            *(int*)lval = EXPR_NOTEQ;
            return tEqOper;
        }
        return '!';

    case '=':
        if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* == */
            if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* === */
                ctx->ptr++;
                *(int*)lval = EXPR_EQEQ;
                return tEqOper;
            }
            *(int*)lval = EXPR_EQ;
            return tEqOper;
        }
        return '=';

    case '/':
        if(++ctx->ptr < ctx->end) {
            if(*ctx->ptr == '=') {  /* /= */
                ctx->ptr++;
                *(int*)lval = EXPR_ASSIGNDIV;
                return kDIVEQ;
            }
        }
        return '/';

    case ':':
        if(++ctx->ptr < ctx->end && *ctx->ptr == ':') {
            ctx->ptr++;
            return kDCOL;
        }
        return ':';

    case '\"':
    case '\'':
        return parse_string_literal(ctx, lval, *ctx->ptr);

    case '_':
    case '$':
        return parse_identifier(ctx, lval);

    case '@':
        return '@';
    }

    WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
    return 0;
}
Exemplo n.º 14
0
static BOOL parse_numeric_literal(parser_ctx_t *ctx, double *ret)
{
    HRESULT hres;

    if(*ctx->ptr == '0') {
        ctx->ptr++;

        if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
            double r = 0;
            int d;
            if(++ctx->ptr == ctx->end) {
                ERR("unexpected end of file\n");
                return FALSE;
            }

            while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
                r = r*16 + d;
                ctx->ptr++;
            }

            if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
                WARN("unexpected identifier char\n");
                lex_error(ctx, JS_E_MISSING_SEMICOLON);
                return FALSE;
            }

            *ret = r;
            return TRUE;
        }

        if(isdigitW(*ctx->ptr)) {
            unsigned base = 8;
            const WCHAR *ptr;
            double val = 0;

            for(ptr = ctx->ptr; ptr < ctx->end && isdigitW(*ptr); ptr++) {
                if(*ptr > '7') {
                    base = 10;
                    break;
                }
            }

            do {
                val = val*base + *ctx->ptr-'0';
            }while(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr));

            /* FIXME: Do we need it here? */
            if(ctx->ptr < ctx->end && (is_identifier_char(*ctx->ptr) || *ctx->ptr == '.')) {
                WARN("wrong char after octal literal: '%c'\n", *ctx->ptr);
                lex_error(ctx, JS_E_MISSING_SEMICOLON);
                return FALSE;
            }

            *ret = val;
            return TRUE;
        }

        if(is_identifier_char(*ctx->ptr)) {
            WARN("wrong char after zero\n");
            lex_error(ctx, JS_E_MISSING_SEMICOLON);
            return FALSE;
        }
    }

    hres = parse_decimal(&ctx->ptr, ctx->end, ret);
    if(FAILED(hres)) {
        lex_error(ctx, hres);
        return FALSE;
    }

    return TRUE;
}
Exemplo n.º 15
0
// Processes a just-completed term
// Returns true if new sentence has just passed checksum test and is validated
bool TinyGPS::term_complete()
{
  if (_is_checksum_term)
  {
    byte checksum = 16 * from_hex(_term[0]) + from_hex(_term[1]);
    if (checksum == _parity)
    {
      if (_gps_data_good)
      {
#ifndef _GPS_NO_STATS
        ++_good_sentences;
#endif
        _last_time_fix = _new_time_fix;
        _last_position_fix = _new_position_fix;

        switch(_sentence_type)
        {
        case _GPS_SENTENCE_GPRMC:
          _time      = _new_time;
          _date      = _new_date;
          _latitude  = _new_latitude;
          _longitude = _new_longitude;
          _speed     = _new_speed;
          _course    = _new_course;
          break;
        case _GPS_SENTENCE_GPGGA:
          _altitude  = _new_altitude;
          _time      = _new_time;
          _latitude  = _new_latitude;
          _longitude = _new_longitude;
		  _sats		 = _new_sats;
          break;
		
		// terry
		case _GPS_SENTENCE_PUBX:
          _time      = _new_time;
          _latitude  = _new_latitude;
          _longitude = _new_longitude;
          _speed     = _new_speed;
          _course    = _new_course;
		  _altitude  = _new_altitude;
		  _sats		 = _new_sats; //nigey
          break;
		//
        }

        return true;
      }
    }

#ifndef _GPS_NO_STATS
    else
      ++_failed_checksum;
#endif
    return false;
  }

  // the first term determines the sentence type
  if (_term_number == 0)
  {
    if (!gpsstrcmp(_term, _GPRMC_TERM))
      _sentence_type = _GPS_SENTENCE_GPRMC;
    else if (!gpsstrcmp(_term, _GPGGA_TERM))
      _sentence_type = _GPS_SENTENCE_GPGGA;
	// terry
	else if (!gpsstrcmp(_term, _PUBX_TERM))
      _sentence_type = _GPS_SENTENCE_PUBX;
	//
    else
      _sentence_type = _GPS_SENTENCE_OTHER;
    return false;
  }

  if (_sentence_type != _GPS_SENTENCE_OTHER && _term[0])
  
  // terry
  switch(_sentence_type) {
	
	case _GPS_SENTENCE_GPRMC:
		_term_id = 100 + _term_number;
		break;
	
	case _GPS_SENTENCE_GPGGA:
		_term_id = 200 + _term_number;
		break;
		
	case _GPS_SENTENCE_PUBX:
		_term_id = 300 + _term_number;
		break;
  }
  //
  
  switch(_term_id) // terry
  {
    case 101: // Time in both sentences
    case 201:
	case 302: // terry (time PUBX 00)
      _new_time = parse_decimal();
      _new_time_fix = millis();
      break;
    case 102: // GPRMC validity
      _gps_data_good = _term[0] == 'A';
      break;
    case 103: // Latitude
    case 202:
	case 303: // terry (lat PUBX 00)
      _new_latitude = parse_degrees();
      _new_position_fix = millis();
      break;
    case 104: // N/S
    case 203:
	case 304: // terry (N/S PUBX 00)
      if (_term[0] == 'S')
        _new_latitude = -_new_latitude;
      break;
    case 105: // Longitude
    case 204:
	case 305: // terry (lon PUBX 00)
      _new_longitude = parse_degrees();
      break;
    case 106: // E/W
    case 205:
	case 306: // terry (E/W PUBX 00)
      if (_term[0] == 'W')
        _new_longitude = -_new_longitude;
      break;
    case 107: // Speed (GPRMC)
      _new_speed = parse_decimal();
      break;
    case 108: // Course (GPRMC)
	case 312: // terry (course PUBX 00)
      _new_course = parse_decimal();
      break;
    case 109: // Date (GPRMC)
      _new_date = gpsatol(_term);
      break;
    case 206: // Fix data (GPGGA)
      _gps_data_good = _term[0] > '0';
      break;
    case 209: // Altitude (GPGGA)
	case 307: // terry (altitude PUBX 00)
      _new_altitude = parse_decimal();
      break;
	  
	// terry
	case 301: // PUBX message ID (we want 0)
	  if (_term[0] == '0' && _term[1] == '0') _gps_data_good = 1;
	  break;
	case 311: // (km/h speed PUBX 00)
	  _new_speed = parse_decimal() / _GPS_KMPH_PER_KNOT;
	  break;
	case 207: // sats (GPGGA); // (nigey) sats
	  _new_sats = atoi(_term);
	  break;
	  
	//
  }

  return false;
}