Repository navigation
Expand file tree
/
Copy pathfile_mask.c
More file actions
111 lines (104 loc) · 2.99 KB
/
Copy pathfile_mask.c
File metadata and controls
111 lines (104 loc) · 2.99 KB
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
/* file_mask.c - matching file against a list of file masks */
#include "file_mask.h"
#include "file.h"
#include <stdlib.h>
#include <string.h>
/**
* Convert the given string to lower-case then put it into
* the specified array of 'file masks'.
*
* @param arr array of file masks
* @param mask a string to add
*/
static void file_mask_add(file_mask_array* vect, const char* mask)
{
rsh_vector_add_ptr(vect, str_tolower(mask));
}
/**
* Construct array from a comma-separated list of strings.
*
* @param comma_separated_list the comma-separated list of strings
* @return constructed array
*/
file_mask_array* file_mask_new_from_list(const char* comma_separated_list)
{
file_mask_array* vect = file_mask_new();
file_mask_add_list(vect, comma_separated_list);
return vect;
}
/**
* Split the given string by comma and put the parts into array.
*
* @param vect the array to put the parsed elements to
* @param comma_separated_list the string to split
*/
void file_mask_add_list(file_mask_array* vect, const char* comma_separated_list)
{
char* buf;
char* cur;
char* next;
if (!comma_separated_list || !*comma_separated_list) {
return;
}
buf = rsh_strdup(comma_separated_list);
for (cur = buf; cur && *cur; cur = next) {
next = strchr(cur, ',');
if (next) *(next++) = '\0';
if (*cur != '\0') file_mask_add(vect, cur);
}
free(buf);
}
/* Match a wildcard pattern against a string. */
static int wildcard_match(const char* wildcard, const char* str)
{
const char* star_wildcard = NULL; /* position of the last '*' in the wildcard */
const char* star_str = NULL; /* string position matched so far by that '*' */
while (*str) {
if (*wildcard == '?' || *wildcard == *str) {
wildcard++;
str++;
} else if (*wildcard == '*') {
/* remember the '*' position, assume it matches zero chars for now */
star_wildcard = wildcard++;
star_str = str;
} else if (star_wildcard) {
/* backtrack: let the last '*' consume one more byte of the string */
wildcard = star_wildcard + 1;
str = ++star_str;
} else {
return 0;
}
}
while (*wildcard == '*') wildcard++; /* a trailing '*' matches the empty string */
return *wildcard == '\0';
}
/**
* Match a file path against a list of wildcards.
* Usually used to match a filename against list of file extensions.
*
* @param vect the array of wildcards
* @param file the file path to match
* @return 1 if matched, 0 otherwise
*/
int file_mask_match(file_mask_array* vect, struct file_t* file)
{
size_t i;
char* lower_filepath;
const char* name = file_get_print_path(file, FPathUtf8 | FPathBaseName);
if (!name)
return 0;
/* empty file_mask_array must match any filename */
if (!vect || vect->size == 0)
return 1;
/* get a lowercase name version to ignore case when matching */
lower_filepath = str_tolower(name);
for (i = 0; i < vect->size; i++) {
const char* wildcard = (const char*)vect->array[i];
if (wildcard_match(wildcard, lower_filepath)) {
free(lower_filepath);
return 1; /* matched */
}
}
free(lower_filepath);
return 0;
}