File: ctkstack.c

package info (click to toggle)
libctk 3.0.24.6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,316 kB
  • ctags: 1,201
  • sloc: ansic: 10,623; sh: 10,438; makefile: 102
file content (139 lines) | stat: -rw-r--r-- 2,644 bytes parent folder | download | duplicates (2)
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
/* 
 * $Id: ctkstack.c,v 1.4 2000/06/27 04:42:30 terpstra Exp $
 *
 * CTK - Console Toolkit
 *
 * Copyright (C) 1998-2000 Stormix Technologies Inc.
 *
 * License: LGPL
 *
 * Authors: Kevin Lindsay, Wesley Terpstra
 *  
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2 of the License, or (at your option) any later version.
 *    
 *    This library 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
 *    Lesser General Public License for more details.
 *    
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <glib.h>

#include "ctk.h"

GNode **focus_stack = NULL;

/* Get Number of items in stack */
gint stack_num_items(GNode **stack)
{
	gint i;

	if (!stack)
	    return 0;
	
	for (i = 0; stack[i]; i += 1)
	{
		// noop
	}

	return i;
}

/* Push an item onto the stack */
gint stack_push(GNode *node)
{
	gint num_items;
	gint i;
	
	if (!node) {
		ctk_close();
		g_error("stack_push: GNode *node == NULL!");
	}
	
	if (focus_stack == NULL)
	{
		focus_stack = g_malloc(sizeof(GNode *));
		focus_stack[0] = NULL;
	}

	num_items = stack_num_items(focus_stack);
	
	focus_stack = realloc(focus_stack,
			      sizeof(GNode *)*(num_items+2));

	focus_stack[num_items+1] = NULL;
	
	for (i = num_items; i > 0; i -= 1)
	{
		focus_stack[i] = focus_stack[i-1];
	}
	
	focus_stack[0] = node;

	return 1;
}

/* Check if an item exists in the stack */
gint stack_item_exist(GNode *node)
{
	gint i;
	
	if (!node)
	{
		ctk_close();
		g_error("stack_item_exist: GNode *node == NULL!");
	}
	
	if (!focus_stack)
	    return 0;
	
	for (i = 0; focus_stack[i]; i += 1)
	{
		if (focus_stack[i] == node)
		    return 1;
	}
	
	return 0;
}

/* Pop an item from the stack.
 * It may not necessarily be from the top either. */
gint stack_pop(GNode *node)
{
	gint i;
	
	if (!node)
	{
		ctk_close();
		g_error("stack_pop: GNode *node == NULL!");
	}
	
	if (!focus_stack)
	    return 0;
	
	for (i = 0; focus_stack[i]; i += 1)
	{
		if (focus_stack[i] == node)
		    break;
	}
		
	if (!focus_stack[i])
	    return 0;
	
	for (i += 1; focus_stack[i]; i += 1) {
		focus_stack[i-1] = focus_stack[i];
	}
	
	focus_stack[i-1] = NULL;

	return 1;
}