1 /* dynlist.c: Dynamic lists and buffers in C.
2 * created 1999-Jan-06 15:34 jmk
3 * autodate: 2000-Mar-08 02:25
5 * by Jim Knoble <jmknoble@pobox.com>
6 * Copyright (C) 1999 Jim Knoble
10 * The software is provided "as is", without warranty of any kind,
11 * express or implied, including but not limited to the warranties of
12 * merchantability, fitness for a particular purpose and
13 * noninfringement. In no event shall the author(s) be liable for any
14 * claim, damages or other liability, whether in an action of
15 * contract, tort or otherwise, arising from, out of or in connection
16 * with the software or the use or other dealings in the software.
18 * Permission to use, copy, modify, distribute, and sell this software
19 * and its documentation for any purpose is hereby granted without
20 * fee, provided that the above copyright notice appear in all copies
21 * and that both that copyright notice and this permission notice
22 * appear in supporting documentation.
31 #define LIST_CHUNK_SIZE 512
32 #define BUF_CHUNK_SIZE 512
34 /* For lists of pointers cast to char *. */
35 int append_to_list(char ***list_ptr
, int *list_len
, int *i
, char *item
)
41 *list_len
+= LIST_CHUNK_SIZE
;
42 tmp_ptr
= realloc(*list_ptr
, (sizeof(**list_ptr
) * *list_len
));
45 return(APPEND_FAILURE
);
49 (*list_ptr
)[*i
] = item
;
51 return(APPEND_SUCCESS
);
54 /* For single-dimensional buffers. */
55 int append_to_buf(char **buf
, int *buflen
, int *i
, int c
)
61 *buflen
+= BUF_CHUNK_SIZE
;
62 tmp_buf
= realloc(*buf
, (sizeof(**buf
) * *buflen
));
65 return(APPEND_FAILURE
);
69 printf("-->Allocated buffer of size %d\n", *buflen
);
72 (*buf
)[*i
] = (char) c
;
74 return(APPEND_SUCCESS
);
77 int append_string_to_buf(char **buf
, int *buflen
, int *i
, char *s
)
86 addlen
= BUF_CHUNK_SIZE
;
92 tmp_buf
= realloc(*buf
, (sizeof(**buf
) * *buflen
));
95 return(APPEND_FAILURE
);
99 printf("-->Allocated buffer of size %d\n", *buflen
);
105 return(APPEND_SUCCESS
);