18db30ca8Sthorpej/* dynlist.c: Dynamic lists and buffers in C.
28db30ca8Sthorpej * created 1999-Jan-06 15:34 jmk
3c056561aSmbalmer * autodate: 2000-Aug-28 01:29
48db30ca8Sthorpej *
5c056561aSmbalmer * by Jim Knoble <jmknoble@jmknoble.cx>
6c056561aSmbalmer * Copyright (C) 1999,2000 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	{
44c056561aSmbalmer	   free(*list_ptr);
45c056561aSmbalmer	   *list_ptr = NULL;
468db30ca8Sthorpej	   return(APPEND_FAILURE);
47c056561aSmbalmer	} else {
48c056561aSmbalmer	   *list_ptr = tmp_ptr;
498db30ca8Sthorpej	}
508db30ca8Sthorpej       *list_ptr = tmp_ptr;
518db30ca8Sthorpej    }
528db30ca8Sthorpej   (*list_ptr)[*i] = item;
538db30ca8Sthorpej   (*i)++;
548db30ca8Sthorpej   return(APPEND_SUCCESS);
558db30ca8Sthorpej}
568db30ca8Sthorpej
578db30ca8Sthorpej/* For single-dimensional buffers. */
588db30ca8Sthorpejint append_to_buf(char **buf, int *buflen, int *i, int c)
598db30ca8Sthorpej{
608db30ca8Sthorpej   char *tmp_buf;
618db30ca8Sthorpej
628db30ca8Sthorpej   if (*i >= *buflen)
638db30ca8Sthorpej    {
648db30ca8Sthorpej       *buflen += BUF_CHUNK_SIZE;
658db30ca8Sthorpej       tmp_buf = realloc(*buf, (sizeof(**buf) * *buflen));
668db30ca8Sthorpej       if (NULL == tmp_buf)
678db30ca8Sthorpej	{
68c056561aSmbalmer	   free(*buf);
69c056561aSmbalmer	   *buf = NULL;
708db30ca8Sthorpej	   return(APPEND_FAILURE);
71c056561aSmbalmer	} else {
72c056561aSmbalmer	   *buf = tmp_buf;
738db30ca8Sthorpej	}
748db30ca8Sthorpej       *buf = tmp_buf;
758db30ca8Sthorpej#ifdef DEBUG
768db30ca8Sthorpej       printf("-->Allocated buffer of size %d\n", *buflen);
778db30ca8Sthorpej#endif /* DEBUG */
788db30ca8Sthorpej    }
798db30ca8Sthorpej   (*buf)[*i] = (char) c;
808db30ca8Sthorpej   (*i)++;
818db30ca8Sthorpej   return(APPEND_SUCCESS);
828db30ca8Sthorpej}
83