summaryrefslogtreecommitdiffhomepage
path: root/Datafile/Main.cpp
blob: c09295b94a3fe4b5b00a6043089b957d62bee30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*  Starshatter OpenSource Distribution
    Copyright (c) 1997-2004, Destroyer Studios LLC.
    All Rights Reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name "Destroyer Studios" nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

SUBSYSTEM:    DataFile.exe
FILE:         main.cpp
AUTHOR:       John DiCamillo

*/

#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <math.h>
#include <time.h>
#include <windows.h>
#include <windowsx.h>
#include "Archive.h"

//#define MOD_MAKER 1

// +------------------------------------------------------------------+

void insertFile(DataArchive& a, const char* sPath, WIN32_FIND_DATA* find)
{
    char    sFile[256];
    char    sFlat[256];
    std::string    sTemp;
    DWORD    find_attrib_forbidden =
        FILE_ATTRIBUTE_DIRECTORY |
        FILE_ATTRIBUTE_HIDDEN    |
        FILE_ATTRIBUTE_SYSTEM    |
        FILE_ATTRIBUTE_OFFLINE;

        if (sPath && *sPath) {
            sTemp = sPath;
            sTemp += '/';
            WideCharToMultiByte(CP_ACP,0,find->cFileName,-1, sFile,260, NULL, NULL);
            sTemp += sFile;
            strcpy(sFile, sTemp.c_str());
        } else {
            WideCharToMultiByte(CP_ACP,0,find->cFileName,-1, sFile,260, NULL, NULL);
        }


    if (find->dwFileAttributes & find_attrib_forbidden) {
        printf("   Skipping:  %-48s \n", sFile);
        return;
    }

    int n = strlen(sFile);

    if (n >= NAMELEN) {
        printf("   Skipping:  %-48s (NAME TOO LONG!)\n", sFile);
        return;
    }

    for (int i = 0; i < n; i++)
        sFlat[i] = tolower(sFile[i]);

    if (strstr(sFlat, ".exe")) {
        printf("   Skipping:  %-48s (executable file)\n", sFile);
    }
    else if (strstr(sFlat, ".cmd")) {
        printf("   Skipping:  %-48s (executable file)\n", sFile);
    }
    else if (strstr(sFlat, ".bat")) {
        printf("   Skipping:  %-48s (executable file)\n", sFile);
    }
    else if (strstr(sFlat, ".bin")) {
        printf("   Skipping:  %-48s (unknown file)\n", sFile);
    }
    else if (strstr(sFlat, ".db")) {
        printf("   Skipping:  %-48s (unknown file)\n", sFile);
    }
    else if (strstr(sFlat, ".dat")) {
        printf("   Skipping:  %-48s (data file)\n", sFile);
    }
    else if (strstr(sFlat, ".zip")) {
        printf("   Skipping:  %-48s (zip file)\n", sFile);
    }
    else if (strstr(sFlat, ".arc")) {
        printf("   Skipping:  %-48s (archive file)\n", sFile);
    }
    else if (strstr(sFlat, ".psd")) {
        printf("   Skipping:  %-48s (PSD file)\n", sFile);
    }
    else {
        a.Insert(sFile);
    }
}

// +------------------------------------------------------------------+

void ins(DataArchive& a, int argc, char* argv[])
{
    char  sPath[256];
    char* pDirSep = 0;

    for (int i = 0; i < argc; i++) {
        if (strchr(argv[i], '*')) {
            strcpy(sPath, argv[i]);

            if ((pDirSep = strrchr(sPath, '\\')) != 0)
                *pDirSep = 0;
            else if ((pDirSep = strrchr(sPath, '/')) != 0)
                *pDirSep = 0;
            else
                sPath[0] = 0;

            WIN32_FIND_DATA   find;
            LPCWSTR tmp = (LPCWSTR)argv[i];
            HANDLE h = FindFirstFile(tmp, &find);
            if (h != INVALID_HANDLE_VALUE) {
                insertFile(a, sPath, &find);

                while (FindNextFile(h,&find)) {
                    insertFile(a, sPath, &find);
                }

            FindClose(h);
            }
        }
        else {
            a.Insert(argv[i]);
        }
    }
}

// +--------------------------------------------------------------------+

void build(DataArchive& a, const char* sBasePath);

