Permalink
Browse files

Start converting to use of a handle/object.

Creates croakalpm macro, for use in typemap. Adapts alpm_intialize and
alpm_release for new handle and arguments.
  • Loading branch information...
1 parent 8083bdd commit 0b1ff4ffa65cd64e9db68af0d633819f5c80d171 @juster committed Jan 18, 2012
Showing with 67 additions and 19 deletions.
  1. +27 −5 ALPM.xs
  2. +4 −0 alpm_xs.h
  3. +36 −14 typemap
View
32 ALPM.xs
@@ -96,11 +96,33 @@ alpm_pkg_load ( filename, ... )
#negative_is_error
#alpm_db_unregister_all ()
-negative_is_error
-alpm_initialize ()
-
-negative_is_error
-alpm_release ()
+ALPM_Handle
+alpm_initialize(root, dbpath)
+ char * root
+ char * dbpath
+ PREINIT:
+ enum _alpm_errno_t err;
+ ALPM_Handle h;
+ CODE:
+ h = alpm_initialize(root, dbpath, &err);
+ if(h == NULL){
+ croak("ALPM Error: %s", alpm_strerror(err));
+ }
+ RETVAL = h
+ OUTPUT:
+ RETVAL
+
+void
+alpm_release(self)
+ ALPM_Handle self;
+ PREINIT:
+ int ret;
+ CODE:
+ ret = alpm_release(self);
+ if(ret == -1){
+ croak("ALPM Error: failed to release ALPM handle");
+ }
+ # errno is only inside a handle, which was just released...
#----------------------------------------------------------------------------
# DATABASE FUNCTIONS
View
4 alpm_xs.h
@@ -9,6 +9,7 @@
#include <alpm.h>
typedef int negative_is_error;
+typedef alpm_handle_t * ALPM_Handle;
typedef pmdb_t * ALPM_DB;
typedef pmpkg_t * ALPM_Package;
typedef pmpkg_t * ALPM_PackageFree;
@@ -45,6 +46,9 @@ extern const char * log_lvl_debug;
extern const char * log_lvl_function;
extern const char * log_lvl_unknown;
+/* GENERAL PURPOSE **********************************************************/
+#define croakalpm(PRE) croak(PRE " Error: %s", alpm_strerror(self))
+
/* CALLBACKS ****************************************************************/
#define DEF_SET_CALLBACK( CBTYPE ) \
View
50 typemap
@@ -1,6 +1,7 @@
TYPEMAP
negative_is_error I_NEG_IS_ERROR
+ALPM_Handle T_ALPM_HANDLE
ALPM_DB T_ALPM_DB
ALPM_Package T_ALPM_PACKAGE
ALPM_PackageFree T_ALPM_PACKAGEFREE
@@ -27,6 +28,13 @@ INPUT
I_NEG_IS_ERROR
$var = SvIV($arg)
+T_ALPM_HANDLE
+ if(sv_derived_from($arg, \"ALPM\")){
+ $var = INT2PTR($type, SvIV((SV *)SvRV($arg)));
+ }else{
+ Perl_croak(\"error: expected an object of class ALPM\");
+ }
+
T_ALPM_DB
if ( sv_derived_from($arg, \"ALPM::DB\") ) {
IV tmp = SvIV( (SV*)SvRV($arg) );
@@ -256,28 +264,42 @@ T_PKGREASON
OUTPUT
I_NEG_IS_ERROR
- if ( $var != 0 )
- croak( "ALPM Error: %s", alpm_strerror( pm_errno ));
- $arg = newSViv( 1 );
+ if($var != 0){
+ croakalpm("ALPM");
+ }
+ $arg = newSViv(1);
+
+T_ALPM_HANDLE
+ if($var == NULL){
+ # croakalpm expects a "alpm_handle_t *self" var to exist.
+ croak(\"error: ALPM handle check failed\");
+ }else{
+ sv_setref_pv($arg, \"ALPM\", (void *)$var);
+ }
T_ALPM_DB
- if ( $var == NULL )
- croak( "ALPM DB Error: %s", alpm_strerror( pm_errno ));
- sv_setref_pv( $arg, \"ALPM::DB\", (void *)$var );
+ if($var == NULL){
+ croakalpm("ALPM DB");
+ }
+ sv_setref_pv($arg, \"ALPM::DB\", (void *)$var);
T_ALPM_PACKAGE
- if ( $var == NULL )
- croak( "ALPM Package Error: %s", alpm_strerror( pm_errno ));
- sv_setref_pv( $arg, \"ALPM::Package\", (void *)$var );
+ if($var == NULL){
+ croakalpm("ALPM Package");
+ }
+ sv_setref_pv($arg, \"ALPM::Package\", (void *)$var);
T_ALPM_PACKAGEFREE
- if ( $var == NULL )
- croak( "ALPM Package Error: %s", alpm_strerror( pm_errno ));
- sv_setref_pv( $arg, \"ALPM::PackageFree\", (void *)$var );
+ if($var == NULL){
+ croakalpm("ALPM Package");
+ }
+ sv_setref_pv($arg, \"ALPM::PackageFree\", (void *)$var);
T_ALPM_PACKAGEORNULL
- if ( $var == NULL ) $arg = &PL_sv_undef;
- sv_setref_pv( $arg, \"ALPM::Package\", (void *)$var );
+ if($var == NULL){
+ $arg = &PL_sv_undef;
+ }
+ sv_setref_pv($arg, \"ALPM::Package\", (void *)$var);
T_ALPM_GROUP
if ( $var == NULL ) {

0 comments on commit 0b1ff4f

Please sign in to comment.