rpm  5.4.10
rpmmi-rb.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 
7 #include "rpm-rb.h"
8 #include "rpmts-rb.h"
9 #include "rpmmi-rb.h"
10 #include "rpmhdr-rb.h"
11 
12 #ifdef NOTYET
13 #include <argv.h>
14 #endif
15 #include <mire.h>
16 
17 #include <rpmdb.h>
18 #include <rpmts.h>
19 #include <rpmio.h>
20 
21 #include "../debug.h"
22 
23 VALUE rpmmiClass;
24 
25 /*@unchecked@*/
26 static int _debug = 0;
27 
28 /* --- helpers */
29 static void *
30 rpmmi_ptr(VALUE s)
31 {
32  void *ptr;
33  Data_Get_Struct(s, void, ptr);
34  return ptr;
35 }
36 
37 /* --- Object methods */
38 static VALUE
39 rpmmi_each(VALUE s)
40 {
41  rpmmi mi = rpmmi_ptr(s);
42  Header h;
43  while((h = rpmmiNext(mi)) != NULL)
44  rb_yield (rpmrb_NewHdr(headerLink(h)));
45  return Qnil;
46 }
47 
48 static VALUE
49 rpmmi_next(VALUE s)
50 {
51  rpmmi mi = rpmmi_ptr(s);
52  Header h = rpmmiNext(mi);
53  return (h != NULL ? rpmrb_NewHdr(headerLink(h)) : Qnil);
54 }
55 
56 static VALUE
57 rpmmi_pattern(int argc, VALUE *argv, VALUE s)
58 {
59  rpmmi mi = rpmmi_ptr(s);
60  VALUE v_tag, v_pattern;
61 
62  rb_scan_args(argc, argv, "20", &v_tag, &v_pattern);
63 
64  rpmmiAddPattern(mi, FIX2INT(v_tag), RPMMIRE_REGEX,
65  StringValueCStr(v_pattern));
66 
67  return Qtrue;
68 }
69 
70 static void
71 initMethods(VALUE klass)
72 {
73  rb_define_method(klass, "each", rpmmi_each, 0);
74  rb_define_method(klass, "next", rpmmi_next, 0);
75  rb_define_method(klass, "pattern", rpmmi_pattern, -1);
76 }
77 
78 /* --- Object properties */
79 static VALUE
81 {
82 if (_debug)
83 fprintf(stderr, "==> %s(0x%lx)\n", __FUNCTION__, s);
84  return INT2FIX(_debug);
85 }
86 
87 static VALUE
88 rpmmi_debug_set(VALUE s, VALUE v)
89 {
90  return INT2FIX(_debug = FIX2INT(v));
91 }
92 
93 static VALUE
95 {
96  rpmmi mi = rpmmi_ptr(s);
97  return INT2FIX(rpmmiCount(mi));
98 }
99 
100 static VALUE
102 {
103  rpmmi mi = rpmmi_ptr(s);
104  return INT2FIX(rpmmiInstance(mi));
105 }
106 
107 static void
108 initProperties(VALUE klass)
109 {
110  rb_define_method(klass, "debug", rpmmi_debug_get, 0);
111  rb_define_method(klass, "debug=", rpmmi_debug_set, 1);
112  rb_define_method(klass, "length", rpmmi_count_get, 0);
113  rb_define_method(klass, "count", rpmmi_count_get, 0);
114  rb_define_method(klass, "offset", rpmmi_offset_get, 0);
115  rb_define_method(klass, "instance", rpmmi_offset_get, 0);
116 }
117 
118 /* --- Object ctors/dtors */
119 static void
121 {
122 if (_debug)
123 fprintf(stderr, "==> %s(%p)\n", __FUNCTION__, mi);
124  mi = rpmmiFree(mi);
125 }
126 
127 static VALUE
128 rpmmi_new(int argc, VALUE *argv, VALUE s)
129 {
130  VALUE v_ts, v_tag, v_key;
131  rpmts ts;
132  rpmTag _tag = RPMDBI_PACKAGES;
133  void * _key = NULL;
134  int _len = 0;
135  rpmmi mi;
136 
137  rb_scan_args(argc, argv, "12", &v_ts, &v_tag, &v_key);
138 
139  ts = rpmmi_ptr(v_ts);
140  if (!NIL_P(v_tag))
141  _tag = FIX2INT(v_tag);
142  if (!NIL_P(v_key))
143  _key = StringValueCStr(v_key);
144 
145  mi = rpmtsInitIterator(ts, _tag, _key, _len);
146 
147 if (_debug)
148 fprintf(stderr, "==> %s(%p[%d], 0x%lx) mi %p\n", __FUNCTION__, argv, argc, s, mi);
149  return Data_Wrap_Struct(s, 0, rpmmi_free, mi);
150 }
151 
152 /* --- Class initialization */
153 
154 void
156 {
157  rpmmiClass = rb_define_class("Mi", rb_cObject);
158 if (_debug)
159 fprintf(stderr, "==> %s() rpmmiClass 0x%lx\n", __FUNCTION__, rpmmiClass);
160  rb_include_module(rpmmiClass, rb_mEnumerable);
161  rb_define_singleton_method(rpmmiClass, "new", rpmmi_new, -1);
164 }
165 
166 VALUE
167 rpmrb_NewMi(void * _ts, int _tag, void * _key, int _len)
168 {
169  rpmmi mi = rpmtsInitIterator(_ts, _tag, _key, _len);
170  return Data_Wrap_Struct(rpmmiClass, 0, rpmmi_free, mi);
171 }