/* %Z%%M% %I% %E% */

/*
 * Copyright (c) 1990, 1991, 1994, 1995 by Wayne C. Gramlich.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * for any purpose is hereby granted without fee provided that the above
 * copyright notice and this permission are retained.  The author makes
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/* An implementation of the dynamic string length routines: */

#ifndef HEAP_EXPORTS_H
#include "heap_exports.h"
#endif

#ifndef STR_EXPORTS_H
#include "str_exports.h"
#endif

#ifndef STRVEC_EXPORTS_H
#include "strvec_exports.h"
#endif

#ifndef UNIX_MEMORY_H
#include "unix_memory.h"
#endif

#ifndef UNIX_STDARG_H
#include "unix_stdarg.h"
#endif

#ifndef WBUF_EXPORTS_H
#include "wbuf_exports.h"
#endif

/* LINTLIBRARY */

/*
 * strvec_chr_append(strvec, chr)
 *	This routine will append "chr" to the end of "strvec".
 */
void
strvec_chr_append(
	Strvec		strvec,
	char		chr)
{
	wbuf_chr((Wbuf)strvec, chr);
}
/*
 * strvec_close(wbuf, handle)
 *	This routine will be called if somehow the user invokes wbuf_close()
 *	on a string vector.
 */
/* ARGSUSED */
void
strvec_close(
	Wbuf		wbuf,
	Heap		heap)
{
	return;
}

/*
 * strvec_create(heap)
 *	This routine will create and return a new Strvec allocated using
 *	"heap".
 */
Strvec
strvec_create(
	Heap		heap)
{
	return wbuf_create((Str)heap_alloc(heap, 4), 3, strvec_close,
			   strvec_flush, (Pointer)heap, heap);
}

/*
 * strvec_destroy(strvec)
 *	This routine will release all storage associated with "strvec".
 */
void
strvec_destroy(
	Strvec		strvec)
{
	wbuf_close(strvec);
}

/*
 * strvec_erase(strvec)
 *	This routine will erase the contents of "strvec".
 */
void
strvec_erase(
	Strvec		strvec)
{
	wbuf_offset_set(strvec, 0);
}

/*
 * strvec_flush(buffer, size, wbuf, heap)
 *	This routine will double the size of the buffer in "wbuf".
 */
void
strvec_flush(
	Str		buffer,
	int		size,
	Wbuf		wbuf,
	Heap		heap)
{
	int		new_size;

	new_size = size + size + 1;
	buffer = (Str)heap_realloc(heap, (Pointer)buffer, new_size);
	wbuf_buffer_set(wbuf, buffer, new_size);
	wbuf_offset_set(wbuf, size);
}

/*
 * strvec_print(strvec, format, arg1, ..., argN)
 *	This routine will print "arg1" through "argN" onto the end of "strvec"
 *	using "format".
 */
void
strvec_print(
	Strvec		strvec,
	Str		format,
	...)
{
	va_list		args;

	va_start(args, format);
	wbuf_print_varargs(strvec, format, args);
	va_end(args);
}

/*
 * strvec_print_varargs(strvec, format, arg1, ..., argN)
 *	This routine will print "arg1" through "argN" into "strvec" using
 *	"format".
 */
void
strvec_print_varargs(
	Strvec		strvec,
	Str		format,
	va_list		args)
{
	wbuf_print_varargs(strvec, format, args);
}

/*
 * strvec_size(strvec)
 *	This routine will return the number of characters in "strvec".
 */
int
strvec_size(
	Strvec		strvec)
{
	return wbuf_offset_get(strvec);
}

/*
 * strvec_str_append(strvec, str)
 *	This routine will append "str" onto the "strvec".
 */
void
strvec_str_append(
	Strvec		strvec,
	Str		str)
{
	wbuf_str(strvec, str);
}

/*
 * strvec_str_get(strvec, heap)
 *	This routine will allocate and return new string allocated from "heap"
 *	containing the value of Strvec as a null terminated string.
 */
Str
strvec_str_get(
	Strvec		strvec,
	Heap		heap)
{
	Str		buffer;
	int		size;
	Str		str;
	
	buffer = wbuf_buffer_get(strvec);
	size = wbuf_offset_get(strvec);
	str = (Str)heap_alloc(heap, size + 1);
	(void)memcpy(str, buffer, size);
	str[size] = '\0';
	return str;
}

