4 * Date: Fri Dec 17 18:40:53 2010
6 * GNU recutils - Flexible buffers.
10 /* Copyright (C) 2010-2019 Jose E. Marchesi */
12 /* This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #define REC_BUF_STEP 512
40 /* Pointers to user-provided variables that will be updated at
41 rec_buf_destroy time. */
51 rec_buf_new (char **data
, size_t *size
)
55 new = malloc (sizeof (struct rec_buf_s
));
58 new->data_pointer
= data
;
59 new->size_pointer
= size
;
61 new->data
= malloc (REC_BUF_STEP
);
62 new->size
= REC_BUF_STEP
;
76 rec_buf_close (rec_buf_t buf
)
78 /* Adjust the buffer. */
80 buf
->data
= realloc (buf
->data
, buf
->used
+ 1);
81 buf
->data
[buf
->used
] = '\0';
83 /* Update the user-provided buffer and size. */
84 *(buf
->data_pointer
) = buf
->data
;
85 *(buf
->size_pointer
) = buf
->used
;
87 /* Don't deallocate buf->data */
92 rec_buf_rewind (rec_buf_t buf
, int n
)
94 if ((buf
->used
- n
) >= 0)
95 buf
->used
= buf
->used
- n
;
99 rec_buf_putc (int c
, rec_buf_t buf
)
106 ret
= (unsigned int) c
;
107 if ((buf
->used
+ 1) > buf
->size
)
109 /* Allocate a new block */
110 buf
->size
= buf
->size
+ REC_BUF_STEP
;
111 buf
->data
= realloc (buf
->data
, buf
->size
);
114 /* Not enough memory.
115 REC_BUF_STEP should not be 0. */
120 /* Add the character */
121 buf
->data
[buf
->used
++] = (char) c
;
127 rec_buf_puts (const char *str
, rec_buf_t buf
)
136 if (rec_buf_putc (*p
, buf
) == EOF
)
150 /* End of rec-buf.c */