commit a43d4f6085d2835ea43498efb97103d707ffb917 from: Sven M. Hallberg date: Sun Jul 02 15:15:02 2023 UTC move HBytes utilities to util.c commit - 74704bb3e842510813dc31075faaca6a63e0b3cc commit + a43d4f6085d2835ea43498efb97103d707ffb917 blob - 66493e88c70af44a834246b6da07d5cf49058adf blob + b2a9a620efdbeb142791a1171d04774ff927e407 --- Makefile +++ Makefile @@ -9,7 +9,7 @@ HAMMER_LIB = ./lib CFLAGS += -I$(HAMMER_INCLUDE) LDFLAGS += -L$(HAMMER_LIB) -OBJECTS = pdf.o lzw.o content.o +OBJECTS = pdf.o lzw.o content.o util.o TARGETS = pdf DOCS = pdf.1.txt blob - f3abe3000fd3bb2479d74fab5e1abd2a3f4edf6f blob + 27707d06f12b1487d50e3ca48394135218c47c01 --- content.c +++ content.c @@ -12,6 +12,7 @@ #include #include "content.h" #include "pdf.h" +#include "util.h" HTokenType TT_ContentOp; @@ -71,20 +72,6 @@ close: fprintf(stream, " }"); } -// XXX put bstrdup (together with bytes_eq, bytescmp) elsewhere? -/* duplicate an HBytes into a null-terminated string (arena-allocated) */ -char * -bstrdup(HArena *arena, HBytes b) -{ - char *p; - - p = h_arena_malloc(arena, b.len + 1); - memcpy(p, b.token, b.len); - p[b.len] = '\0'; - - return p; -} - /* * semantic actions */ blob - cb6715c4f88056ae66a14e7738ec7fc821542de1 blob + 7f433e5c324618bb2936317a7524e41dbdd533d6 --- pdf.c +++ pdf.c @@ -1,12 +1,13 @@ /* beginnings of a PDF parser in hammer */ -#include /* strncmp(), memset(), memcpy() */ +#include /* memcpy() */ #include /* exit() */ #include #include #include #include "pdf.h" #include "content.h" +#include "util.h" #ifdef LOG #define VIOL(P,VIOL) h_action(h_sequence(P, h_tell(), NULL), act_viol, VIOL) @@ -102,31 +103,7 @@ p_sepBy_n__m(HAllocator *mm__, HParser *p, HParser *se return h_action__m(mm__, seq, h_act_flatten, NULL); } -/* a helper to compare an HBytes to a string */ bool -bytes_eq(HBytes b, const char *s) -{ - return strncmp(s, (const char *)(b.token), b.len) == 0 && b.len == strlen(s); -} - -/* compare two HBytes like strcmp(3) */ -int -bytescmp(HBytes b1, HBytes b2) -{ - size_t lmin = b1.len < b2.len ? b1.len : b2.len; - int d; - - if ((d = memcmp(b1.token, b2.token, lmin)) != 0) - return d; /* strings distinguished by prefix */ - else if (b1.len < b2.len) - return -1; /* b1 is prefix of b2 ("b1 < b2") */ - else if (b1.len > b2.len) - return 1; /* b2 is prefix of b1 ("b1 > b2") */ - else - return 0; /* strings are equal */ -} - -bool validate_eq_uint(HParseResult *p, void *u) { const HParsedToken *v = p->ast; blob - /dev/null blob + 6936a103cbedf6bd7f12eff3bc67d57595458ad6 (mode 644) --- /dev/null +++ util.c @@ -0,0 +1,40 @@ +#include /* strncmp(), memcpy() */ +#include "util.h" + +/* a helper to compare an HBytes to a string */ +bool +bytes_eq(HBytes b, const char *s) +{ + return strncmp(s, (const char *)b.token, b.len) == 0 && + b.len == strlen(s); +} + +/* compare two HBytes like strcmp(3) */ +int +bytescmp(HBytes b1, HBytes b2) +{ + size_t lmin = b1.len < b2.len ? b1.len : b2.len; + int d; + + if ((d = memcmp(b1.token, b2.token, lmin)) != 0) + return d; /* strings distinguished by prefix */ + else if (b1.len < b2.len) + return -1; /* b1 is prefix of b2 ("b1 < b2") */ + else if (b1.len > b2.len) + return 1; /* b2 is prefix of b1 ("b1 > b2") */ + else + return 0; /* strings are equal */ +} + +/* duplicate an HBytes into a null-terminated string (arena-allocated) */ +char * +bstrdup(HArena *arena, HBytes b) +{ + char *p; + + p = h_arena_malloc(arena, b.len + 1); + memcpy(p, b.token, b.len); + p[b.len] = '\0'; + + return p; +} blob - /dev/null blob + 7e6dc375f4043f85ad90e4f1b0477206ad7eaaba (mode 644) --- /dev/null +++ util.h @@ -0,0 +1,10 @@ +#ifndef SCPDF_UTIL_H +#define SCPDF_UTIL_H + +#include + +bool bytes_eq(HBytes, const char *); +int bytescmp(HBytes, HBytes); +char *bstrdup(HArena *, HBytes); + +#endif /* SCPDF_UTIL_H */