aboutsummaryrefslogtreecommitdiffstats
path: root/dvbv5_dvb-t/fi-Nokia_Siuro
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2014-05-13 22:21:41 +0100
committerJonathan McCrohan <jmccrohan@gmail.com>2014-05-13 22:21:41 +0100
commitf5a002e361636dd41dc84a5fa1a2e1ee77862edb (patch)
treee940c5d28325c6fb8578b3b8b785f9f5cc2749d2 /dvbv5_dvb-t/fi-Nokia_Siuro
parent8ba2155000fb193a9f99d9065066159513e4a109 (diff)
downloaddtv-scan-tables-f5a002e361636dd41dc84a5fa1a2e1ee77862edb.tar.gz
Imported Upstream version 0+git20140512.1246b27upstream/0+git20140512.1246b27
Diffstat (limited to 'dvbv5_dvb-t/fi-Nokia_Siuro')
-rw-r--r--dvbv5_dvb-t/fi-Nokia_Siuro23
1 files changed, 23 insertions, 0 deletions
diff --git a/dvbv5_dvb-t/fi-Nokia_Siuro b/dvbv5_dvb-t/fi-Nokia_Siuro
new file mode 100644
index 0000000..1ba3cca
--- /dev/null
+++ b/dvbv5_dvb-t/fi-Nokia_Siuro
@@ -0,0 +1,23 @@
+# 2014-04-18 Antti Palosaari <crope@iki.fi>
+# generated from http://www.digita.fi/kuluttajat/tv/nakyvyysalueet/kanavanumerot_ja_taajuudet
+
+[Nokia_Siuro]
+ DELIVERY_SYSTEM = DVBT
+ FREQUENCY = 714000000
+ BANDWIDTH_HZ = 8000000
+
+[Nokia_Siuro]
+ DELIVERY_SYSTEM = DVBT
+ FREQUENCY = 746000000
+ BANDWIDTH_HZ = 8000000
+
+[Nokia_Siuro]
+ DELIVERY_SYSTEM = DVBT
+ FREQUENCY = 530000000
+ BANDWIDTH_HZ = 8000000
+
+[Nokia_Siuro]
+ DELIVERY_SYSTEM = DVBT
+ FREQUENCY = 690000000
+ BANDWIDTH_HZ = 8000000
+
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
#include <ruby.h>
#include <libconfig.h>

static VALUE cConfig, cConfigBaseSetting, cConfigSetting, cConfigAggregate;
static VALUE cConfigFormatDefault, cConfigFormatHex;
static VALUE cConfigFixnum, cConfigBignum, cConfigFloat, cConfigBoolean, cConfigString;
static VALUE cConfigGroup, cConfigList, cConfigArray;

static VALUE rSettingNameRegexp;
static VALUE aConfigSettings, aConfigScalars, aConfigAggregates;
static VALUE eConfigParseError, eSettingNotFoundError, eSettingFormatError, eSettingNameError;

static VALUE rconfig_wrap_value(config_setting_t* setting)
{
  switch(config_setting_type(setting)) {
    case CONFIG_TYPE_INT:
      return LONG2FIX(config_setting_get_int(setting));
    
    case CONFIG_TYPE_INT64:
      return rb_ll2inum(config_setting_get_int64(setting));
    
    case CONFIG_TYPE_FLOAT:
      return rb_float_new(config_setting_get_float(setting));
    
    case CONFIG_TYPE_STRING:
      return rb_str_new2(config_setting_get_string(setting));
    
    case CONFIG_TYPE_BOOL:
      return config_setting_get_bool(setting) ? Qtrue : Qfalse;
    
    default:
      rb_bug("unknown value type %d", config_setting_type(setting));
  }
}

static void rconfig_free_setting(config_setting_t* setting)
{
  // dummy
}

static VALUE rconfig_prepare_setting(config_setting_t* setting)
{
  VALUE wrapper = Data_Wrap_Struct(rb_cObject, 0, rconfig_free_setting, setting);
  config_setting_set_hook(setting, (void*) wrapper);
  return wrapper;
}

static void rconfig_destroy_setting(void* hook)
{
  if(hook != NULL) {
    VALUE wrapper = (VALUE) hook;
    rb_iv_set(wrapper, "@setting", Qnil);
  }
}

