src/line_protocol_parser.c
/* Convert the field value string to correct type */
Convert the field value string to correct type
Convert the field value string to correct type
static int
parse_value(struct LP_Item* item)
{
    size_t i = 0;
    size_t length = 0;
    double candidate_d = 0;
    signed long long candidate_i = 0;
    unsigned long long candidate_u = 0ULL;
    char boolstr[6];
    char *endptr = NULL;
    if (item->type != LP_STRING){
        return 0;
    }
    endptr = NULL;
    candidate_d = strtod(item->value.s, &endptr);
    if (*endptr == '\0') {
        LP_FREE(item->value.s);
        item->value.f = candidate_d;
        item->type = LP_FLOAT;
        LP_DEBUG_PRINT("Type is double: %f\n", candidate_d);
        return 1;
    }
    endptr = NULL;
    candidate_i = strtoll(item->value.s, &endptr, 10);
    if (*endptr == 'i' && *(endptr + 1) == '\0') {
        LP_FREE(item->value.s);
        item->value.i = candidate_i;
        item->type = LP_INTEGER;
        LP_DEBUG_PRINT("Type is integer: %lld\n", candidate_i);
        return 1;
    }
    endptr = NULL;
    errno = 0; 
    candidate_u = strtoull(item->value.s, &endptr, 10);
    if (*endptr == 'u' && *(endptr + 1) == '\0') {
        LP_FREE(item->value.s);
        if (candidate_u == ULLONG_MAX && errno == ERANGE)
            return 0;
        item->value.i = candidate_u;
        item->type = LP_UINTEGER;
        LP_DEBUG_PRINT("Type is uinteger: %llu\n", candidate_u);
        return 1;
    }
    length = strlen(item->value.s);
    if (*(item->value.s) == '"') {
        LP_DEBUG_PRINT("Type is string: %s\n", item->value.s);
        for (i = 1; i < length - 1; i++) {
            item->value.s[i - 1] = item->value.s[i];
        }
        item->value.s[length - 2] = '\0';
        return 1;
    }
    if (length == 1 && tolower(*(item->value.s)) == 't' ) {
        LP_FREE(item->value.s);
        item->value.b = 1;
        item->type = LP_BOOLEAN;
        LP_DEBUG_PRINT("Type is boolean: %d\n", item->value.b);
        return 1;
    } else if (length == 1 && tolower(*(item->value.s)) == 'f' ) {
        LP_FREE(item->value.s);
        item->value.b = 0;
        item->type = LP_BOOLEAN;
        LP_DEBUG_PRINT("Type is boolean: %d\n", item->value.b);
        return 1;
    } else if (length == 4 || length == 5) {
        for (i = 0; i <= length; i++) {
            boolstr[i] = tolower(item->value.s[i]);
        }
        if ((strcmp(boolstr, "true") == 0)) {
            LP_FREE(item->value.s);
            item->value.b = 1;
            item->type = LP_BOOLEAN;
            LP_DEBUG_PRINT("Type is boolean: %d\n", item->value.b);
            return 1;
        } else if ((strcmp(boolstr, "false") == 0)) {
            LP_FREE(item->value.s);
            item->value.b = 0;
            item->type = LP_BOOLEAN;
            LP_DEBUG_PRINT("Type is boolean: %d\n", item->value.b);
            return 1;
        }
    }
    return 0; 
}
static int parse_value ( struct LP_Item * item ) { size_t i = 0 ; size_t length = 0 ; double candidate_d = 0 ; signed long long candidate_i = 0 ; unsigned long long candidate_u = 0ULL ; char boolstr [ 6 ] ; char * endptr = NULL ; if ( item -> type != LP_STRING ) { return 0 ; } endptr = NULL ; candidate_d = strtod ( item -> value . s , & endptr ) ; if ( * endptr == ' \0 ' ) { LP_FREE ( item -> value . s ) ; item -> value . f = candidate_d ; item -> type = LP_FLOAT ; LP_DEBUG_PRINT ( " \n " , candidate_d ) ; return 1 ; } endptr = NULL ; candidate_i = strtoll ( item -> value . s , & endptr , 10 ) ; if ( * endptr == ' ' && * ( endptr + 1 ) == ' \0 ' ) { LP_FREE ( item -> value . s ) ; item -> value . i = candidate_i ; item -> type = LP_INTEGER ; LP_DEBUG_PRINT ( " \n " , candidate_i ) ; return 1 ; } endptr = NULL ; errno = 0 ; candidate_u = strtoull ( item -> value . s , & endptr , 10 ) ; if ( * endptr == ' ' && * ( endptr + 1 ) == ' \0 ' ) { LP_FREE ( item -> value . s ) ; if ( candidate_u == ULLONG_MAX && errno == ERANGE ) return 0 ; item -> value . i = candidate_u ; item -> type = LP_UINTEGER ; LP_DEBUG_PRINT ( " \n " , candidate_u ) ; return 1 ; } length = strlen ( item -> value . s ) ; if ( * ( item -> value . s ) == ' ' ) { LP_DEBUG_PRINT ( " \n " , item -> value . s ) ; for ( i = 1 ; i < length - 1 ; i ++ ) { item -> value . s [ i - 1 ] = item -> value . s [ i ] ; } item -> value . s [ length - 2 ] = ' \0 ' ; return 1 ; } if ( length == 1 && tolower ( * ( item -> value . s ) ) == ' ' ) { LP_FREE ( item -> value . s ) ; item -> value . b = 1 ; item -> type = LP_BOOLEAN ; LP_DEBUG_PRINT ( " \n " , item -> value . b ) ; return 1 ; } else if ( length == 1 && tolower ( * ( item -> value . s ) ) == ' ' ) { LP_FREE ( item -> value . s ) ; item -> value . b = 0 ; item -> type = LP_BOOLEAN ; LP_DEBUG_PRINT ( " \n " , item -> value . b ) ; return 1 ; } else if ( length == 4 || length == 5 ) { for ( i = 0 ; i <= length ; i ++ ) { boolstr [ i ] = tolower ( item -> value . s [ i ] ) ; } if ( ( strcmp ( boolstr , " " ) == 0 ) ) { LP_FREE ( item -> value . s ) ; item -> value . b = 1 ; item -> type = LP_BOOLEAN ; LP_DEBUG_PRINT ( " \n " , item -> value . b ) ; return 1 ; } else if ( ( strcmp ( boolstr , " " ) == 0 ) ) { LP_FREE ( item -> value . s ) ; item -> value . b = 0 ; item -> type = LP_BOOLEAN ; LP_DEBUG_PRINT ( " \n " , item -> value . b ) ; return 1 ; } } return 0 ; }
Convert the field value string to correct type
Convert the field value string to correct type
// Try parse float // Try parse integer // Try parse unsigned integer // we need to reset, otherwise errno MIGHT be the value of the strtoll above // Try parse string // Strip the surronding quotes // Try parse boolan /* Convert to lower case to reduce nr of comparisons*/ // Error