DarkPlaces
Game engine based on the Quake 1 engine by id Software, developed by LadyHavoc
 
filematch.c File Reference
#include <dirent.h>
#include "darkplaces.h"
+ Include dependency graph for filematch.c:

Go to the source code of this file.

Functions

static void adddirentry (stringlist_t *list, const char *path, const char *name)
 
void listdirectory (stringlist_t *list, const char *basepath, const char *path)
 
int matchpattern (const char *in, const char *pattern, int caseinsensitive)
 
int matchpattern_with_separator (const char *in, const char *pattern, int caseinsensitive, const char *separators, qbool wildcard_least_one)
 
void stringlistappend (stringlist_t *list, const char *text)
 
void stringlistfreecontents (stringlist_t *list)
 
void stringlistinit (stringlist_t *list)
 
void stringlistsort (stringlist_t *list, qbool uniq)
 
static int stringlistsort_cmp (const void *a, const void *b)
 

Function Documentation

◆ adddirentry()

static void adddirentry ( stringlist_t * list,
const char * path,
const char * name )
static

Definition at line 159 of file filematch.c.

160{
161 if (strcmp(name, ".") && strcmp(name, ".."))
162 {
163 char temp[MAX_OSPATH];
164 dpsnprintf( temp, sizeof( temp ), "%s%s", path, name );
165 stringlistappend(list, temp);
166 }
167}
int dpsnprintf(char *buffer, size_t buffersize, const char *format,...)
Returns the number of printed characters, excluding the final '\0' or returns -1 if the buffer isn't ...
Definition common.c:997
void stringlistappend(stringlist_t *list, const char *text)
Definition filematch.c:103
const GLchar * name
Definition glquake.h:601
#define MAX_OSPATH
max length of a filesystem pathname
Definition qdefs.h:175

References dpsnprintf(), MAX_OSPATH, name, and stringlistappend().

Referenced by listdirectory().

◆ listdirectory()

void listdirectory ( stringlist_t * list,
const char * basepath,
const char * path )

Definition at line 198 of file filematch.c.

199{
200 char fullpath[MAX_OSPATH];
201 DIR *dir;
202 struct dirent *ent;
203 dpsnprintf(fullpath, sizeof(fullpath), "%s%s", basepath, path);
204#ifdef __ANDROID__
205 // SDL currently does not support listing assets, so we have to emulate
206 // it. We're using relative paths for assets, so that will do.
207 if (basepath[0] != '/')
208 {
209 char listpath[MAX_OSPATH];
210 qfile_t *listfile;
211 dpsnprintf(listpath, sizeof(listpath), "%sls.txt", fullpath);
212 char *buf = (char *) FS_SysLoadFile(listpath, tempmempool, true, NULL);
213 if (!buf)
214 return;
215 char *p = buf;
216 for (;;)
217 {
218 char *q = strchr(p, '\n');
219 if (q == NULL)
220 break;
221 *q = 0;
222 adddirentry(list, path, p);
223 p = q + 1;
224 }
225 Mem_Free(buf);
226 return;
227 }
228#endif
229 dir = opendir(fullpath);
230 if (!dir)
231 return;
232
233 while ((ent = readdir(dir)))
234 adddirentry(list, path, ent->d_name);
235 closedir(dir);
236}
static void adddirentry(stringlist_t *list, const char *path, const char *name)
Definition filematch.c:159
unsigned char * FS_SysLoadFile(const char *path, mempool_t *pool, qbool quiet, fs_offset_t *filesizepointer)
Definition fs.c:3555
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition glquake.h:657
#define NULL
Definition qtypes.h:12
vec2 dir
mempool_t * tempmempool
Definition zone.c:794
#define Mem_Free(mem)
Definition zone.h:96

References adddirentry(), buf, dir, dpsnprintf(), FS_SysLoadFile(), MAX_OSPATH, Mem_Free, NULL, and tempmempool.

Referenced by FS_AddGameDirectory(), FS_ListGameDirs(), FS_Search(), FS_SysCheckGameDir(), and ModList_RebuildList().

◆ matchpattern()

int matchpattern ( const char * in,
const char * pattern,
int caseinsensitive )

Definition at line 16 of file filematch.c.

17{
18 return matchpattern_with_separator(in, pattern, caseinsensitive, "/\\:", false);
19}
int matchpattern_with_separator(const char *in, const char *pattern, int caseinsensitive, const char *separators, qbool wildcard_least_one)
Definition filematch.c:23

References matchpattern_with_separator().

Referenced by Curl_FindPackURL(), FS_Search(), PRVM_ED_Print(), PRVM_Globals_f(), and R_ReplaceWorldTexture_f().

◆ matchpattern_with_separator()

int matchpattern_with_separator ( const char * in,
const char * pattern,
int caseinsensitive,
const char * separators,
qbool wildcard_least_one )

Definition at line 23 of file filematch.c.