static VALUE rconfig_wrap_setting(config_setting_t* setting)
{
  VALUE rbSetting = rconfig_prepare_setting(setting);
  
  switch(config_setting_type(setting)) {
    case CONFIG_TYPE_INT:
      return rb_funcall(cConfigFixnum, rb_intern("new"), 2, LONG2FIX(config_setting_get_int(setting)), rbSetting);
    
    case CONFIG_TYPE_INT64:
      return rb_funcall(cConfigBignum, rb_intern("new"), 2, rb_ll2inum(config_setting_get_int64(setting)), rbSetting);
    
    case CONFIG_TYPE_FLOAT:
      return rb_funcall(cConfigFloat, rb_intern("new"), 2, rb_float_new(config_setting_get_float(setting)), rbSetting);
    
    case CONFIG_TYPE_STRING:
      return rb_funcall(cConfigString, rb_intern("new"), 2, rb_str_new2(config_setting_get_string(setting)), rbSetting);
    
    case CONFIG_TYPE_BOOL:
      return rb_funcall(cConfigBoolean, rb_intern("new"), 2, config_setting_get_bool(setting) ? Qtrue : Qfalse, rbSetting);
    
    case CONFIG_TYPE_ARRAY:
      return rb_funcall(cConfigArray, rb_intern("new"), 2, Qnil, rbSetting);
    
    case CONFIG_TYPE_LIST:
      return rb_funcall(cConfigList, rb_intern("new"), 1, rbSetting);
    
    case CONFIG_TYPE_GROUP:
      return rb_funcall(cConfigGroup, rb_intern("new"), 1, rbSetting);
    
    default:
      rb_bug("[r] unknown setting type %d", config_setting_type(setting));
  }
}

static void rconfig_update_setting(config_setting_t* setting, VALUE value)
{
  switch(config_setting_type(setting)) {
    case CONFIG_TYPE_INT:
      config_setting_set_int(setting, FIX2LONG(value));
      break;
    
    case CONFIG_TYPE_INT64:
      if(TYPE(value) == T_BIGNUM)
        config_setting_set_int64(setting, rb_big2ll(value));
      else // T_FIXNUM
        config_setting_set_int64(setting, FIX2INT(value));
      break;
    
    case CONFIG_TYPE_FLOAT:
// ruby1.9 check
#if HAVE_RB_BLOCK_CALL
      config_setting_set_float(setting, RFLOAT(value)->float_value);
#else
      config_setting_set_float(setting, RFLOAT(value)->value);
#endif
      break;
    
    case CONFIG_TYPE_STRING:
      config_setting_set_string(setting, RSTRING_PTR(value));
      break;
    
    case CONFIG_TYPE_BOOL:
      config_setting_set_bool(setting, value == Qtrue);
      break;
    
    default:
      rb_bug("[w] unknown setting type %d", config_setting_type(setting));
  }
}

static void rconfig_check_setting_type(VALUE object, VALUE value)
{
  if(rb_obj_class(object) == cConfigFixnum) {
    Check_Type(value, T_FIXNUM);
  } else if(rb_obj_class(object) == cConfigBignum) {
    if(TYPE(value) != T_BIGNUM && TYPE(value) != T_FIXNUM)
      rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or Bignum)", rb_obj_classname(value));
  } else if(rb_obj_class(object) == cConfigFloat) {
    Check_Type(value, T_FLOAT);
  } else if(rb_obj_class(object) == cConfigString) {
    Check_Type(value, T_STRING);
  } else if(rb_obj_class(object) == cConfigBoolean) {
    if(value != Qtrue && value != Qfalse)
      rb_raise(rb_eTypeError, "wrong argument type %s (expected boolean)", rb_obj_classname(value));
  } else {
    rb_raise(rb_eException, "never use Config::Setting itself");
  }
}

