Blame dc/array.c

Packit 70b277
/*
Packit 70b277
 * implement arrays for dc
Packit 70b277
 *
Packit 70b277
 * Copyright (C) 1994, 1997, 1998, 2000, 2006, 2008
Packit 70b277
 * Free Software Foundation, Inc.
Packit 70b277
 *
Packit 70b277
 * This program is free software; you can redistribute it and/or modify
Packit 70b277
 * it under the terms of the GNU General Public License as published by
Packit 70b277
 * the Free Software Foundation; either version 3, or (at your option)
Packit 70b277
 * any later version.
Packit 70b277
 *
Packit 70b277
 * This program is distributed in the hope that it will be useful,
Packit 70b277
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 70b277
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 70b277
 * GNU General Public License for more details.
Packit 70b277
 *
Packit 70b277
 * You should have received a copy of the GNU General Public License
Packit 70b277
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 70b277
 *
Packit 70b277
 */
Packit 70b277
Packit 70b277
/* This module is the only one that knows what arrays look like. */
Packit 70b277
Packit 70b277
#include "config.h"
Packit 70b277
Packit 70b277
#include <stdio.h>	/* "dc-proto.h" wants this */
Packit 70b277
#ifdef HAVE_STDLIB_H
Packit 70b277
/* get size_t definition from "almost ANSI" compiling environments. */
Packit 70b277
#include <stdlib.h>
Packit 70b277
#endif
Packit 70b277
#include "dc.h"
Packit 70b277
#include "dc-proto.h"
Packit 70b277
#include "dc-regdef.h"
Packit 70b277
Packit 70b277
/* what's most useful: quick access or sparse arrays? */
Packit 70b277
/* I'll go with sparse arrays for now */
Packit 70b277
struct dc_array {
Packit 70b277
	int Index;
Packit 70b277
	dc_data value;
Packit 70b277
	struct dc_array *next;
Packit 70b277
};
Packit 70b277
Packit 70b277
Packit 70b277
/* initialize the arrays */
Packit 70b277
void
Packit 70b277
dc_array_init DC_DECLVOID()
Packit 70b277
{
Packit 70b277
}
Packit 70b277
Packit 70b277
/* store value into array_id[Index] */
Packit 70b277
void
Packit 70b277
dc_array_set DC_DECLARG((array_id, Index, value))
Packit 70b277
	int array_id DC_DECLSEP
Packit 70b277
	int Index DC_DECLSEP
Packit 70b277
	dc_data value DC_DECLEND
Packit 70b277
{
Packit 70b277
	struct dc_array *cur;
Packit 70b277
	struct dc_array *prev = NULL;
Packit 70b277
Packit 70b277
	cur = dc_get_stacked_array(array_id);
Packit 70b277
	while (cur != NULL  &&  cur->Index < Index){
Packit 70b277
		prev = cur;
Packit 70b277
		cur = cur->next;
Packit 70b277
	}
Packit 70b277
	if (cur != NULL  &&  cur->Index == Index){
Packit 70b277
		if (cur->value.dc_type == DC_NUMBER)
Packit 70b277
			dc_free_num(&cur->value.v.number);
Packit 70b277
		else if (cur->value.dc_type == DC_STRING)
Packit 70b277
			dc_free_str(&cur->value.v.string);
Packit 70b277
		else
Packit 70b277
			dc_garbage(" in array", array_id);
Packit 70b277
		cur->value = value;
Packit 70b277
	}else{
Packit 70b277
		struct dc_array *newentry = dc_malloc(sizeof *newentry);
Packit 70b277
		newentry->Index = Index;
Packit 70b277
		newentry->value = value;
Packit 70b277
		newentry->next = cur;
Packit 70b277
		if (prev != NULL)
Packit 70b277
			prev->next = newentry;
Packit 70b277
		else
Packit 70b277
			dc_set_stacked_array(array_id, newentry);
Packit 70b277
	}
Packit 70b277
}
Packit 70b277
Packit 70b277
/* retrieve a dup of a value from array_id[Index] */
Packit 70b277
/* A zero value is returned if the specified value is unintialized. */
Packit 70b277
dc_data
Packit 70b277
dc_array_get DC_DECLARG((array_id, Index))
Packit 70b277
	int array_id DC_DECLSEP
Packit 70b277
	int Index DC_DECLEND
Packit 70b277
{
Packit 70b277
	struct dc_array *cur = dc_get_stacked_array(array_id);
Packit 70b277
Packit 70b277
	while (cur != NULL  &&  cur->Index < Index)
Packit 70b277
		cur = cur->next;
Packit 70b277
	if (cur !=NULL  &&  cur->Index == Index)
Packit 70b277
		return dc_dup(cur->value);
Packit 70b277
	return dc_int2data(0);
Packit 70b277
}
Packit 70b277
Packit 70b277
/* free an array chain */
Packit 70b277
void
Packit 70b277
dc_array_free DC_DECLARG((a_head))
Packit 70b277
	struct dc_array *a_head DC_DECLEND
Packit 70b277
{
Packit 70b277
	struct dc_array *cur;
Packit 70b277
	struct dc_array *next;
Packit 70b277
Packit 70b277
	for (cur=a_head; cur!=NULL; cur=next) {
Packit 70b277
		next = cur->next;
Packit 70b277
		if (cur->value.dc_type == DC_NUMBER)
Packit 70b277
			dc_free_num(&cur->value.v.number);
Packit 70b277
		else if (cur->value.dc_type == DC_STRING)
Packit 70b277
			dc_free_str(&cur->value.v.string);
Packit 70b277
		else
Packit 70b277
			dc_garbage("in stack", -1);
Packit 70b277
		free(cur);
Packit 70b277
	}
Packit 70b277
}
Packit 70b277
Packit 70b277

Packit 70b277
/*
Packit 70b277
 * Local Variables:
Packit 70b277
 * mode: C
Packit 70b277
 * tab-width: 4
Packit 70b277
 * End:
Packit 70b277
 * vi: set ts=4 :
Packit 70b277
 */