Compare commits

..

No commits in common. "c46eb7b3aadc74c20f32ea920b51625de061b7ff" and "152d119601beae4891a19202f6bdb9f0f0220dca" have entirely different histories.

6 changed files with 26 additions and 44 deletions

28
LEAKS
View File

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

View File

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

24
README
View File

@ -13,34 +13,22 @@ TECHNICALS
struct nod struct nod
* data structure that represents a node of the tree * data structure that represents a node of the tree
void frenod(struct nod *nod) struct nod *mknod(struct nod *prnt)
* free memory of node and all of its children * create a child node below given parent node
struct nod *allocnod(void)
* allocate memory for a node and its pool size for
character hashmap
* returns: poiner to said node or NULL if function fails
struct nod *mknod(struct nod *nod, int loc)
* create a child node below given parent node
* loc is the character of the node (consult source code
of mkstr for more information)
* returns: pointer to said node or NULL if function fails * returns: pointer to said node or NULL if function fails
int mkstr(struct nod *nod, 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, 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, char *delim) int gentree(struct nod *rot, char *strs)
* 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 *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
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
@ -48,7 +36,7 @@ LEAKS
TODO TODO
* everything done so far * gentree with custom seperator
COMMITS COMMITS

View File

@ -76,18 +76,14 @@ int mkstr(struct nod *nod, char *str) {
return strs_cnt; return strs_cnt;
} }
int gentree(struct nod *nod, char *strs, char *delim) { int gentree(struct nod *nod, char *strs) {
char *strs_cpy = strdup(strs); char *strs_cpy = strdup(strs);
if (strs_cpy == NULL) { if (strs_cpy == NULL) {
goto _err; goto _err;
} }
if (delim == NULL) { for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok
delim = DEF_DELIM; = strtok(NULL, "\n")) {
}
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,7 +2,6 @@
#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;
@ -19,7 +18,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, char *delim); int gentree(struct nod *nod, char *strs);
int streecmp(struct nod *nod, char *str); int streecmp(struct nod *nod, char *str);
#endif #endif

3
test.c
View File

@ -3,7 +3,6 @@
#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"
@ -127,7 +126,7 @@ int main(void) {
return -1; return -1;
} }
ret = gentree(rot, strs, NULL); ret = gentree(rot, strs);
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;