5 #include "line_buffer.h"
8 #define LINE_BUFFER_LEN 10000
9 #define COPY_BUFFER_LEN 4096
11 /* Create memory pool for char sequence of known length */
12 obj_pool_gen(blob
, char, 4096);
14 static char line_buffer
[LINE_BUFFER_LEN
];
15 static char byte_buffer
[COPY_BUFFER_LEN
];
16 static uint32_t line_buffer_len
= 0;
17 static uint32_t line_len
= 0;
19 char *buffer_read_line(void)
25 memmove(line_buffer
, &line_buffer
[line_len
],
26 line_buffer_len
- line_len
);
27 line_buffer_len
-= line_len
;
31 end
= memchr(line_buffer
, '\n', line_buffer_len
);
32 while (line_buffer_len
< LINE_BUFFER_LEN
- 1 &&
33 !feof(stdin
) && NULL
== end
) {
35 fread(&line_buffer
[line_buffer_len
], 1,
36 LINE_BUFFER_LEN
- 1 - line_buffer_len
,
38 end
= memchr(&line_buffer
[line_buffer_len
], '\n', n_read
);
39 line_buffer_len
+= n_read
;
46 line_len
= end
- line_buffer
;
47 line_buffer
[line_len
++] = '\0';
49 line_len
= line_buffer_len
;
50 line_buffer
[line_buffer_len
] = '\0';
59 char *buffer_read_string(uint32_t len
)
62 blob_free(blob_pool
.size
);
63 s
= blob_pointer(blob_alloc(len
+ 1));
65 if (line_buffer_len
> line_len
) {
66 offset
= line_buffer_len
- line_len
;
69 memcpy(s
, &line_buffer
[line_len
], offset
);
72 while (offset
< len
&& !feof(stdin
)) {
73 offset
+= fread(&s
[offset
], 1, len
- offset
, stdin
);
79 void buffer_copy_bytes(uint32_t len
)
82 if (line_buffer_len
> line_len
) {
83 in
= line_buffer_len
- line_len
;
87 while (out
< in
&& !ferror(stdout
)) {
89 fwrite(&line_buffer
[line_len
+ out
], 1, in
- out
, stdout
);
94 while (len
> 0 && !feof(stdin
)) {
95 in
= len
< COPY_BUFFER_LEN
? len
: COPY_BUFFER_LEN
;
96 in
= fread(byte_buffer
, 1, in
, stdin
);
99 while (out
< in
&& !ferror(stdout
)) {
100 out
+= fwrite(&byte_buffer
[out
], 1, in
- out
, stdout
);
105 void buffer_skip_bytes(uint32_t len
)
108 if (line_buffer_len
> line_len
) {
109 in
= line_buffer_len
- line_len
;
115 while (len
> 0 && !feof(stdin
)) {
116 in
= len
< COPY_BUFFER_LEN
? len
: COPY_BUFFER_LEN
;
117 in
= fread(byte_buffer
, 1, in
, stdin
);
122 void buffer_reset(void)