2009-10-05 Chris Toshok <toshok@ximian.com>
[moon.git] / plugin / plugin-utils.cpp
blob42b6dc04a07ccdc2d38ab0871335164358b1d2f2
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * plugin-utils.cpp:
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007-2009 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
15 #include <config.h>
17 #include "plugin-class.h"
19 enum MethodArgType {
20 MethodArgTypeNone = (0),
21 MethodArgTypeVoid = (1 << NPVariantType_Void),
22 MethodArgTypeNull = (1 << NPVariantType_Null),
23 MethodArgTypeBool = (1 << NPVariantType_Bool),
24 MethodArgTypeInt32 = (1 << NPVariantType_Int32),
25 MethodArgTypeDouble = (1 << NPVariantType_Double),
26 MethodArgTypeString = (1 << NPVariantType_String),
27 MethodArgTypeObject = (1 << NPVariantType_Object),
28 MethodArgTypeAny = (0xff)
31 static MethodArgType
32 decode_arg_ctype (char c)
34 switch (c) {
35 case 'v': return MethodArgTypeVoid;
36 case 'n': return MethodArgTypeNull;
37 case 'b': return MethodArgTypeBool;
38 case 'i': return MethodArgTypeInt32;
39 case 'd': return MethodArgTypeDouble;
40 case 's': return MethodArgTypeString;
41 case 'o': return MethodArgTypeObject;
42 case '*': return MethodArgTypeAny;
43 default:
44 return MethodArgTypeNone;
48 static MethodArgType
49 decode_arg_type (const char **in)
51 MethodArgType t, type = MethodArgTypeNone;
52 register const char *inptr = *in;
54 if (*inptr == '(') {
55 inptr++;
56 while (*inptr && *inptr != ')') {
57 t = decode_arg_ctype (*inptr);
58 type = (MethodArgType) ((int) type | (int) t);
59 inptr++;
61 } else {
62 type = decode_arg_ctype (*inptr);
65 inptr++;
66 *in = inptr;
68 return type;
71 /**
72 * check_arg_list:
73 * @arglist: a string representing an arg-list token (see grammar below)
74 * @args: NPVariant argument count
75 * @argv: NPVariant argument vector
77 * Checks that the NPVariant arguments satisfy the argument count and
78 * types expected (provided via @typestr).
80 * The @typestr argument should follow the following syntax:
82 * simple-arg-type ::= "v" / "n" / "b" / "i" / "d" / "s" / "o" / "*"
83 * ; each char represents one of the following
84 * ; NPVariant types: Void, Null, Bool, Int32,
85 * ; Double, String, Object and wildcard
87 * arg-type ::= simple-arg-type / "(" 1*(simple-arg-type) ")"
89 * optional-args ::= "[" *(arg-type) "]"
91 * arg-list ::= *(arg-type) (optional-args)
94 * Returns: %true if @argv matches the arg-list criteria specified in
95 * @arglist or %false otherwise.
96 **/
97 bool
98 check_arg_list (const char *arglist, guint32 argc, const NPVariant *argv)
100 const char *inptr = arglist;
101 MethodArgType mask;
102 guint32 i = 0;
104 // check all of the required arguments
105 while (*inptr && *inptr != '[' && i < argc) {
106 mask = decode_arg_type (&inptr);
107 if (!(mask & (1 << argv[i].type))) {
108 // argv[i] does not match any of the expected types
109 return false;
112 i++;
115 if (*inptr && *inptr != '[' && i < argc) {
116 // we were not provided enough arguments
117 return false;
120 // now check all of the optional arguments
121 inptr++;
122 while (*inptr && *inptr != ']' && i < argc) {
123 mask = decode_arg_type (&inptr);
124 if (!(mask & (1 << argv[i].type))) {
125 // argv[i] does not match any of the expected types
126 return false;
129 i++;
132 if (i < argc) {
133 // we were provided too many arguments
134 return false;
137 return true;