feat: add custom delimiter to gentree

This commit is contained in:
Kevin J. 2024-08-27 18:59:30 +02:00
parent 66e392b922
commit c46eb7b3aa
6 changed files with 32 additions and 22 deletions

28
LEAKS
View File

@ -1,8 +1,8 @@
==6704== Memcheck, a memory error detector ==12180== Memcheck, a memory error detector
==6704== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. ==12180== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==6704== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info ==12180== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==6704== Command: ./test ==12180== Command: ./test
==6704== ==12180==
------------------------ ------------------------
[+][1/1] Success: could find 'A-IM' in tree [+][1/1] Success: could find 'A-IM' in tree
[+][2/2] Success: could find 'Accept' in tree [+][2/2] Success: could find 'Accept' in tree
@ -115,12 +115,12 @@
------------------------ ------------------------
[+] Freed all memory in use [+] Freed all memory in use
[f] Finished [f] Finished
==6704== ==12180==
==6704== HEAP SUMMARY: ==12180== HEAP SUMMARY:
==6704== in use at exit: 0 bytes in 0 blocks ==12180== in use at exit: 0 bytes in 0 blocks
==6704== total heap usage: 2,056 allocs, 2,056 frees, 2,206,614 bytes allocated ==12180== total heap usage: 2,056 allocs, 2,056 frees, 2,206,614 bytes allocated
==6704== ==12180==
==6704== All heap blocks were freed -- no leaks are possible ==12180== All heap blocks were freed -- no leaks are possible
==6704== ==12180==
==6704== For lists of detected and suppressed errors, rerun with: -s ==12180== For lists of detected and suppressed errors, rerun with: -s
==6704== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) ==12180== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

View File

@ -17,7 +17,7 @@ test:
run: run:
./$(OUTPUT) ./$(OUTPUT)
run_test: runtest:
./$(TOUTPUT) ./$(TOUTPUT)
memcheck: memcheck:

8
README
View File

@ -29,14 +29,18 @@ TECHNICALS
* warning: string has to be null-byte terminated * warning: string has to be null-byte terminated
* returns: the assigned ID of the string, 0 if string * returns: the assigned ID of the string, 0 if string
is already inside tree, or -1 if function fails is already inside tree, or -1 if function fails
int gentree(struct nod *nod, char *strs) int gentree(struct nod *nod, char *strs, char *delim)
* generate tree from character seperated strings * generate tree from character seperated strings
* if delim is NULL, then "\n" is assumed
* warning: seperation character is '\n' * warning: seperation character is '\n'
* returns: 0 if ok or -1 if function fails * returns: 0 if ok or -1 if function fails
int streecmp(struct nod *nod, char *str) int streecmp(struct nod *nod, 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
test.c provides a practical example for comparing against
HTTP header fields
LEAKS LEAKS
LEAKS file contains the most recent valgrind memory leaks LEAKS file contains the most recent valgrind memory leaks
@ -44,7 +48,7 @@ LEAKS
TODO TODO
* gentree with custom seperator * everything done so far
COMMITS COMMITS

View File

@ -76,14 +76,18 @@ int mkstr(struct nod *nod, char *str) {
return strs_cnt; return strs_cnt;
} }
int gentree(struct nod *nod, char *strs) { int gentree(struct nod *nod, char *strs, char *delim) {
char *strs_cpy = strdup(strs); char *strs_cpy = strdup(strs);
if (strs_cpy == NULL) { if (strs_cpy == NULL) {
goto _err; goto _err;
} }
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok if (delim == NULL) {
= strtok(NULL, "\n")) { delim = DEF_DELIM;
}
for (char *tok = strtok(strs_cpy, delim); tok != NULL; tok
= strtok(NULL, delim)) {
int ret = mkstr(nod, tok); int ret = mkstr(nod, tok);
if (ret < 0) { if (ret < 0) {
goto _err_mkstr; goto _err_mkstr;

View File

@ -2,6 +2,7 @@
#define STREECMP_H #define STREECMP_H
#define POOL_SIZE 256 #define POOL_SIZE 256
#define DEF_DELIM "\n"
struct nodval { struct nodval {
int ival; int ival;
@ -18,7 +19,7 @@ void frenod(struct nod *nod);
struct nod *allocnod(void); struct nod *allocnod(void);
struct nod *mknod(struct nod *nod, int loc); struct nod *mknod(struct nod *nod, int loc);
int mkstr(struct nod *nod, char *str); int mkstr(struct nod *nod, char *str);
int gentree(struct nod *nod, char *strs); int gentree(struct nod *nod, char *strs, char *delim);
int streecmp(struct nod *nod, char *str); int streecmp(struct nod *nod, char *str);
#endif #endif

3
test.c
View File

@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
#include "streecmp.h" #include "streecmp.h"
// SRC: en.wikipedia.org/wiki/List_of_HTTP_header_fields
char *strs = "A-IM\n" char *strs = "A-IM\n"
"Accept\n" "Accept\n"
"Accept-Charset\n" "Accept-Charset\n"
@ -126,7 +127,7 @@ int main(void) {
return -1; return -1;
} }
ret = gentree(rot, strs); ret = gentree(rot, strs, NULL);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "[-] Failed: could not generate tree\n"); fprintf(stderr, "[-] Failed: could not generate tree\n");
return -1; return -1;