File: plugin_files.cpp

package info (click to toggle)
bzflag 2.4.30-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,488 kB
  • sloc: cpp: 150,376; ansic: 3,463; sh: 2,535; makefile: 2,194; perl: 486; python: 260; objc: 246; php: 206
file content (445 lines) | stat: -rw-r--r-- 10,873 bytes parent folder | download
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
/* bzflag
 * Copyright (c) 1993-2025 Tim Riker
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <stdio.h>
#include <stdarg.h>
#include <sstream>
#include "plugin_files.h"
#include "plugin_utils.h"



#ifdef _WIN32
#define _DirDelim '\\'

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <io.h>
#include <direct.h>

bool WindowsAddFileStack(const char *szPathName, const char* fileMask,
                         bool bRecursive,std::vector<std::string> &list,
                         bool justDirs = false)
{
    struct _finddata_t fileInfo;

    long hFile;
    std::string searchstr;

    std::string FilePath;

    bool bDone = false;

    searchstr = szPathName;
    searchstr += "\\";
    if (bRecursive)
        searchstr += "*.*";
    else if (fileMask)
        searchstr += fileMask;
    else
        searchstr += "*.*";

    std::string extensionSearch;

    if (fileMask && strchr(fileMask,'.'))
        extensionSearch = strchr(fileMask,'.')+1;

    hFile = (long)_findfirst(searchstr.c_str(),&fileInfo);

    if (hFile != -1)
    {
        while (!bDone)
        {
            if ((strlen(fileInfo.name) >0) && (strcmp(fileInfo.name,".") != 0) &&
                    (strcmp(fileInfo.name,"..") != 0))
            {
                FilePath = szPathName;
                //if (!(fileInfo.attrib & _A_SUBDIR))
                FilePath += "\\";
                FilePath += fileInfo.name;

                if (justDirs && (fileInfo.attrib & _A_SUBDIR))
                {
                    // we never do just dirs recursively
                    list.push_back(FilePath);
                }
                else if (!justDirs)
                {
                    if ((fileInfo.attrib & _A_SUBDIR) && bRecursive)
                        WindowsAddFileStack(FilePath.c_str(),fileMask,bRecursive,list);

                    else if (!(fileInfo.attrib & _A_SUBDIR))
                    {
                        if (bRecursive && fileMask)
                        {
                            // if we are recursive we need to check extension manually,
                            // so we get dirs and stuff
                            if (strrchr(FilePath.c_str(),'.'))
                            {
                                if (compare_nocase(std::string(strrchr(FilePath.c_str(),'.')+1),
                                                   extensionSearch) == 0)
                                    list.push_back(FilePath);
                            }
                        }
                        else
                            list.push_back(FilePath);
                    }
                }
            }
            if (_findnext(hFile,&fileInfo) == -1)
                bDone = true;
        }
    }
    return true;
}
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <ctype.h>

#define _DirDelim '/'
static int match_multi(const char **mask, const char **string)
{
    const char *msk;
    const char *str;
    const char *msktop;
    const char *strtop;

    msk = *mask;
    str = *string;

    while ((*msk != '\0') && (*msk == '*'))
        msk++;  /* get rid of multiple '*'s */

    if (*msk == '\0') /* '*' was last, auto-match */
        return +1;

    msktop = msk;
    strtop = str;

    while (*msk != '\0')
    {
        if (*msk == '*')
        {
            *mask = msk;
            *string = str;
            return 0; /* matched this segment */
        }
        else if (*str == '\0')
        {
            return -1; /* can't match */
        }
        else
        {
            if ((*msk == '?') || (*msk == *str))
            {
                msk++;
                str++;
                if ((*msk == '\0') && (*str != '\0'))   /* advanced check */
                {
                    str++;
                    strtop++;
                    str = strtop;
                    msk = msktop;
                }
            }
            else
            {
                str++;
                strtop++;
                str = strtop;
                msk = msktop;
            }
        }
    }

    *mask = msk;
    *string = str;
    return +1; /* full match */
}

static int match_mask(const char *mask, const char *string)
{
    if (mask == NULL)
        return 0;

    if (string == NULL)
        return 0;

    if ((mask[0] == '*') && (mask[1] == '\0'))
        return 1; /* instant match */

    while (*mask != '\0')
    {
        if (*mask == '*')
        {
            mask++;
            switch (match_multi(&mask, &string))
            {
            case +1:
                return 1;
            case -1:
                return 0;
            }
        }
        else if (*string == '\0')
            return 0;

        else if ((*mask == '?') || (*mask == *string))
        {
            mask++;
            string++;
        }
        else
            return 0;
    }

    if (*string == '\0')
        return 1;
    else
        return 0;
}

