#ifndef BUF_H #define BUF_H #include #include /* * Growable byte buffer (Milestone 2). Holds arbitrary bytes; buf_cstr() always * returns a NUL-terminated view so the buffer can be used as a string. * Opaque: callers only see buf_t *. */ typedef struct buf buf_t; buf_t *buf_new(void); /* empty buffer; NULL on OOM */ int buf_append(buf_t *b, const char *s, size_t n); /* append n bytes; 0 ok, -1 OOM */ int buf_appendc(buf_t *b, char c); /* append one byte; 0 ok, -1 OOM */ const char *buf_cstr(const buf_t *b); /* NUL-terminated view; "" if empty */ size_t buf_len(const buf_t *b); /* bytes currently held */ void buf_reset(buf_t *b); /* drop contents, keep allocation */ void buf_free(buf_t *b); /* free buffer + storage */ /* * Read one full line (including the trailing newline) from f into b. * Returns the line length (>= 0), -1 at EOF with nothing read, or -2 on error. */ long buf_readline(buf_t *b, FILE *f); #endif /* BUF_H */