summaryrefslogtreecommitdiff
path: root/ruby/src/IceRuby/Properties.cpp
blob: 83ac449a2d202d254ea89b3302495bc9aa960256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
169
170
171
172
173
174
175
176
177
178
179
180
181
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
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
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Properties.h>
#include <Util.h>
#include <Ice/Initialize.h>
#include <Ice/Properties.h>

using namespace std;
using namespace IceRuby;

static VALUE _propertiesClass;

extern "C"
void
IceRuby_Properties_free(Ice::PropertiesPtr* p)
{
    assert(p);
    delete p;
}

extern "C"
VALUE
IceRuby_createProperties(int argc, VALUE* argv, VALUE self)
{
    ICE_RUBY_TRY
    {
        Ice::StringSeq seq;
        if(argc >= 1 && !NIL_P(argv[0]) && !arrayToStringSeq(argv[0], seq))
        {
            throw RubyException(rb_eTypeError, "invalid array argument to Ice::createProperties");
        }

        Ice::PropertiesPtr defaults;
        if(argc == 2)
        {
            if(!NIL_P(argv[1]) && callRuby(rb_obj_is_instance_of, argv[1], _propertiesClass) == Qfalse)
            {
                throw RubyException(rb_eTypeError, "invalid properties argument to Ice::createProperties");
            }
            defaults = getProperties(argv[1]);
        }

        //
        // Insert the program name (stored in the Ruby global variable $0) as the first
        // element of the sequence.
        //
        volatile VALUE progName = callRuby(rb_gv_get, "$0");
        seq.insert(seq.begin(), getString(progName));

        Ice::PropertiesPtr obj;
        if(argc >= 1)
        {
            obj = Ice::createProperties(seq, defaults);
        }
        else
        {
            obj = Ice::createProperties();
        }

        //
        // Replace the contents of the given argument list with the filtered arguments.
        //
        if(argc > 0 && !NIL_P(argv[0]))
        {
            callRuby(rb_ary_clear, argv[0]);

            //
            // We start at index 1 in order to skip the element that we inserted earlier.
            //
            for(Ice::StringSeq::size_type i = 1; i < seq.size(); ++i)
            {
                volatile VALUE str = createString(seq[i]);
                callRuby(rb_ary_push, argv[0], str);
            }
        }

        return createProperties(obj);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_getProperty(VALUE self, VALUE key)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        string k = getString(key);
        string v = p->getProperty(k);
        return createString(v);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_getPropertyWithDefault(VALUE self, VALUE key, VALUE def)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        string k = getString(key);
        string d = getString(def);
        string v = p->getPropertyWithDefault(k, d);
        return createString(v);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_getPropertyAsInt(VALUE self, VALUE key)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        string k = getString(key);
        Ice::Int v = p->getPropertyAsInt(k);
        return INT2FIX(v);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_getPropertyAsIntWithDefault(VALUE self, VALUE key, VALUE def)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        string k = getString(key);
        Ice::Int d = getInteger(def);
        Ice::Int v = p->getPropertyAsIntWithDefault(k, d);
        return INT2FIX(v);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_getPropertyAsList(VALUE self, VALUE key)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        string k = getString(key);
        Ice::StringSeq v = p->getPropertyAsList(k);
        return stringSeqToArray(v);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_getPropertyAsListWithDefault(VALUE self, VALUE key, VALUE def)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        string k = getString(key);
        Ice::StringSeq d;
        if(!arrayToStringSeq(def, d))
        {
            throw RubyException(rb_eTypeError, "invalid array argument to Ice::getPropertyAsListWithDefault");
        }
        Ice::StringSeq v = p->getPropertyAsListWithDefault(k, d);
        return stringSeqToArray(v);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_getPropertiesForPrefix(VALUE self, VALUE prefix)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        string pfx = getString(prefix);
        Ice::PropertyDict dict = p->getPropertiesForPrefix(pfx);
        volatile VALUE result = callRuby(rb_hash_new);
        for(Ice::PropertyDict::const_iterator q = dict.begin(); q != dict.end(); ++q)
        {
            volatile VALUE key = createString(q->first);
            volatile VALUE value = createString(q->second);
            callRuby(rb_hash_aset, result, key, value);
        }
        return result;
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_setProperty(VALUE self, VALUE key, VALUE value)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        string k = getString(key);
        string v = getString(value);
        p->setProperty(k, v);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_getCommandLineOptions(VALUE self)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        Ice::StringSeq options = p->getCommandLineOptions();
        return stringSeqToArray(options);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_parseCommandLineOptions(VALUE self, VALUE prefix, VALUE options)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        string pfx = getString(prefix);
        Ice::StringSeq seq;
        if(!arrayToStringSeq(options, seq))
        {
            throw RubyException(rb_eTypeError, "invalid array argument to Ice::parseCommandLineOptions");
        }
        Ice::StringSeq filtered = p->parseCommandLineOptions(pfx, seq);
        return stringSeqToArray(filtered);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_parseIceCommandLineOptions(VALUE self, VALUE options)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        Ice::StringSeq seq;
        if(!arrayToStringSeq(options, seq))
        {
            throw RubyException(rb_eTypeError, "invalid array argument to Ice::parseIceCommandLineOptions");
        }
        Ice::StringSeq filtered = p->parseIceCommandLineOptions(seq);
        return stringSeqToArray(filtered);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_load(VALUE self, VALUE file)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        string f = getString(file);
        p->load(f);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_clone(VALUE self)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        Ice::PropertiesPtr props = p->clone();
        return createProperties(props);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

extern "C"
VALUE
IceRuby_Properties_to_s(VALUE self)
{
    ICE_RUBY_TRY
    {
        Ice::PropertiesPtr p = getProperties(self);
        Ice::PropertyDict dict = p->getPropertiesForPrefix("");
        string str;
        for(Ice::PropertyDict::const_iterator q = dict.begin(); q != dict.end(); ++q)
        {
            if(q != dict.begin())
            {
                str.append("\n");
            }
            str.append(q->first + "=" + q->second);
        }
        return createString(str);
    }
    ICE_RUBY_CATCH
    return Qnil;
}

void
IceRuby::initProperties(VALUE iceModule)
{
    rb_define_module_function(iceModule, "createProperties", CAST_METHOD(IceRuby_createProperties), -1);

    _propertiesClass = rb_define_class_under(iceModule, "PropertiesI", rb_cObject);
    rb_define_method(_propertiesClass, "getProperty", CAST_METHOD(IceRuby_Properties_getProperty), 1);
    rb_define_method(_propertiesClass, "getPropertyWithDefault",
                     CAST_METHOD(IceRuby_Properties_getPropertyWithDefault), 2);
    rb_define_method(_propertiesClass, "getPropertyAsInt", CAST_METHOD(IceRuby_Properties_getPropertyAsInt), 1);
    rb_define_method(_propertiesClass, "getPropertyAsIntWithDefault",
                     CAST_METHOD(IceRuby_Properties_getPropertyAsIntWithDefault), 2);
    rb_define_method(_propertiesClass, "getPropertyAsList", CAST_METHOD(IceRuby_Properties_getPropertyAsList), 1);
    rb_define_method(_propertiesClass, "getPropertyAsListWithDefault",
                     CAST_METHOD(IceRuby_Properties_getPropertyAsListWithDefault), 2);
    rb_define_method(_propertiesClass, "getPropertiesForPrefix",
                     CAST_METHOD(IceRuby_Properties_getPropertiesForPrefix), 1);
    rb_define_method(_propertiesClass, "setProperty", CAST_METHOD(IceRuby_Properties_setProperty), 2);
    rb_define_method(_propertiesClass, "getCommandLineOptions", CAST_METHOD(IceRuby_Properties_getCommandLineOptions),
                     0);
    rb_define_method(_propertiesClass, "parseCommandLineOptions",
                     CAST_METHOD(IceRuby_Properties_parseCommandLineOptions), 2);
    rb_define_method(_propertiesClass, "parseIceCommandLineOptions",
                     CAST_METHOD(IceRuby_Properties_parseIceCommandLineOptions), 1);
    rb_define_method(_propertiesClass, "load", CAST_METHOD(IceRuby_Properties_load), 1);
    rb_define_method(_propertiesClass, "clone", CAST_METHOD(IceRuby_Properties_clone), 0);
    rb_define_method(_propertiesClass, "to_s", CAST_METHOD(IceRuby_Properties_to_s), 0);
}

Ice::PropertiesPtr
IceRuby::getProperties(VALUE v)
{
    Ice::PropertiesPtr* p = reinterpret_cast<Ice::PropertiesPtr*>(DATA_PTR(v));
    assert(p);
    return *p;
}

VALUE
IceRuby::createProperties(const Ice::PropertiesPtr& p)
{
    return Data_Wrap_Struct(_propertiesClass, 0, IceRuby_Properties_free, new Ice::PropertiesPtr(p));
}