src/core/config/parser.c
/**
 * This is a array context aware delimiter search. We need to find delimiters
 * as they relate to the outer array context.
 *
 * If we were searching for the first ',' char in an array items string like:
 *
 * [1, 2, 3],[4, 5, 6],[7, 8, 9]
 *
 * ---------^
 *
 * we would not want to find the first ',' in the first nested array, but the
 * first ',' splitting the arrays
 */
This is a array context aware delimiter search. We need to find delimiters
as they relate to the outer array context.

If we were searching for the first ',' char in an array items string like.





we would not want to find the first ',' in the first nested array, but the
first ',' splitting the arrays
This is a array context aware delimiter search . We need to find delimiters as they relate to the outer array context . If we were searching for the first ' ' char in an array items string like . we would not want to find the first ' ' in the first nested array but the first ' ' splitting the arrays
static enum natwm_error array_context_get_delimiter(const char *string, char delimiter,
                                                    char **result, size_t *length, bool consume)
{
        enum natwm_error err = GENERIC_ERROR;
        struct stack *stack = stack_create();
        size_t index = 0;
        char ch = '\0';
        while ((ch = string[index]) != '\0') {
                enum parser_token token = char_to_token(ch);
                if (token == ARRAY_START) {
                        stack_push(stack, &index);
                }
                if (token == ARRAY_END) {
                        struct stack_item *stack_item = stack_pop(stack);
                        if (stack_item != NULL) {
                                stack_item_destroy(stack_item);
                        }
                }
                if (ch != delimiter) {
                        ++index;
                        continue;
                }
                if (!stack_has_item(stack)) {
                        if (consume) {
                                ++index;
                        }
                        ++index;
                        break;
                }
                ++index;
                continue;
        }
        if (ch == '\0') {
                stack_destroy(stack);
                return NOT_FOUND_ERROR;
        }
        char *resulting_string = NULL;
        size_t string_length = 0;
        err = string_splice(string, 0, index - 1, &resulting_string, &string_length);
        if (err != NO_ERROR) {
                stack_destroy(stack);
                return err;
        }
        *result = resulting_string;
        *length = string_length;
        stack_destroy(stack);
        return NO_ERROR;
}
static enum natwm_error array_context_get_delimiter ( const char * string , char delimiter , char * * result , size_t * length , bool consume ) { enum natwm_error err = GENERIC_ERROR ; struct stack * stack = stack_create ( ) ; size_t index = 0 ; char ch = ' \0 ' ; while ( ( ch = string [ index ] ) != ' \0 ' ) { enum parser_token token = char_to_token ( ch ) ; if ( token == ARRAY_START ) { stack_push ( stack , & index ) ; } if ( token == ARRAY_END ) { struct stack_item * stack_item = stack_pop ( stack ) ; if ( stack_item != NULL ) { stack_item_destroy ( stack_item ) ; } } if ( ch != delimiter ) { ++ index ; continue ; } if ( ! stack_has_item ( stack ) ) { if ( consume ) { ++ index ; } ++ index ; break ; } ++ index ; continue ; } if ( ch == ' \0 ' ) { stack_destroy ( stack ) ; return NOT_FOUND_ERROR ; } char * resulting_string = NULL ; size_t string_length = 0 ; err = string_splice ( string , 0 , index - 1 , & resulting_string , & string_length ) ; if ( err != NO_ERROR ) { stack_destroy ( stack ) ; return err ; } * result = resulting_string ; * length = string_length ; stack_destroy ( stack ) ; return NO_ERROR ; }
This is a array context aware delimiter search.
This is a array context aware delimiter search .
// We just need to delete this // We found the delimiter - check if the stack is empty. // If we want to include the delimiter in the final // string increment the index // We found the delimiter, but we are not in the correct // context, so just incremement the index and continue // We did not find anything