Пример #1
0
dns_map* create_dns_map(FILE* h_domain2token, FILE* h_token2ip) {
    dns_map* map = (dns_map*) malloc(sizeof(dns_map));

    map->domain_to_token = create_hash_map(str_cmp);
    map->token_to_ip = create_hash_map(str_cmp);
    map->tokens = create_hash_map(str_cmp);

    map->h_domain2token = h_domain2token;
    map->h_token2ip = h_token2ip;

    // Read data from files
    read_files(map, h_domain2token, h_token2ip);

    return map;
}
Пример #2
0
int main(int argc, char const *argv[]) {
    char* read_buf = malloc(READ_BUF_LEN);
    hashmap_t *hm = create_hash_map(HASHMAP_SIZE);

    int result = scanf("%511[a-z|A-Z|']", read_buf);
    int *count;
    while (result != EOF) {
        if (result == 1) {
            to_lower_case(read_buf);

            count = lookup_val(read_buf, hm);
            if (count != NULL) {
                *count = *count + 1;
            } else {
                add_val(read_buf, 1, hm);
            }
        } else if (result == 0) {
            scanf("%511[^a-z|A-Z|']", read_buf);
        }

        result = scanf("%511[a-z|A-Z|']", read_buf);
    }

    node_t **words = to_sorted_array(hm);
    for (int i = 0; i < hm->size; i++) {
        printf("%s %d\n", words[i]->string, words[i]->count);
    }

    return 0;
}
Пример #3
0
ERROR_CODE _bencoding_dictionary_start_callback(struct bencoding_engine_t *bencoding_engine) {
    /* allocates space for the hash map and for the type
    structure that will encapsulate it */
    struct hash_map_t *hash_map;
    struct sort_map_t *sort_map;
    struct type_t *type;

    /* retrieves the bencoding handler from the template engine context
    then uses it to store the (current) value */
    struct bencoding_handler_t *bencoding_handler = (struct bencoding_handler_t *) bencoding_engine->context;

    /* switchs over the "target" map type to be used to store conceptual
    maps in the bencoding structure */
    switch(bencoding_engine->map_type) {
        case MAP_TYPE:
            /* creates a new hash map for the new structure (sequence) context
            and then creates the respective type for the map */
            create_hash_map(&hash_map, 0);
            create_type(&type, MAP_TYPE);
            *type = map_type(hash_map);

        case SORT_MAP_TYPE:
            /* creates a new sort map for the new structure (sequence) context
            and then creates the respective type for the map */
            create_sort_map(&sort_map, 0);
            create_type(&type, SORT_MAP_TYPE);
            *type = sort_map_type(sort_map);

        default:
            /* creates a new sort map for the new structure (sequence) context
            and then creates the respective type for the map */
            create_sort_map(&sort_map, 0);
            create_type(&type, SORT_MAP_TYPE);
            *type = sort_map_type(sort_map);
    }

    /* adds the sequence type to the sequence stack and then adds the
    current handler key to the key stack, then updates the current sequence
    reference and the current key in the handler */
    append_value_linked_list(bencoding_handler->sequence_stack, (void *) type);
    append_value_linked_list(bencoding_handler->key_stack, (void *) bencoding_handler->key);
    bencoding_handler->sequence = type;
    bencoding_handler->key = NULL;

    /* in case there is no top type defined for the handler sets the
    current type as the top type (base type) */
    if(bencoding_handler->top == NULL) { bencoding_handler->top = type; }

    /* sets the next key flag so that in the next iteration the string
    is "accepted" as the current key value */
    bencoding_handler->next_key = 1;

    /* raises no error */
    RAISE_NO_ERROR;
}
Пример #4
0
ERROR_CODE parameters_http_c(char *buffer, size_t size, size_t count, ...) {
    /* allocates space for the variable list of arguments
    provided as arguments to this function and tha should
    contain sequences of key, value and length */
    va_list arguments;

    /* allocates space for the various indexes to be
    used in the iteration including the sequence offset
    and the global index counter */
    size_t index;
    size_t index_g;
    size_t offset;

    /* allocates space for the three components of the
    parameter the key, the value and the length of the
    provided value buffer (it's not null terminated) */
    char *key_s;
    char *buffer_s;
    size_t length_s;

    /* allocates space for the pointer to the buffer that
    will hold the created parameters string */
    char *params_buffer;
    size_t params_size;

    /* creates space for the pointer to the map that will
    contain all the arranjed sequence of keys and values
    representing the various parameters */
    struct hash_map_t *parameters_map;

    /* statically allocates space in the stack for the various
    value strings representing the parameter values */
    struct string_t strings[256];

    /* in case the number of tuples provided as arguments is
    more that the space available for the value strings must
    fail with an error otherwise a buffer overflow occurs */
    if(count > 256) {
        RAISE_ERROR_M(
            RUNTIME_EXCEPTION_ERROR_CODE,
            (unsigned char *) "Problem creating parameters"
        );
    }

    /* sets the inial key string value as null so that the
    value is started initialized, required for safe execution */
    key_s = NULL;

    /* multiplies the count by three as it must contain
    the real number of arguments and not just the number
    of tuples of three in it */
    count *= 3;

    /* creates the hash map that is going to be used to
    temporarly store the various key value associations */
    create_hash_map(&parameters_map, 0);

    /* iterates over the sequence of dynamic arguments
    provided to the function to retrieve them as sequences
    of key, value and length, then sets them in the map
    representing the various parameters */
    va_start(arguments, count);
    for(index = 0; index < count; index++) {
        offset = index % 3;
        index_g = index / 3;

        switch(offset) {
            case 0:
                key_s = va_arg(arguments, char *);
                break;

            case 1:
                buffer_s = va_arg(arguments, char *);
                strings[index_g].buffer = (unsigned char *) buffer_s;
                break;

            case 2:
                length_s = va_arg(arguments, size_t);
                strings[index_g].length = length_s;

                set_value_string_hash_map(
                    parameters_map,
                    (unsigned char *) key_s,
                    (void *) &strings[index_g]
                );

                break;
        }
    }
    va_end(arguments);

    /* generates the (get) parameters for an http request
    from the provided hash map of key values, the returned
    buffer is owned by the caller and must be released */
    parameters_http(
        parameters_map,
        (unsigned char **) &params_buffer,
        &params_size
    );
    delete_hash_map(parameters_map);

    /* in case the amount of bytes to be copied from the
    dynamically created params buffer to the buffer is
    greater than the size provided raises an error */
    if(params_size > size - 1) {
        FREE(params_buffer);
        RAISE_ERROR_M(
            RUNTIME_EXCEPTION_ERROR_CODE,
            (unsigned char *) "Problem creating parameters"
        );
    }

    /* copies the generated params buffer into the final
    buffer defined in the parameters structure, then closes
    the string with the final character and releases the
    temporary buffer (params buffer) */
    memcpy(buffer, params_buffer, params_size);
    buffer[params_size] = '\0';
    FREE(params_buffer);

    /* raises no error as the creation of the parameters
    buffer has completed with success */
    RAISE_NO_ERROR;
}