diff --git a/spp.c b/spp.c index ec020b6..242c417 100644 --- a/spp.c +++ b/spp.c @@ -56,6 +56,38 @@ read_file_into_mem(const char* path) return buffer; } +static size_t +word_count(const struct SPP_Stream s, const char* path) +{ + size_t words = 0; + bool in_word = false; + + while (!spp_eof(s)) { + char c = **s.cursor; + + // Treat common ASCII whitespace as separators + bool is_ws = (c == ' ' || c == '\t' || c == '\n' || c == '\r' || + c == '\v' || c == '\f'); + + if (is_ws) { + in_word = false; + (*s.cursor)++; + } else { + if (!in_word) { + words++; + printf("%s - Word Count: %lld\r", path, words); + fflush(stdout); + in_word = true; + } + (*s.cursor)++; + } + } + + printf("%s - Word Count: %lld\n", path, words); + fflush(stdout); + return words; +} + static size_t line_count(const struct SPP_Stream s, const char* path) { @@ -145,7 +177,7 @@ main(int argc, char* argv[]) const char* buffer = read_file_into_mem(path); struct SPP_Stream stream = { .cursor = &buffer }; - line_count(stream, path); + word_count(stream, path); return EXIT_SUCCESS; } diff --git a/spp.h b/spp.h index c5d7257..d1ba52c 100644 --- a/spp.h +++ b/spp.h @@ -32,6 +32,13 @@ struct SPP_Stream }; typedef struct SPP_Stream SPP_Stream; +struct SPP_Token +{ + const char* buf; + size_t len; +}; +typedef struct SPP_Stream SPP_Stream; + extern bool spp_is(struct SPP_Stream s, const char* list);