aboutsummaryrefslogtreecommitdiff
path: root/saga/stack.cpp
blob: c7c5cc42ae0aac9ee5fe65fdedb17e200ae5b80d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 The ScummVM project
 *
 * The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */
/*
 Description:	
 
	Simple integer stack module

 Notes: 
*/

#include "reinherit.h"

#include "stack.h"
#include "stack_mod.h"

namespace Saga {

int ISTACK_Create(R_ISTACK * stack, int stack_len, int flags)
{

	R_ISTACK new_stack;
	int *new_stack_data;

	*stack = NULL;

	new_stack = (R_ISTACK_tag *)malloc(sizeof(struct R_ISTACK_tag));

	if (new_stack == NULL) {
		return STACK_MEM;
	}

	new_stack_data = (int *)calloc(stack_len, sizeof(int));

	if (new_stack_data == NULL) {

		free(new_stack);
		return STACK_MEM;
	}

	new_stack->flags = flags;
	new_stack->len = stack_len;
	new_stack->top = -1;

	return STACK_SUCCESS;
}

int ISTACK_Destroy(R_ISTACK * stack)
{

	if (stack != NULL) {
		free((*stack)->data);
	}

	free(stack);

	*stack = NULL;

	return STACK_SUCCESS;
}

int ISTACK_Clear(R_ISTACK stack)
{

	stack->top = -1;
	return STACK_SUCCESS;
}

int ISTACK_PushNull(R_ISTACK stack)
{

	if (stack->top >= (stack->len - 1)) {

		if (stack->flags & STACK_FIXED) {

			return STACK_OVERFLOW;
		} else if (ISTACK_Grow(stack) != STACK_SUCCESS) {

			return STACK_MEM;
		}
	}

	stack->top++;

	return STACK_SUCCESS;
}

int ISTACK_Push(R_ISTACK stack, int value)
{

	if (stack->top >= (stack->len - 1)) {

		if (stack->flags & STACK_FIXED) {

			return STACK_OVERFLOW;
		} else if (ISTACK_Grow(stack) != STACK_SUCCESS) {

			return STACK_MEM;
		}
	}

	stack->top++;
	stack->data[stack->top] = value;

	return STACK_SUCCESS;
}

int ISTACK_Pop(R_ISTACK stack, int *value)
{

	if (stack->top <= -1) {

		return STACK_UNDERFLOW;
	}

	if (value == NULL) {

		stack->top--;
		return STACK_SUCCESS;
	}

	*value = stack->data[stack->top];
	stack->top--;

	return STACK_SUCCESS;
}

int ISTACK_Top(R_ISTACK stack, int *value)
{
	*value = 0;

	if (stack->top <= -1) {

		return STACK_UNDERFLOW;
	}

	*value = stack->data[stack->top];

	return STACK_SUCCESS;

}

int ISTACK_Grow(R_ISTACK stack)
{

	int *new_data;

	new_data = (int *)realloc(stack->data, (stack->len * 2) * sizeof(int));

	if (new_data == NULL) {

		return STACK_MEM;
	};

	stack->data = new_data;
	stack->len *= 2;

	return STACK_SUCCESS;
}

} // End of namespace Saga