Class: LibConfig

Inherits:
Object
  • Object
show all
Defined in:
ext/rlibconfig.c

Defined Under Namespace

Classes: Aggregate, Array, BaseSetting, Bignum, Boolean, Fixnum, Float, Group, List, Setting, String

Constant Summary collapse

FORMAT_DEFAULT =
cConfigFormatDefault
FORMAT_HEX =
cConfigFormatHex
SCALARS =
aConfigScalars
AGGREGATES =
aConfigAggregates
SETTINGS =
aConfigSettings

Instance Method Summary collapse

Constructor Details

#initializeObject



522
523
524
525
526
527
528
529
530
531
532
# File 'ext/rlibconfig.c', line 522

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;
}

Instance Method Details

#[](handle) ⇒ Object



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'ext/rlibconfig.c', line 593

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));
  }
}

#append(name, target) ⇒ Object



613
614
615
616
# File 'ext/rlibconfig.c', line 613

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

#delete(name) ⇒ Object



618
619
620
621
# File 'ext/rlibconfig.c', line 618

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

#lookup(handle) ⇒ Object



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'ext/rlibconfig.c', line 593

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));
  }
}

#read(path) ⇒ Object



565
566
567
568
569
570
571
572
573
# File 'ext/rlibconfig.c', line 565

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;
}

#read!(path) ⇒ Object



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'ext/rlibconfig.c', line 534

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;
}

#rootObject



585
586
587
588
589
590
591
# File 'ext/rlibconfig.c', line 585

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));
}

#sizeObject



623
624
625
626
# File 'ext/rlibconfig.c', line 623

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

#write(path) ⇒ Object



575
576
577
578
579
580
581
582
583
# File 'ext/rlibconfig.c', line 575

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;
}

#write!(path) ⇒ Object



552
553
554
555
556
557
558
559
560
561
562
563
# File 'ext/rlibconfig.c', line 552

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;
}