This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
a_string.c
154 lines (137 loc) · 3.07 KB
/
a_string.c
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
/*
* Author : Zarjazz
* Email : [email protected]
* ----
* $Id: a_string.c,v 1.1 2002/07/02 16:41:22 zarjazz Exp $
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
/*
* ================
* a_strstrip
* ----------------
* remove initial and trailing white-space char's from a string
* ================
*/
char *a_strstrip(char *str)
{
int l;
assert(str);
/* skip pre-non-valid chars */
while (*str && isspace(*str))
str++;
/* remove trailing white space chars */
l = strlen(str);
while (l > 0 && isspace(str[--l]))
str[l] = '\0';
/* return the stripped string */
return str;
}
/*
* ================
* a_strcpy
* ----------------
* buffer safe string copy
* ================
*/
char *a_strcpy(char *dst, const char *src, int dst_len)
{
char *save = dst;
assert(src);
assert(dst);
/* copy string and ensure null terminated */
while (*src && (--dst_len > 0))
*dst++ = *src++;
*dst = '\0';
return save;
}
/*
* ================
* a_strextract
* ----------------
* extract the first non-space token from 'src' and
* ensure string is null terminated. returns the
* string postition after extracted token
* ================
*/
char *a_strextract(char *dst, const char *src, int dst_len)
{
assert(src);
assert(dst);
/* skip pre-non-valid chars */
while (*src && isspace(*src))
src++;
/* copy string and ensure null terminated */
while (*src && (--dst_len > 0) && !isspace(*src))
*dst++ = *src++;
/* skip post-non-valid chars */
while (*src && isspace(*src))
src++;
*dst = '\0';
return (char *) src;
}
/*
* ================
* a_strmatch
* ----------------
* does a string match using a very simple "regex" (ie only special characters are: "*?\")
* ================
* */
int a_strmatch(const char *str, const char *reg, int case_sensitive)
{
assert(str);
assert(reg);
/* compare strings */
while (*str && *reg) {
switch (*reg) {
case '*':
/* FIXME: '..*?..' support broken */
while (*reg == '*' || *reg == '?')
reg++;
switch (*reg) {
case '\0':
return 1;
case '\\':
if (*++reg == '\0')
return 0;
default:
if (case_sensitive) {
while ((str = strchr(str, *reg)))
if (a_strmatch(str++, reg, case_sensitive))
return 1;
} else {
char *save = (char *) str;
while ((str = strchr(str, tolower(*reg))))
if (a_strmatch(str++, reg, case_sensitive))
return 1;
str = save;
while ((str = strchr(str, toupper(*reg))))
if (a_strmatch(str++, reg, case_sensitive))
return 1;
}
return 0;
}
break;
case '?':
str++, reg++;
break;
case '\\':
if (*++reg == '\0')
return 0;
default:
if (case_sensitive) {
if (*str++ != *reg++)
return 0;
} else {
if (tolower(*str++) != tolower(*reg++))
return 0;
}
break;
}
}
if (*str || *reg)
return 0;
return 1;
}