93 lines
1.7 KiB
C
93 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
#include "streecmp.h"
|
|
|
|
int strs_cnt = 0;
|
|
|
|
struct nod *allocnod(void) {
|
|
struct nod *nod = (struct nod *) calloc(1, sizeof(struct nod));
|
|
if (nod == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
nod->pool = (struct nod **) calloc(256, 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) {
|
|
struct nod *target = nod->pool[*str];
|
|
if (target != NULL) {
|
|
if (*str == '\0') {
|
|
return 0;
|
|
}
|
|
|
|
return mkstr(target, str+1);
|
|
}
|
|
|
|
struct nod *new = mknod(nod, *str);
|
|
if (new == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
new->val.cval = *str;
|
|
if (*str != '\0') {
|
|
return mkstr(new, str+1);
|
|
}
|
|
|
|
strs_cnt++;
|
|
new->val.ival = strs_cnt;
|
|
return strs_cnt;
|
|
}
|
|
|
|
int gentree(struct nod *nod, char *strs) {
|
|
char *strs_cpy = strdup(strs);
|
|
if (strs_cpy == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok
|
|
= strtok(NULL, "\n")) {
|
|
int ret = mkstr(nod, tok);
|
|
if (ret < 0) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int streecmp(struct nod *nod, char *str) {
|
|
struct nod *target = nod->pool[*str];
|
|
if (target == NULL) {
|
|
return 0;
|
|
}
|
|
|
|
if (target->val.cval == '\0') {
|
|
return target->val.ival;
|
|
}
|
|
|
|
return streecmp(target, str+1);
|
|
}
|