diff options
| author | 2026-11-10 09:28:03 +0000 | |
|---|---|---|
| committer | 2026-11-10 09:28:03 +0000 | |
| commit | f3c478bc0ce492e9ebc516f445312dfd9e6a2a7a (patch) | |
| tree | dfabf11ce902f157508fce9e4db2316c00a247da /src/buffer.h | |
| download | lab-04-master.tar.gz | |
Diffstat (limited to 'src/buffer.h')
| -rw-r--r-- | src/buffer.h | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/buffer.h b/src/buffer.h new file mode 100644 index 0000000..7fc2939 --- /dev/null +++ b/src/buffer.h | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | #ifndef INCLUDED_BUFFER_H | ||
| 2 | #define INCLUDED_BUFFER_H | ||
| 3 | |||
| 4 | #define BUFFER_SIZE 4096 | ||
| 5 | #define BUFFER_END(BUFFER) ((BUFFER).data + BUFFER_SIZE) | ||
| 6 | #define BUFFER_SPACE_WRITE(BUFFER) (BUFFER_SIZE - ((BUFFER).head_write - (BUFFER).data)) | ||
| 7 | |||
| 8 | struct buffer { | ||
| 9 | char *head_read; | ||
| 10 | char *head_write; | ||
| 11 | char data[BUFFER_SIZE]; | ||
| 12 | }; | ||
| 13 | |||
| 14 | void | ||
| 15 | buffer__align (struct buffer *buf) | ||
| 16 | { | ||
| 17 | if (buf->head_read == buf->data) | ||
| 18 | return; | ||
| 19 | |||
| 20 | size_t size = buf->head_write - buf->head_read; | ||
| 21 | memmove (buf->data, buf->head_read, size); | ||
| 22 | buf->head_read = buf->data; | ||
| 23 | buf->head_write = buf->data + size; | ||
| 24 | } | ||
| 25 | |||
| 26 | int | ||
| 27 | buffer__peek (struct buffer *buf, int *fd) | ||
| 28 | { | ||
| 29 | if (buf->head_read < buf->head_write) | ||
| 30 | return *buf->head_read; | ||
| 31 | |||
| 32 | buf->head_read = buf->data; | ||
| 33 | buf->head_write = buf->data; | ||
| 34 | |||
| 35 | ssize_t bytes_read = read (*fd, buf->head_write, BUFFER_SPACE_WRITE (*buf)); | ||
| 36 | if (bytes_read <= 0) { | ||
| 37 | *fd = -1; | ||
| 38 | return EOF; | ||
| 39 | } | ||
| 40 | buf->head_write += bytes_read; | ||
| 41 | |||
| 42 | return *buf->head_read; | ||
| 43 | } | ||
| 44 | |||
| 45 | int | ||
| 46 | buffer__consume_one (struct buffer *buf, int *fd) | ||
| 47 | { | ||
| 48 | int c = buffer__peek (buf, fd); | ||
| 49 | if (c == EOF) return EOF; | ||
| 50 | buf->head_read += 1; | ||
| 51 | return c; | ||
| 52 | } | ||
| 53 | |||
| 54 | char * | ||
| 55 | buffer__consume_until (struct buffer *buf, int *fd, char c) | ||
| 56 | { | ||
| 57 | char *ret = buf->head_read; | ||
| 58 | for (;;) { | ||
| 59 | int r = buffer__consume_one (buf, fd); | ||
| 60 | if (r == EOF) return NULL; | ||
| 61 | if (r == c ) return ret; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | char * | ||
| 66 | buffer__consume_until_one_of (struct buffer *buf, int *fd, char *cs) | ||
| 67 | { | ||
| 68 | char *ret = buf->head_read; | ||
| 69 | for (;;) { | ||
| 70 | int r = buffer__consume_one (buf, fd); | ||
| 71 | if (r == EOF) return NULL; | ||
| 72 | if (strchr (cs, r)) return ret; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | char * | ||
| 77 | buffer__consume_while_one_of (struct buffer *buf, int *fd, char *cs) | ||
| 78 | { | ||
| 79 | char *ret = buf->head_read; | ||
| 80 | for (;;) { | ||
| 81 | int r = buffer__peek (buf, fd); | ||
| 82 | if (r == EOF) return NULL; | ||
| 83 | if (!strchr (cs, r)) return ret; | ||
| 84 | buffer__consume_one (buf, fd); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | bool | ||
| 89 | buffer__skip_n (struct buffer *buf, int *fd, ssize_t n) | ||
| 90 | { | ||
| 91 | buffer__align (buf); | ||
| 92 | while (n > 0) { | ||
| 93 | ssize_t bytes_read = read (*fd, buf->head_write, | ||
| 94 | n > BUFFER_SPACE_WRITE (*buf) | ||
| 95 | ? BUFFER_SPACE_WRITE (*buf) : n); | ||
| 96 | if (bytes_read <= 0) { | ||
| 97 | *fd = -1; | ||
| 98 | return false; | ||
| 99 | } | ||
| 100 | n -= bytes_read; | ||
| 101 | } | ||
| 102 | return true; | ||
| 103 | } | ||
| 104 | |||
| 105 | #endif // INCLUDED_BUFFER_H | ||
