Commit e7fcefc0 authored by Filip Bartek's avatar Filip Bartek
Browse files

reformatting part 4

parent 7a97010c
Loading
Loading
Loading
Loading
+14 −21
Original line number Diff line number Diff line
@@ -4,15 +4,6 @@
#include "xparser.h"
#include "xpath.h"

typedef struct arg_struct {
    int argc;
    char **argv;
    FILE *input;
    FILE *output;
    bool xml;
    bool txt;
} arg_struct;

void close_files(arg_struct *args)
{
    if (args->input != stdin) {
@@ -45,22 +36,22 @@ bool parse_file_arg(arg_struct *args, FILE **file_handle, int i)
bool parse_xml_text_option_arg(const bool *flag1, bool *flag2)
{
    if (*flag1) {
        perror("Cannot specify both options for output");
        fprintf(stderr, "Cannot specify both options for output");
        return false;
    }
    *flag2 = true;
    return true;
}

void print_result(arg_struct *args, bool result, struct node **to_be_printed, size_t size)
void print_result(arg_struct *args, bool result_tag, struct printing_nodes *result)
{
    if (args->xml && result) {
    if (args->xml && result_tag) {
        fprintf(args->output, "<result>\n");
    }
    for (size_t i = 0; i < size; ++i) {
        print_output(to_be_printed[i], args->output, args->xml, result ? 1 : 0);
    for (size_t i = 0; i < result->size; ++i) {
        print_output(result->nodes[i], args->output, args->xml, result_tag ? 1 : 0);
    }
    if (args->xml && result) {
    if (args->xml && result_tag) {
        fprintf(args->output, "</result>\n");
    }
}
@@ -133,15 +124,17 @@ int main(int argc, char **argv)
        node_destroy(root);
        return 1;
    }
    struct node **to_be_printed = malloc_nodes_for_printing(args, root, xpath);
    if (to_be_printed == NULL) {
    struct printing_nodes *result = &(struct printing_nodes){
        .nodes = malloc_nodes_for_printing(args, root, xpath),
        .size = 0
    };
    if (result->nodes == NULL) {
        return 1;
    }

    size_t size_to_be_printed = 0;
    traverse_xroot(root, xpath, args->output, args->xml, to_be_printed, &size_to_be_printed);
    print_result(args, size_to_be_printed > 1, to_be_printed, size_to_be_printed);
    traverse_xroot(root, xpath, result);
    print_result(args, result->size > 1, result);

    destroy_resources(args, root, xpath, to_be_printed);
    destroy_resources(args, root, xpath, result->nodes);
    return 0;
}
+15 −14
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ mchar *parse_string(struct parsing_state *state, predicate valid_chars)
        return string;
    }

    alloc_error(state, "string");
    alloc_error(state, "string in parse_string");
    free(string);
    string = NULL;

@@ -107,7 +107,7 @@ mchar *parse_word(struct parsing_state *state, predicate start, predicate rest,
        return word;
    }

    alloc_error(state, "word");
    alloc_error(state, "word in parse_word");
    free(word);
    word = NULL;

@@ -176,14 +176,14 @@ mchar *parse_text(struct parsing_state *state, bool normalize)

    mchar *text = str_create("");
    if (text == NULL) {
        alloc_error(state, "text");
        alloc_error(state, "text in parse_text, text == NULL");
        return NULL;
    }

    bool space = false;
    while (!end_of_text(peek_char(state))) {
        if (!add_to_text(state, &text, &space, normalize)) {
            alloc_error(state, "text");
            alloc_error(state, "text in parse_text, couldn't add char");
            str_destroy(text);
            return NULL;
        }
@@ -254,24 +254,24 @@ static bool end_of_attributes(struct parsing_state *state)
    return c == EOF || c == '/' || c == '>' || c == '<';
}

bool parse_attributes(struct parsing_state *state, mchar **keys, mchar **values, size_t *attribute_size)
bool parse_attributes(struct parsing_state *state, mchar **keys, mchar **values, size_t *attr_size)
{
    assert(state != NULL);

    while (!end_of_attributes(state)) {
        keys[*attribute_size] = NULL;
        values[*attribute_size] = NULL;
        if (!parse_attribute(state, &keys[*attribute_size], &values[*attribute_size])) {
            destroy_attributes(keys, values, *attribute_size);
        keys[*attr_size] = NULL;
        values[*attr_size] = NULL;
        if (!parse_attribute(state, &keys[*attr_size], &values[*attr_size])) {
            destroy_attributes(keys, values, *attr_size);
            return false;
        }
        for (size_t i = 0; i < *attribute_size; ++i) {
            if (strcmp(keys[*attribute_size], keys[i]) == 0) {
                destroy_attributes(keys, values, *attribute_size);
        for (size_t i = 0; i < *attr_size; ++i) {
            if (strcmp(keys[*attr_size], keys[i]) == 0) {
                destroy_attributes(keys, values, *attr_size);
                return false;
            }
        }
        *attribute_size += 1;
        *attr_size += 1;
    }

    return true;
@@ -316,6 +316,7 @@ static struct node *parse_content(struct parsing_state *state, mchar *name, mcha
    return NULL;
}

// TODO reduce number of args
static bool parse_tag(struct parsing_state *state, mchar **name, mchar **keys, mchar **values, size_t *attribute_size, bool *is_empty)
{
    if (!pattern_char(state, '<') || !read_spaces(state, 0) || (*name = parse_name(state)) == NULL) {
@@ -441,7 +442,7 @@ struct node *parse_root(struct parsing_state *state)
    }

    node_destroy(root);
    parsing_error(state, "EOF");
    parsing_error(state, "EOF in parse_root");

    return NULL;
}
+11 −1
Original line number Diff line number Diff line
@@ -23,6 +23,16 @@ struct node
    struct vector *children;
};

typedef struct arg_struct {
    int argc;
    char **argv;
    FILE *input;
    FILE *output;
    bool xml;
    bool txt;
} arg_struct;


struct node *node_create(mchar *name, mchar **keys, mchar **values, mchar *text, struct vector *children, size_t *attribute_size);

void node_destroy(struct node *node);
@@ -55,7 +65,7 @@ mchar *parse_value(struct parsing_state *state);

bool parse_attribute(struct parsing_state *state, mchar **key, mchar **value);

bool parse_attributes(struct parsing_state *state, mchar **keys, mchar **values, size_t *attribute_size);
bool parse_attributes(struct parsing_state *state, mchar **keys, mchar **values, size_t *attr_size);

struct node *parse_xnode(struct parsing_state *state);

+45 −62
Original line number Diff line number Diff line
@@ -5,30 +5,16 @@
#include "xparser.h"
#include <ctype.h>

struct xpath
struct xpath_node *xpath_node_create(struct xpath_node *node_obj)
{
    size_t size;
    struct xpath_node **path;
};

struct xpath_node
{
    mchar *name;
    size_t index;
    mchar *key;
    mchar *value;
};

struct xpath_node *xpath_node_create(mchar *name, size_t index, mchar *key, mchar *value)
{
    assert(name != NULL);
    assert(node_obj->name != NULL);
    struct xpath_node *node = malloc(sizeof(struct xpath_node));
    if (node != NULL) {
        *node = (struct xpath_node){
            .name = name,
            .index = index,
            .key = key,
            .value = value,
            .name = node_obj->name,
            .index = node_obj->index,
            .key = node_obj->key,
            .value = node_obj->value,
        };
    }
    return node;
@@ -69,14 +55,14 @@ void destroy_xpath(struct xpath *xpath)
    free(xpath);
}

bool parse_attr_value(struct parsing_state *state, mchar *key, mchar *name, mchar *value, struct xpath *xpath_obj)
bool parse_attr_value(struct parsing_state *state, struct xpath *xpath_obj, struct xpath_node *node_obj)
{
    value = parse_value(state);
    if (value == NULL) {
    node_obj->value = parse_value(state);
    if (node_obj->value == NULL) {
        print_error(state, stderr);
        destroy_xpath(xpath_obj);
        str_destroy(key);
        str_destroy(name);
        str_destroy(node_obj->key);
        str_destroy(node_obj->name);
        return false;
    }
    next_char(state);
@@ -91,21 +77,21 @@ void digit_error(struct parsing_state *state, mchar *name, struct xpath *xpath_o
    str_destroy(name);
}

bool calculate_index(struct parsing_state *state, struct xpath *xpath_obj, mchar *name, size_t *index)
bool calculate_index(struct parsing_state *state, struct xpath *xpath_obj, struct xpath_node *node_obj)
{
    int n = -1;
    *index = 0;
    node_obj->index = 0;
    while ((n = peek_char(state)) != ']') {
        if (!isdigit(n)) {
            digit_error(state, name, xpath_obj);
            digit_error(state, node_obj->name, xpath_obj);
            return false;
        }
        *index *= 10;
        *index += (n - '0');
        node_obj->index *= 10;
        node_obj->index += (n - '0');
        next_char(state);
    }
    if (*index == 0) {
        digit_error(state, name, xpath_obj);
    if (node_obj->index == 0) {
        digit_error(state, node_obj->name, xpath_obj);
        return false;
    }
    return true;
@@ -118,32 +104,31 @@ void state_error(struct parsing_state *state, struct xpath *xpath_obj, mchar *na
    str_destroy(name);
}

bool parse_name_attr(struct parsing_state *state, struct xpath *xpath_obj, mchar *name, mchar *key, mchar *value)
bool parse_name_attr(struct parsing_state *state, struct xpath *xpath_obj, struct xpath_node *node_obj)
{
    if ((key = parse_name(state)) == NULL) {
        state_error(state, xpath_obj, name);
    if ((node_obj->key = parse_name(state)) == NULL) {
        state_error(state, xpath_obj, node_obj->name);
        return false;
    }
    int n = next_char(state);
    if (n == '=' && !parse_attr_value(state, key, name, value, xpath_obj)) {
    if (n == '=' && !parse_attr_value(state, xpath_obj, node_obj)) {
        return false;
    } else if (n != ']') {
        state_error(state, xpath_obj, name);
        str_destroy(key);
        state_error(state, xpath_obj, node_obj->name);
        str_destroy(node_obj->key);
        return false;
    }
    return true;
}

bool parse_attrs(struct parsing_state *state, mchar *key, mchar *name,
                mchar *value, struct xpath *xpath_obj, size_t *index)
bool parse_attrs(struct parsing_state *state, struct xpath *xpath_obj, struct xpath_node *node_obj)
{
    if (accept_char(state, '@')) {
        if (!parse_name_attr(state, xpath_obj, name, key, value)) {
        if (!parse_name_attr(state, xpath_obj, node_obj)) {
            return false;
        }
    } else {
        if (!calculate_index(state, xpath_obj, name, index)) {
        if (!calculate_index(state, xpath_obj, node_obj)) {
            return false;
        }
        next_char(state);
@@ -155,7 +140,7 @@ bool realloc_xpath_object(struct parsing_state *state, struct xpath *xpath_obj,
{
    struct xpath_node **tmp = realloc(xpath_obj->path, (xpath_obj->size + 1) * sizeof(struct xpath_node *));
    if (tmp == NULL) {
        alloc_error(state, "realloc");
        alloc_error(state, "tmp in realloc_xpath_object");
        print_error(state, stderr);
        destroy_xpath(xpath_obj);
        str_destroy(name);
@@ -168,24 +153,23 @@ bool realloc_xpath_object(struct parsing_state *state, struct xpath *xpath_obj,
bool parse_xpath_elements(struct parsing_state state, struct xpath *xpath_obj)
{
    while (accept_char(&state, '/')) {
        mchar *name = NULL;
        mchar *key = NULL;
        mchar *value = NULL;
        size_t index = 0;
        struct xpath_node *node_obj = &(struct xpath_node) {
            .name = NULL,
            .key = NULL,
            .value = NULL,
            .index = 0
        };

        if ((name = parse_name(&state)) == NULL) {
        if ((node_obj->name = parse_name(&state)) == NULL) {
            print_error(&state, stderr);
            destroy_xpath(xpath_obj);
            return false;
        }

        if (accept_char(&state, '[') && !parse_attrs(&state, key, name, value, xpath_obj, &index)) {
            return false;
        }
        if (!realloc_xpath_object(&state, xpath_obj, name)) {
        if ((accept_char(&state, '[') && !parse_attrs(&state, xpath_obj, node_obj)) || !realloc_xpath_object(&state, xpath_obj, node_obj->name)) {
            return false;
        }
        xpath_obj->path[xpath_obj->size] = xpath_node_create(name, index, key, value);

        xpath_obj->path[xpath_obj->size] = xpath_node_create(node_obj);
        xpath_obj->size++;
    }
    return true;
@@ -198,7 +182,7 @@ struct xpath *parse_xpath(char *path, FILE *output)
    struct xpath *xpath_obj = xpath_create();

    if (xpath_obj == NULL) {
        alloc_error(&state, "malloc");
        alloc_error(&state, "xpath_obj in parse_xpath");
        print_error(&state, output);
        return NULL;
    }
@@ -227,7 +211,7 @@ bool contains_key(struct node *node, mchar *key, mchar *value)
    return false;
}

void traverse_xroot(struct node *node, struct xpath *xpath_obj, FILE *output, bool xml_output, struct node **to_be_printed, size_t *size_to_be_printed)
void traverse_xroot(struct node *node, struct xpath *xpath_obj, struct printing_nodes *result)
{
    struct xpath_node *cur_path_node = xpath_obj->path[0];

@@ -238,7 +222,7 @@ void traverse_xroot(struct node *node, struct xpath *xpath_obj, FILE *output, bo
        return;
    }

    traverse_xpath(node, xpath_obj, output, xml_output, 1, to_be_printed, size_to_be_printed);
    traverse_xpath(node, xpath_obj, result, 1);
}

void print_indent(FILE *output, size_t size_of_indent)
@@ -292,12 +276,11 @@ void print_output(struct node *node, FILE *output, bool xml_output, size_t size_
    }
}

//TODO reduce args
void traverse_xpath(struct node *node, struct xpath *xpath_obj, FILE *output, bool xml_output, size_t index, struct node **to_be_printed, size_t *size_to_be_printed)
void traverse_xpath(struct node *node, struct xpath *xpath_obj, struct printing_nodes *result, size_t index)
{
    if (index >= xpath_obj->size) {
        to_be_printed[*size_to_be_printed] = node;
        *size_to_be_printed += 1;
        result->nodes[result->size] = node;
        result->size += 1;
        return;
    }
    if (node->children == NULL) {
@@ -320,6 +303,6 @@ void traverse_xpath(struct node *node, struct xpath *xpath_obj, FILE *output, bo
            continue;
        }

        traverse_xpath(*child, xpath_obj, output, xml_output, index + 1, to_be_printed, size_to_be_printed);
        traverse_xpath(*child, xpath_obj, result, index + 1);
    }
}
+21 −2
Original line number Diff line number Diff line
@@ -3,14 +3,33 @@

#include "xparser.h"

struct xpath
{
    size_t size;
    struct xpath_node **path;
};

struct xpath_node
{
    mchar *name;
    size_t index;
    mchar *key;
    mchar *value;
};

struct printing_nodes {
    struct node **nodes;
    size_t size;
};

void print_output(struct node *node, FILE *output, bool xml_output, size_t indent);

struct xpath *parse_xpath(char *path, FILE *output);

void destroy_xpath(struct xpath *xpath);

void traverse_xroot(struct node *node, struct xpath *xpath_obj, FILE *output, bool xml_output, struct node **to_be_printed, size_t *size_to_be_printed);
void traverse_xroot(struct node *node, struct xpath *xpath_obj, struct printing_nodes *result);

void traverse_xpath(struct node *node, struct xpath *xpath_obj, FILE *output, bool xml_output, size_t index, struct node **to_be_printed, size_t *size_to_be_printed);
void traverse_xpath(struct node *node, struct xpath *xpath_obj, struct printing_nodes *result, size_t index);

#endif /* XPATH_H */