wmclockmon: update change-log
[dockapps.git] / wmclock / dynlist.c
blob350f8d0748a6d483d2873f21bcb0a4b143c47547
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
8 * Disclaimer:
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.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "dynlist.h"
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)
37 char **tmp_ptr;
39 if (*i >= *list_len)
41 *list_len += LIST_CHUNK_SIZE;
42 tmp_ptr = realloc(*list_ptr, (sizeof(**list_ptr) * *list_len));
43 if (NULL == tmp_ptr)
45 return(APPEND_FAILURE);
47 *list_ptr = tmp_ptr;
49 (*list_ptr)[*i] = item;
50 (*i)++;
51 return(APPEND_SUCCESS);
54 /* For single-dimensional buffers. */
55 int append_to_buf(char **buf, int *buflen, int *i, int c)
57 char *tmp_buf;
59 if (*i >= *buflen)
61 *buflen += BUF_CHUNK_SIZE;
62 tmp_buf = realloc(*buf, (sizeof(**buf) * *buflen));
63 if (NULL == tmp_buf)
65 return(APPEND_FAILURE);
67 *buf = tmp_buf;
68 #ifdef DEBUG
69 printf("-->Allocated buffer of size %d\n", *buflen);
70 #endif /* DEBUG */
72 (*buf)[*i] = (char) c;
73 (*i)++;
74 return(APPEND_SUCCESS);
77 int append_string_to_buf(char **buf, int *buflen, int *i, char *s)
79 int addlen;
80 int n;
81 char *tmp_buf;
83 n = strlen(s);
84 if (*i >= *buflen)
86 addlen = BUF_CHUNK_SIZE;
87 if (addlen <= n)
89 addlen = n + 1;
91 *buflen += addlen;
92 tmp_buf = realloc(*buf, (sizeof(**buf) * *buflen));
93 if (NULL == tmp_buf)
95 return(APPEND_FAILURE);
97 *buf = tmp_buf;
98 #ifdef DEBUG
99 printf("-->Allocated buffer of size %d\n", *buflen);
100 #endif /* DEBUG */
102 (*buf)[*i] = '\0';
103 strncat(*buf, s, n);
104 *i += n;
105 return(APPEND_SUCCESS);