static int rconfig_do_append(config_setting_t* setting, VALUE target, VALUE name)
{
  int type;
  if(rb_obj_class(target) == cConfigFixnum)
    type = CONFIG_TYPE_INT;
  else if(rb_obj_class(target) == cConfigBignum)
    type = CONFIG_TYPE_INT64;
  else if(rb_obj_class(target) == cConfigFloat)
    type = CONFIG_TYPE_FLOAT;
  else if(rb_obj_class(target) == cConfigString)
    type = CONFIG_TYPE_STRING;
  else if(rb_obj_class(target) == cConfigBoolean)
    type = CONFIG_TYPE_BOOL;
  else if(rb_obj_class(target) == cConfigGroup)
    type = CONFIG_TYPE_GROUP;
  else if(rb_obj_class(target) == cConfigList)
    type = CONFIG_TYPE_LIST;
  else if(rb_obj_class(target) == cConfigArray)
    type = CONFIG_TYPE_ARRAY;
  else
    rb_bug("unknown setting class %s", rb_obj_classname(target));
  
  config_setting_t* new_setting;
  if(name == Qnil) {
    new_setting = config_setting_add(setting, NULL, type);
  } else {
    Check_Type(name, T_STRING);
    new_setting = config_setting_add(setting, RSTRING_PTR(name), type);
  }
  
  if(new_setting == NULL)
    return 0;
  
  VALUE rbNewSetting = rconfig_prepare_setting(new_setting);
  rb_iv_set(target, "@setting", rbNewSetting);
  
  if(rb_ary_includes(aConfigScalars, rb_obj_class(target)) == Qtrue)
    rconfig_update_setting(new_setting, rb_iv_get(target, "@value"));

  if(rb_ary_includes(aConfigAggregates, rb_obj_class(target)) == Qtrue) {
    if(rb_obj_class(target) == cConfigGroup) {
      VALUE hash = rb_iv_get(target, "@hash");
      VALUE children = rb_funcall(hash, rb_intern("keys"), 0);
      int i;
      for(i = 0; i < RARRAY_LEN(children); i++) {
        VALUE key = RARRAY_PTR(children)[i];
        rconfig_do_append(new_setting, rb_hash_aref(hash, key), key);
      }
    } else {
      VALUE children = rb_iv_get(target, "@list");
      int i;
      for(i = 0; i < RARRAY_LEN(children); i++) {
        rconfig_do_append(new_setting, RARRAY_PTR(children)[i], Qnil);
      }
    }
  }
  
  return 1;
}

static VALUE rbConfigBaseSetting_initialize(VALUE self, VALUE setting)
{
  if(setting != Qnil)
    Check_Type(setting, T_DATA);
  rb_iv_set(self, "@setting", setting);
  
  return self;
}

static VALUE rbConfigBaseSetting_name(VALUE self)
{
  if(rb_iv_get(self, "@setting") != Qnil) {
    config_setting_t* setting = NULL;
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
    return rb_str_new2(config_setting_name(setting));
  } else {
    return Qnil;
  }
}

static VALUE rbConfigBaseSetting_parent(VALUE self)
{
  if(rb_iv_get(self, "@setting") != Qnil) {
    config_setting_t* setting = NULL;
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
    return rconfig_wrap_setting(config_setting_parent(setting));
  } else {
    return Qnil;
  }
}

static VALUE rbConfigBaseSetting_is_root(VALUE self)
{
  if(rb_iv_get(self, "@setting") != Qnil) {
    config_setting_t* setting = NULL;
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
    return config_setting_is_root(setting) ? Qtrue : Qfalse;
  } else {
    return Qnil;
  }
}

static VALUE rbConfigBaseSetting_index(VALUE self)
{
  if(rb_iv_get(self, "@setting") != Qnil) {
    config_setting_t* setting = NULL;
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
    return INT2FIX(config_setting_index(setting));
  } else {
    return Qnil;
  }
}

static VALUE rbConfigBaseSetting_line(VALUE self)
{
  if(rb_iv_get(self, "@setting") != Qnil) {
    config_setting_t* setting = NULL;
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
    return INT2FIX(config_setting_source_line(setting));
  } else {
    return Qnil;
  }
}

static VALUE rbConfigSetting_initialize(int argc, VALUE* argv, VALUE self)
{
  VALUE value, setting;
  rb_scan_args(argc, argv, "11", &value, &setting);

  rb_call_super(1, &setting);

  rconfig_check_setting_type(self, value);
  rb_iv_set(self, "@value", value);

  if(rb_iv_get(self, "@setting") != Qnil) {
    config_setting_t* c_setting = NULL;
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, c_setting);
    rb_iv_set(self, "@format", INT2FIX(config_setting_get_format(c_setting)));
  } else {
    rb_iv_set(self, "@format", cConfigFormatDefault);
  }
  
  return self;
}

