~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-common/src/config/yamlemitter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2011-04-05 14:14:13 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110405141413-hbwbunfsxpn2rtre
Tags: 0.9.13-1
* New upstream release
  - remove Debian patch (applied upstream)
* Fix watch file
* Remove unnecessary versioned dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
    close();
46
46
}
47
47
 
48
 
void YamlEmitter::open()
 
48
void YamlEmitter::open() throw(YamlEmitterException)
49
49
{
50
50
 
51
51
    _debug ("YamlEmiter: Open");
63
63
 
64
64
    yaml_emitter_set_output_file (&emitter, fd);
65
65
 
66
 
    if (!yaml_document_initialize (&document, NULL, NULL, NULL, 0, 0))
 
66
    if (yaml_document_initialize (&document, NULL, NULL, NULL, 0, 0) == 0) {
67
67
        throw YamlEmitterException ("Could not initialize yaml document while saving configuration");
 
68
    }
68
69
 
69
70
    // Init the main configuration mapping
70
 
    if ( (topLevelMapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
 
71
    if ( (topLevelMapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) {
71
72
        throw YamlEmitterException ("Could not create top level mapping");
 
73
    }
72
74
}
73
75
 
74
 
void YamlEmitter::close()
 
76
void YamlEmitter::close() throw(YamlEmitterException)
75
77
{
76
78
    _debug ("YamlEmitter: Close");
77
79
 
85
87
        throw YamlEmitterException ("Error closing file descriptor");
86
88
 
87
89
 
88
 
    _debug ("Config: Configuration file closed successfully");
89
 
 
90
 
}
91
 
 
92
 
void YamlEmitter::read() {}
93
 
 
94
 
void YamlEmitter::write()
95
 
{
96
 
 
97
 
}
98
 
 
99
 
void YamlEmitter::serializeData()
 
90
    _debug ("YamlEmitter: Configuration file closed successfully");
 
91
 
 
92
}
 
93
 
 
94
void YamlEmitter::serializeData() throw(YamlEmitterException)
100
95
{
101
96
    // Document object is destroyed once its content is emitted
102
 
    if (!yaml_emitter_dump (&emitter, &document))
 
97
    if (yaml_emitter_dump (&emitter, &document) == 0) {
103
98
        throw YamlEmitterException ("Error while emitting configuration yaml document");
 
99
    }
104
100
}
105
101
 
106
 
 
107
 
void YamlEmitter::serializeAccount (MappingNode *map)
 
102
void YamlEmitter::serializeAccount (MappingNode *map) throw(YamlEmitterException)
108
103
{
109
104
 
110
105
    std::string accountstr ("accounts");
111
106
 
112
107
    int accountid, accountmapping;
113
108
 
114
 
    _debug ("YamlEmitter: Serialize account");
115
 
 
116
 
    if (map->getType() != MAPPING)
 
109
    if (map->getType() != MAPPING) {
117
110
        throw YamlEmitterException ("Node type is not a mapping while writing account");
 
111
    }
118
112
 
119
113
    if (isFirstAccount) {
 
114
        _debug("YamlEmitter: Create account sequence");
 
115
 
120
116
        // accountSequence need to be static outside this scope since reused each time an account is written
121
 
        if ( (accountid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) accountstr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
 
117
        if ( (accountid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) accountstr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0) {
122
118
            throw YamlEmitterException ("Could not add preference scalar to document");
 
119
        }
123
120
 
124
 
        if ( (accountSequence = yaml_document_add_sequence (&document, NULL, YAML_BLOCK_SEQUENCE_STYLE)) == 0)
 
121
        if ( (accountSequence = yaml_document_add_sequence (&document, NULL, YAML_BLOCK_SEQUENCE_STYLE)) == 0) {
125
122
            throw YamlEmitterException ("Could not add sequence to document");
 
123
        }
126
124
 
127
 
        if (!yaml_document_append_mapping_pair (&document, topLevelMapping, accountid, accountSequence))
 
125
        if (yaml_document_append_mapping_pair (&document, topLevelMapping, accountid, accountSequence) == 0) {
128
126
            throw YamlEmitterException ("Could not add mapping pair to top level mapping");
 
127
        }
129
128
 
130
129
        isFirstAccount = false;
131
130
    }
132
131
 
133
 
    if ( (accountmapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
 
132
    if ( (accountmapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) {
134
133
        throw YamlEmitterException ("Could not add account mapping to document");
 
134
    }
135
135
 
136
 
    if (!yaml_document_append_sequence_item (&document, accountSequence, accountmapping))
 
136
    if (yaml_document_append_sequence_item (&document, accountSequence, accountmapping) == 0) {
137
137
        throw YamlEmitterException ("Could not append account mapping to sequence");
 
138
    }
138
139
 
139
140
    Mapping *internalmap = map->getMapping();
140
141
    Mapping::iterator iter = internalmap->begin();
141
142
 
142
 
    while (iter != internalmap->end()) {
143
 
        addMappingItem (accountmapping, iter->first, iter->second);
144
 
        iter++;
145
 
    }
146
 
 
 
143
    try {
 
144
        while (iter != internalmap->end()) {
 
145
                addMappingItem (accountmapping, iter->first, iter->second);
 
146
                iter++;
 
147
        }
 
148
    }
 
149
    catch(YamlEmitterException &e) {
 
150
        _error("YamlEmitterException: %s", e.what());
 
151
        throw;
 
152
    }
147
153
}
148
154
 
149
 
void YamlEmitter::serializePreference (MappingNode *map)
 
155
void YamlEmitter::serializePreference (MappingNode *map) throw(YamlEmitterException)
150
156
{
151
157
    std::string preferencestr ("preferences");
152
158
 
153
159
    int preferenceid, preferencemapping;
154
160
 
155
 
    _debug ("YamlEmitter: Serialize preference");
156
 
 
157
161
    if (map->getType() != MAPPING)
158
162
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
159
163
 
163
167
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
164
168
        throw YamlEmitterException ("Could not add mapping to document");
165
169
 
166
 
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
170
    if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
167
171
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
168
172
 
169
173
    Mapping *internalmap = map->getMapping();
170
174
    Mapping::iterator iter = internalmap->begin();
171
175
 
172
 
    while (iter != internalmap->end()) {
173
 
        addMappingItem (preferencemapping, iter->first, iter->second);
174
 
        iter++;
 
176
    try {
 
177
        while (iter != internalmap->end()) {
 
178
                addMappingItem (preferencemapping, iter->first, iter->second);
 
179
                iter++;
 
180
        }
 
181
    }
 
182
    catch(YamlEmitterException &e) {
 
183
        throw;
175
184
    }
176
185
 
177
186
}
178
187
 
179
 
void YamlEmitter::serializeVoipPreference (MappingNode *map)
 
188
void YamlEmitter::serializeVoipPreference (MappingNode *map) throw(YamlEmitterException)
180
189
{
181
190
    std::string preferencestr ("voipPreferences");
182
191
 
183
192
    int preferenceid, preferencemapping;
184
193
 
185
 
    _debug ("YamlEmitter: Serialize voip preference");
186
 
 
187
194
    if (map->getType() != MAPPING)
188
195
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
189
196
 
193
200
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
194
201
        throw YamlEmitterException ("Could not add mapping to document");
195
202
 
196
 
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
203
    if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
197
204
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
198
205
 
199
206
    Mapping *internalmap = map->getMapping();
200
207
    Mapping::iterator iter = internalmap->begin();
201
208
 
202
 
    while (iter != internalmap->end()) {
203
 
        addMappingItem (preferencemapping, iter->first, iter->second);
204
 
        iter++;
 
209
    try {
 
210
        while (iter != internalmap->end()) {
 
211
                addMappingItem (preferencemapping, iter->first, iter->second);
 
212
                iter++;
 
213
        }
 
214
    }
 
215
    catch (YamlEmitterException &e) {
 
216
        throw;
205
217
    }
206
218
 
207
219
}
208
220
 
209
 
void YamlEmitter::serializeAddressbookPreference (MappingNode *map)
 
221
void YamlEmitter::serializeAddressbookPreference (MappingNode *map) throw(YamlEmitterException)
210
222
{
211
223
    std::string preferencestr ("addressbook");
212
224
 
213
225
    int preferenceid, preferencemapping;
214
226
 
215
 
    _debug ("YamlEmitter: Serialize addressbook preferences");
216
 
 
217
227
    if (map->getType() != MAPPING)
218
228
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
219
229
 
223
233
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
224
234
        throw YamlEmitterException ("Could not add mapping to document");
225
235
 
226
 
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
236
    if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
227
237
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
228
238
 
229
239
    Mapping *internalmap = map->getMapping();
230
240
    Mapping::iterator iter = internalmap->begin();
231
241
 
232
 
    while (iter != internalmap->end()) {
233
 
        addMappingItem (preferencemapping, iter->first, iter->second);
234
 
        iter++;
235
 
    }
236
 
 
 
242
        try {
 
243
                while (iter != internalmap->end()) {
 
244
                        addMappingItem (preferencemapping, iter->first, iter->second);
 
245
                        iter++;
 
246
                }
 
247
        }
 
248
        catch(YamlEmitterException &e) {
 
249
                throw;
 
250
        }
237
251
}
238
252
 
239
 
void YamlEmitter::serializeHooksPreference (MappingNode *map)
 
253
void YamlEmitter::serializeHooksPreference (MappingNode *map) throw(YamlEmitterException)
240
254
{
241
255
    std::string preferencestr ("hooks");
242
256
 
243
257
    int preferenceid, preferencemapping;
244
258
 
245
 
    _debug ("YamlEmitter: Serialize hooks preferences");
246
 
 
247
259
    if (map->getType() != MAPPING)
248
260
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
249
261
 
253
265
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
254
266
        throw YamlEmitterException ("Could not add mapping to document");
255
267
 
256
 
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
268
    if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
257
269
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
258
270
 
259
271
    Mapping *internalmap = map->getMapping();
260
272
    Mapping::iterator iter = internalmap->begin();
261
273
 
262
 
    while (iter != internalmap->end()) {
263
 
        addMappingItem (preferencemapping, iter->first, iter->second);
264
 
        iter++;
 
274
    try {
 
275
                while (iter != internalmap->end()) {
 
276
                        addMappingItem (preferencemapping, iter->first, iter->second);
 
277
                        iter++;
 
278
                }
265
279
    }
266
 
 
 
280
        catch(YamlEmitterException &e) {
 
281
                throw;
 
282
        }
267
283
}
268
284
 
269
285
 
270
 
void YamlEmitter::serializeAudioPreference (MappingNode *map)
 
286
void YamlEmitter::serializeAudioPreference (MappingNode *map) throw(YamlEmitterException)
271
287
{
272
288
    std::string preferencestr ("audio");
273
289
 
274
290
    int preferenceid, preferencemapping;
275
291
 
276
 
    _debug ("YamlEmitter: Serialize hooks preferences");
277
 
 
278
292
    if (map->getType() != MAPPING)
279
293
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
280
294
 
284
298
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
285
299
        throw YamlEmitterException ("Could not add mapping to document");
286
300
 
287
 
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
301
    if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
288
302
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
289
303
 
290
304
    Mapping *internalmap = map->getMapping();
291
305
    Mapping::iterator iter = internalmap->begin();
292
306
 
293
 
    while (iter != internalmap->end()) {
294
 
        addMappingItem (preferencemapping, iter->first, iter->second);
295
 
        iter++;
 
307
    try {
 
308
        while (iter != internalmap->end()) {
 
309
                addMappingItem (preferencemapping, iter->first, iter->second);
 
310
                iter++;
 
311
        }
 
312
    }
 
313
    catch(YamlEmitterException &e) {
 
314
        throw;
296
315
    }
297
316
 
298
317
}
299
318
 
300
319
 
301
 
void YamlEmitter::serializeShortcutPreference (MappingNode *map)
 
320
void YamlEmitter::serializeShortcutPreference (MappingNode *map) throw(YamlEmitterException)
302
321
{
303
322
    std::string preferencestr ("shortcuts");
304
323
 
305
324
    int preferenceid, preferencemapping;
306
325
 
307
 
    _debug ("YamlEmitter: Serialize shortcuts preferences");
308
 
 
309
326
    if (map->getType() != MAPPING)
310
327
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
311
328
 
315
332
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
316
333
        throw YamlEmitterException ("Could not add mapping to document");
317
334
 
318
 
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
335
    if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
319
336
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
320
337
 
321
338
    Mapping *internalmap = map->getMapping();
322
339
    Mapping::iterator iter = internalmap->begin();
323
340
 
324
 
    while (iter != internalmap->end()) {
325
 
        addMappingItem (preferencemapping, iter->first, iter->second);
326
 
        iter++;
327
 
    }
328
 
 
 
341
    try {
 
342
                while (iter != internalmap->end()) {
 
343
                        addMappingItem (preferencemapping, iter->first, iter->second);
 
344
                        iter++;
 
345
                }
 
346
    }
 
347
    catch(YamlEmitterException &e) {
 
348
        throw;
 
349
    }
329
350
}
330
351
 
331
352
 
344
365
        if ( (temp2 = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) sclr->getValue().c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
345
366
            throw YamlEmitterException ("Could not add scalar to document");
346
367
 
347
 
        if (!yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2))
 
368
        if (yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2) == 0)
348
369
            throw YamlEmitterException ("Could not append mapping pair to mapping");
349
370
 
350
371
    } else if (node->getType() == MAPPING) {
357
378
        if ( (temp2 = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
358
379
            throw YamlEmitterException ("Could not add scalar to document");
359
380
 
360
 
        if (!yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2))
 
381
        if (yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2) == 0)
361
382
            throw YamlEmitterException ("Could not add mapping pair to mapping");
362
383
 
363
384
        MappingNode *map = (MappingNode *) node;
364
385
        Mapping *internalmap = map->getMapping();
365
386
        Mapping::iterator iter = internalmap->begin();
366
387
 
367
 
        while (iter != internalmap->end()) {
368
 
            addMappingItem (temp2, iter->first, iter->second);
369
 
            iter++;
370
 
        }
371
 
    } else
 
388
        try {
 
389
                while (iter != internalmap->end()) {
 
390
                        addMappingItem (temp2, iter->first, iter->second);
 
391
                        iter++;
 
392
                }
 
393
        }
 
394
        catch(YamlEmitterException) {
 
395
                throw;
 
396
        }
 
397
    } else {
372
398
        throw YamlEmitterException ("Unknown node type while adding mapping node");
 
399
    }
373
400
}
374
 
 
375
401
 
376
402
}