src/core/config/parser.c
/**
 * Here we will handle parsing arrays
 *
 * Arrays are different in that they support multi line value strings. The
 * string we are going to receive into this function will terminate at the
 * first '\n' so we need to create our own "array value string" from the
 * parser
 *
 * That "array value string" will resemble something like this:
 *
 * [<value>,<value>,<value>]
 *
 * or
 *
 * [
 *     <value>,
 *     <value>,
 *     <value>,
 * ]
 *
 * Both of which should be treated the same and return the same result.
 */
Here we will handle parsing arrays
Arrays are different in that they support multi line value strings. The
string we are going to receive into this function will terminate at the
first '\n' so we need to create our own "array value string" from the
parser

That "array value string" will resemble something like this.



or



Both of which should be treated the same and return the same result.
Here we will handle parsing arrays Arrays are different in that they support multi line value strings . The string we are going to receive into this function will terminate at the first ' \ n ' so we need to create our own " array value string " from the parser That " array value string " will resemble something like this . or Both of which should be treated the same and return the same result .
static struct config_value *parser_parse_array(struct parser *parser, char *string)
{
        char *value_items_string = NULL;
        size_t value_items_string_length = 0;
        enum natwm_error err = GENERIC_ERROR;
        err = array_find_value_items_string(
                parser, string, &value_items_string, &value_items_string_length);
        if (err != NO_ERROR) {
                return NULL;
        }
         We now need to normalize it
        char **value_items = NULL;
        size_t value_items_length = 0;
        err = array_split_value_items_string(value_items_string, &value_items, &value_items_length);
        if (err != NO_ERROR) {
                LOG_ERROR(natwm_logger,
                          "Failed to parse array value items string - Line %zu",
                          parser->line_num);
                free(value_items_string);
                return NULL;
        }
         Remove spaces around the items
        for (size_t i = 0; i < value_items_length; ++i) {
                char *value_item = NULL;
                err = string_strip_surrounding_spaces(value_items[i], &value_item, NULL);
                 If we encounter an '' (empty string) as the last item in the
                 array we have two senarios:
                 1) The array is empty and there are no values
                 2) There was a trailing comma
                 In both cases we should free the string, decrement the array
                 length, and terminate the loop
                if (err == NOT_FOUND_ERROR && (i == value_items_length - 1)) {
                        free(value_items[i]);
                        value_items_length -= 1;
                        break;
                }
                if (err != NO_ERROR) {
                        LOG_ERROR(natwm_logger,
                                  "Failed to parse array value item '%s' - "
                                  "Line %zu",
                                  value_items[i],
                                  parser->line_num);
                        goto free_and_error;
                }
                free(value_items[i]);
                value_items[i] = value_item;
        }
         Now we should have an array of stripped items
         Last step is to parse them into a new config_value
        struct config_value *config_value
                = parser_resolve_array_values(parser, value_items, value_items_length);
        if (config_value == NULL) {
                free(value_items_string);
                free(value_items);
                return NULL;
        }
        free(string);
        free(value_items_string);
        free(value_items);
        return config_value;
free_and_error:
         We need to free up our intermediate strings and the array of array
         values we allocated
        free(value_items_string);
        free(value_items);
        return NULL;
}
static struct config_value * parser_parse_array ( struct parser * parser , char * string ) { char * value_items_string = NULL ; size_t value_items_string_length = 0 ; enum natwm_error err = GENERIC_ERROR ; err = array_find_value_items_string ( parser , string , & value_items_string , & value_items_string_length ) ; if ( err != NO_ERROR ) { return NULL ; } char * * value_items = NULL ; size_t value_items_length = 0 ; err = array_split_value_items_string ( value_items_string , & value_items , & value_items_length ) ; if ( err != NO_ERROR ) { LOG_ERROR ( natwm_logger , " " , parser -> line_num ) ; free ( value_items_string ) ; return NULL ; } for ( size_t i = 0 ; i < value_items_length ; ++ i ) { char * value_item = NULL ; err = string_strip_surrounding_spaces ( value_items [ i ] , & value_item , NULL ) ; if ( err == NOT_FOUND_ERROR && ( i == value_items_length - 1 ) ) { free ( value_items [ i ] ) ; value_items_length -= 1 ; break ; } if ( err != NO_ERROR ) { LOG_ERROR ( natwm_logger , " " " " , value_items [ i ] , parser -> line_num ) ; goto free_and_error ; } free ( value_items [ i ] ) ; value_items [ i ] = value_item ; } struct config_value * config_value = parser_resolve_array_values ( parser , value_items , value_items_length ) ; if ( config_value == NULL ) { free ( value_items_string ) ; free ( value_items ) ; return NULL ; } free ( string ) ; free ( value_items_string ) ; free ( value_items ) ; return config_value ; free_and_error : free ( value_items_string ) ; free ( value_items ) ; return NULL ; }
Here we will handle parsing arrays
 Arrays are different in that they support multi line value strings.
Here we will handle parsing arrays Arrays are different in that they support multi line value strings .
// We should now have a valid string containing the string // representation of the array values // // We now need to normalize it // Remove spaces around the items // If we encounter an '' (empty string) as the last item in the // array we have two senarios: // // 1) The array is empty and there are no values // 2) There was a trailing comma // // In both cases we should free the string, decrement the array // length, and terminate the loop // Now we should have an array of stripped items // Last step is to parse them into a new config_value // We need to free up our intermediate strings and the array of array // values we allocated