static VALUE rbConfigSetting_get_value(VALUE self)
{
  if(rb_iv_get(self, "@setting") != Qnil) {
    config_setting_t* setting;
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
    return rconfig_wrap_value(setting);
  } else {
    return rb_iv_get(self, "@value");
  }
}

static VALUE rbConfigSetting_set_value(VALUE self, VALUE new_value)
{
  rconfig_check_setting_type(self, new_value);

  if(rb_iv_get(self, "@setting") != Qnil) {
    config_setting_t* setting;
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
    rconfig_update_setting(setting, new_value);
  }

  rb_iv_set(self, "@value", new_value);
  
  return new_value;
}

static VALUE rbConfigSetting_get_format(VALUE self)
{
  if(rb_iv_get(self, "@setting") != Qnil) {
    config_setting_t* setting;
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
    return INT2FIX(config_setting_get_format(setting));
  } else {
    return rb_iv_get(self, "format");
  }
}

static VALUE rbConfigSetting_set_format(VALUE self, VALUE new_format)
{
  if(rb_iv_get(self, "@setting") != Qnil) {
    config_setting_t* setting;
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
    if(!config_setting_set_format(setting, FIX2INT(new_format)))
      rb_raise(eSettingFormatError, "invalid setting format %d", FIX2INT(new_format));
  }

  rb_iv_set(self, "@format", new_format);
  
  return new_format;
}

static VALUE rbConfigAggregate_get(VALUE self, VALUE index);

static VALUE rbConfigAggregate_initialize(int argc, VALUE* argv, VALUE self)
{
  VALUE setting = Qnil;
  if(rb_obj_class(self) == cConfigGroup || rb_obj_class(self) == cConfigList) {
    rb_scan_args(argc, argv, "01", &setting);
  } else if(rb_obj_class(self) == cConfigArray) {
    VALUE type = Qnil;
    rb_scan_args(argc, argv, "02", &type, &setting);
    
    if(type != Qnil && rb_ary_includes(aConfigScalars, type) != Qtrue)
      rb_raise(rb_eTypeError, "invalid setting array type %s", rb_class2name(type));
    
    rb_iv_set(self, "@type", type);
  } else {
    rb_raise(rb_eException, "never create Config::Aggregate itself");
  }

  rb_call_super(1, &setting);

  rb_iv_set(self, "@list", rb_ary_new());
  if(rb_obj_class(self) == cConfigGroup)
    rb_iv_set(self, "@hash", rb_hash_new());
  
  if(setting != Qnil && rb_obj_class(self) == cConfigArray) {
    config_setting_t* c_setting;
    Data_Get_Struct(setting, config_setting_t, c_setting);
    if(config_setting_length(c_setting) > 0)
      rb_iv_set(self, "@type", rb_obj_class(rbConfigAggregate_get(self, INT2FIX(0))));
  }

  return self;
}

static VALUE rbConfigAggregate_size(VALUE self)
{
  config_setting_t* setting = NULL;
  if(rb_iv_get(self, "@setting") != Qnil)
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
  
  if(setting)
    return INT2FIX(config_setting_length(setting));
  else
    return INT2FIX(RARRAY_LEN(rb_iv_get(self, "@list")));
}

static VALUE rbConfigAggregate_get(VALUE self, VALUE index)
{
  config_setting_t* setting = NULL;
  if(rb_iv_get(self, "@setting") != Qnil)
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
  
  VALUE rbTarget = Qnil;
  
  if(TYPE(index) == T_STRING && rb_obj_class(self) == cConfigGroup) {
    if(setting) {
      config_setting_t* target = config_setting_get_member(setting, RSTRING_PTR(index));
      if(target)
        rbTarget = rconfig_wrap_setting(target);
    } else {
      rbTarget = rb_hash_aref(rb_iv_get(self, "@hash"), index);
    }
  } else if(TYPE(index) == T_FIXNUM) {
    if(setting) {
      config_setting_t* target = config_setting_get_elem(setting, FIX2INT(index));
      if(target)
        rbTarget = rconfig_wrap_setting(target);
    } else {
      rbTarget = rb_ary_entry(rb_iv_get(self, "@list"), FIX2INT(index));
    }
  } else {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_obj_classname(index));
  }
  
  if(rbTarget == Qnil)
    if(TYPE(index) == T_STRING)
      rb_raise(eSettingNotFoundError, "setting `%s' not found", RSTRING_PTR(index));
    else
      rb_raise(eSettingNotFoundError, "setting [%d] not found", FIX2INT(index));

  return rbTarget;
}

