From 6d0c675377e26da4b42fe8d553db22a8af78cfa3 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Sat, 12 Sep 2015 16:16:30 +0000 Subject: [PATCH] Implement Tox#id --- ext/tox/extconf.rb | 1 + ext/tox/tox.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/ext/tox/extconf.rb b/ext/tox/extconf.rb index 751174b..3a56001 100755 --- a/ext/tox/extconf.rb +++ b/ext/tox/extconf.rb @@ -9,5 +9,6 @@ have_library LIBTOXCORE, 'tox_options_default' have_library LIBTOXCORE, 'tox_new' have_library LIBTOXCORE, 'tox_get_savedata_size' have_library LIBTOXCORE, 'tox_get_savedata' +have_library LIBTOXCORE, 'tox_self_get_address' create_makefile "#{NAME}/#{NAME}" diff --git a/ext/tox/tox.c b/ext/tox/tox.c index 43d844c..7303719 100644 --- a/ext/tox/tox.c +++ b/ext/tox/tox.c @@ -13,6 +13,7 @@ static VALUE cTox_alloc(VALUE klass); static void cTox_free(void *ptr); static VALUE cTox_initialize(VALUE self, VALUE options); static VALUE cTox_savedata(VALUE self); +static VALUE cTox_id(VALUE self); typedef struct Tox_Options cTox_cOptions_; @@ -28,6 +29,7 @@ void Init_tox() rb_define_alloc_func(cTox, cTox_alloc); rb_define_method(cTox, "initialize", cTox_initialize, 1); rb_define_method(cTox, "savedata", cTox_savedata, 0); + rb_define_method(cTox, "id", cTox_id, 0); cTox_cOptions = rb_define_class_under(cTox, "Options", rb_cObject); rb_define_alloc_func(cTox_cOptions, cTox_cOptions_alloc); @@ -92,6 +94,25 @@ VALUE cTox_savedata(const VALUE self) return rb_str_new(data, data_size); } +VALUE cTox_id(const VALUE self) +{ + cTox_ *tox; + + char address[TOX_ADDRESS_SIZE]; + char id[2 * TOX_ADDRESS_SIZE]; + + unsigned long i; + + Data_Get_Struct(self, cTox_, tox); + + tox_self_get_address(tox->tox, (uint8_t*)address); + + for (i = 0; i < TOX_ADDRESS_SIZE; ++i) + sprintf(&id[2 * i], "%02X", address[i] & 0xFF); + + return rb_str_new(id, 2 * TOX_ADDRESS_SIZE); +} + /****************************************************************************** * Tox::Options ******************************************************************************/