1 /* # skkinput (Simple Kana-Kanji Input)
2  *
3  * This file is part of skkinput.
4  * Copyright (C) 2002
5  * Takashi SAKAMOTO (PXG01715@nifty.ne.jp)
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with skkinput; see the file COPYING.  If not, write to
19  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 #if !defined (vstack_h)
22 #define	vstack_h
23 
24 #include "local.h"
25 
26 enum {
27 	DEFAULT_VSTACK_SIZE	= 512,
28 } ;
29 
30 typedef struct {
31 	unsigned char	m_achInternal [DEFAULT_VSTACK_SIZE] ;
32 	void*			m_pBuffer ;
33 	int				m_nWidth ;
34 	int				m_nUsage ;
35 	int				m_nSize ;
36 }	VStack ;
37 
38 /*	Prototypes */
39 
40 /*	����Ĺ�����������å��ν������*/
41 extern inline	Boolean
Vstack_Initialize(register VStack * pStack,register int nWidth)42 Vstack_Initialize   (
43 	register VStack*		pStack,
44 	register int			nWidth)
45 {
46 	assert (pStack != NULL) ;
47 	assert (nWidth >  0) ;
48 
49 	pStack->m_pBuffer	= pStack->m_achInternal ;
50 	pStack->m_nSize		= DEFAULT_VSTACK_SIZE / nWidth ;
51 	pStack->m_nUsage	= 0 ;
52 	pStack->m_nWidth	= nWidth ;
53 	return	True ;
54 }
55 
56 extern inline	Boolean
Vstack_Uninitialize(register VStack * pStack)57 Vstack_Uninitialize (
58 	register VStack*		pStack)
59 {
60 	assert (pStack != NULL) ;
61 
62 	if (pStack->m_pBuffer != pStack->m_achInternal) {
63 		FREE (pStack->m_pBuffer) ;
64 		pStack->m_pBuffer	= pStack->m_achInternal ;
65 	}
66 	pStack->m_nUsage	= 0 ;
67 	pStack->m_nSize		= 0 ;
68 	pStack->m_nWidth	= 0 ;
69 	return	True ;
70 }
71 
72 extern inline	Boolean
Vstack_Push(register VStack * pStack,register const void * pData)73 Vstack_Push         (
74 	register VStack*		pStack,
75 	register const void*	pData)
76 {
77 	register int		nPosition ;
78 
79 	assert (pStack != NULL) ;
80 	assert (pData  != NULL) ;
81 
82 	nPosition	= pStack->m_nWidth * pStack->m_nUsage ;
83 
84 	if ((pStack->m_nUsage + 1) > pStack->m_nSize) {
85 		unsigned char*	pNewBuffer ;
86 		int				nNewSize ;
87 
88 		nNewSize	= (pStack->m_nUsage + 1 + DEFAULT_VSTACK_SIZE) ;
89 		nNewSize	= nNewSize - nNewSize % DEFAULT_VSTACK_SIZE ;
90 
91 		pNewBuffer	= MALLOC (nNewSize * pStack->m_nWidth) ;
92 		if (pNewBuffer == NULL)
93 			return	False ;
94 
95 		assert (pNewBuffer != NULL) ;
96 		if (pStack->m_pBuffer != pStack->m_achInternal) {
97 			memcpy (pNewBuffer, pStack->m_pBuffer, nPosition) ;
98 			FREE (pStack->m_pBuffer) ;
99 		} else {
100 			memcpy (pNewBuffer, pStack->m_achInternal, nPosition) ;
101 		}
102 		pStack->m_pBuffer	= pNewBuffer ;
103 		pStack->m_nSize		= nNewSize ;
104 	}
105 	memcpy ((unsigned char*)pStack->m_pBuffer + nPosition, pData, pStack->m_nWidth) ;
106 	pStack->m_nUsage	++ ;
107 	return	True ;
108 }
109 
110 extern inline	Boolean
Vstack_Pop(register VStack * pStack,register void * pData)111 Vstack_Pop          (
112 	register VStack*	pStack,
113 	register void*		pData)
114 {
115 	register int			nPos ;
116 
117 	assert (pStack != NULL) ;
118 
119 	if (pStack->m_nUsage <= 0)
120 		return	False ;
121 
122 	pStack->m_nUsage	-- ;
123 	if (pData == NULL)
124 		return	True ;
125 
126 	nPos	= pStack->m_nWidth * pStack->m_nUsage ;
127 	memcpy (pData, (unsigned char*)pStack->m_pBuffer + nPos, pStack->m_nWidth) ;
128 	return	True ;
129 }
130 
131 extern inline	Boolean
Vstack_Clear(register VStack * pStack)132 Vstack_Clear        (
133 	register VStack*	pStack)
134 {
135 	pStack->m_nUsage	= 0 ;
136 	return	True ;
137 }
138 
139 extern inline	Boolean
Vstack_Emptyp(register VStack * pStack)140 Vstack_Emptyp       (
141 	register VStack*	pStack)
142 {
143 	return	(pStack->m_nUsage == 0)? True : False ;
144 }
145 
146 extern inline	Boolean
Vstack_GetHead(VStack * pStack,void * pData)147 Vstack_GetHead		(VStack* pStack, void* pData)
148 {
149 	register int		nPosition ;
150 
151 	if (pStack->m_nUsage <= 0)
152 		return	False ;
153 
154 	nPosition	= pStack->m_nWidth * (pStack->m_nUsage - 1) ;
155 	memcpy (pData, (unsigned char *)pStack->m_pBuffer + nPosition, pStack->m_nWidth) ;
156 	return	True ;
157 }
158 
159 #endif
160 
161