Listing 1
static VALUE Record_subscript(VALUE self, VALUE index)
{
VALUE ret;
switch(rb_type(index))
{
case T_STRING:
ret = Record_subscript_string(self, StringValuePtr(index), 1);
break;
case T_FIXNUM:
ret = Record_subscript_fixnum(self, FIX2INT(index), 1);
break;
default:
rb_raise(rb_eTypeError, "field index / name must be integer or string");
break;
}
return ret;
}
static VALUE Record_subscript_string(VALUE self, char const *index, int bThrowOnFail)
{
ORJRecord const *record = Record_get_record_(self);
ORJFieldA const *field = ORJ_Record_FindFieldByNameA(record, index,
NULL);
if(NULL == field)
{
if(bThrowOnFail)
{
rb_raise(cFieldNameError,
"record does not contain field named: %s", index);
}
return Qnil;
}
else
{
return rb_str_from_ORJStringA(&field->value);
}
}
static VALUE Record_subscript_fixnum(VALUE self, int index, int bThrowOnFail)
{
ORJRecord const *record = Record_get_record_(self);
size_t cFields = ORJ_Record_GetNumFieldsA(record);
if( 0 <= index &&
index < cFields)
{
VALUE __database__ = rb_iv_get(self, "@__database__");
return Field_create_(__database__, self, &record->fields[index]);
}
else
{
if(bThrowOnFail)
{
rb_raise(rb_eIndexError, "record does not contain field at index: %d", index);
}
return Qnil;
}
}