Seems finished
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#include "key_value_pair.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -17,6 +16,38 @@ char *key_value_pair_to_string(key_value_pair *kv)
|
||||
return str;
|
||||
}
|
||||
|
||||
char *key_value_pair_list_to_string(key_value_pair **head){
|
||||
key_value_pair *kvp_it = *head;
|
||||
|
||||
char *ret = NULL;
|
||||
|
||||
while(kvp_it != NULL){
|
||||
char *kvp2str = kvp_it->toString(kvp_it);
|
||||
if(kvp2str == NULL){
|
||||
kvp_it = kvp_it->next;
|
||||
continue;
|
||||
}
|
||||
if(!ret){
|
||||
ret = strdup(kvp2str);
|
||||
kvp_it = kvp_it->next;
|
||||
}
|
||||
else{
|
||||
char *tmp = realloc(ret, strlen(ret) + strlen(kvp2str) + 1);
|
||||
if(!tmp){
|
||||
free(ret);
|
||||
perror("Failed to reallocate memory for ret");
|
||||
free(kvp2str);
|
||||
return NULL;
|
||||
}
|
||||
ret = tmp;
|
||||
strcat(ret, kvp2str);
|
||||
free(kvp2str);
|
||||
kvp_it = kvp_it->next;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// initializes a new instance of key_value_pair with given values on given pointer
|
||||
key_value_pair *init_key_value_pair(const char *key, const char *value)
|
||||
{
|
||||
@@ -133,19 +164,25 @@ void remove_key_value_pair(key_value_pair *kv, key_value_pair **head, key_value_
|
||||
}
|
||||
}
|
||||
|
||||
void extract_key_value_pairs(char *red_str, key_value_pair **head, key_value_pair **tail){
|
||||
void extract_key_value_pairs(char *red_str, key_value_pair **head, key_value_pair **tail, bool doReduce){
|
||||
char *it = red_str;
|
||||
bool reading_value = false;
|
||||
char *word_start = it;
|
||||
size_t word_length = 0;
|
||||
size_t value_length = 0;
|
||||
|
||||
while(*it){
|
||||
if(isalpha(*it) && reading_value){
|
||||
while(true){
|
||||
if(!*it || (isalpha(*it) && reading_value)){
|
||||
char key[word_length + 1];
|
||||
strncpy(key, word_start, word_length);
|
||||
key[word_length] = '\0';
|
||||
char value[16];
|
||||
snprintf(value, 16, "%d", (int)value_length);
|
||||
if(doReduce)
|
||||
snprintf(value, 16, "%d", (int)value_length);
|
||||
else{
|
||||
strncpy(value, word_start + word_length, value_length);
|
||||
value[value_length] = '\0';
|
||||
}
|
||||
|
||||
key_value_pair *kv = init_key_value_pair(key, value);
|
||||
add_key_value_pair(kv, head, tail);
|
||||
@@ -155,6 +192,8 @@ void extract_key_value_pairs(char *red_str, key_value_pair **head, key_value_pai
|
||||
value_length = 0;
|
||||
word_start = it;
|
||||
reading_value = false;
|
||||
|
||||
if(!*it) break;
|
||||
continue;
|
||||
}
|
||||
if(isalpha(*it)){
|
||||
@@ -163,7 +202,7 @@ void extract_key_value_pairs(char *red_str, key_value_pair **head, key_value_pai
|
||||
}
|
||||
else{
|
||||
reading_value = true; // reassigning true is faster than if checks every iteration
|
||||
word_length++;
|
||||
value_length++;
|
||||
it++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user