update for HEAD-2003091401
[reactos.git] / lib / richedit / charlist.c
1 /*
2  *
3  *  Character List
4  *
5  *  Copyright (c) 2000 by Jean-Claude Batista
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library 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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stddef.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27
28 #include "charlist.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "debug.h"
32
33 #if 0
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
37 #else
38   #define TRACE DPRINT
39   #define WARN DPRINT
40   #define FIXME DPRINT
41 #endif
42
43 extern HANDLE RICHED32_hHeap;
44
45 void CHARLIST_Enqueue( CHARLIST* pCharList, char myChar )
46 {
47     CHARLISTENTRY* pNewEntry = HeapAlloc(RICHED32_hHeap, 0,sizeof(CHARLISTENTRY));
48     pNewEntry->pNext = NULL;
49     pNewEntry->myChar = myChar;
50
51     TRACE("\n");
52
53     if(pCharList->pTail == NULL)
54     {
55          pCharList->pHead = pCharList->pTail = pNewEntry;
56     }
57     else
58     {
59          CHARLISTENTRY* pCurrent = pCharList->pTail;
60          pCharList->pTail = pCurrent->pNext = pNewEntry;
61     }
62
63     pCharList->nCount++;
64 }
65
66 void CHARLIST_Push( CHARLIST* pCharList, char myChar)
67 {
68     CHARLISTENTRY* pNewEntry = malloc(sizeof(CHARLISTENTRY));
69
70     TRACE("\n");
71
72     pNewEntry->myChar = myChar;
73
74     if(pCharList->pHead == NULL)
75     {
76          pCharList->pHead = pCharList->pTail = pNewEntry;
77          pNewEntry->pNext = NULL;
78
79     }
80     else
81     {
82          pNewEntry->pNext = pCharList->pHead;
83          pCharList->pHead = pNewEntry;
84     }
85
86     pCharList->nCount++;
87 }
88
89 char CHARLIST_Dequeue(CHARLIST* pCharList)
90 {
91     CHARLISTENTRY* pCurrent;
92     char myChar;
93
94     TRACE("\n");
95
96     if(pCharList->nCount == 0)
97       return 0;
98
99     pCharList->nCount--;
100     myChar = pCharList->pHead->myChar;
101     pCurrent = pCharList->pHead->pNext;
102     HeapFree(RICHED32_hHeap, 0,pCharList->pHead);
103
104     if(pCharList->nCount == 0)
105     {
106         pCharList->pHead = pCharList->pTail = NULL;
107     }
108     else
109     {
110         pCharList->pHead = pCurrent;
111     }
112
113     return myChar;
114 }
115
116 int CHARLIST_GetNbItems(CHARLIST* pCharList)
117 {
118     TRACE("\n");
119
120     return pCharList->nCount;
121 }
122
123 void CHARLIST_FreeList(CHARLIST* pCharList){
124     TRACE("\n");
125
126     while(pCharList->nCount)
127         CHARLIST_Dequeue(pCharList);
128 }
129
130 /* this function counts the number of occurrences of a caracter */
131 int CHARLIST_CountChar(CHARLIST* pCharList, char myChar)
132 {
133     CHARLISTENTRY *pCurrent;
134     int nCount = 0;
135
136     TRACE("\n");
137
138     for(pCurrent =pCharList->pHead ;pCurrent;pCurrent=pCurrent->pNext)
139         if(pCurrent->myChar == myChar)
140             nCount++;
141
142     return nCount;
143 }
144
145 int CHARLIST_toBuffer(CHARLIST* pCharList, char* pBuffer, int nBufferSize)
146 {
147
148    TRACE("\n");
149
150    /* we add one to store a NULL caracter */
151    if(nBufferSize < pCharList->nCount + 1)
152         return pCharList->nCount;
153
154    for(;pCharList->nCount;pBuffer++)
155        *pBuffer = CHARLIST_Dequeue(pCharList);
156
157    *pBuffer = '\0';
158
159    return 0;
160 }
161