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