/*
 * Copyright (c)2004 Cat's Eye Technologies.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *   Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 *   Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 * 
 *   Neither the name of Cat's Eye Technologies nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

/*
 * strpkg.c
 * $Id: strpkg.c,v 1.2 2004/07/10 01:06:43 cpressey Exp $
 */

#include <stdlib.h>

#include "extbuf.h"
#include "encoding.h"
#include "mem.h"
#include "dump.h"

struct dfui_strpkg *
dfui_strpkg_new_dynamic(size_t size)
{
	struct dfui_strpkg *p;

	DFUI_MALLOC(p, dfui_strpkg);

	p->sbuf = NULL;
	if ((p->ebuf = extbuf_new(size)) == NULL) {
		free(p);
		return(NULL);
	}
	p->size = size;
	p->pos = 0;

	p->is_static = 0;

	return(p);
}

/*
 * buf should already be allocated when passed here.
 */
struct dfui_strpkg *
dfui_strpkg_new_static(char *buf, size_t size)
{
	struct dfui_strpkg *p;

	DFUI_MALLOC(p, dfui_strpkg);
	p->sbuf = buf;
	p->ebuf = NULL;
	p->size = size;
	p->pos = 0;

	p->is_static = 1;

	return(p);
}

void
dfui_strpkg_free(struct dfui_strpkg *p)
{
	if (!p->is_static) {
		if (p->ebuf != NULL)
			extbuf_free(p->ebuf);
	}
	DFUI_FREE(p, dfui_strpkg);
}

char *
dfui_strpkg_buf(struct dfui_strpkg *p)
{
	if (p->is_static) {
		return(p->sbuf);
	} else {
		return(extbuf_buf(p->ebuf));
	}
}

/** Dynamic strpkg Functions **/

int
dfui_strpkg_write(struct dfui_strpkg *p, char *str)
{
	if (p->is_static)
		return(0);
	extbuf_cat(p->ebuf, str);
	return(1);
}

/** Static strpkg Functions **/

int
dfui_strpkg_at_end(struct dfui_strpkg *p)
{
	if (p->pos >= p->size) {
		return(1);
	} else {
		return(0);
	}
}

char
dfui_strpkg_char(struct dfui_strpkg *p)
{
	if (!p->is_static)
		return(0);
	return(p->sbuf[p->pos]);
}

char
dfui_strpkg_char_advance(struct dfui_strpkg *p)
{
	if (!p->is_static)
		return(0);
	return(p->sbuf[p->pos++]);
}

int
dfui_strpkg_compare(struct dfui_strpkg *p, char *str)
{
	int i, pos;

	if (!p->is_static)
		return(0);

	for (i = 0, pos = p->pos; str[i] != '\0' && pos < p->size; i++, pos++) {
		if (p->sbuf[pos] != str[i])
			return(0);
	}

	if (pos <= p->size) {
		return(pos);
	} else {
		return(0);
	}
}

int
dfui_strpkg_expect(struct dfui_strpkg *p, char *str)
{
	int pos;

	if (!p->is_static)
		return(0);
	pos = dfui_strpkg_compare(p, str);
	if (pos > 0) {
		p->pos = pos;
		return(1);
	} else {
		return(0);
	}
}