bool LinuxAddFileStack(const char *szPathName, const char* fileMask,
                       bool bRecursive, std::vector<std::string> &list,
                       bool justDirs = false)
{
    DIR *directory;
    dirent *fileInfo;
    struct stat statbuf;
    std::string FilePath;

    std::string searchstr = szPathName;
    if (searchstr.empty() || *(searchstr.end()-1) != _DirDelim)
        searchstr += _DirDelim;
    directory = opendir(searchstr.c_str());
    if (!directory)
        return false;

    // TODO: make it use the filemask
    while ((fileInfo = readdir(directory)))
    {
        if (!((strcmp(fileInfo->d_name, ".") == 0) ||
                (strcmp(fileInfo->d_name, "..") == 0)))
        {
            FilePath = searchstr;
            FilePath += fileInfo->d_name;


            stat(FilePath.c_str(), &statbuf);

            if (justDirs && S_ISDIR(statbuf.st_mode))
            {
                // we never do just dirs recursively
                list.push_back(FilePath);
            }
            else if (!justDirs)
            {
                if (S_ISDIR(statbuf.st_mode) && bRecursive)
                    LinuxAddFileStack(FilePath.c_str(),fileMask,bRecursive, list);
                else if (match_mask (fileMask, fileInfo->d_name))
                    list.push_back(FilePath);
            }
        }
    }
    closedir(directory);
    return true;
}
#endif

// ensures all the delims are constant
std::string convertPathToDelims(const char* file)
{
    if (!file)
        return std::string();

    std::string delim;
    delim += _DirDelim;
    return replace_all(replace_all(file,"/",delim),"\\",delim);
}

std::vector<std::string> getFilesInDir(const char* dir, const char* filter,
                                       bool recrusive)
{
    std::vector<std::string> list;
    if (!dir)
        return list;

    std::string realFilter = "*.*";
    if (filter)
        realFilter = filter;

    std::string directory  = convertPathToDelims(dir);

    if (directory.size() && *(directory.end()-1) == _DirDelim)
        directory.erase(directory.end()-1);

#ifdef _WIN32
    WindowsAddFileStack(directory.c_str(), realFilter.c_str(),recrusive,list);
#else
    LinuxAddFileStack(directory.c_str(), realFilter.c_str(),recrusive,list);
#endif
    return list;
}

std::vector<std::string> getDirsInDir(const char* dir)
{
    std::vector<std::string> list;
    if (!dir)
        return list;

    std::string directory  = convertPathToDelims(dir);

#ifdef _WIN32
    WindowsAddFileStack(directory.c_str(), "*.*",false,list,true);
#else
    LinuxAddFileStack(directory.c_str(), "*.*",false,list,true);
#endif
    return list;
}

std::string getPathForOS(const char* file)
{
    return convertPathToDelims(file);
}

std::string concatPaths ( const char* path1, const char* path2 )
{
    std::string ret = getPathForOS(path1);
    ret += getPathForOS(path2);

    return ret;
}

std::string getFileDir(const char* file)
{
    char *f = strdup(convertPathToDelims(file).c_str());
    if (!f)
        return std::string();

    char *p = strrchr(f,_DirDelim);
    if (p)
        // it's ok to go one past, because even if it's the end, that's the
        // NUL char so we can set it to NUL again without worry
        *(p+1) = 0;

    std::string ret = f;
    free(f);
    return ret;
}

std::string getFileExtension(const char* file)
{
    std::string f = convertPathToDelims(file);

    const char *p = strrchr(f.c_str(),'.');
    if (!p)
        // it's ok to go one past, because even if it's the end, that's the
        // NUL char so we can set it to NUL again without worry
        return std::string();

    return std::string(p+1);
}

std::string getFileTitle(const char* file)
{
    char *f = strdup(convertPathToDelims(file).c_str());
    if (!f)
        return std::string();
    char *temp = f;

    char *p = strrchr(f,_DirDelim);
    if (p)
        // it's ok to go one past, because even if it's the end, that's the
        // NUL char so we can set it to NUL again without worry
        temp = p+1;

    char *p2 = strrchr(temp,'.');
    if (p2)
        *(p2) = 0; // kill the . too

    std::string ret = temp;
    free(f);
    return ret;
}

std::string getFileText(const char* file)
{
    std::string text;
    if (!file)
        return text;

    FILE *fp = fopen(convertPathToDelims(file).c_str(),"rb");
    if (!fp)
        return text;

    fseek(fp,0,SEEK_END);
    unsigned int i  = (unsigned int)ftell(fp);
    fseek(fp,0,SEEK_SET);

    char *temp = (char*)malloc(i+1);
    temp[i] = 0;

    size_t items_read = fread(temp,i,1,fp);
    fclose(fp);

    if (items_read == 1)
        text = temp;

    free(temp);

    return replace_all(text,"\r",std::string());
}

std::vector<std::string> getFileTextLines(const char* file)
{
    return tokenize(getFileText(file),"\n",0,false);
}

unsigned int getFileLen(const char* file)
{
    if (!file)
        return 0;

    FILE *fp = fopen(convertPathToDelims(file).c_str(),"rb");
    if (!fp)
        return 0;

    fseek(fp,0,SEEK_END);
    unsigned int i = (unsigned int)ftell(fp);
    fclose(fp);

    return i;
}

bool fileExists ( const char *path )
{
    if (!path)
        return false;

    FILE *fp = fopen(convertPathToDelims(path).c_str(),"rb");
    if (!fp)
        return false;
    fclose(fp);

    return true;
}

// Local Variables: ***
// mode: C++ ***
// tab-width: 4 ***
// c-basic-offset: 4 ***
// indent-tabs-mode: nil ***
// End: ***
// ex: shiftwidth=4 tabstop=4