Class: UCL

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

Overview

UCL configuration file.

Defined Under Namespace

Classes: Error

Constant Summary collapse

KEY_LOWERCASE =

Constants

INT2FIX(UCL_PARSER_KEY_LOWERCASE)
NO_TIME =
INT2FIX(UCL_PARSER_NO_TIME      )
DISABLE_MACRO =
INT2FIX(UCL_PARSER_DISABLE_MACRO)
NO_FILEVARS =
INT2FIX(UCL_PARSER_NO_FILEVARS  )
KEY_SYMBOL =
INT2FIX(UCL_PARSER_KEY_SYMBOL   )

Class Method Summary collapse

Class Method Details

.flagsObject



107
108
109
110
111
# File 'ext/ucl.c', line 107

static VALUE
ucl_s_get_flags(VALUE klass)
{
    return rb_iv_get(klass, "@flags");
}

.flags=(val) ⇒ Object



114
115
116
117
118
119
120
# File 'ext/ucl.c', line 114

static VALUE
ucl_s_set_flags(VALUE klass, VALUE val)
{
    rb_check_type(val, T_FIXNUM);
    rb_iv_set(klass, "@flags", val);
    return val;
}

.load_file(*args) ⇒ Object

Load configuration file

Examples:

UCL.load_file('foo.conf', UCL::KEY_SYMBOL)

Parameters:

  • file (String)
  • flags (Integer)

Returns:

  • configuration file as ruby objects.



182
183
184
185
186
187
188
189
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
# File 'ext/ucl.c', line 182

static VALUE
ucl_s_load_file(int argc, VALUE *argv, VALUE klass)
{
    VALUE file, flags;
    rb_scan_args(argc, argv, "11", &file, &flags);
    if (NIL_P(flags)) flags = ucl_s_get_flags(mUCL);

    rb_check_type(file,  T_STRING);
    rb_check_type(flags, T_FIXNUM);
	
    int   c_flags = FIX2INT(flags) & ucl_allowed_c_flags;
    char *c_file  = StringValueCStr(file);

    struct ucl_parser *parser =
	ucl_parser_new(c_flags | UCL_PARSER_NO_IMPLICIT_ARRAYS);

    ucl_parser_add_file(parser, c_file);
    ucl_parser_set_filevars(parser, c_file, false);
	
    if (ucl_parser_get_error(parser)) {
	const char *errormsg = ucl_parser_get_error(parser);
	if (parser != NULL) { ucl_parser_free(parser); }
	rb_raise(eUCLError, "%s", errormsg);
    }

    bool          failed = false;
    ucl_object_t *root   = ucl_parser_get_object(parser);
    VALUE         res    = _iterate_valid_ucl(root, FIX2INT(flags), &failed);

    if (parser != NULL) { ucl_parser_free(parser); }
    if (root   != NULL) { ucl_object_unref(root);  }
    
    if (failed) {
    	rb_raise(eUCLError, "failed to iterate over ucl object");
    }

    return res;
}

.parse(*args) ⇒ Object

Parse a configuration file

Parameters:

  • data (String)
  • flags (Integer)

Returns:

  • configuration file as ruby objects.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'ext/ucl.c', line 131

static VALUE
ucl_s_parse(int argc, VALUE *argv, VALUE klass)
{
    VALUE data, flags;
    rb_scan_args(argc, argv, "11", &data, &flags);
    if (NIL_P(flags)) flags = ucl_s_get_flags(mUCL);

    rb_check_type(data,  T_STRING);
    rb_check_type(flags, T_FIXNUM);
	
    int c_flags = FIX2INT(flags) & ucl_allowed_c_flags;

    struct ucl_parser *parser =
	ucl_parser_new(c_flags | UCL_PARSER_NO_IMPLICIT_ARRAYS);

    ucl_parser_add_chunk(parser,
			 (unsigned char *)RSTRING_PTR(data),
			 RSTRING_LEN(data));
    
    if (ucl_parser_get_error(parser)) {
	const char *errormsg = ucl_parser_get_error(parser);
	if (parser != NULL) { ucl_parser_free(parser); }
	rb_raise(eUCLError, "%s", errormsg);
    }

    bool          failed = false;
    ucl_object_t *root   = ucl_parser_get_object(parser);
    VALUE         res    = _iterate_valid_ucl(root, FIX2INT(flags), &failed);

    if (parser != NULL) { ucl_parser_free(parser); }
    if (root   != NULL) { ucl_object_unref(root);  }

    if (failed) {
    	rb_raise(eUCLError, "failed to iterate over ucl object");
    }

    return res;
}