toxon
/
lita-tox
Archived
1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lita-tox/ext/tox/tox.c

139 lines
3.3 KiB
C
Raw Normal View History

2015-07-25 13:16:16 +00:00
#include <ruby.h>
2015-09-09 16:36:01 +00:00
#include <tox/tox.h>
2015-07-25 13:16:16 +00:00
void Init_tox();
2015-09-11 20:22:28 +00:00
typedef struct cTox_ {
Tox *tox;
} cTox_;
2015-09-09 16:36:01 +00:00
static VALUE cTox;
2015-09-11 20:22:28 +00:00
static VALUE cTox_alloc(VALUE klass);
static void cTox_free(void *ptr);
static VALUE cTox_initialize(VALUE self, VALUE options);
2015-09-11 22:32:46 +00:00
static VALUE cTox_savedata(VALUE self);
2015-09-09 16:36:01 +00:00
2015-09-11 20:24:53 +00:00
typedef struct Tox_Options cTox_cOptions_;
2015-09-09 16:36:01 +00:00
static VALUE cTox_cOptions;
static VALUE cTox_cOptions_alloc(VALUE klass);
static void cTox_cOptions_free(void *ptr);
2015-09-09 19:00:18 +00:00
static VALUE cTox_cOptions_initialize(VALUE self);
2015-09-12 00:13:52 +00:00
static VALUE cTox_cOptions_data_EQUALS(VALUE self, VALUE data);
2015-09-09 16:36:01 +00:00
2015-07-25 13:16:16 +00:00
void Init_tox()
{
2015-09-09 16:36:01 +00:00
cTox = rb_define_class("Tox", rb_cObject);
2015-09-11 20:22:28 +00:00
rb_define_alloc_func(cTox, cTox_alloc);
rb_define_method(cTox, "initialize", cTox_initialize, 1);
2015-09-11 22:32:46 +00:00
rb_define_method(cTox, "savedata", cTox_savedata, 0);
2015-09-09 16:36:01 +00:00
cTox_cOptions = rb_define_class_under(cTox, "Options", rb_cObject);
rb_define_alloc_func(cTox_cOptions, cTox_cOptions_alloc);
2015-09-09 19:00:18 +00:00
rb_define_method(cTox_cOptions, "initialize", cTox_cOptions_initialize, 0);
2015-09-12 00:13:52 +00:00
rb_define_method(cTox_cOptions, "data=", cTox_cOptions_data_EQUALS, 1);
2015-09-09 16:36:01 +00:00
}
2015-09-11 20:22:28 +00:00
/******************************************************************************
* Tox
******************************************************************************/
VALUE cTox_alloc(const VALUE klass)
{
cTox_ *tox;
tox = ALLOC(cTox_);
return Data_Wrap_Struct(klass, NULL, cTox_free, tox);
}
void cTox_free(void *const ptr)
{
free(ptr);
}
VALUE cTox_initialize(const VALUE self, const VALUE options)
{
cTox_ *tox;
2015-09-11 20:24:53 +00:00
cTox_cOptions_ *tox_options;
2015-09-11 20:22:28 +00:00
2015-09-12 00:37:14 +00:00
TOX_ERR_NEW error;
// check if `options` is instance of `Tox::Options`
if (Qfalse == rb_funcall(options, rb_intern("is_a?"), 1, cTox_cOptions))
rb_raise(rb_eTypeError, "argument 1 should be Tox::Options");
2015-09-11 20:22:28 +00:00
Data_Get_Struct(self, cTox_, tox);
2015-09-11 20:24:53 +00:00
Data_Get_Struct(options, cTox_cOptions_, tox_options);
2015-09-11 20:22:28 +00:00
2015-09-12 00:37:14 +00:00
tox->tox = tox_new(tox_options, &error);
if (error != TOX_ERR_NEW_OK)
rb_raise(rb_eRuntimeError, "tox_new() failed");
2015-09-11 20:22:28 +00:00
return self;
}
VALUE cTox_savedata(const VALUE self)
2015-09-11 22:32:46 +00:00
{
cTox_ *tox;
size_t data_size;
char *data;
Data_Get_Struct(self, cTox_, tox);
data_size = tox_get_savedata_size(tox->tox);
data = ALLOC_N(char, data_size);
tox_get_savedata(tox->tox, (uint8_t*)data);
return rb_str_new(data, data_size);
}
2015-09-11 20:22:28 +00:00
/******************************************************************************
* Tox::Options
******************************************************************************/
2015-09-09 16:36:01 +00:00
VALUE cTox_cOptions_alloc(const VALUE klass)
{
2015-09-11 20:24:53 +00:00
cTox_cOptions_ *tox_options;
2015-09-09 16:36:01 +00:00
2015-09-11 20:24:53 +00:00
tox_options = ALLOC(cTox_cOptions_);
2015-09-09 16:36:01 +00:00
return Data_Wrap_Struct(klass, NULL, cTox_cOptions_free, tox_options);
}
void cTox_cOptions_free(void *const ptr)
{
free(ptr);
2015-07-25 13:16:16 +00:00
}
2015-09-09 19:00:18 +00:00
VALUE cTox_cOptions_initialize(const VALUE self)
{
2015-09-11 20:24:53 +00:00
cTox_cOptions_ *tox_options;
2015-09-09 19:00:18 +00:00
2015-09-11 20:24:53 +00:00
Data_Get_Struct(self, cTox_cOptions_, tox_options);
2015-09-09 19:00:18 +00:00
2015-09-11 20:24:53 +00:00
memset(tox_options, 0, sizeof(cTox_cOptions_));
2015-09-09 19:00:18 +00:00
tox_options_default(tox_options);
return self;
}
2015-09-12 00:13:52 +00:00
VALUE cTox_cOptions_data_EQUALS(const VALUE self, const VALUE savedata)
2015-09-12 00:13:52 +00:00
{
cTox_cOptions_ *tox_options;
Check_Type(savedata, T_STRING);
Data_Get_Struct(self, cTox_cOptions_, tox_options);
tox_options->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
tox_options->savedata_data = (uint8_t*)RSTRING_PTR(savedata);
tox_options->savedata_length = RSTRING_LEN(savedata);
return savedata;
}