static VALUE rbConfigAggregate_append(VALUE self, VALUE target)
{
  config_setting_t* setting = NULL;
  if(rb_iv_get(self, "@setting") != Qnil)
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);

  Check_Type(target, T_OBJECT);

  VALUE type = rb_iv_get(self, "@type");
  if(rb_obj_class(self) == cConfigArray) {
    if(type != Qnil && type != rb_obj_class(target))
      rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", rb_obj_classname(target), rb_class2name(type));
    if(type == Qnil && rb_ary_includes(aConfigScalars, rb_obj_class(target)) != Qtrue)
      rb_raise(rb_eTypeError, "invalid setting array type %s", rb_obj_classname(target));
  }

  if(rb_ary_includes(aConfigSettings, rb_obj_class(target)) == Qtrue) {
    if(setting)
      rconfig_do_append(setting, target, Qnil);
    rb_ary_push(rb_iv_get(self, "@list"), target);
  } else {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected Config::BaseSetting)", rb_obj_classname(target));
  }
  
  if(rb_obj_class(self) == cConfigArray && type == Qnil)
    rb_iv_set(self, "@type", rb_obj_class(target));
  
  return target;
}

static VALUE rbConfigGroup_append(VALUE self, VALUE name, VALUE target)
{
  Check_Type(name, T_STRING);
  Check_Type(target, T_OBJECT);
  
  config_setting_t* setting = NULL;
  if(rb_iv_get(self, "@setting") != Qnil)
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
  
  if(rb_ary_includes(aConfigSettings, rb_obj_class(target)) == Qtrue) {
    if(rb_reg_match(rSettingNameRegexp, name) == Qnil)
      rb_raise(eSettingNameError, "setting name `%s' contains invalid characters", RSTRING_PTR(name));
    if(setting) {
      if(!rconfig_do_append(setting, target, name))
        rb_raise(eSettingNameError, "setting `%s' already exists", RSTRING_PTR(name));
    } else if(rb_hash_aref(rb_iv_get(self, "@hash"), name) != Qnil) {
      rb_raise(eSettingNameError, "setting `%s' already exists", RSTRING_PTR(name));
    }
    rb_ary_push(rb_iv_get(self, "@list"), target);
    rb_hash_aset(rb_iv_get(self, "@hash"), name, target);
  } else {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected Config::BaseSetting)", rb_obj_classname(target));
  }
  
  return target;
}

static VALUE rbConfigAggregate_delete(VALUE self, VALUE target)
{
  config_setting_t* setting = NULL;
  if(rb_iv_get(self, "@setting") != Qnil)
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
  
  VALUE hash = rb_iv_get(self, "@hash"), list = rb_iv_get(self, "@list");
  
  if(TYPE(target) == T_STRING && rb_obj_class(self) == cConfigGroup) {
    if(setting)
      config_setting_remove(setting, RSTRING_PTR(target));
    
    rb_ary_delete_at(list, rb_hash_aref(hash, target));
    rb_hash_delete(hash, target);
  } else if(TYPE(target) == T_FIXNUM) {
    int index = FIX2INT(target);
    if(setting)
      config_setting_remove_elem(setting, index);

    if(rb_obj_class(self) == cConfigGroup)
      rb_hash_delete(hash, rbConfigBaseSetting_name(rb_ary_entry(list, index)));
    rb_ary_delete_at(list, index);
  } else if(rb_ary_includes(aConfigSettings, rb_obj_class(target)) == Qtrue) {
    VALUE name = rbConfigBaseSetting_name(target);
    if(setting)
      config_setting_remove(setting, RSTRING_PTR(name));

    if(rb_obj_class(self) == cConfigGroup)
      rb_hash_delete(hash, name);
    rb_ary_delete(list, target);
  } else {
    if(rb_obj_class(self) == cConfigGroup)
      rb_raise(rb_eTypeError, "wrong argument type %s (expected String, Fixnum or Config::BaseSetting)", rb_obj_classname(target));
    else
      rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or Config::BaseSetting)", rb_obj_classname(target));
  }
  
  return Qnil;
}

static VALUE rbConfig_initialize(VALUE self)
{
  config_t* config = (config_t*) malloc(sizeof(config_t));
  config_init(config);
  config_set_destructor(config, &rconfig_destroy_setting);

  VALUE rbConfig = Data_Wrap_Struct(rb_cObject, 0, config_destroy, config);
  rb_iv_set(self, "@config", rbConfig);

  return self;
}