24{
25 int c1, c2;
26 while (*pattern)
27 {
28 switch (*pattern)
29 {
30 case 0:
31 return 1; // end of pattern
32 case '?': // match any single character
33 if (*in == 0 || strchr(separators, *in))
34 return 0; // no match
35 in++;
36 pattern++;
37 break;
38 case '*': // match anything until following string
39 if(wildcard_least_one)
40 {
41 if (*in == 0 || strchr(separators, *in))
42 return 0; // no match
43 in++;
44 }
45 pattern++;
46 while (*in)
47 {
48 if (strchr(separators, *in))
49 break;
50 // see if pattern matches at this offset
51 if (matchpattern_with_separator(in, pattern, caseinsensitive, separators, wildcard_least_one))
52 return 1;
53 // nope, advance to next offset
54 in++;
55 }
56 break;
57 default:
58 if (*in != *pattern)
59 {
60 if (!caseinsensitive)
61 return 0; // no match
62 c1 = *in;
63 if (c1 >= 'A' && c1 <= 'Z')
64 c1 += 'a' - 'A';
65 c2 = *pattern;
66 if (c2 >= 'A' && c2 <= 'Z')
67 c2 += 'a' - 'A';
68 if (c1 != c2)
69 return 0; // no match
70 }
71 in++;
72 pattern++;
73 break;
74 }
75 }
76 if (*in)
77 return 0; // reached end of pattern but not end of input
78 return 1; // success
79}

References matchpattern_with_separator().

Referenced by Cmd_Apropos_f(), Cmd_List_f(), Cvar_List_f(), Key_History_Find_All(), Key_History_Find_Backwards(), Key_History_Find_Forwards(), match_rule(), matchpattern(), matchpattern_with_separator(), RCon_Authenticate(), and VM_buf_cvarlist().

◆ stringlistappend()

void stringlistappend ( stringlist_t * list,
const char * text )

Definition at line 103 of file filematch.c.

104{
105 size_t textlen;
106 char **oldstrings;
107
108 if (list->numstrings >= list->maxstrings)
109 {
110 oldstrings = list->strings;
111 list->maxstrings += 4096;
112 list->strings = (char **) Z_Malloc(list->maxstrings * sizeof(*list->strings));
113 if (list->numstrings)
114 memcpy(list->strings, oldstrings, list->numstrings * sizeof(*list->strings));
115 if (oldstrings)
116 Z_Free(oldstrings);
117 }
118 textlen = strlen(text) + 1;
119 list->strings[list->numstrings] = (char *) Z_Malloc(textlen);
120 memcpy(list->strings[list->numstrings], text, textlen);
121 list->numstrings++;
122}
float strlen(string s)
char ** strings
Definition filematch.h:31
int maxstrings
maxstrings changes as needed, causing reallocation of strings[] array
Definition filematch.h:29
#define Z_Malloc(size)
Definition zone.h:161
#define Z_Free(data)
Definition zone.h:164

References stringlist_t::maxstrings, stringlist_t::numstrings, stringlist_t::strings, strlen(), Z_Free, and Z_Malloc.

Referenced by adddirentry(), Con_CompleteCommandLine(), FS_ListGameDirs(), and FS_Search().

◆ stringlistfreecontents()

void stringlistfreecontents ( stringlist_t * list)

Definition at line 87 of file filematch.c.

88{
89 int i;
90 for (i = 0;i < list->numstrings;i++)
91 {
92 if (list->strings[i])
93 Z_Free(list->strings[i]);
94 list->strings[i] = NULL;
95 }
96 list->numstrings = 0;
97 list->maxstrings = 0;
98 if (list->strings)
99 Z_Free(list->strings);
100 list->strings = NULL;
101}
int i

References i, stringlist_t::maxstrings, NULL, stringlist_t::numstrings, stringlist_t::strings, and Z_Free.

Referenced by Con_CompleteCommandLine(), FS_AddGameDirectory(), FS_ListGameDirs(), FS_Search(), FS_SysCheckGameDir(), and ModList_RebuildList().

◆ stringlistinit()

void stringlistinit ( stringlist_t * list)

Definition at line 82 of file filematch.c.

83{
84 memset(list, 0, sizeof(*list));
85}

Referenced by Con_CompleteCommandLine(), FS_AddGameDirectory(), FS_ListGameDirs(), FS_Search(), FS_SysCheckGameDir(), and ModList_RebuildList().

◆ stringlistsort()

void stringlistsort ( stringlist_t * list,
qbool uniq )

Definition at line 129 of file filematch.c.

130{
131 int i, j;
132 if(list->numstrings < 1)
133 return;
134 qsort(&list->strings[0], list->numstrings, sizeof(list->strings[0]), stringlistsort_cmp);
135 if(uniq)
136 {
137 // i: the item to read
138 // j: the item last written
139 for (i = 1, j = 0; i < list->numstrings; ++i)
140 {
141 char *save;
142 if(!strcasecmp(list->strings[i], list->strings[j]))
143 continue;
144 ++j;
145 save = list->strings[j];
146 list->strings[j] = list->strings[i];
147 list->strings[i] = save;
148 }
149 for(i = j+1; i < list->numstrings; ++i)
150 {
151 if (list->strings[i])
152 Z_Free(list->strings[i]);
153 }
154 list->numstrings = j+1;
155 }
156}
static int stringlistsort_cmp(const void *a, const void *b)
Definition filematch.c:124

References i, stringlist_t::numstrings, stringlistsort_cmp(), stringlist_t::strings, and Z_Free.

Referenced by Con_CompleteCommandLine(), FS_AddGameDirectory(), FS_ListGameDirs(), FS_Search(), and ModList_RebuildList().

◆ stringlistsort_cmp()

static int stringlistsort_cmp ( const void * a,
const void * b )
static

Definition at line 124 of file filematch.c.

125{
126 return strcasecmp(*(const char **)a, *(const char **)b);
127}
dp_FragColor b
ret a

References a, and b.

Referenced by stringlistsort().