rpm  5.4.4
ruby/rpmmi-rb.c
Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include "rpm-rb.h"
00008 #include "rpmts-rb.h"
00009 #include "rpmmi-rb.h"
00010 #include "rpmhdr-rb.h"
00011 
00012 #ifdef  NOTYET
00013 #include <argv.h>
00014 #endif
00015 #include <mire.h>
00016 
00017 #include <rpmdb.h>
00018 #include <rpmts.h>
00019 #include <rpmio.h>
00020 
00021 #include "../debug.h"
00022 
00023 VALUE rpmmiClass;
00024 
00025 /*@unchecked@*/
00026 static int _debug = 0;
00027 
00028 /* --- helpers */
00029 static void *
00030 rpmmi_ptr(VALUE s)
00031 {
00032     void *ptr;
00033     Data_Get_Struct(s, void, ptr);
00034     return ptr;
00035 }
00036 
00037 /* --- Object methods */
00038 static VALUE
00039 rpmmi_each(VALUE s)
00040 {
00041     rpmmi mi = rpmmi_ptr(s);
00042     Header h;
00043     while((h = rpmmiNext(mi)) != NULL)
00044         rb_yield (rpmrb_NewHdr(headerLink(h)));
00045     return Qnil;
00046 }
00047 
00048 static VALUE
00049 rpmmi_next(VALUE s)
00050 {
00051     rpmmi mi = rpmmi_ptr(s);
00052     Header h = rpmmiNext(mi);
00053     return (h != NULL ? rpmrb_NewHdr(headerLink(h)) : Qnil);
00054 }
00055 
00056 static VALUE
00057 rpmmi_pattern(int argc, VALUE *argv, VALUE s)
00058 {
00059     rpmmi mi = rpmmi_ptr(s);
00060     VALUE v_tag, v_pattern;
00061 
00062     rb_scan_args(argc, argv, "20", &v_tag, &v_pattern);
00063 
00064     rpmmiAddPattern(mi, FIX2INT(v_tag), RPMMIRE_REGEX,
00065                 StringValueCStr(v_pattern));
00066 
00067     return Qtrue;
00068 }
00069 
00070 static void
00071 initMethods(VALUE klass)
00072 {
00073     rb_define_method(klass, "each", rpmmi_each, 0);
00074     rb_define_method(klass, "next", rpmmi_next, 0);
00075     rb_define_method(klass, "pattern", rpmmi_pattern, -1);
00076 }
00077 
00078 /* --- Object properties */
00079 static VALUE
00080 rpmmi_debug_get(VALUE s)
00081 {
00082 if (_debug)
00083 fprintf(stderr, "==> %s(0x%lx)\n", __FUNCTION__, s);
00084     return INT2FIX(_debug);
00085 }
00086 
00087 static VALUE
00088 rpmmi_debug_set(VALUE s, VALUE v)
00089 {
00090     return INT2FIX(_debug = FIX2INT(v));
00091 }
00092 
00093 static VALUE
00094 rpmmi_count_get(VALUE s)
00095 {
00096     rpmmi mi = rpmmi_ptr(s);
00097     return INT2FIX(rpmmiCount(mi));
00098 }
00099 
00100 static VALUE
00101 rpmmi_offset_get(VALUE s)
00102 {
00103     rpmmi mi = rpmmi_ptr(s);
00104     return INT2FIX(rpmmiInstance(mi));
00105 }
00106 
00107 static void
00108 initProperties(VALUE klass)
00109 {
00110     rb_define_method(klass, "debug", rpmmi_debug_get, 0);
00111     rb_define_method(klass, "debug=", rpmmi_debug_set, 1);
00112     rb_define_method(klass, "length", rpmmi_count_get, 0);
00113     rb_define_method(klass, "count", rpmmi_count_get, 0);
00114     rb_define_method(klass, "offset", rpmmi_offset_get, 0);
00115     rb_define_method(klass, "instance", rpmmi_offset_get, 0);
00116 }
00117 
00118 /* --- Object ctors/dtors */
00119 static void
00120 rpmmi_free(rpmmi mi)
00121 {
00122 if (_debug)
00123 fprintf(stderr, "==> %s(%p)\n", __FUNCTION__, mi);
00124     mi = rpmmiFree(mi);
00125 }
00126 
00127 static VALUE
00128 rpmmi_new(int argc, VALUE *argv, VALUE s)
00129 {
00130     VALUE v_ts, v_tag, v_key;
00131     rpmts ts;
00132     rpmTag _tag = RPMDBI_PACKAGES;
00133     void * _key = NULL;
00134     int _len = 0;
00135     rpmmi mi;
00136 
00137     rb_scan_args(argc, argv, "12", &v_ts, &v_tag, &v_key);
00138 
00139     ts = rpmmi_ptr(v_ts);
00140     if (!NIL_P(v_tag))
00141         _tag = FIX2INT(v_tag);
00142     if (!NIL_P(v_key))
00143         _key = StringValueCStr(v_key);
00144 
00145     mi = rpmtsInitIterator(ts, _tag, _key, _len);
00146 
00147 if (_debug)
00148 fprintf(stderr, "==> %s(%p[%d], 0x%lx) mi %p\n", __FUNCTION__, argv, argc, s, mi);
00149     return Data_Wrap_Struct(s, 0, rpmmi_free, mi);
00150 }
00151 
00152 /* --- Class initialization */
00153 
00154 void
00155 Init_rpmmi(void)
00156 {
00157     rpmmiClass = rb_define_class("Mi", rb_cObject);
00158 if (_debug)
00159 fprintf(stderr, "==> %s() rpmmiClass 0x%lx\n", __FUNCTION__, rpmmiClass);
00160     rb_include_module(rpmmiClass, rb_mEnumerable);
00161     rb_define_singleton_method(rpmmiClass, "new", rpmmi_new, -1);
00162     initProperties(rpmmiClass);
00163     initMethods(rpmmiClass);
00164 }
00165 
00166 VALUE
00167 rpmrb_NewMi(void * _ts, int _tag, void * _key, int _len)
00168 {
00169     rpmmi mi = rpmtsInitIterator(_ts, _tag, _key, _len);
00170     return Data_Wrap_Struct(rpmmiClass, 0, rpmmi_free, mi);
00171 }