static VALUE rbConfig_read_bang(VALUE self, VALUE path)
{
  Check_Type(path, T_STRING);  

  config_t* config;
  Data_Get_Struct(rb_iv_get(self, "@config"), config_t, config);
  
  if(!config_read_file(config, RSTRING_PTR(path))) {
    if(config_error_line(config) == 0)
      rb_raise(rb_eIOError, "cannot load config: I/O error");
    else
      rb_raise(eConfigParseError, "cannot parse config on line %d: `%s'", 
                                  config_error_line(config), config_error_text(config));
  }
  
  return Qtrue;
}

static VALUE rbConfig_write_bang(VALUE self, VALUE path)
{
  Check_Type(path, T_STRING);  

  config_t* config;
  Data_Get_Struct(rb_iv_get(self, "@config"), config_t, config);
  
  if(!config_write_file(config, RSTRING_PTR(path)))
    rb_raise(rb_eIOError, "cannot save config: I/O error");
  
  return Qtrue;
}

static VALUE rbConfig_read(VALUE self, VALUE path)
{
  Check_Type(path, T_STRING);  

  config_t* config;
  Data_Get_Struct(rb_iv_get(self, "@config"), config_t, config);
  
  return config_read_file(config, RSTRING_PTR(path)) ? Qtrue : Qfalse;
}

static VALUE rbConfig_write(VALUE self, VALUE path)
{
  Check_Type(path, T_STRING);  

  config_t* config;
  Data_Get_Struct(rb_iv_get(self, "@config"), config_t, config);
  
  return config_write_file(config, RSTRING_PTR(path)) ? Qtrue : Qfalse;
}

static VALUE rbConfig_root(VALUE self)
{
  config_t* config;
  Data_Get_Struct(rb_iv_get(self, "@config"), config_t, config);
  
  return rconfig_wrap_setting(config_root_setting(config));
}

static VALUE rbConfig_lookup(VALUE self, VALUE handle)
{
  if(TYPE(handle) == T_STRING) {
    config_t* config;
    Data_Get_Struct(rb_iv_get(self, "@config"), config_t, config);
    
    config_setting_t* setting;
    setting = config_lookup(config, RSTRING_PTR(handle));
    
    if(setting == NULL)
      rb_raise(eSettingNotFoundError, "setting `%s' not found", RSTRING_PTR(handle));
    
    return rconfig_wrap_setting(setting);
  } else if(TYPE(handle) == T_FIXNUM) {
    return rbConfigAggregate_get(rbConfig_root(self), handle);
  } else {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_obj_classname(handle));
  }
}

static VALUE rbConfig_append(VALUE self, VALUE name, VALUE target)
{
  return rbConfigGroup_append(rbConfig_root(self), name, target);
}

static VALUE rbConfig_delete(VALUE self, VALUE name)
{
  return rbConfigAggregate_delete(rbConfig_root(self), name);
}

static VALUE rbConfig_size(VALUE self)
{
  return rbConfigAggregate_size(rbConfig_root(self));
}

