ERROR_CODE _encode_type(struct type_t *type, struct string_buffer_t *string_buffer) { char *buffer; struct type_t key; struct type_t *current; struct iterator_t *iterator; struct hash_map_element_t *element; switch(type->type) { case INTEGER_TYPE: buffer = MALLOC(16); SPRINTF(buffer, 16, "%d", type->value.value_int); append_string_buffer(string_buffer, (unsigned char *) "i"); _append_string_buffer(string_buffer, (unsigned char *) buffer); append_string_buffer(string_buffer, (unsigned char *) "e"); break; case STRING_TYPE: buffer = MALLOC(16); SPRINTF(buffer, 16, "%lu", (long unsigned int) type->size); _append_string_buffer(string_buffer, (unsigned char *) buffer); append_string_buffer(string_buffer, (unsigned char *) ":"); append_string_l_buffer( string_buffer, (unsigned char *) type->value.value_string, type->size ); break; case LIST_TYPE: append_string_buffer(string_buffer, (unsigned char *) "l"); create_iterator_linked_list(type->value.value_list, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) ¤t); if(current == NULL) { break; } _encode_type(current, string_buffer); } delete_iterator_linked_list(type->value.value_list, iterator); append_string_buffer(string_buffer, (unsigned char *) "e"); break; case MAP_TYPE: append_string_buffer(string_buffer, (unsigned char *) "d"); create_element_iterator_hash_map(type->value.value_map, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) &element); if(element == NULL) { break; } key = string_type((char *) element->key_string); _encode_type(&key, string_buffer); _encode_type((struct type_t *) element->value, string_buffer); } delete_iterator_hash_map(type->value.value_map, iterator); append_string_buffer(string_buffer, (unsigned char *) "e"); break; case SORT_MAP_TYPE: append_string_buffer(string_buffer, (unsigned char *) "d"); create_element_iterator_sort_map(type->value.value_sort_map, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) &element); if(element == NULL) { break; } key = string_type((char *) element->key_string); _encode_type(&key, string_buffer); _encode_type((struct type_t *) element->value, string_buffer); } delete_iterator_sort_map(type->value.value_sort_map, iterator); append_string_buffer(string_buffer, (unsigned char *) "e"); break; default: break; } /* raises no error */ RAISE_NO_ERROR; }
ERROR_CODE print_type(struct type_t *type) { /* allocates space for the current type for the type to handler the key values for map for the possible iterator, for the hash map element and for the is first (loop) flag */ struct type_t *current; struct type_t key; struct iterator_t *iterator; struct hash_map_element_t *element; unsigned char is_first = 1; /* switches over the type's type in order to execute the proper print operation */ switch(type->type) { case INTEGER_TYPE: PRINTF_F("%d", type->value.value_int); /* breaks the switch */ break; case FLOAT_TYPE: PRINTF_F("%f", type->value.value_float); /* breaks the switch */ break; case STRING_TYPE: PRINTF_F("'%s'", type->value.value_string); /* breaks the switch */ break; case LIST_TYPE: PRINTF("["); create_iterator_linked_list(type->value.value_list, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) ¤t); if(current == NULL) { break; } if(is_first == 0) { PRINTF(", "); }; print_type(current); is_first = 0; } delete_iterator_linked_list(type->value.value_list, iterator); PRINTF("]"); /* breaks the switch */ break; case MAP_TYPE: PRINTF("{"); create_element_iterator_hash_map(type->value.value_map, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) &element); if(element == NULL) { break; } if(is_first == 0) { PRINTF(", "); }; key = string_type((char *) element->key_string); print_type(&key); PRINTF(" : "); print_type((struct type_t *) element->value); is_first = 0; } delete_iterator_hash_map(type->value.value_map, iterator); PRINTF("}"); /* breaks the switch */ break; case SORT_MAP_TYPE: PRINTF("{"); create_element_iterator_sort_map(type->value.value_sort_map, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) &element); if(element == NULL) { break; } if(is_first == 0) { PRINTF(", "); }; key = string_type((char *) element->key_string); print_type(&key); PRINTF(" : "); print_type((struct type_t *) element->value); is_first = 0; } delete_iterator_sort_map(type->value.value_sort_map, iterator); PRINTF("}"); /* breaks the switch */ break; default: PRINTF("undefined"); /* breaks the switch */ break; } /* raises no error */ RAISE_NO_ERROR; }
ERROR_CODE free_type(struct type_t *type) { /* allocats space for the current type in iteration for the iterator and for the possible hash map element */ struct type_t *current; struct iterator_t *iterator; struct hash_map_element_t *element; /* switches over the type's type in order to execute the proper free operation */ switch(type->type) { case INTEGER_TYPE: /* breaks the switch */ break; case FLOAT_TYPE: /* breaks the switch */ break; case STRING_TYPE: FREE(type->value.value_string); /* breaks the switch */ break; case LIST_TYPE: create_iterator_linked_list(type->value.value_list, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) ¤t); if(current == NULL) { break; } free_type(current); } delete_iterator_linked_list(type->value.value_list, iterator); delete_linked_list(type->value.value_list); /* breaks the switch */ break; case MAP_TYPE: create_element_iterator_hash_map(type->value.value_map, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) &element); if(element == NULL) { break; } free_type((struct type_t *) element->value); } delete_iterator_hash_map(type->value.value_map, iterator); delete_hash_map(type->value.value_map); /* breaks the switch */ break; case SORT_MAP_TYPE: create_element_iterator_sort_map(type->value.value_sort_map, &iterator); while(TRUE) { get_next_iterator(iterator, (void **) &element); if(element == NULL) { break; } free_type((struct type_t *) element->value); } delete_iterator_sort_map(type->value.value_sort_map, iterator); delete_sort_map(type->value.value_sort_map); /* breaks the switch */ break; default: /* breaks the switch */ break; } /* deltes the base type structure for the current type, this applies to all the types */ delete_type(type); /* raises no error */ RAISE_NO_ERROR; }
ERROR_CODE parameters_http(struct hash_map_t *hash_map, unsigned char **buffer_pointer, size_t *buffer_length_pointer) { /* allocates space for an iterator object for an hash map element and for the string buffer to be used to collect all the partial strings that compose the complete url parameters string */ struct iterator_t *iterator; struct hash_map_element_t *element; struct string_buffer_t *string_buffer; /* allocates space for the string structure to hold the value of the element for the string value reference for the joined string, for the buffer string from the element and for the corresponding string lengths for both cases */ struct string_t *string; unsigned char *string_value; unsigned char *_buffer; size_t string_length; size_t _length; /* allocates and sets the initial value on the flag that controls if the iteratio to generate key values is the first one */ char is_first = 1; /* creates a new string buffer and a new hash map iterator, these structures are going to be used to handle the string from the hash map and to iterate over the hash map elements */ create_string_buffer(&string_buffer); create_element_iterator_hash_map(hash_map, &iterator); /* iterates continuously arround the hash map element the iterator is going to stop the iteration */ while(TRUE) { /* retrieves the next element from the iterator and in case such element is invalid breaks the loop */ get_next_iterator(iterator, (void **) &element); if(element == NULL) { break; } /* checks if this is the first loop in the iteration in it's not emits the and character */ if(is_first) { is_first = 0; } else { append_string_buffer(string_buffer, (unsigned char *) "&"); } /* retrieves the current element value as a string structure then encodes that value using the url encoding (percent encoding) and resets the string reference to contain the new buffer as it' own contents (avoids extra memory usage) */ string = (struct string_t *) element->value; url_encode(string->buffer, string->length, &_buffer, &_length); string->buffer = _buffer; string->length = _length; /* adds the various elements for the value to the string buffer first the key the the attribution operator and then the value */ append_string_buffer(string_buffer, (unsigned char *) element->key_string); append_string_l_buffer(string_buffer, (unsigned char *) "=", sizeof("=") - 1); _append_string_t_buffer(string_buffer, string); } /* "joins" the string buffer values into a single value (from the internal string list) and then retrieves the length of the string buffer */ join_string_buffer(string_buffer, &string_value); string_length = string_buffer->string_length; /* deletes the hash map iterator and string buffer structures, to avoid memory leak */ delete_iterator_hash_map(hash_map, iterator); delete_string_buffer(string_buffer); /* updates the buffer pointer reference and the buffer length pointer reference with the string value and the string length values */ *buffer_pointer = string_value; *buffer_length_pointer = string_length; /* raises no error */ RAISE_NO_ERROR; }