source: trunk/dll/makelist.c@ 793

Last change on this file since 793 was 793, checked in by Gregg Young, 18 years ago

Move #pragma alloc_text to end for OpenWatcom compat

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1
2/***********************************************************************
3
4 $Id: makelist.c 793 2007-08-21 02:53:38Z gyoung $
5
6 Make file lists
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2003, 2007 Steven H.Levine
10
11 12 Feb 03 SHL AddToFileList: standardize EA math
12 22 Jul 06 SHL Use Runtime_Error
13 22 Jul 06 SHL AddToList optimize
14 06 Apr 07 GKY Work around PM DragInfo and DrgFreeDISH limits
15 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
16
17***********************************************************************/
18
19#define INCL_DOS
20#define INCL_WIN
21#include <os2.h>
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <ctype.h>
27
28#include "fm3dll.h"
29#include "fm3str.h"
30
31static PSZ pszSrcFile = __FILE__;
32
33VOID SortList(LISTINFO * li)
34{
35 /* bubble-sort entries by size, descending */
36
37 INT x;
38 CHAR *s;
39 ULONG l;
40 BOOL swapped;
41
42 if (li && li->list && li->list[0] && li->cbFile) {
43 do {
44 swapped = FALSE;
45 for (x = 0; li->list[x] && li->list[x + 1]; x++) {
46 if (li->cbFile[x] < li->cbFile[x + 1]) {
47 s = li->list[x];
48 li->list[x] = li->list[x + 1];
49 li->list[x + 1] = s;
50 l = li->cbFile[x];
51 li->cbFile[x] = li->cbFile[x + 1];
52 li->cbFile[x + 1] = l;
53 if (li->ulitemID) {
54 l = li->ulitemID[x];
55 li->ulitemID[x] = li->ulitemID[x + 1];
56 li->ulitemID[x + 1] = l;
57 }
58 swapped = TRUE;
59 }
60 }
61 } while (swapped);
62 }
63}
64
65VOID FreeListInfo(LISTINFO * li)
66{
67 if (li) {
68 if (li->ulitemID)
69 free(li->ulitemID);
70 if (li->cbFile)
71 free(li->cbFile);
72 if (li->list)
73 FreeList(li->list);
74 free(li);
75 }
76}
77
78VOID FreeList(CHAR ** list)
79{
80 register INT x;
81
82 if (list) {
83 for (x = 0; list[x]; x++) {
84#ifdef __DEBUG_ALLOC__
85 _heap_check();
86#endif
87 free(list[x]);
88 }
89#ifdef __DEBUG_ALLOC__
90 _heap_check();
91#endif
92 free(list);
93 }
94 DosPostEventSem(CompactSem);
95}
96
97INT AddToFileList(CHAR * string, FILEFINDBUF4 * ffb4, FILELIST *** list,
98 INT * numfiles, INT * numalloced)
99{
100 FILELIST *pfl;
101
102 if (string && ffb4) {
103 // Ensure room for NULL entry
104 if (((*numfiles) + 3) > *numalloced) {
105 FILELIST **pflArray;
106
107 // Use plain realloc for speed
108 // 06 Aug 07 SHL fixme to know why + 6
109 pflArray = realloc(*list, (*numalloced + 6) * sizeof(FILELIST *));
110 if (!pflArray) {
111 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
112 return 1;
113 }
114 (*numalloced) += 6;
115 *list = pflArray;
116 }
117 // Use plain malloc for speed
118 pfl = malloc(sizeof(FILELIST) + strlen(string));
119 if (!pfl) {
120 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
121 return 2;
122 }
123 pfl->attrFile = ffb4->attrFile;
124 pfl->date = ffb4->fdateLastWrite;
125 pfl->time = ffb4->ftimeLastWrite;
126 pfl->ladate = ffb4->fdateLastAccess;
127 pfl->latime = ffb4->ftimeLastAccess;
128 pfl->crdate = ffb4->fdateCreation;
129 pfl->crtime = ffb4->ftimeCreation;
130 pfl->cbFile = ffb4->cbFile;
131 pfl->easize = CBLIST_TO_EASIZE(ffb4->cbList);
132 strcpy(pfl->fname, string);
133 (*list)[*numfiles] = pfl;
134 (*numfiles)++;
135 // Ensure list always ends with two NULL entries
136 // 06 Aug 07 SHL fixme to know why
137 (*list)[*numfiles] = NULL;
138 (*list)[(*numfiles) + 1] = NULL;
139#ifdef __DEBUG_ALLOC__
140 _heap_check();
141#endif
142 }
143 return 0;
144}
145
146/**
147 * Add string to string list
148 * Enlarges as needed
149 * Ensures 2 NULL end markers exist
150 */
151
152INT AddToList(CHAR * string, CHAR *** list, INT * numfiles, INT * numalloced)
153{
154 CHAR **ppsz;
155 PSZ psz;
156
157 if (string) {
158 if (((*numfiles) + 3) > *numalloced) {
159 // Use plain realloc for speed
160 ppsz = realloc(*list, (*numalloced + 6) * sizeof(CHAR *));
161 if (!ppsz) {
162 Runtime_Error(pszSrcFile, __LINE__, "realloc");
163 return 1;
164 }
165 (*numalloced) += 6;
166 *list = ppsz;
167 }
168 // Use plain malloc for speed
169 psz = malloc(strlen(string) + 1);
170 if (!psz) {
171 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
172 return 2;
173 }
174 (*list)[*numfiles] = psz;
175 strcpy((*list)[*numfiles], string); // Add entry
176 (*numfiles)++;
177 (*list)[*numfiles] = NULL; // Add end marker
178 (*list)[(*numfiles) + 1] = NULL; // Add 2nd end marker - fixme to know why?
179#ifdef __DEBUG_ALLOC__
180 _heap_check();
181#endif
182 }
183 return 0;
184}
185
186CHAR **BuildList(HWND hwndCnr)
187{
188 PCNRITEM pci;
189 CHAR **list = NULL, **test;
190 INT numfiles = 0, numalloc = 0, error = 0, attribute = CRA_CURSORED;
191
192 pci = (PCNRITEM) CurrentRecord(hwndCnr);
193 if (pci && (INT) pci != -1 && !(pci->flags & RECFLAGS_ENV)) {
194 if (pci->rc.flRecordAttr & CRA_SELECTED) {
195 attribute = CRA_SELECTED;
196 pci = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
197 MPFROMSHORT(attribute));
198 }
199 }
200 while (pci && (INT) pci != -1 && !error) {
201 if (!(pci->rc.flRecordAttr & CRA_FILTERED))
202 error = AddToList(pci->pszFileName, &list, &numfiles, &numalloc);
203 pci = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pci),
204 MPFROMSHORT(attribute));
205 }
206 if (numalloc > numfiles + 1) {
207 // Use plain realloc for speed
208 test = realloc(list, sizeof(CHAR *) * (numfiles + 1));
209 if (!test)
210 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
211 else
212 list = test;
213 } // while
214 return list;
215}
216
217CHAR **BuildArcList(HWND hwndCnr)
218{
219 PARCITEM pai;
220 CHAR **list = NULL;
221 INT numfiles = 0, numalloc = 0, error = 0, attribute = CRA_CURSORED;
222
223 pai = (PARCITEM) CurrentRecord(hwndCnr);
224 if (pai && (INT) pai != -1) {
225 if (pai->rc.flRecordAttr & CRA_SELECTED) {
226 attribute = CRA_SELECTED;
227 pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
228 MPFROMSHORT(attribute));
229 }
230 }
231 while (pai && (INT) pai != -1 && !error) {
232 if (!(pai->rc.flRecordAttr & CRA_FILTERED))
233 error = AddToList(pai->pszFileName, &list, &numfiles, &numalloc);
234 pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pai),
235 MPFROMSHORT(attribute));
236 }
237 return list;
238}
239
240CHAR **RemoveFromList(CHAR ** list, CHAR * item)
241{
242 register INT x, y;
243
244 if (list && list[0] && item) {
245 for (x = 0; list[x]; x++) {
246 if (item == list[x]) {
247 free(list[x]);
248 list[x] = NULL;
249 for (y = x;; y++) {
250 if (y != x && !list[y])
251 break;
252 list[y] = list[y + 1];
253 }
254 if (!list[0]) {
255 FreeList(list);
256 list = NULL;
257 }
258 break;
259 }
260 }
261 }
262 return list;
263}
264
265CHAR **CombineLists(CHAR ** prime, CHAR ** add)
266{
267 register INT x;
268 INT numalloc, numfiles = 0;
269
270 if (add && add[0]) {
271 if (prime) {
272 for (x = 0; prime[x]; x++)
273 numfiles++;
274 }
275 numalloc = numfiles;
276 for (x = 0; add[x]; x++) {
277 if (*add[x])
278 AddToList(add[x], &prime, &numfiles, &numalloc);
279 }
280 FreeList(add);
281 }
282 return prime;
283}
284
285#pragma alloc_text(MAKELIST,AddToList,AddToFileList,BuildList,FreeListInfo,FreeList)
286#pragma alloc_text(MAKELIST,SortList,BuildArcList,RemoveFromList,CombineLists)
Note: See TracBrowser for help on using the repository browser.