void buildFile(DataArchive& a, const char* sPath, WIN32_FIND_DATA& find)
{
    if (find.cFileName[0] == '.') {
    } else if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
        char subdir[256];
        std::string sTemp;
        if (sPath && *sPath) {
            sTemp = sPath;
            sTemp += '/';
            WideCharToMultiByte(CP_ACP,0,find.cFileName,-1, subdir,260, NULL, NULL);
            sTemp += subdir;
            strcpy(subdir, sTemp.c_str());
        }
        else
            WideCharToMultiByte(CP_ACP,0,find.cFileName,-1, subdir,256, NULL, NULL);

        build(a, subdir);
    } else {
        insertFile(a, sPath, &find);
    }
}

void build(DataArchive& a, const char* sBasePath)
{
    char  sPath[256];
    char  sFind[256];

    if (sBasePath && *sBasePath) {
        strcpy(sPath, sBasePath);
        sprintf(sFind, "%s\\*.*", sPath);
    } else {
        sPath[0] = 0;
        strcpy(sFind, "*.*");
    }

    WIN32_FIND_DATA   find;
    std::wstring sTemp;
    std::string sStd = sFind;
    sTemp = ToWideString(sStd);
    HANDLE h = FindFirstFile(sTemp.c_str(), &find);
    if (h != INVALID_HANDLE_VALUE) {
        do buildFile(a, sPath, find);
        while (FindNextFile(h, &find));

    FindClose(h);
    }
}

void mak(DataArchive& a)
{
    build(a, 0);
}

// +--------------------------------------------------------------------+
// for now, pattern must be either "*" or "*.???"

int match(const char* sFile, const char* sPattern)
{
    int   nPatternType = 0;
    char* sExt = 0;

    const int PATTERN_NOWILD         = 0;
    const int PATTERN_STAR           = 1;
    const int PATTERN_STAR_DOT_STAR  = 2;
    const int PATTERN_STAR_DOT_EXT   = 3;

    // what kind of pattern matching?
    if (strchr(sPattern, '*')) {
        if (strchr(sPattern, '.')) {
            if (strcmp(sPattern, "*.*") == 0) {
                nPatternType = PATTERN_STAR_DOT_STAR;
            } else {
                nPatternType = PATTERN_STAR_DOT_EXT;
                sExt = const_cast<char*>(strchr(sPattern, '.'));
            }
        } else {
            nPatternType = PATTERN_STAR;
        }
    }

    int file_matches_pattern = 0;

    switch (nPatternType) {
        case PATTERN_NOWILD:
        default:
            file_matches_pattern = (_stricmp(sFile, sPattern) == 0);
        break;

        case PATTERN_STAR:
        case PATTERN_STAR_DOT_STAR:
            file_matches_pattern = 1;
        break;

        case PATTERN_STAR_DOT_EXT:
            file_matches_pattern = (strstr(sFile, sExt) != 0);
        break;
    }

    return file_matches_pattern;
}

void ext(DataArchive& a, int argc, char* argv[])
{
    if (argc) {
        char  sPath[256];
        char  sPatt[256];
        char* pDirSep;
        int   nPath;

        for (int i = 0; i < argc; i++) {
            if (strchr(argv[i], '*')) {
                strcpy(sPath, argv[i]);

                if ((pDirSep = strrchr(sPath, '\\')) != 0) {
                    strcpy(sPatt, pDirSep+1);
                    *pDirSep = 0;
                    nPath = strlen(sPath);
                } else if ((pDirSep = strrchr(sPath, '/')) != 0) {
                    strcpy(sPatt, pDirSep+1);
                    *pDirSep = 0;
                    nPath = strlen(sPath);
                } else {
                    strcpy(sPatt, sPath);
                    sPath[0] = 0;
                    nPath    = 0;
                }

                // for each file in the archive:
                for (unsigned j = 0; j < a.NumFiles(); j++) {
                    DataEntry* pde = a.GetFile(j);

                    if (pde) {
                        // if we are extracting from a sub-directory,
                        if (nPath) {
                            // and this file is in the sub-directory,
                            if (_strnicmp(pde->name, sPath, nPath) == 0) {
                                // and this file matches the pattern:
                                if (match(pde->name+nPath+1, sPatt)) {
                                    char sName[256];
                                    strcpy(sName, pde->name);
                                    a.Extract(sName);
                                }
                            }
                        } else {
                            // if we are extracting from the main directory,
                            // and this file is in the main directory,
                            if (strchr(pde->name, '/') == 0) {
                                // and this file matches the pattern:
                                if (match(pde->name, sPatt)) {
                                    char sName[256];
                                    strcpy(sName, pde->name);
                                    a.Extract(sName);
                                }
                            }
                        }
                    }
                }
            } else {
                // for each file in the archive:
                for (unsigned j = 0; j < a.NumFiles(); j++) {
                    DataEntry* pde = a.GetFile(j);

                    if (pde) {
                        if (_stricmp(pde->name, argv[i]) == 0) {
                            a.Extract(argv[i]);
                        }
                    }
                }
            }
        }
    } else { // full archive extraction:
        for (int i = 0; i < (int)a.NumFiles(); i++)
            a.Extract(a.GetFile(i)->name);
    }
}

