- Localize origin, location, and rectangle computation.
[wine] / dlls / 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 "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
34
35 extern HANDLE RICHED32_hHeap;
36
37 void CHARLIST_Enqueue( CHARLIST* pCharList, char myChar )
38 {
39     CHARLISTENTRY* pNewEntry = HeapAlloc(RICHED32_hHeap, 0,sizeof(CHARLISTENTRY));
40     pNewEntry->pNext = NULL;
41     pNewEntry->myChar = myChar;
42
43     TRACE("\n");
44
45     if(pCharList->pTail == NULL)
46     {
47          pCharList->pHead = pCharList->pTail = pNewEntry;
48     }
49     else
50     {
51          CHARLISTENTRY* pCurrent = pCharList->pTail;
52          pCharList->pTail = pCurrent->pNext = pNewEntry;
53     }
54
55     pCharList->nCount++;
56 }
57
58 void CHARLIST_Push( CHARLIST* pCharList, char myChar)
59 {
60     CHARLISTENTRY* pNewEntry = malloc(sizeof(CHARLISTENTRY));
61
62     TRACE("\n");
63
64     pNewEntry->myChar = myChar;
65
66     if(pCharList->pHead == NULL)
67     {
68          pCharList->pHead = pCharList->pTail = pNewEntry;
69          pNewEntry->pNext = NULL;
70
71     }
72     else
73     {
74          pNewEntry->pNext = pCharList->pHead;
75          pCharList->pHead = pNewEntry;
76     }
77
78     pCharList->nCount++;
79 }
80
81 char CHARLIST_Dequeue(CHARLIST* pCharList)
82 {
83     CHARLISTENTRY* pCurrent;
84     char myChar;
85
86     TRACE("\n");
87
88     if(pCharList->nCount == 0)
89       return 0;
90
91     pCharList->nCount--;
92     myChar = pCharList->pHead->myChar;
93     pCurrent = pCharList->pHead->pNext;
94     HeapFree(RICHED32_hHeap, 0,pCharList->pHead);
95
96     if(pCharList->nCount == 0)
97     {
98         pCharList->pHead = pCharList->pTail = NULL;
99     }
100     else
101     {
102         pCharList->pHead = pCurrent;
103     }
104
105     return myChar;
106 }
107
108 int CHARLIST_GetNbItems(CHARLIST* pCharList)
109 {
110     TRACE("\n");
111
112     return pCharList->nCount;
113 }
114
115 void CHARLIST_FreeList(CHARLIST* pCharList){
116     TRACE("\n");
117
118     while(pCharList->nCount)
119         CHARLIST_Dequeue(pCharList);
120 }
121
122 /* this function counts the number of occurrences of a caracter */
123 int CHARLIST_CountChar(CHARLIST* pCharList, char myChar)
124 {
125     CHARLISTENTRY *pCurrent;
126     int nCount = 0;
127
128     TRACE("\n");
129
130     for(pCurrent =pCharList->pHead ;pCurrent;pCurrent=pCurrent->pNext)
131         if(pCurrent->myChar == myChar)
132             nCount++;
133
134     return nCount;
135 }
136
137 int CHARLIST_toBuffer(CHARLIST* pCharList, char* pBuffer, int nBufferSize)
138 {
139
140    TRACE("\n");
141
142    /* we add one to store a NULL caracter */
143    if(nBufferSize < pCharList->nCount + 1)
144         return pCharList->nCount;
145
146    for(;pCharList->nCount;pBuffer++)
147        *pBuffer = CHARLIST_Dequeue(pCharList);
148
149    *pBuffer = '\0';
150
151    return 0;
152 }
153