1 /*
2  *
3  * CLEX File Manager
4  *
5  * Copyright (C) 2001-2018 Vlado Potisk <vlado_potisk@clex.sk>
6  *
7  * CLEX is free software without warranty of any kind; see the
8  * GNU General Public License as set out in the "COPYING" document
9  * which accompanies the CLEX File Manager package.
10  *
11  * CLEX can be downloaded from http://www.clex.sk
12  *
13  */
14 
15 #include "clexheaders.h"
16 
17 #include <fnmatch.h>		/* fnmatch */
18 #include <wctype.h>			/* towlower() */
19 
20 #include "match.h"
21 
22 #include "mbwstring.h"		/* us_convert2mb() */
23 
24 /* match pattern */
25 
26 static USTRING pattern = UNULL;
27 
28 void
match_pattern_set(const wchar_t * expr)29 match_pattern_set(const wchar_t *expr)
30 {
31 	us_convert2mb(expr,&pattern);
32 }
33 
34 int
match_pattern(const char * word)35 match_pattern(const char *word)
36 {
37 	return fnmatch(USTR(pattern),word,FOPT(FOPT_ALL) ? 0 : FNM_PERIOD) == 0;
38 }
39 
40 /* match substring */
41 
42 static USTRINGW substr_orig = UNULL;	/* original */
43 static USTRINGW substr_lc   = UNULL;	/* lowercase copy */
44 static FLAG lc;							/* substr_lc is valid */
45 
46 static void
inplace_tolower(wchar_t * p)47 inplace_tolower(wchar_t *p)
48 {
49 	wchar_t ch;
50 
51 	for (; (ch = *p) != L'\0'; p++)
52 		if (iswupper(ch))
53 			*p = towlower(ch);
54 }
55 
56 void
match_substr_set(const wchar_t * expr)57 match_substr_set(const wchar_t *expr)
58 {
59 	usw_copy(&substr_orig,expr);
60 	lc = 0;
61 }
62 
63 int
match_substr(const wchar_t * str)64 match_substr(const wchar_t *str)
65 {
66 	if (FOPT(FOPT_IC))
67 		return match_substr_ic(str);
68 
69 	return wcsstr(str,USTR(substr_orig)) != 0;
70 }
71 
72 int
match_substr_ic(const wchar_t * str)73 match_substr_ic(const wchar_t *str)
74 {
75 	static USTRINGW buff = UNULL;
76 	wchar_t *str_lc;
77 
78 	if (!lc) {
79 		inplace_tolower(usw_copy(&substr_lc,USTR(substr_orig)));
80 		lc = 1;
81 	}
82 
83 	str_lc = usw_copy(&buff,str);
84 	inplace_tolower(str_lc);
85 	return wcsstr(str_lc,USTR(substr_lc)) != 0;
86 }
87