diff options
Diffstat (limited to 'analyzer/parser')
| -rw-r--r-- | analyzer/parser/fight_results.c | 76 | ||||
| -rw-r--r-- | analyzer/parser/fight_results.h | 10 |
2 files changed, 73 insertions, 13 deletions
diff --git a/analyzer/parser/fight_results.c b/analyzer/parser/fight_results.c index d7b185a..5cda649 100644 --- a/analyzer/parser/fight_results.c +++ b/analyzer/parser/fight_results.c @@ -11,10 +11,11 @@ void cleanup_fight_results(void *data) { FightResults *fight_results = data; for (size_t i = 0; i < fight_results->num_friendly; i++) { - free(fight_results->friendly_results[i].name); - free(fight_results->friendly_results[i].gear); - free(fight_results->friendly_results[i].items); - free(fight_results->friendly_results[i].drifs); + FriendlyResults friendly = fight_results->friendly_results[i]; + free(friendly.name); + for (size_t j = 0; j < friendly.num_items; j++) + free(friendly.items[j]); + free(friendly.items); } for (size_t i = 0; i < fight_results->num_enemy; i++) { free(fight_results->enemy_results[i].name); @@ -86,6 +87,59 @@ char *get_item_name(int item_id) { return item_id < id_to_item_name_len ? id_to_item_name[item_id] : "null"; } +char **expand_items(char **items, size_t *items_capacity, size_t to_fit) { + while (*items_capacity < to_fit) + *items_capacity <<= 1; + return realloc(items, *items_capacity * sizeof(char *)); +} + +void add_gear(FriendlyResults *results, size_t *items_capacity, char *gear_str) { + if (strlen(gear_str) == 0) + return; + + // TODO: parse + if (results->num_items >= *items_capacity) + results->items = expand_items(results->items, items_capacity, results->num_items + 1); + + results->items[results->num_items++] = strdup(gear_str); +} + +void add_drifs(FriendlyResults *results, size_t *items_capacity, char *drifs_str) { + if (strlen(drifs_str) == 0) + return; + + // TODO: parse + if (results->num_items >= *items_capacity) + results->items = expand_items(results->items, items_capacity, results->num_items + 1); + + results->items[results->num_items++] = strdup(drifs_str); +} + +void add_items(FriendlyResults *results, size_t *items_capacity, char *items_str) { + if (strlen(items_str) == 0) + return; + + char *items_separator; + do { + char buffer[SINGLE_ITEM_BUFFER_LEN]; + items_separator = strstr(items_str, SEPARATOR_ITEMS); + + int item_id, item_count; + sscanf(items_str, "%d,%d", &item_id, &item_count); + sprintf(buffer, "%dx %s", item_count, get_item_name(item_id)); + + // if there are still items - modify items_str pointer + if (items_separator) + items_str = items_separator + strlen(SEPARATOR_ITEMS); + + // expand if needed + if (results->num_items >= *items_capacity) + results->items = expand_items(results->items, items_capacity, results->num_items + 1); + + results->items[results->num_items++] = strdup(buffer); + } while (items_separator); +} + char *get_saturation_type(int saturation_id) { return saturation_id < id_to_saturation_type_len ? id_to_saturation_type[saturation_id] : "null"; } @@ -97,16 +151,18 @@ FriendlyResults parse_to_friendly(EntityResults entity_results) { results.name = strdup(entity_results.name); results.exp = entity_results.exp; results.gold = entity_results.gold; - // TODO: parse that to more readable format - results.gear = strdup(entity_results.gear); - // TODO: parse that to more readable format, determine types from ids - results.items = strdup(entity_results.items); results.level = entity_results.level; results.psycho = entity_results.psycho; - // TODO: parse that to more readable format - results.drifs = strdup(entity_results.drifs); results.splinters = entity_results.splinters; + // create items array and add to it gear, drifs and items + results.num_items = 0; + results.items = malloc(INIT_ITEMS_CAPACITY * sizeof(char *)); + size_t items_cap = INIT_ITEMS_CAPACITY; + add_gear(&results, &items_cap, entity_results.gear); + add_drifs(&results, &items_cap, entity_results.drifs); + add_items(&results, &items_cap, entity_results.items); + results.saturation = 0; if (strlen(entity_results.saturation) > 0) { int saturation_id; diff --git a/analyzer/parser/fight_results.h b/analyzer/parser/fight_results.h index 17c2f05..ca45348 100644 --- a/analyzer/parser/fight_results.h +++ b/analyzer/parser/fight_results.h @@ -133,6 +133,11 @@ typedef struct { } EntityResults; #endif +// items/gear/drifs fields parsing info +#define INIT_ITEMS_CAPACITY 8 +#define SINGLE_ITEM_BUFFER_LEN 128 +#define SEPARATOR_ITEMS "[-]" + // values of field "team" #define TEAM_FRIEND 1 #define TEAM_ENEMY 2 @@ -144,11 +149,10 @@ typedef struct { char *name; int exp; int gold; - char *gear; - char *items; + char **items; + size_t num_items; int level; int psycho; - char *drifs; int splinters; char *saturation_type; int saturation; |
