-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-utils.cpp
More file actions
137 lines (120 loc) · 3.3 KB
/
plugin-utils.cpp
File metadata and controls
137 lines (120 loc) · 3.3 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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* plugin-utils.cpp:
*
* Contact:
* Moonlight List (moonlight-list@lists.ximian.com)
*
* Copyright 2007-2009 Novell, Inc. (http://www.novell.com)
*
* See the LICENSE file included with the distribution for details.
*
*/
#include <config.h>
#include "plugin-class.h"
enum MethodArgType {
MethodArgTypeNone = (0),
MethodArgTypeVoid = (1 << NPVariantType_Void),
MethodArgTypeNull = (1 << NPVariantType_Null),
MethodArgTypeBool = (1 << NPVariantType_Bool),
MethodArgTypeInt32 = (1 << NPVariantType_Int32),
MethodArgTypeDouble = (1 << NPVariantType_Double),
MethodArgTypeString = (1 << NPVariantType_String),
MethodArgTypeObject = (1 << NPVariantType_Object),
MethodArgTypeAny = (0xff)
};
static MethodArgType
decode_arg_ctype (char c)
{
switch (c) {
case 'v': return MethodArgTypeVoid;
case 'n': return MethodArgTypeNull;
case 'b': return MethodArgTypeBool;
case 'd': return (MethodArgType) (MethodArgTypeDouble | MethodArgTypeInt32);
case 's': return MethodArgTypeString;
case 'o': return MethodArgTypeObject;
case '*': return MethodArgTypeAny;
default:
return MethodArgTypeNone;
}
}
static MethodArgType
decode_arg_type (const char **in)
{
MethodArgType t, type = MethodArgTypeNone;
register const char *inptr = *in;
if (*inptr == '(') {
inptr++;
while (*inptr && *inptr != ')') {
t = decode_arg_ctype (*inptr);
type = (MethodArgType) ((int) type | (int) t);
inptr++;
}
} else {
type = decode_arg_ctype (*inptr);
}
inptr++;
*in = inptr;
return type;
}
/**
* check_arg_list:
* @arglist: a string representing an arg-list token (see grammar below)
* @args: NPVariant argument count
* @argv: NPVariant argument vector
*
* Checks that the NPVariant arguments satisfy the argument count and
* types expected (provided via @typestr).
*
* The @typestr argument should follow the following syntax:
*
* simple-arg-type ::= "v" / "n" / "b" / "d" / "s" / "o" / "*"
* ; each char represents one of the following
* ; NPVariant types: Void, Null, Bool, Int32 or
* ; Double, String, Object and wildcard
*
* arg-type ::= simple-arg-type / "(" 1*(simple-arg-type) ")"
*
* optional-args ::= "[" *(arg-type) "]"
*
* arg-list ::= *(arg-type) (optional-args)
*
*
* Returns: %true if @argv matches the arg-list criteria specified in
* @arglist or %false otherwise.
**/
bool
check_arg_list (const char *arglist, guint32 argc, const NPVariant *argv)
{
const char *inptr = arglist;
MethodArgType mask;
guint32 i = 0;
// check all of the required arguments
while (*inptr && *inptr != '[' && i < argc) {
mask = decode_arg_type (&inptr);
if (!(mask & (1 << argv[i].type))) {
// argv[i] does not match any of the expected types
return false;
}
i++;
}
if (*inptr && *inptr != '[' && i < argc) {
// we were not provided enough arguments
return false;
}
// now check all of the optional arguments
inptr++;
while (*inptr && *inptr != ']' && i < argc) {
mask = decode_arg_type (&inptr);
if (!(mask & (1 << argv[i].type))) {
// argv[i] does not match any of the expected types
return false;
}
i++;
}
if (i < argc) {
// we were provided too many arguments
return false;
}
return true;
}