fix: check for existing string before mkstr
This commit is contained in:
parent
e6bb483dc2
commit
cbd536b369
8
README
8
README
|
@ -15,22 +15,22 @@ TECHNICALS
|
|||
* data structure that represents a node of the tree
|
||||
struct nod *mknod(struct nod *prnt)
|
||||
* create a child node below given parent node
|
||||
* returns: pointer to said node or NULL if fails
|
||||
* returns: pointer to said node or NULL if function fails
|
||||
int mkstr(struct nod *rot, char *str)
|
||||
* fit string into tree if it does not already exist
|
||||
* warning: string has to be null-byte terminated
|
||||
* returns: the assigned ID of the string or -1 if fails
|
||||
* returns: the assigned ID of the string, 0 if string
|
||||
is already inside tree, or -1 if function fails
|
||||
int gentree(struct nod *rot, char *strs)
|
||||
* generate tree from character seperated strings
|
||||
* warning: seperation character is '\n'
|
||||
* returns: 0 if ok or -1 if fails
|
||||
* returns: 0 if ok or -1 if function fails
|
||||
int streecmp(struct nod *rot, char *str)
|
||||
* traverse tree and search for str
|
||||
* returns: id of string or 0 if there is no match
|
||||
|
||||
TODO
|
||||
|
||||
* Detect strings which are already inside the tree
|
||||
* Memory leaks
|
||||
* Alloc/free functions
|
||||
* NULL checking
|
||||
|
|
|
@ -39,6 +39,10 @@ struct nod *mknod(struct nod *nod, int 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);
|
||||
}
|
||||
|
||||
|
|
11
test.c
11
test.c
|
@ -136,14 +136,15 @@ int main(void) {
|
|||
|
||||
int cnt = 1;
|
||||
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok
|
||||
= strtok(NULL, "\n"), cnt++) {
|
||||
= strtok(NULL, "\n")) {
|
||||
int found = streecmp(rot, tok);
|
||||
if (found == cnt) {
|
||||
fprintf(stdout, "[+][%d\t] Success: could find"
|
||||
" '%s' in tree\n", cnt, tok);
|
||||
fprintf(stdout, "[+][%d/%d] Success: could find"
|
||||
" '%s' in tree\n", cnt, found, tok);
|
||||
cnt++;
|
||||
} else {
|
||||
fprintf(stderr, "[-][%d\t] Error: could not find"
|
||||
" '%s' in tree\n", cnt, tok);
|
||||
fprintf(stderr, "[-][%d/%d] Error: could not find"
|
||||
" '%s' in tree\n", cnt, found, tok);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user