154 lines
3.1 KiB
C
154 lines
3.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
#include "streecmp.h"
|
|
|
|
static int strs_cnt = 1;
|
|
|
|
void frenod(struct nod *nod) {
|
|
for (int i = 0; i < POOL_SIZE; i++) {
|
|
struct nod *p = nod->pool[i];
|
|
if (p == NULL) {
|
|
continue;
|
|
}
|
|
|
|
frenod(p);
|
|
}
|
|
|
|
free(nod->pool);
|
|
memset(nod, 0, sizeof(struct nod));
|
|
free(nod);
|
|
}
|
|
|
|
struct nod *allocnod(void) {
|
|
struct nod *nod = (struct nod *) calloc(1, sizeof(struct nod));
|
|
if (nod == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
nod->pool = (struct nod **) calloc(POOL_SIZE, sizeof(struct nod *));
|
|
if (nod->pool == NULL) {
|
|
free(nod);
|
|
return NULL;
|
|
}
|
|
|
|
return nod;
|
|
}
|
|
|
|
struct nod *mknod(struct nod *nod, int loc) {
|
|
if (nod == NULL) {
|
|
return allocnod();
|
|
}
|
|
|
|
if (nod->pool == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
nod->pool_size++;
|
|
nod->pool[loc] = allocnod();
|
|
|
|
return nod->pool[loc];
|
|
}
|
|
|
|
int mkstr(struct nod *nod, char *str, int val, int flag) {
|
|
char ch = (flag & LOOSE_CHECK) &&
|
|
(*str >= 'a') &&
|
|
(*str <= 'z') ? *str-32 : *str;
|
|
|
|
struct nod *target = nod->pool[ch];
|
|
if (target != NULL) {
|
|
if (*str == '\0') {
|
|
return 0;
|
|
}
|
|
|
|
return mkstr(target, str+1, val, flag);
|
|
}
|
|
|
|
struct nod *new = mknod(nod, ch);
|
|
if (new == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
new->val.cval = ch;
|
|
if (*str != '\0') {
|
|
return mkstr(new, str+1, val, flag);
|
|
}
|
|
|
|
new->val.ival = val;
|
|
return val;
|
|
}
|
|
|
|
int gentree(struct nod *nod, char *strs, char *delim, int flag) {
|
|
strs_cnt = 1;
|
|
|
|
char *strs_cpy = strdup(strs);
|
|
if (strs_cpy == NULL) {
|
|
goto _err;
|
|
}
|
|
|
|
if (delim == NULL) {
|
|
delim = DEF_DELIM;
|
|
}
|
|
|
|
for (char *tok = strtok(strs_cpy, delim); tok != NULL; tok
|
|
= strtok(NULL, delim)) {
|
|
int ret = mkstr(nod, tok, strs_cnt, flag);
|
|
if (ret < 0) {
|
|
goto _err_mkstr;
|
|
}
|
|
strs_cnt++;
|
|
}
|
|
|
|
free(strs_cpy);
|
|
return 0;
|
|
|
|
_err_mkstr:
|
|
free(strs_cpy);
|
|
_err:
|
|
return -1;
|
|
}
|
|
|
|
int streecmp(struct nod *nod, char *str, int flag) {
|
|
char ch = (flag & LOOSE_CHECK) &&
|
|
(*str >= 'a') &&
|
|
(*str <= 'z') ? *str-32 : *str;
|
|
|
|
struct nod *target = nod->pool[ch];
|
|
if (target == NULL) {
|
|
return 0;
|
|
}
|
|
|
|
if (target->val.cval == '\0') {
|
|
return target->val.ival;
|
|
}
|
|
|
|
return streecmp(target, str+1, flag);
|
|
}
|
|
|
|
int streencmp(struct nod *nod, char *str, int len, int flag) {
|
|
struct nod *nptr = nod;
|
|
for (char *sptr = str; sptr < str+len; sptr++) {
|
|
char ch = (flag & LOOSE_CHECK) &&
|
|
(*sptr >= 'a') &&
|
|
(*sptr <= 'z') ? *sptr-32 : *sptr;
|
|
|
|
struct nod *target = nptr->pool[ch];
|
|
if (target == NULL) {
|
|
goto _not_found;
|
|
}
|
|
|
|
nptr = target;
|
|
}
|
|
|
|
nptr = nptr->pool['\0'];
|
|
if (nptr == NULL) {
|
|
goto _not_found;
|
|
}
|
|
|
|
return nptr->val.ival;
|
|
|
|
_not_found:
|
|
return 0;
|
|
}
|