-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.tmp
More file actions
executable file
·137 lines (101 loc) · 5.15 KB
/
sample.tmp
File metadata and controls
executable file
·137 lines (101 loc) · 5.15 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
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
/* ( (
* )\ ) ( ) d'Arc)\ ) (
* (()/( ( )\ ( /( ( ( (()/( ) ) )\ (
* /(_)) )\ ((_))\()) ))\ )( /(_))( /( ( ` ) ((_) ))\
* (_))_|((_) _ (_))/ /((_)(()\ (_)) )(_)) )\ ' /(/( _ /(( )
* | |_ (_)| || |_ (_)) ((_) / __|((_)_ _((_)) ((_)_\ | |(_)) /
* | __| | || || _|/ -_) | '_| \__ \/ _` || ' \()| '_ \)| |/ -_)
* |_| |_||_| \__|\___| |_| |___/\__,_||_|_|_| | .__/ |_|\___|
* |_|
*
* This is a sample for d'Arc 0.2.0, a Lua sub API for Lua and LuaJIT
*
* Swearword filter: returns true when a swear is found in a value.
*
* > =filter.check("dork")
* false
*
* > =filter.check({"nice", "tender", "bitch")
* true
*
* This is not even entirely fictitious: word filters are historically
* annoyingly resource consuming. Still, this only for illustration.
*/
#include <string.h>
#include "darc.h"
int inspect(const char *string);
/*****************************************************************************\
* core function: traverse a table looking at each element *
\*****************************************************************************/
/* Relevant things: 1) the MACROS and 2) the DARC_TRAVERSE() function. */
int word_filter(TValue *variable, void *state)
{
/* if a string: screen it for bad words */
if(XLUA_IS_STRING(variable))
/* screen it */
return inspect(XLUA_STRING(variable));
/* if a table: traverse it */
else if(XLUA_IS_TABLE(variable))
/* traverse table, using this function as callback, for each element */
return darc_traverse(XLUA_TABLE(variable), word_filter, state);
return 1;
}
/* Check out filter4bench.c for how this is done using the official API. */
/*****************************************************************************\
* detect a foul word *
\*****************************************************************************/
/* nothing relevant in this section */
int inspect(const char *string)
{
if(strstr(string, "bitch") == 0)
return 1;
/* yeah, genius. But you get the point. */
return 0;
}
/****************************************************************************)\
*** LUA HOOKS *(*)
\*****************************************************************************/
/* nothing relevant in this section */
static int filter_check (lua_State *L);
static const struct luaL_Reg filter [] = {
{"check", filter_check }, /* main function: check strings for swearwords */
{ NULL, NULL }
};
/*---------------------------------------------------------------------------*\
* Main function: Test a Lua value for whether it contains swear words *
\*---------------------------------------------------------------------------*/
/* nothing relevant in this section */
static int filter_check(lua_State *L) {
lua_settop(L, 1); // arguments, max: 1
TValue *subject = index2addr(L, -1);
lua_pop(L, 1);
int ok = word_filter(subject, (void *)0);
lua_pushboolean(L, !ok);
return 1;
}
/*--------------------------------------------------------------------- ( ----*\
* Registration function for opening the filter module (,\ *
\*----------------------------------------------------------------------------*/
/*
* We register a function of a different name for Lua and LuaJIT respectively.
*
* Because as a convention, we always compile two libs, in this case:
* 'filter' for Lua, 'filterjit' for LuaJIT. That's one for each target,
* Lua and LuaJIT. And we want to have the name of the resulting lib
* be identical to the function registered here, which opens the module.
*
* Using the official API, one and the same lib works for both Lua and LuaJIT.
* Not so for d'Arc, by definition, because it bypasses the official API.
*/
#ifdef LUA_5_1
LUALIB_API int luaopen_filter (lua_State *L) {
luaL_register(L, "filter", filter); /* registering this module (PUC Lua) */
return 1;
}
#endif
#ifdef JIT_2
LUALIB_API int luaopen_filterjit /* <<-- 'jit' */ (lua_State *L) {
luaL_register(L, "filter", filter); /* registering this module (LuaJIT) */
return 1;
}
#endif