-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_class.c
69 lines (51 loc) · 1.58 KB
/
string_class.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
#include"strglob.h"
#include"string_class.h"
static const size_t arrlen(char **strs) {
register size_t acnt = 1;
for(register char**pp = strs;*pp;pp++)
acnt++;
return (const size_t)acnt;
}
/*! @fn int string_class(const char *sclnm, STR_GLOB *urglb)
*
* @brief copy strings from string class defined by `sclnm` argument to the
* string glob data structure passed in as `urglb`
*
* @param [in] sclnm
*
* @param [in] urglb
*
* @return zero on success and non-zero on error
*
* @see char_class
*
* @note stype_strs is a static string array containg the name element of each
* string group data structure
*/
int string_class(const char *sclnm, STR_GLOB *urglb) {
int invalid_class = 1;
for(register char **restrict snp = stype_strs;*snp;++snp) {
if(!strcmp(*snp, sclnm)) {
invalid_class = 0;
break;
}
}
if(invalid_class)
return true; /* the `sclnm` string was not associated with any string class */
urglb->type = 5; /* string class */
register const STRING_GROUP *restrict sgp = String_Groups;
while(sgp && sgp->name && *(sgp->name)) { /* iterate over string groups array while current element and its name are non-null or empty */
if(!strcmp(sgp->name, sclnm)) {
const size_t al = 1 + arrlen(sgp->strs);
urglb->out = malloc(al * sizeof *(urglb->out));
if(!urglb->out)
exit_verbose("malloc", __FILE__, __LINE__);
register char **restrict pp = urglb->out, **restrict wp = sgp->strs;
while(*wp)
*pp++ = *wp++;
pp[7] = NULL;
}
sgp++;
}
return false;
}