Beispiel #1
0
json parser::parse_object(
    const std::string& input, size_t& offset, std::error_code& error)
{
    assert(!error);
    json object = json(class_type::object);

    offset++;
    consume_white_space(input, offset, error);
    if (input[offset] == '}')
    {
        offset++;
        return object;
    }

    while (true)
    {
        json Key = parse_next(input, offset, error);
        consume_white_space(input, offset, error);
        if (input[offset] != ':')
        {
            error = std::make_error_code(std::errc::invalid_argument);
            std::cerr << "Error: object: Expected colon, found '"
                      << input[offset] << "'\n";
            break;
        }
        offset++;
        consume_white_space(input, offset, error);
        json Value = parse_next(input, offset, error);
        object[Key.to_string()] = Value;

        consume_white_space(input, offset, error);
        if (input[offset] == ',')
        {
            offset++;
            continue;
        }
        else if (input[offset] == '}')
        {
            offset++;
            break;
        }
        else
        {
            error = std::make_error_code(std::errc::invalid_argument);
            std::cerr << "ERROR: object: Expected comma, found '"
                      << input[offset] << "'\n";
            break;
        }
    }

    return object;
}
Beispiel #2
0
json parser::parse_next(
    const std::string& input, size_t& offset, std::error_code& error)
{
    char value;
    consume_white_space(input, offset, error);
    value = input[offset];
    switch (value)
    {
    case '[': return parse_array(input, offset, error);
    case '{': return parse_object(input, offset, error);
    case '\"': return parse_string(input, offset, error);
    case 't':
    case 'f': return parse_bool(input, offset, error);
    case 'n': return parse_null(input, offset, error);
    default:
    {
        if ((value <= '9' && value >= '0') || value == '-')
        {
            return parse_number(input, offset, error);
        }
    }
    }
    // Error: Unknown starting character
    error = std::make_error_code(std::errc::invalid_argument);
    return json();
}
int read_calibration_from_file(const char *calibration_file,
                               struct calibration_data *accel,
                               struct calibration_data *magn,
                               struct calibration_data *gyro,
                               double *magnetic_declination_mrad)
{
#define STR_BUFFER_SIZE 128
    int ret = 0;
    FILE *stream;
    int len;
    char buffer[STR_BUFFER_SIZE];

    stream = fopen(calibration_file, "r");
    if (stream == NULL)
    {
        ret = -errno;
        fprintf(stderr, "Error: cannot open %s\n", calibration_file);
        return ret;
    }

    while (fgets(buffer, sizeof(buffer), stream) != NULL)
    {
        len = strlen(buffer);
        // strip trailing '\n' if it exists
        if (buffer[len-1] == '\n')
        {
            buffer[len-1] = 0;
            len--;
        }
        else
        {
            // line is longer than our buffer, skip to the next line
            continue;
        }

        if (buffer[0] != '#')
        {
            // find the '=' sign
            char *equal = strstr(buffer, "=");
            if (equal)
            {
                char *key = buffer;
                char *value = equal+1;
                *equal = 0;

                consume_white_space(&key);
                process_key_value(key, value, accel, magn, gyro, magnetic_declination_mrad);
            }
        }
    }

    fclose(stream);
    return ret;
}
Beispiel #4
0
json parser::parse_array(
    const std::string& input, size_t& offset, std::error_code& error)
{
    json array = json(class_type::array);
    uint32_t index = 0;

    offset++;
    consume_white_space(input, offset, error);
    if (input[offset] == ']')
    {
        offset++;
        return array;
    }

    while (true)
    {
        array[index++] = parse_next(input, offset, error);
        consume_white_space(input, offset, error);

        if (input[offset] == ',')
        {
            offset++; continue;
        }
        else if (input[offset] == ']')
        {
            offset++; break;
        }
        else
        {
            error = std::make_error_code(std::errc::invalid_argument);
            std::cerr << "ERROR: array: Expected ',' or ']', found '"
                      << input[offset] << "'\n";
            return json(class_type::array);
        }
    }

    return array;
}