void Init_rconfig()
{
  cConfig = rb_define_class("Config", rb_cObject);
  rb_define_method(cConfig, "initialize", rbConfig_initialize, 0);
  rb_define_method(cConfig, "read!", rbConfig_read_bang, 1);
  rb_define_method(cConfig, "write!", rbConfig_write_bang, 1);
  rb_define_method(cConfig, "read", rbConfig_read, 1);
  rb_define_method(cConfig, "write", rbConfig_write, 1);
  rb_define_method(cConfig, "root", rbConfig_root, 0);
  rb_define_method(cConfig, "lookup", rbConfig_lookup, 1);
  rb_define_method(cConfig, "[]", rbConfig_lookup, 1);
  rb_define_method(cConfig, "append", rbConfig_append, 2);
  rb_define_method(cConfig, "delete", rbConfig_delete, 1);
  rb_define_method(cConfig, "size", rbConfig_size, 0);
  
  cConfigBaseSetting = rb_define_class_under(cConfig, "BaseSetting", rb_cObject);
  rb_define_method(cConfigBaseSetting, "initialize", rbConfigBaseSetting_initialize, 1);
  rb_define_method(cConfigBaseSetting, "name", rbConfigBaseSetting_name, 0);
  rb_define_method(cConfigBaseSetting, "parent", rbConfigBaseSetting_parent, 0);
  rb_define_method(cConfigBaseSetting, "root?", rbConfigBaseSetting_is_root, 0);
  rb_define_method(cConfigBaseSetting, "index", rbConfigBaseSetting_index, 0);
  rb_define_method(cConfigBaseSetting, "line", rbConfigBaseSetting_line, 0);

  cConfigSetting = rb_define_class_under(cConfig, "Setting", cConfigBaseSetting);
  rb_define_method(cConfigSetting, "initialize", rbConfigSetting_initialize, -1);
  rb_define_method(cConfigSetting, "value", rbConfigSetting_get_value, 0);
  rb_define_method(cConfigSetting, "value=", rbConfigSetting_set_value, 1);
  rb_define_method(cConfigSetting, "format", rbConfigSetting_get_format, 0);
  rb_define_method(cConfigSetting, "format=", rbConfigSetting_set_format, 1);
  
  cConfigFormatDefault = INT2FIX(CONFIG_FORMAT_DEFAULT);
  rb_define_const(cConfig, "FORMAT_DEFAULT", cConfigFormatDefault);
  cConfigFormatHex = INT2FIX(CONFIG_FORMAT_HEX);
  rb_define_const(cConfig, "FORMAT_HEX", cConfigFormatHex);
  
  cConfigFixnum = rb_define_class_under(cConfig, "Fixnum", cConfigSetting);
  cConfigBignum = rb_define_class_under(cConfig, "Bignum", cConfigSetting);
  cConfigFloat = rb_define_class_under(cConfig, "Float", cConfigSetting);
  cConfigBoolean = rb_define_class_under(cConfig, "Boolean", cConfigSetting);
  cConfigString = rb_define_class_under(cConfig, "String", cConfigSetting);
  
  cConfigAggregate = rb_define_class_under(cConfig, "Aggregate", cConfigBaseSetting);
  rb_define_method(cConfigAggregate, "initialize", rbConfigAggregate_initialize, -1);
  rb_define_method(cConfigAggregate, "size", rbConfigAggregate_size, 0);
  rb_define_method(cConfigAggregate, "get", rbConfigAggregate_get, 1);
  rb_define_method(cConfigAggregate, "[]", rbConfigAggregate_get, 1);
  rb_define_method(cConfigAggregate, "delete", rbConfigAggregate_delete, 1);
  
  cConfigGroup = rb_define_class_under(cConfig, "Group", cConfigAggregate);
  rb_define_method(cConfigGroup, "append", rbConfigGroup_append, 2);
  cConfigArray = rb_define_class_under(cConfig, "Array", cConfigAggregate);
  rb_define_method(cConfigArray, "append", rbConfigAggregate_append, 1);
  rb_define_method(cConfigArray, "<<", rbConfigAggregate_append, 1);
  cConfigList = rb_define_class_under(cConfig, "List", cConfigAggregate);
  rb_define_method(cConfigList, "append", rbConfigAggregate_append, 1);
  rb_define_method(cConfigList, "<<", rbConfigAggregate_append, 1);
  
  aConfigScalars = rb_ary_new3(5, cConfigFixnum, cConfigBignum, cConfigFloat, cConfigBoolean, cConfigString);
  aConfigAggregates = rb_ary_new3(3, cConfigGroup, cConfigArray, cConfigList);
  aConfigSettings = rb_ary_plus(aConfigScalars, aConfigAggregates);
  
  rb_define_const(cConfig, "SCALARS", aConfigScalars);
  rb_define_const(cConfig, "AGGREGATES", aConfigAggregates);
  rb_define_const(cConfig, "SETTINGS", aConfigSettings);
  
  char* settingNameRegexp = "^[A-Za-z*][A-Za-z\\-_*]*$";
  rSettingNameRegexp = rb_reg_new(settingNameRegexp, strlen(settingNameRegexp), 0);
  
  eConfigParseError = rb_define_class("ConfigParseError", rb_eException);
  eSettingNotFoundError = rb_define_class("SettingNotFoundError", rb_eException);
  eSettingFormatError = rb_define_class("SettingFormatError", rb_eException);
  eSettingNameError = rb_define_class("SettingNameError", rb_eException);
}