timesynk/old/parser.c

212 lines
5.2 KiB
C

/****** parser.c
This file defines the code for parsing, checking, comparing, etc., slots, stats, rolls, and otherwise.
All functions work by taking in pointers to char arrays and doing the operations upon those strings.
******/
#include <ctype.h>
#include <stdio.h> // remove me
#include <stdlib.h>
#include <string.h> // memset!
#include "parser.h"
/* slots */
/**** slotsCompareSlots
Attempts to search for/compare needle to haystack. If haystack is missing a portion of the needle, then the difference in values is placed in the passed string and a non-zero int is returned. Ex: "A2H1" (one handed dude), "H2" (two-handed weapon), returns "H1"
****/
int slotsCompareSlots(char *diff, const char *haystack, char *needle) {
char difference[64];
char *h_slots = slotsBuildTable(haystack);
char *n_slots = slotsBuildTable(needle);
int i = 0;
int j = 0;
int v_i = 0;
while (i < 64) {
if (h_slots[i] < n_slots[i]) {
// value is the difference between the haystack and needle's current slot pair
char value[16];
itoa(n_slots[i]-h_slots[i], &value, 10);
difference[j++] = i+64; // "convert" to alpha
// add our converted value to our difference string
v_i = 0;
while (value[v_i] != '\0') {
difference[j++] = value[v_i];
v_i++;
}
}
i++;
}
difference[j] = '\0'; // terminate our diff string
if (diff)
strcpy(diff, difference);
slotsFreeTable(h_slots);
slotsFreeTable(n_slots);
if (j > 0)
return 1;
else
return 0;
}
int slotsCanAdd(char *needed_slots, const char *slots_total, const char *slots_used, const char *slots_to_add) {
char *t_slots = slotsBuildTable(slots_total);
char *u_slots = slotsBuildTable(slots_used);
char *a_slots = slotsBuildTable(slots_to_add);
char value[16];
int v_i = 0;
char return_slots[64];
int return_value = 0;
int i = 0;
int j = 0;
while (i < 64) {
if (t_slots[i] >= a_slots[i]) {
// if used slots and added_slots are greater than total slots
if (u_slots[i] + a_slots[i] > t_slots[i]) {
return_slots[j++] = i+64; // "convert integer to alpha
itoa((u_slots[i]+a_slots[i]) - t_slots[i], &value, 10);
v_i = 0;
while (value[v_i] != '\0') {
return_slots[j++] = value[v_i];
v_i++;
}
return_slots[j++] = '\0';
return_value = 2;
break;
}
// if slots to add are greater than our total slots
} else {
return_slots[j++] = i+64; // "convert integer to alpha
itoa(a_slots[i] - t_slots[i], &value, 10);
v_i = 0;
while (value[v_i] != '\0') {
return_slots[j++] = value[v_i];
v_i++;
}
return_slots[j++] = '\0';
return_value = 1;
break;
}
i++;
}
if (needed_slots)
strcpy(needed_slots, return_slots); // copy over to the passed string
slotsFreeTable(t_slots);
slotsFreeTable(u_slots);
slotsFreeTable(a_slots);
return(return_value);
}
int slotsGreaterThanSlots(char *diff, const char *haystack, char *needle) {
char difference[64];
char *h_slots = slotsBuildTable(haystack);
char *n_slots = slotsBuildTable(needle);
int i = 0;
int j = 0;
int v_i = 0;
while (i < 64) {
if (n_slots[i] >= h_slots[i]) {
printf("%d avail, need %d\n", n_slots[i], h_slots[i]);
j++;
}
i++;
}
slotsFreeTable(h_slots);
slotsFreeTable(n_slots);
if (diff)
diff[0] = '\0';
if (j > 0)
return 1;
else
return 0;
}
void slotsAddSlots(char *target, char *adding) {
char *t_slots = slotsBuildTable(target);
char *a_slots = slotsBuildTable(adding);
char difference[64];
int j = 0;
int i = 0;
int v_i = 0;
while (i < 64) {
if (a_slots[i] > 0 || t_slots[i] > 0) {
char value[16];
itoa(t_slots[i]+a_slots[i], &value, 10);
difference[j++] = i+64;
v_i = 0;
while (value[v_i] != '\0') {
difference[j++] = value[v_i];
v_i++;
}
}
i++;
}
difference[j] = '\0';
slotsFreeTable(t_slots);
slotsFreeTable(a_slots);
strcpy(target, difference);
}
void slotsRemoveSlots(char *target, char *removing) {
char *t_slots = slotsBuildTable(target);
char *r_slots = slotsBuildTable(removing);
char difference[64];
int j = 0;
int i = 0;
int v_i = 0;
while (i < 64) {
if (r_slots[i] > 0 || t_slots[i] > 0) {
char value[16];
itoa(t_slots[i]-r_slots[i], &value, 10);
difference[j++] = i+64;
v_i = 0;
while (value[v_i] != '\0') {
difference[j++] = value[v_i];
v_i++;
}
}
i++;
}
difference[j] = '\0';
slotsFreeTable(t_slots);
slotsFreeTable(r_slots);
strcpy(target, difference);
}
char *slotsBuildTable(const char *slots) {
char *table = malloc(64);
memset(&table[0], 0, 64); // clear our table
int i = 0;
int j = 0;
char temp[64];
int t = 0;
while (slots[i] != '\0') {
if (slots[i] > 64) { // found a letter (kinda, we get some other chars too)!
j = i+1;
t = 0;
while (isdigit(slots[j])) {
temp[t] = slots[j];
j++;
t++;
}
temp[t] = '\0';
table[slots[i]-64] = atoi(temp);
}
i++;
}
/*i = 0;
while (i < 64) {
printf("slot %c reads %d\n:", i+64, table[i]);
i++;
}*/
return table;
}
void slotsFreeTable(char *table) {
free(table);
}