nexus/flag.c

121 lines
2.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "flag.h"
#include "text.h"
flag_t *addflag(flagpile_t *fp, enum FLAG id, int val1, int val2, int val3, char *text) {
flag_t *f;
int i;
if (fp->first == NULL) {
fp->first = malloc(sizeof(flag_t));
f = fp->first;
f->prev = NULL;
} else {
// go to end of list
f = fp->last;
f->next = malloc(sizeof(flag_t));
f->next->prev = f;
f = f->next;
}
fp->last = f;
f->next = NULL;
// fill in props
f->id = id; // increment next ob id
// first blank values
for (i = 0; i < 3; i++) {
f->val[i] = NA;
}
f->val[0] = val1;
f->nvals = 1;
if (val2 != NA) {
f->val[1] = val2;
f->nvals++;
}
if (val3 != NA) {
f->val[2] = val3;
f->nvals++;
}
if (text) {
f->text = strdup(text);
} else {
f->text = strdup("");
}
f->pile = fp;
return f;
}
flagpile_t *addflagpile(void) {
flagpile_t *fp;
fp = malloc(sizeof(flagpile_t));
fp->first = NULL;
fp->last = NULL;
return fp;
}
flag_t *hasflag(flagpile_t *fp, int id) {
flag_t *f;
for (f = fp->first ; f ; f = f->next) {
if (f->id == id) return f;
}
return NULL;
}
flag_t *hasflagval(flagpile_t *fp, int id, int val1, int val2, int val3, char *text) {
flag_t *f;
for (f = fp->first ; f ; f = f->next) {
if (f->id == id) {
if ( ((val1 == NA) || (f->val[0] == val1)) &&
((val2 == NA) || (f->val[1] == val2)) &&
((val3 == NA) || (f->val[2] == val3)) &&
((text == NULL) || strstr(f->text, text))) {
return f;
}
}
}
return NULL;
}
void killflag(flag_t *f) {
int i;
flag_t *nextone, *lastone;
// free mem
// remove from list
nextone = f->next;
if (nextone != NULL) {
nextone->prev = f->prev;
} else { /* last */
f->pile->last = f->prev;
}
if (f->prev == NULL) {
/* first */
nextone = f->next;
f->pile->first = nextone;
free(f);
} else {
lastone = f->prev;
free (lastone->next );
lastone->next = nextone;
}
}
void killflagpile(flagpile_t *fp) {
while (fp->first) {
killflag(fp->first);
}
free(fp);
}