source: trunk/dll/makelist.c@ 433

Last change on this file since 433 was 361, checked in by root, 19 years ago

Use Runtime_Error
AddToList optimize

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1
2/***********************************************************************
3
4 $Id: makelist.c 361 2006-07-26 21:38:07Z root $
5
6 Make file lists
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2003, 2006 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
15***********************************************************************/
16
17#define INCL_DOS
18#define INCL_WIN
19#include <os2.h>
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <ctype.h>
25
26#include "fm3dll.h"
27#include "fm3str.h"
28
29static PSZ pszSrcFile = __FILE__;
30
31#pragma alloc_text(MAKELIST,AddToList,AddToFileList,BuildList,FreeListInfo,FreeList)
32#pragma alloc_text(MAKELIST,SortList,BuildArcList,RemoveFromList,CombineLists)
33
34VOID SortList (LISTINFO *li)
35{
36 /* bubble-sort entries by size, descending */
37
38 INT x;
39 CHAR *s;
40 ULONG l;
41 BOOL swapped;
42
43 if(li && li->list && li->list[0] && li->cbFile) {
44 do {
45 swapped = FALSE;
46 for(x = 0;li->list[x] && li->list[x + 1];x++) {
47 if(li->cbFile[x] < li->cbFile[x + 1]) {
48 s = li->list[x];
49 li->list[x] = li->list[x + 1];
50 li->list[x + 1] = s;
51 l = li->cbFile[x];
52 li->cbFile[x] = li->cbFile[x + 1];
53 li->cbFile[x + 1] = l;
54 if(li->ulitemID) {
55 l = li->ulitemID[x];
56 li->ulitemID[x] = li->ulitemID[x + 1];
57 li->ulitemID[x + 1] = l;
58 }
59 swapped = TRUE;
60 }
61 }
62 } while(swapped);
63 }
64}
65
66
67VOID FreeListInfo (LISTINFO *li)
68{
69 if(li) {
70 if(li->ulitemID)
71 free(li->ulitemID);
72 if(li->cbFile)
73 free(li->cbFile);
74 if(li->list)
75 FreeList(li->list);
76 free(li);
77 }
78}
79
80
81VOID FreeList (CHAR **list)
82{
83 register INT x;
84
85 if(list) {
86 for(x = 0;list[x];x++) {
87#ifdef __DEBUG_ALLOC__
88 _heap_check();
89#endif
90 free(list[x]);
91 }
92#ifdef __DEBUG_ALLOC__
93 _heap_check();
94#endif
95 free(list);
96 }
97 DosPostEventSem(CompactSem);
98}
99
100
101INT AddToFileList (CHAR *string,FILEFINDBUF4 *ffb4,FILELIST ***list,
102 INT *numfiles,INT *numalloced)
103{
104 FILELIST *pfl;
105 if (string && ffb4) {
106 if (((*numfiles) + 3) > *numalloced) {
107 FILELIST **pflArray;
108 // Use plain realloc for speed
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 (*list)[*numfiles] = NULL;
136 (*list)[(*numfiles) + 1] = NULL;
137#ifdef __DEBUG_ALLOC__
138 _heap_check();
139#endif
140 }
141 return 0;
142}
143
144
145INT AddToList (CHAR *string,CHAR ***list,INT *numfiles,INT *numalloced)
146{
147 CHAR **ppsz;
148 PSZ psz;
149
150 if (string) {
151 if (((*numfiles) + 3) > *numalloced) {
152 // Use plain realloc for speed
153 ppsz = realloc(*list,(*numalloced + 6) * sizeof(CHAR *));
154 if (!ppsz) {
155 Runtime_Error(pszSrcFile, __LINE__, "realloc");
156 return 1;
157 }
158 (*numalloced) += 6;
159 *list = ppsz;
160 }
161 // Use plain malloc for speed
162 psz = malloc(strlen(string) + 1);
163 if (!psz) {
164 Runtime_Error(pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
165 return 2;
166 }
167 (*list)[*numfiles] = psz;
168 strcpy((*list)[*numfiles],string);
169 (*numfiles)++;
170 (*list)[*numfiles] = NULL;
171 (*list)[(*numfiles) + 1] = NULL;
172#ifdef __DEBUG_ALLOC__
173 _heap_check();
174#endif
175 }
176 return 0;
177}
178
179
180CHAR ** BuildList (HWND hwndCnr)
181{
182 PCNRITEM pci;
183 CHAR **list = NULL,**test;
184 INT numfiles = 0,numalloc = 0,error = 0,attribute = CRA_CURSORED;
185
186 pci = (PCNRITEM)CurrentRecord(hwndCnr);
187 if(pci && (INT)pci != -1 && !(pci->flags & RECFLAGS_ENV)) {
188 if(pci->rc.flRecordAttr & CRA_SELECTED) {
189 attribute = CRA_SELECTED;
190 pci = WinSendMsg(hwndCnr,CM_QUERYRECORDEMPHASIS,MPFROMLONG(CMA_FIRST),
191 MPFROMSHORT(attribute));
192 }
193 }
194 while(pci && (INT)pci != -1 && !error) {
195 if (!(pci->rc.flRecordAttr & CRA_FILTERED))
196 error = AddToList(pci->szFileName,&list,&numfiles,&numalloc);
197 pci = WinSendMsg(hwndCnr,CM_QUERYRECORDEMPHASIS,MPFROMP(pci),
198 MPFROMSHORT(attribute));
199 }
200 if (numalloc > numfiles + 1) {
201 // Use plain realloc for speed
202 test = realloc(list,sizeof(CHAR *) * (numfiles + 1));
203 if (!test)
204 Runtime_Error(pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
205 else
206 list = test;
207 }
208 return list;
209}
210
211
212CHAR ** BuildArcList (HWND hwndCnr)
213{
214 PARCITEM pai;
215 CHAR **list = NULL;
216 INT numfiles = 0,numalloc = 0,error = 0,attribute = CRA_CURSORED;
217
218 pai = (PARCITEM)CurrentRecord(hwndCnr);
219 if(pai && (INT)pai != -1) {
220 if(pai->rc.flRecordAttr & CRA_SELECTED) {
221 attribute = CRA_SELECTED;
222 pai = WinSendMsg(hwndCnr,CM_QUERYRECORDEMPHASIS,MPFROMLONG(CMA_FIRST),
223 MPFROMSHORT(attribute));
224 }
225 }
226 while( pai && (INT)pai != -1 && !error) {
227 if(!(pai->rc.flRecordAttr & CRA_FILTERED))
228 error = AddToList(pai->szFileName,&list,&numfiles,&numalloc);
229 pai = WinSendMsg(hwndCnr,CM_QUERYRECORDEMPHASIS,MPFROMP(pai),
230 MPFROMSHORT(attribute));
231 }
232 return list;
233}
234
235
236CHAR ** RemoveFromList (CHAR **list,CHAR *item)
237{
238 register INT x,y;
239
240 if(list && list[0] && item) {
241 for(x = 0;list[x];x++) {
242 if(item == list[x]) {
243 free(list[x]);
244 list[x] = NULL;
245 for(y = x;;y++) {
246 if(y != x && !list[y])
247 break;
248 list[y] = list[y + 1];
249 }
250 if(!list[0]) {
251 FreeList(list);
252 list = NULL;
253 }
254 break;
255 }
256 }
257 }
258 return list;
259}
260
261
262CHAR ** CombineLists (CHAR **prime,CHAR **add)
263{
264 register INT x;
265 INT numalloc,numfiles = 0;
266
267 if(add && add[0]) {
268 if(prime) {
269 for(x = 0;prime[x];x++)
270 numfiles++;
271 }
272 numalloc = numfiles;
273 for(x = 0;add[x];x++) {
274 if(*add[x])
275 AddToList(add[x],&prime,&numfiles,&numalloc);
276 }
277 FreeList(add);
278 }
279 return prime;
280}
Note: See TracBrowser for help on using the repository browser.