dynlist.c revision 8db30ca8
18db30ca8Sthorpej/* dynlist.c: Dynamic lists and buffers in C.
28db30ca8Sthorpej * created 1999-Jan-06 15:34 jmk
38db30ca8Sthorpej * autodate: 1999-Dec-24 00:12
48db30ca8Sthorpej *
58db30ca8Sthorpej * by Jim Knoble <jmknoble@pobox.com>
68db30ca8Sthorpej * Copyright � 1999 Jim Knoble
78db30ca8Sthorpej *
88db30ca8Sthorpej * Disclaimer:
98db30ca8Sthorpej *
108db30ca8Sthorpej * The software is provided "as is", without warranty of any kind,
118db30ca8Sthorpej * express or implied, including but not limited to the warranties of
128db30ca8Sthorpej * merchantability, fitness for a particular purpose and
138db30ca8Sthorpej * noninfringement. In no event shall the author(s) be liable for any
148db30ca8Sthorpej * claim, damages or other liability, whether in an action of
158db30ca8Sthorpej * contract, tort or otherwise, arising from, out of or in connection
168db30ca8Sthorpej * with the software or the use or other dealings in the software.
178db30ca8Sthorpej *
188db30ca8Sthorpej * Permission to use, copy, modify, distribute, and sell this software
198db30ca8Sthorpej * and its documentation for any purpose is hereby granted without
208db30ca8Sthorpej * fee, provided that the above copyright notice appear in all copies
218db30ca8Sthorpej * and that both that copyright notice and this permission notice
228db30ca8Sthorpej * appear in supporting documentation.
238db30ca8Sthorpej */
248db30ca8Sthorpej
258db30ca8Sthorpej#include <stdio.h>
268db30ca8Sthorpej#include <stdlib.h>
278db30ca8Sthorpej
288db30ca8Sthorpej#include "dynlist.h"
298db30ca8Sthorpej
308db30ca8Sthorpej#define LIST_CHUNK_SIZE		512
318db30ca8Sthorpej#define BUF_CHUNK_SIZE		512
328db30ca8Sthorpej
338db30ca8Sthorpej/* For lists of pointers cast to char *. */
348db30ca8Sthorpejint append_to_list(char ***list_ptr, int *list_len, int *i, char *item)
358db30ca8Sthorpej{
368db30ca8Sthorpej   char **tmp_ptr;
378db30ca8Sthorpej
388db30ca8Sthorpej   if (*i >= *list_len)
398db30ca8Sthorpej    {
408db30ca8Sthorpej       *list_len += LIST_CHUNK_SIZE;
418db30ca8Sthorpej       tmp_ptr = realloc(*list_ptr, (sizeof(**list_ptr) * *list_len));
428db30ca8Sthorpej       if (NULL == tmp_ptr)
438db30ca8Sthorpej	{
448db30ca8Sthorpej	   return(APPEND_FAILURE);
458db30ca8Sthorpej	}
468db30ca8Sthorpej       *list_ptr = tmp_ptr;
478db30ca8Sthorpej    }
488db30ca8Sthorpej   (*list_ptr)[*i] = item;
498db30ca8Sthorpej   (*i)++;
508db30ca8Sthorpej   return(APPEND_SUCCESS);
518db30ca8Sthorpej}
528db30ca8Sthorpej
538db30ca8Sthorpej/* For single-dimensional buffers. */
548db30ca8Sthorpejint append_to_buf(char **buf, int *buflen, int *i, int c)
558db30ca8Sthorpej{
568db30ca8Sthorpej   char *tmp_buf;
578db30ca8Sthorpej
588db30ca8Sthorpej   if (*i >= *buflen)
598db30ca8Sthorpej    {
608db30ca8Sthorpej       *buflen += BUF_CHUNK_SIZE;
618db30ca8Sthorpej       tmp_buf = realloc(*buf, (sizeof(**buf) * *buflen));
628db30ca8Sthorpej       if (NULL == tmp_buf)
638db30ca8Sthorpej	{
648db30ca8Sthorpej	   return(APPEND_FAILURE);
658db30ca8Sthorpej	}
668db30ca8Sthorpej       *buf = tmp_buf;
678db30ca8Sthorpej#ifdef DEBUG
688db30ca8Sthorpej       printf("-->Allocated buffer of size %d\n", *buflen);
698db30ca8Sthorpej#endif /* DEBUG */
708db30ca8Sthorpej    }
718db30ca8Sthorpej   (*buf)[*i] = (char) c;
728db30ca8Sthorpej   (*i)++;
738db30ca8Sthorpej   return(APPEND_SUCCESS);
748db30ca8Sthorpej}
758db30ca8Sthorpej
76