From c46eb7b3aadc74c20f32ea920b51625de061b7ff Mon Sep 17 00:00:00 2001 From: Kevin Jerebica Date: Tue, 27 Aug 2024 18:59:30 +0200 Subject: [PATCH] feat: add custom delimiter to gentree --- LEAKS | 28 ++++++++++++++-------------- Makefile | 2 +- README | 8 ++++++-- streecmp.c | 10 +++++++--- streecmp.h | 3 ++- test.c | 3 ++- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/LEAKS b/LEAKS index b34b439..1f740ec 100644 --- a/LEAKS +++ b/LEAKS @@ -1,8 +1,8 @@ -==6704== Memcheck, a memory error detector -==6704== 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 -==6704== Command: ./test -==6704== +==12180== Memcheck, a memory error detector +==12180== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. +==12180== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info +==12180== Command: ./test +==12180== ------------------------ [+][1/1] Success: could find 'A-IM' in tree [+][2/2] Success: could find 'Accept' in tree @@ -115,12 +115,12 @@ ------------------------ [+] Freed all memory in use [f] Finished -==6704== -==6704== HEAP SUMMARY: -==6704== in use at exit: 0 bytes in 0 blocks -==6704== total heap usage: 2,056 allocs, 2,056 frees, 2,206,614 bytes allocated -==6704== -==6704== All heap blocks were freed -- no leaks are possible -==6704== -==6704== For lists of detected and suppressed errors, rerun with: -s -==6704== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +==12180== +==12180== HEAP SUMMARY: +==12180== in use at exit: 0 bytes in 0 blocks +==12180== total heap usage: 2,056 allocs, 2,056 frees, 2,206,614 bytes allocated +==12180== +==12180== All heap blocks were freed -- no leaks are possible +==12180== +==12180== For lists of detected and suppressed errors, rerun with: -s +==12180== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) diff --git a/Makefile b/Makefile index 6d853f7..f304924 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ test: run: ./$(OUTPUT) -run_test: +runtest: ./$(TOUTPUT) memcheck: diff --git a/README b/README index a0569df..903a3f3 100644 --- a/README +++ b/README @@ -29,14 +29,18 @@ TECHNICALS * warning: string has to be null-byte terminated * returns: the assigned ID of the string, 0 if string 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 + * if delim is NULL, then "\n" is assumed * warning: seperation character is '\n' * returns: 0 if ok or -1 if function fails int streecmp(struct nod *nod, char *str) * traverse tree and search for str * 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 file contains the most recent valgrind memory leaks @@ -44,7 +48,7 @@ LEAKS TODO - * gentree with custom seperator + * everything done so far COMMITS diff --git a/streecmp.c b/streecmp.c index 8abed44..4a52dae 100644 --- a/streecmp.c +++ b/streecmp.c @@ -76,14 +76,18 @@ int mkstr(struct nod *nod, char *str) { return strs_cnt; } -int gentree(struct nod *nod, char *strs) { +int gentree(struct nod *nod, char *strs, char *delim) { char *strs_cpy = strdup(strs); if (strs_cpy == NULL) { goto _err; } - for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok - = strtok(NULL, "\n")) { + if (delim == NULL) { + delim = DEF_DELIM; + } + + for (char *tok = strtok(strs_cpy, delim); tok != NULL; tok + = strtok(NULL, delim)) { int ret = mkstr(nod, tok); if (ret < 0) { goto _err_mkstr; diff --git a/streecmp.h b/streecmp.h index ebe5aff..318df1c 100644 --- a/streecmp.h +++ b/streecmp.h @@ -2,6 +2,7 @@ #define STREECMP_H #define POOL_SIZE 256 +#define DEF_DELIM "\n" struct nodval { int ival; @@ -18,7 +19,7 @@ void frenod(struct nod *nod); struct nod *allocnod(void); struct nod *mknod(struct nod *nod, int loc); 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); #endif diff --git a/test.c b/test.c index d66ff3c..7a5fcfb 100644 --- a/test.c +++ b/test.c @@ -3,6 +3,7 @@ #include #include "streecmp.h" +// SRC: en.wikipedia.org/wiki/List_of_HTTP_header_fields char *strs = "A-IM\n" "Accept\n" "Accept-Charset\n" @@ -126,7 +127,7 @@ int main(void) { return -1; } - ret = gentree(rot, strs); + ret = gentree(rot, strs, NULL); if (ret < 0) { fprintf(stderr, "[-] Failed: could not generate tree\n"); return -1;