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