Debian Codegraph @

doc/tutorial_src/stream.c

raw symbols SCIP
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 char *read_paragraph(int (*read)(void *), void *stream) {
6 int buffer_size = 0, length = 0;
7 char *buffer = NULL;
8 int ch;
9 while ((ch = (*read)(stream)) != EOF) {
10 if (++length > buffer_size) {
11 buffer_size += 100;
12 buffer = realloc(buffer, buffer_size + 1);
13 }
14 if ((buffer[length - 1] = ch) == '\n') {
15 buffer[--length] = '\0';
16 break;
17 }
18 buffer[length] = '\0';
19 }
20 return buffer;
21 }
22
23 void by_paragraph(int (*read)(void *), void *in, void (*write)(void *, char *), void *out) {
24 while (1) {
25 char *line = read_paragraph(read, in);
26 if ((line == NULL) || (strlen(line) == 0)) {
27 return;
28 }
29 (*write)(out, line);
30 free(line);
31 }
32 }