pgplot 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/ext/kwarg.c ADDED
@@ -0,0 +1,78 @@
1
+ /*
2
+ kwarg.c : Process keyword arguments for Ruby
3
+
4
+ Copyright (c) 2001 Masahiro TANAKA <masa@ir.isas.ac.jp>
5
+
6
+ This program is free software.
7
+ You can distribute/modify this program
8
+ under the same terms as Ruby itself.
9
+ NO WARRANTY.
10
+ */
11
+ #include <ruby.h>
12
+
13
+ /* void rb_scan_kw_args __((VALUE, ...)); */
14
+
15
+ static VALUE
16
+ kw_hash_i(i, tmp)
17
+ VALUE i, tmp;
18
+ {
19
+ VALUE key;
20
+
21
+ key = RARRAY_PTR(i)[0];
22
+ if (TYPE(key)==T_SYMBOL) {
23
+ key = rb_funcall(key, rb_intern("id2name"), 0);
24
+ } else
25
+ if (TYPE(key)!=T_STRING) {
26
+ rb_raise(rb_eArgError, "keywords must be String or Symbol");
27
+ }
28
+
29
+ rb_hash_aset(tmp, key, RARRAY_PTR(i)[1]);
30
+ return Qnil;
31
+ }
32
+
33
+ #ifdef HAVE_STDARG_PROTOTYPES
34
+ #include <stdarg.h>
35
+ #define va_init_list(a,b) va_start(a,b)
36
+ #else
37
+ #include <varargs.h>
38
+ #define va_init_list(a,b) va_start(a)
39
+ #endif
40
+
41
+ void
42
+ #ifdef HAVE_STDARG_PROTOTYPES
43
+ rb_scan_kw_args(VALUE hash, ...)
44
+ #else
45
+ rb_scan_kw_args(hash, va_alist)
46
+ VALUE hash;
47
+ va_dcl
48
+ #endif
49
+ {
50
+ char *key;
51
+ VALUE *var, val, str, tmp;
52
+ va_list vargs;
53
+
54
+ va_init_list(vargs, hash);
55
+
56
+ tmp = rb_hash_new();
57
+ if (TYPE(hash) == T_HASH)
58
+ rb_iterate(rb_each, hash, kw_hash_i, tmp);
59
+ else if (hash != Qnil)
60
+ rb_fatal("rb_san_kw_args: non-hash arg passed");
61
+
62
+ for (;;) {
63
+ key = va_arg(vargs, char*);
64
+ if (!key) break;
65
+ var = va_arg(vargs, VALUE*);
66
+ str = rb_str_new2(key);
67
+ val = rb_funcall(tmp, rb_intern("delete"), 1, str);
68
+ if (var) *var = val;
69
+ }
70
+
71
+ if (rb_funcall(tmp, rb_intern("empty?"), 0)==Qfalse) {
72
+ val = rb_funcall(tmp, rb_intern("keys"), 0);
73
+ val = rb_funcall(val, rb_intern("join"), 1, rb_str_new2(","));
74
+ rb_raise(rb_eArgError, "unknown keywords: %s",StringValuePtr(val));
75
+ }
76
+
77
+ va_end(vargs);
78
+ }