diff --git a/src/scanner/substring.c b/src/scanner/substring.c index badc2bb..65febf6 100644 --- a/src/scanner/substring.c +++ b/src/scanner/substring.c @@ -24,9 +24,24 @@ #include "substring.h" #include +#include void sbstrcpy(const struct substring *from, char *to) { strncpy(to, from->start, SUBSTRING_LENGTH(*from) - 1); to[SUBSTRING_LENGTH(*from) - 1] = '\0'; } + +char *sbstr2str(const struct substring *sbstr) +{ + if (SUBSTRING_LENGTH(*sbstr) > 1024) { + fprintf(stderr, "Max substring size of 1024 allowed.\ +Use sbstrcpy() instead.\n"); + return NULL; + } + + static char string[1024]; + sbstrcpy(sbstr, string); + + return string; +} diff --git a/src/scanner/substring.h b/src/scanner/substring.h index c497e5e..15fe094 100644 --- a/src/scanner/substring.h +++ b/src/scanner/substring.h @@ -31,4 +31,9 @@ struct substring { char *end; }; +/* write the contents of `from` to `to` + * `to` must have the size of SUBSTRING_LENGTH(from) */ extern void sbstrcpy(const struct substring *from, char *to); +/* return a manually allocated string of max size 1024 + * if the size exceeds the limit, retun null*/ +extern char *sbstr2str(const struct substring *sbstr);