| 1 |
|
|---|
| 2 | /***********************************************************************
|
|---|
| 3 |
|
|---|
| 4 | $Id: makelist.c 841 2007-09-23 16:27:51Z 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 | #define INCL_LONGLONG
|
|---|
| 22 | #include <os2.h>
|
|---|
| 23 |
|
|---|
| 24 | #include <stdio.h>
|
|---|
| 25 | #include <stdlib.h>
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 | #include <ctype.h>
|
|---|
| 28 |
|
|---|
| 29 | #include "fm3dll.h"
|
|---|
| 30 | #include "fm3str.h"
|
|---|
| 31 |
|
|---|
| 32 | static PSZ pszSrcFile = __FILE__;
|
|---|
| 33 |
|
|---|
| 34 | VOID 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 | VOID FreeListInfo(LISTINFO * li)
|
|---|
| 67 | {
|
|---|
| 68 | if (li) {
|
|---|
| 69 | if (li->ulitemID)
|
|---|
| 70 | free(li->ulitemID);
|
|---|
| 71 | if (li->cbFile)
|
|---|
| 72 | free(li->cbFile);
|
|---|
| 73 | if (li->list)
|
|---|
| 74 | FreeList(li->list);
|
|---|
| 75 | free(li);
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | VOID FreeList(CHAR ** list)
|
|---|
| 80 | {
|
|---|
| 81 | register INT x;
|
|---|
| 82 |
|
|---|
| 83 | if (list) {
|
|---|
| 84 | for (x = 0; list[x]; x++) {
|
|---|
| 85 | #ifdef __DEBUG_ALLOC__
|
|---|
| 86 | _heap_check();
|
|---|
| 87 | #endif
|
|---|
| 88 | free(list[x]);
|
|---|
| 89 | }
|
|---|
| 90 | #ifdef __DEBUG_ALLOC__
|
|---|
| 91 | _heap_check();
|
|---|
| 92 | #endif
|
|---|
| 93 | free(list);
|
|---|
| 94 | }
|
|---|
| 95 | DosPostEventSem(CompactSem);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | INT AddToFileList(CHAR * string, FILEFINDBUF4L * ffb4, FILELIST *** list,
|
|---|
| 99 | INT * numfiles, INT * numalloced)
|
|---|
| 100 | {
|
|---|
| 101 | FILELIST *pfl;
|
|---|
| 102 |
|
|---|
| 103 | if (string && ffb4) {
|
|---|
| 104 | // Ensure room for NULL entry
|
|---|
| 105 | if (((*numfiles) + 3) > *numalloced) {
|
|---|
| 106 | FILELIST **pflArray;
|
|---|
| 107 |
|
|---|
| 108 | // Use plain realloc for speed
|
|---|
| 109 | // 06 Aug 07 SHL fixme to know why + 6
|
|---|
| 110 | pflArray = realloc(*list, (*numalloced + 6) * sizeof(FILELIST *));
|
|---|
| 111 | if (!pflArray) {
|
|---|
| 112 | Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
|
|---|
| 113 | return 1;
|
|---|
| 114 | }
|
|---|
| 115 | (*numalloced) += 6;
|
|---|
| 116 | *list = pflArray;
|
|---|
| 117 | }
|
|---|
| 118 | // Use plain malloc for speed
|
|---|
| 119 | pfl = malloc(sizeof(FILELIST) + strlen(string));
|
|---|
| 120 | if (!pfl) {
|
|---|
| 121 | Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
|
|---|
| 122 | return 2;
|
|---|
| 123 | }
|
|---|
| 124 | pfl->attrFile = ffb4->attrFile;
|
|---|
| 125 | pfl->date = ffb4->fdateLastWrite;
|
|---|
| 126 | pfl->time = ffb4->ftimeLastWrite;
|
|---|
| 127 | pfl->ladate = ffb4->fdateLastAccess;
|
|---|
| 128 | pfl->latime = ffb4->ftimeLastAccess;
|
|---|
| 129 | pfl->crdate = ffb4->fdateCreation;
|
|---|
| 130 | pfl->crtime = ffb4->ftimeCreation;
|
|---|
| 131 | pfl->cbFile = ffb4->cbFile;
|
|---|
| 132 | pfl->easize = CBLIST_TO_EASIZE(ffb4->cbList);
|
|---|
| 133 | strcpy(pfl->fname, string);
|
|---|
| 134 | (*list)[*numfiles] = pfl;
|
|---|
| 135 | (*numfiles)++;
|
|---|
| 136 | // Ensure list always ends with two NULL entries
|
|---|
| 137 | // 06 Aug 07 SHL fixme to know why
|
|---|
| 138 | (*list)[*numfiles] = NULL;
|
|---|
| 139 | (*list)[(*numfiles) + 1] = NULL;
|
|---|
| 140 | #ifdef __DEBUG_ALLOC__
|
|---|
| 141 | _heap_check();
|
|---|
| 142 | #endif
|
|---|
| 143 | }
|
|---|
| 144 | return 0;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| 148 | * Add string to string list
|
|---|
| 149 | * Enlarges as needed
|
|---|
| 150 | * Ensures 2 NULL end markers exist
|
|---|
| 151 | */
|
|---|
| 152 |
|
|---|
| 153 | INT AddToList(CHAR * string, CHAR *** list, INT * numfiles, INT * numalloced)
|
|---|
| 154 | {
|
|---|
| 155 | CHAR **ppsz;
|
|---|
| 156 | PSZ psz;
|
|---|
| 157 |
|
|---|
| 158 | if (string) {
|
|---|
| 159 | if (((*numfiles) + 3) > *numalloced) {
|
|---|
| 160 | // Use plain realloc for speed
|
|---|
| 161 | ppsz = realloc(*list, (*numalloced + 6) * sizeof(CHAR *));
|
|---|
| 162 | if (!ppsz) {
|
|---|
| 163 | Runtime_Error(pszSrcFile, __LINE__, "realloc");
|
|---|
| 164 | return 1;
|
|---|
| 165 | }
|
|---|
| 166 | (*numalloced) += 6;
|
|---|
| 167 | *list = ppsz;
|
|---|
| 168 | }
|
|---|
| 169 | // Use plain malloc for speed
|
|---|
| 170 | psz = malloc(strlen(string) + 1);
|
|---|
| 171 | if (!psz) {
|
|---|
| 172 | Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
|
|---|
| 173 | return 2;
|
|---|
| 174 | }
|
|---|
| 175 | (*list)[*numfiles] = psz;
|
|---|
| 176 | strcpy((*list)[*numfiles], string); // Add entry
|
|---|
| 177 | (*numfiles)++;
|
|---|
| 178 | (*list)[*numfiles] = NULL; // Add end marker
|
|---|
| 179 | (*list)[(*numfiles) + 1] = NULL; // Add 2nd end marker - fixme to know why?
|
|---|
| 180 | #ifdef __DEBUG_ALLOC__
|
|---|
| 181 | _heap_check();
|
|---|
| 182 | #endif
|
|---|
| 183 | }
|
|---|
| 184 | return 0;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | CHAR **BuildList(HWND hwndCnr)
|
|---|
| 188 | {
|
|---|
| 189 | PCNRITEM pci;
|
|---|
| 190 | CHAR **list = NULL, **test;
|
|---|
| 191 | INT numfiles = 0, numalloc = 0, error = 0, attribute = CRA_CURSORED;
|
|---|
| 192 |
|
|---|
| 193 | pci = (PCNRITEM) CurrentRecord(hwndCnr);
|
|---|
| 194 | if (pci && (INT) pci != -1 && !(pci->flags & RECFLAGS_ENV)) {
|
|---|
| 195 | if (pci->rc.flRecordAttr & CRA_SELECTED) {
|
|---|
| 196 | attribute = CRA_SELECTED;
|
|---|
| 197 | pci = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
|
|---|
| 198 | MPFROMSHORT(attribute));
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 | while (pci && (INT) pci != -1 && !error) {
|
|---|
| 202 | if (!(pci->rc.flRecordAttr & CRA_FILTERED))
|
|---|
| 203 | error = AddToList(pci->pszFileName, &list, &numfiles, &numalloc);
|
|---|
| 204 | pci = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pci),
|
|---|
| 205 | MPFROMSHORT(attribute));
|
|---|
| 206 | }
|
|---|
| 207 | if (numalloc > numfiles + 1) {
|
|---|
| 208 | // Use plain realloc for speed
|
|---|
| 209 | test = realloc(list, sizeof(CHAR *) * (numfiles + 1));
|
|---|
| 210 | if (!test)
|
|---|
| 211 | Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
|
|---|
| 212 | else
|
|---|
| 213 | list = test;
|
|---|
| 214 | } // while
|
|---|
| 215 | return list;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | CHAR **BuildArcList(HWND hwndCnr)
|
|---|
| 219 | {
|
|---|
| 220 | PARCITEM pai;
|
|---|
| 221 | CHAR **list = NULL;
|
|---|
| 222 | INT numfiles = 0, numalloc = 0, error = 0, attribute = CRA_CURSORED;
|
|---|
| 223 |
|
|---|
| 224 | pai = (PARCITEM) CurrentRecord(hwndCnr);
|
|---|
| 225 | if (pai && (INT) pai != -1) {
|
|---|
| 226 | if (pai->rc.flRecordAttr & CRA_SELECTED) {
|
|---|
| 227 | attribute = CRA_SELECTED;
|
|---|
| 228 | pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
|
|---|
| 229 | MPFROMSHORT(attribute));
|
|---|
| 230 | }
|
|---|
| 231 | }
|
|---|
| 232 | while (pai && (INT) pai != -1 && !error) {
|
|---|
| 233 | if (!(pai->rc.flRecordAttr & CRA_FILTERED))
|
|---|
| 234 | error = AddToList(pai->pszFileName, &list, &numfiles, &numalloc);
|
|---|
| 235 | pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pai),
|
|---|
| 236 | MPFROMSHORT(attribute));
|
|---|
| 237 | }
|
|---|
| 238 | return list;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | CHAR **RemoveFromList(CHAR ** list, CHAR * item)
|
|---|
| 242 | {
|
|---|
| 243 | register INT x, y;
|
|---|
| 244 |
|
|---|
| 245 | if (list && list[0] && item) {
|
|---|
| 246 | for (x = 0; list[x]; x++) {
|
|---|
| 247 | if (item == list[x]) {
|
|---|
| 248 | free(list[x]);
|
|---|
| 249 | list[x] = NULL;
|
|---|
| 250 | for (y = x;; y++) {
|
|---|
| 251 | if (y != x && !list[y])
|
|---|
| 252 | break;
|
|---|
| 253 | list[y] = list[y + 1];
|
|---|
| 254 | }
|
|---|
| 255 | if (!list[0]) {
|
|---|
| 256 | FreeList(list);
|
|---|
| 257 | list = NULL;
|
|---|
| 258 | }
|
|---|
| 259 | break;
|
|---|
| 260 | }
|
|---|
| 261 | }
|
|---|
| 262 | }
|
|---|
| 263 | return list;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | CHAR **CombineLists(CHAR ** prime, CHAR ** add)
|
|---|
| 267 | {
|
|---|
| 268 | register INT x;
|
|---|
| 269 | INT numalloc, numfiles = 0;
|
|---|
| 270 |
|
|---|
| 271 | if (add && add[0]) {
|
|---|
| 272 | if (prime) {
|
|---|
| 273 | for (x = 0; prime[x]; x++)
|
|---|
| 274 | numfiles++;
|
|---|
| 275 | }
|
|---|
| 276 | numalloc = numfiles;
|
|---|
| 277 | for (x = 0; add[x]; x++) {
|
|---|
| 278 | if (*add[x])
|
|---|
| 279 | AddToList(add[x], &prime, &numfiles, &numalloc);
|
|---|
| 280 | }
|
|---|
| 281 | FreeList(add);
|
|---|
| 282 | }
|
|---|
| 283 | return prime;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | #pragma alloc_text(MAKELIST,AddToList,AddToFileList,BuildList,FreeListInfo,FreeList)
|
|---|
| 287 | #pragma alloc_text(MAKELIST,SortList,BuildArcList,RemoveFromList,CombineLists)
|
|---|