File: dynlist.c

package info (click to toggle)
wmclock 1.0.12.2-2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 348 kB
  • ctags: 209
  • sloc: ansic: 1,435; sh: 131; makefile: 40
file content (107 lines) | stat: -rw-r--r-- 2,665 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* dynlist.c: Dynamic lists and buffers in C.
 * created 1999-Jan-06 15:34 jmk
 * autodate: 2000-Mar-08 02:25
 * 
 * by Jim Knoble <jmknoble@pobox.com>
 * Copyright  1999 Jim Knoble
 * 
 * Disclaimer:
 * 
 * The software is provided "as is", without warranty of any kind,
 * express or implied, including but not limited to the warranties of
 * merchantability, fitness for a particular purpose and
 * noninfringement. In no event shall the author(s) be liable for any
 * claim, damages or other liability, whether in an action of
 * contract, tort or otherwise, arising from, out of or in connection
 * with the software or the use or other dealings in the software.
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dynlist.h"

#define LIST_CHUNK_SIZE		512
#define BUF_CHUNK_SIZE		512

/* For lists of pointers cast to char *. */
int append_to_list(char ***list_ptr, int *list_len, int *i, char *item)
{
   char **tmp_ptr;
   
   if (*i >= *list_len)
    {
       *list_len += LIST_CHUNK_SIZE;
       tmp_ptr = realloc(*list_ptr, (sizeof(**list_ptr) * *list_len));
       if (NULL == tmp_ptr)
	{
	   return(APPEND_FAILURE);
	}
       *list_ptr = tmp_ptr;
    }
   (*list_ptr)[*i] = item;
   (*i)++;
   return(APPEND_SUCCESS);
}

/* For single-dimensional buffers. */
int append_to_buf(char **buf, int *buflen, int *i, int c)
{
   char *tmp_buf;
   
   if (*i >= *buflen)
    {
       *buflen += BUF_CHUNK_SIZE;
       tmp_buf = realloc(*buf, (sizeof(**buf) * *buflen));
       if (NULL == tmp_buf)
	{
	   return(APPEND_FAILURE);
	}
       *buf = tmp_buf;
#ifdef DEBUG
       printf("-->Allocated buffer of size %d\n", *buflen);
#endif /* DEBUG */
    }
   (*buf)[*i] = (char) c;
   (*i)++;
   return(APPEND_SUCCESS);
}

int append_string_to_buf(char **buf, int *buflen, int *i, char *s)
{
   int addlen;
   int n;
   char *tmp_buf;
   
   n = strlen(s);
   if (*i >= *buflen)
    {
       addlen = BUF_CHUNK_SIZE;
       if (addlen <= n)
	{
	   addlen = n + 1;
	}
       *buflen += addlen;
       tmp_buf = realloc(*buf, (sizeof(**buf) * *buflen));
       if (NULL == tmp_buf)
	{
	   return(APPEND_FAILURE);
	}
       *buf = tmp_buf;
#ifdef DEBUG
       printf("-->Allocated buffer of size %d\n", *buflen);
#endif /* DEBUG */
    }
   (*buf)[*i] = '\0';
   strncat(*buf, s, n);
   *i += n;
   return(APPEND_SUCCESS);
}