2024-08-25 12:04:27 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-08-25 20:24:08 +00:00
|
|
|
#include <math.h>
|
2024-08-25 12:04:27 +00:00
|
|
|
#include "streecmp.h"
|
|
|
|
|
|
|
|
int strs_cnt = 0;
|
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
struct nod *allocnod(void) {
|
|
|
|
struct nod *nod = (struct nod *) calloc(1, sizeof(struct nod));
|
|
|
|
if (nod == NULL) {
|
2024-08-25 12:04:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
nod->pool = (struct nod **) calloc(256, sizeof(struct nod *));
|
|
|
|
if (nod->pool == NULL) {
|
|
|
|
free(nod);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nod;
|
|
|
|
}
|
2024-08-25 12:04:27 +00:00
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
struct nod *mknod(struct nod *nod, int loc) {
|
|
|
|
if (nod == NULL) {
|
|
|
|
return allocnod();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nod->pool == NULL) {
|
2024-08-25 12:04:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
nod->pool_size++;
|
|
|
|
nod->pool[loc] = allocnod();
|
2024-08-25 12:04:27 +00:00
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
return nod->pool[loc];
|
2024-08-25 12:04:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
int mkstr(struct nod *nod, char *str) {
|
|
|
|
struct nod *target = nod->pool[*str];
|
|
|
|
if (target != NULL) {
|
|
|
|
return mkstr(target, str+1);
|
2024-08-25 12:04:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
struct nod *new = mknod(nod, *str);
|
2024-08-25 12:04:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
int gentree(struct nod *nod, char *strs) {
|
2024-08-25 12:04:27 +00:00
|
|
|
char *strs_cpy = strdup(strs);
|
|
|
|
if (strs_cpy == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-08-25 12:19:42 +00:00
|
|
|
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok
|
|
|
|
= strtok(NULL, "\n")) {
|
2024-08-25 20:24:08 +00:00
|
|
|
int ret = mkstr(nod, tok);
|
2024-08-25 12:04:27 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
int streecmp(struct nod *nod, char *str) {
|
|
|
|
struct nod *target = nod->pool[*str];
|
|
|
|
if (target == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2024-08-25 12:04:27 +00:00
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
if (target->val.cval == '\0') {
|
|
|
|
return target->val.ival;
|
2024-08-25 12:04:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-25 20:24:08 +00:00
|
|
|
return streecmp(target, str+1);
|
2024-08-25 12:04:27 +00:00
|
|
|
}
|