void del(DataArchive& a, int argc, char* argv[])
{
    char  sPath[256];
    char  sPatt[256];
    char* pDirSep;
    int   nPath;

    for (int i = 0; i < argc; i++) {
        if (strchr(argv[i], '*')) {
            strcpy(sPath, argv[i]);

            if ((pDirSep = strrchr(sPath, '\\')) != 0) {
                strcpy(sPatt, pDirSep+1);
                *pDirSep = 0;
                nPath = strlen(sPath);
            } else if ((pDirSep = strrchr(sPath, '/')) != 0) {
                strcpy(sPatt, pDirSep+1);
                *pDirSep = 0;
                nPath = strlen(sPath);
            } else {
                strcpy(sPatt, sPath);
                sPath[0] = 0;
                nPath    = 0;
            }

            // for each file in the archive:
            for (unsigned j = 0; j < a.NumFiles(); j++) {
                DataEntry* pde = a.GetFile(j);

                if (pde) {
                    // if we are deleting from a sub-directory,
                    if (nPath) {
                        // and this file is in the sub-directory,
                        if (_strnicmp(pde->name, sPath, nPath) == 0) {
                            // and this file matches the pattern:
                            if (match(pde->name+nPath+1, sPatt)) {
                                char sName[256];
                                strcpy(sName, pde->name);
                                a.Remove(sName);
                            }
                        }
                    } else {
                        // if we are deleting from the main directory,
                        // and this file is in the main directory,
                        if (strchr(pde->name, '/') == 0) {
                            // and this file matches the pattern:
                            if (match(pde->name, sPatt)) {
                                char sName[256];
                                strcpy(sName, pde->name);
                                a.Remove(sName);
                            }
                        }
                    }
                }
            }
        } else {
            a.Remove(argv[i]);
        }
    }
}


// +--------------------------------------------------------------------+

void Usage()
{
    printf("Usage: datafile <dat-file> -option <file list>\n");
    printf("options:  -ins  (insert files into datafile)\n");
    printf("          -ext  (extract files from datafile)\n");
    printf("          -del  (delete files from datafile)\n");
    printf("          -mak  (insert all files in current directory and all subdirectories)\n");
    printf("          -list (display list of entries in datafile)\n");

    exit(-1);
}

#define OPT_NONE 0
#define OPT_INS  1
#define OPT_EXT  2
#define OPT_DEL  3
#define OPT_MAK  4
#define OPT_LIST 5

int main(int argc, char* argv[])
{
    printf("DATAFILE\n");

    if (argc < 3)
    Usage();

    DataArchive a(argv[1]);
    int option = OPT_NONE;

    if      (!_stricmp(argv[2], "-ins"))    option = OPT_INS;
    else if (!_stricmp(argv[2], "-ext"))    option = OPT_EXT;
    else if (!_stricmp(argv[2], "-del"))    option = OPT_DEL;
    else if (!_stricmp(argv[2], "-mak"))    option = OPT_MAK;
    else if (!_stricmp(argv[2], "-list"))   option = OPT_LIST;

    argc -= 3;
    argv += 3;

    switch (option) {
    default:
        case OPT_NONE:    Usage();             break;
        case OPT_INS:     ins(a, argc, argv);  break;
        case OPT_EXT:     ext(a, argc, argv);  break;
        case OPT_DEL:     del(a, argc, argv);  break;
        case OPT_MAK:     mak(a);              break;
        case OPT_LIST:    a.List();            break;
    }

    return 0;
}