diff --git a/daemon/src/config/yamlemitter.cpp b/daemon/src/config/yamlemitter.cpp index c4361e937ca5f415b0ecdc1071c2c83f9f8ccb3d..6ab19a941624d21669e946cd8d71e0186f95970d 100644 --- a/daemon/src/config/yamlemitter.cpp +++ b/daemon/src/config/yamlemitter.cpp @@ -29,6 +29,7 @@ */ #include "yamlemitter.h" +#include "yamlnode.h" #include <cstdio> #include "logger.h" @@ -73,11 +74,13 @@ void YamlEmitter::close() { yaml_emitter_delete(&emitter_); + // Refererence: + // http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.9 if (!fd_) - throw YamlEmitterException("File descriptor not valid"); + ERROR("File descriptor not valid"); if (fclose(fd_)) - throw YamlEmitterException("Error closing file descriptor"); + ERROR("Error closing file descriptor"); } void YamlEmitter::serializeData() @@ -89,8 +92,6 @@ void YamlEmitter::serializeData() void YamlEmitter::serializeAccount(MappingNode *map) { - int accountmapping; - if (map->getType() != MAPPING) throw YamlEmitterException("Node type is not a mapping while writing account"); @@ -111,154 +112,43 @@ void YamlEmitter::serializeAccount(MappingNode *map) isFirstAccount_ = false; } - if ((accountmapping = yaml_document_add_mapping(&document_, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) + int accountMapping; + if ((accountMapping = yaml_document_add_mapping(&document_, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) throw YamlEmitterException("Could not add account mapping to document"); - if (yaml_document_append_sequence_item(&document_, accountSequence_, accountmapping) == 0) + if (yaml_document_append_sequence_item(&document_, accountSequence_, accountMapping) == 0) throw YamlEmitterException("Could not append account mapping to sequence"); - Mapping *internalmap = map->getMapping(); - for (Mapping::const_iterator i = internalmap->begin(); i != internalmap->end(); ++i) - addMappingItem(accountmapping, i->first, i->second); + addMappingItems(accountMapping, map->getMapping()); } -void YamlEmitter::serializePreference(MappingNode *map) +void YamlEmitter::serializePreference(MappingNode *map, const char *preference_str) { if (map->getType() != MAPPING) throw YamlEmitterException("Node type is not a mapping while writing preferences"); - static const char * const PREFERENCE_STR = "preferences"; int preferenceid; - - if ((preferenceid = yaml_document_add_scalar(&document_, NULL, (yaml_char_t *) PREFERENCE_STR, -1, YAML_PLAIN_SCALAR_STYLE)) == 0) + if ((preferenceid = yaml_document_add_scalar(&document_, NULL, (yaml_char_t *) preference_str, -1, YAML_PLAIN_SCALAR_STYLE)) == 0) throw YamlEmitterException("Could not add scalar to document"); - int preferencemapping; - if ((preferencemapping = yaml_document_add_mapping(&document_, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) + int preferenceMapping; + if ((preferenceMapping = yaml_document_add_mapping(&document_, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) throw YamlEmitterException("Could not add mapping to document"); - if (yaml_document_append_mapping_pair(&document_, topLevelMapping_, preferenceid, preferencemapping) == 0) + if (yaml_document_append_mapping_pair(&document_, topLevelMapping_, preferenceid, preferenceMapping) == 0) throw YamlEmitterException("Could not add mapping pair to top leve mapping"); - Mapping *internalmap = map->getMapping(); - for (Mapping::const_iterator i = internalmap->begin(); i != internalmap->end(); ++i) - addMappingItem(preferencemapping, i->first, i->second); + addMappingItems(preferenceMapping, map->getMapping()); } -void YamlEmitter::serializeVoipPreference(MappingNode *map) -{ - if (map->getType() != MAPPING) - throw YamlEmitterException("Node type is not a mapping while writing preferences"); - - - static const char *const PREFERENCE_STR = "voipPreferences"; - int preferenceid; - if ((preferenceid = yaml_document_add_scalar(&document_, NULL, (yaml_char_t *) PREFERENCE_STR, -1, YAML_PLAIN_SCALAR_STYLE)) == 0) - throw YamlEmitterException("Could not add scalar to document"); +typedef std::map<std::string, YamlNode*> Mapping; - int preferencemapping; - if ((preferencemapping = yaml_document_add_mapping(&document_, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) - throw YamlEmitterException("Could not add mapping to document"); - - if (yaml_document_append_mapping_pair(&document_, topLevelMapping_, preferenceid, preferencemapping) == 0) - throw YamlEmitterException("Could not add mapping pair to top leve mapping"); - - Mapping *internalmap = map->getMapping(); - for (Mapping::const_iterator i = internalmap->begin(); i != internalmap->end(); ++i) - addMappingItem(preferencemapping, i->first, i->second); -} - -void YamlEmitter::serializeAddressbookPreference(MappingNode *map) +void YamlEmitter::addMappingItems(int mappingID, Mapping *iMap) { - if (map->getType() != MAPPING) - throw YamlEmitterException("Node type is not a mapping while writing preferences"); - - static const char * const PREFERENCE_STR = "addressbook"; - int preferenceid; - if ((preferenceid = yaml_document_add_scalar(&document_, NULL, (yaml_char_t *) PREFERENCE_STR, -1, YAML_PLAIN_SCALAR_STYLE)) == 0) - throw YamlEmitterException("Could not add scalar to document"); - int preferencemapping; - if ((preferencemapping = yaml_document_add_mapping(&document_, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) - throw YamlEmitterException("Could not add mapping to document"); - - if (yaml_document_append_mapping_pair(&document_, topLevelMapping_, preferenceid, preferencemapping) == 0) - throw YamlEmitterException("Could not add mapping pair to top leve mapping"); - - Mapping *internalmap = map->getMapping(); - for (Mapping::const_iterator i = internalmap->begin(); i != internalmap->end(); ++i) - addMappingItem(preferencemapping, i->first, i->second); + for (Mapping::const_iterator i = iMap->begin(); i != iMap->end(); ++i) + addMappingItem(mappingID, i->first, i->second); } -void YamlEmitter::serializeHooksPreference(MappingNode *map) -{ - if (map->getType() != MAPPING) - throw YamlEmitterException("Node type is not a mapping while writing preferences"); - - static const char * const PREFERENCE_STR = "hooks"; - int preferenceid; - if ((preferenceid = yaml_document_add_scalar(&document_, NULL, (yaml_char_t *) PREFERENCE_STR, -1, YAML_PLAIN_SCALAR_STYLE)) == 0) - throw YamlEmitterException("Could not add scalar to document"); - - int preferencemapping; - if ((preferencemapping = yaml_document_add_mapping(&document_, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) - throw YamlEmitterException("Could not add mapping to document"); - - if (yaml_document_append_mapping_pair(&document_, topLevelMapping_, preferenceid, preferencemapping) == 0) - throw YamlEmitterException("Could not add mapping pair to top leve mapping"); - - Mapping *internalmap = map->getMapping(); - for (Mapping::const_iterator i = internalmap->begin(); i != internalmap->end(); ++i) - addMappingItem(preferencemapping, i->first, i->second); -} - - -void YamlEmitter::serializeAudioPreference(MappingNode *map) -{ - static const char *const PREFERENCE_STR = "audio"; - - int preferenceid, preferencemapping; - - if (map->getType() != MAPPING) - throw YamlEmitterException("Node type is not a mapping while writing preferences"); - - if ((preferenceid = yaml_document_add_scalar(&document_, NULL, (yaml_char_t *) PREFERENCE_STR, -1, YAML_PLAIN_SCALAR_STYLE)) == 0) - throw YamlEmitterException("Could not add scalar to document"); - - if ((preferencemapping = yaml_document_add_mapping(&document_, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) - throw YamlEmitterException("Could not add mapping to document"); - - if (yaml_document_append_mapping_pair(&document_, topLevelMapping_, preferenceid, preferencemapping) == 0) - throw YamlEmitterException("Could not add mapping pair to top leve mapping"); - - Mapping *internalmap = map->getMapping(); - for (Mapping::const_iterator i = internalmap->begin(); i != internalmap->end(); ++i) - addMappingItem(preferencemapping, i->first, i->second); -} - - -void YamlEmitter::serializeShortcutPreference(MappingNode *map) -{ - if (map->getType() != MAPPING) - throw YamlEmitterException("Node type is not a mapping while writing preferences"); - - static const char *const PREFERENCE_STR = "shortcuts"; - int preferenceid; - if ((preferenceid = yaml_document_add_scalar(&document_, NULL, (yaml_char_t *) PREFERENCE_STR, -1, YAML_PLAIN_SCALAR_STYLE)) == 0) - throw YamlEmitterException("Could not add scalar to document"); - - int preferencemapping; - if ((preferencemapping = yaml_document_add_mapping(&document_, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) - throw YamlEmitterException("Could not add mapping to document"); - - if (yaml_document_append_mapping_pair(&document_, topLevelMapping_, preferenceid, preferencemapping) == 0) - throw YamlEmitterException("Could not add mapping pair to top leve mapping"); - - Mapping *mapping = map->getMapping(); - for (Mapping::const_iterator i = mapping->begin(); i != mapping->end(); ++i) - addMappingItem(preferencemapping, i->first, i->second); -} - - void YamlEmitter::addMappingItem(int mappingid, const std::string &key, YamlNode *node) { if (node->getType() == SCALAR) { @@ -289,9 +179,7 @@ void YamlEmitter::addMappingItem(int mappingid, const std::string &key, YamlNode if (yaml_document_append_mapping_pair(&document_, mappingid, temp1, temp2) == 0) throw YamlEmitterException("Could not add mapping pair to mapping"); - Mapping *internalmap = map->getMapping(); - for (Mapping::const_iterator i = internalmap->begin(); i != internalmap->end(); ++i) - addMappingItem(temp2, i->first, i->second); + addMappingItems(temp2, map->getMapping()); } else if (node->getType() == SEQUENCE) { SequenceNode *seqnode = static_cast<SequenceNode *>(node); @@ -318,9 +206,7 @@ void YamlEmitter::addMappingItem(int mappingid, const std::string &key, YamlNode throw YamlEmitterException("Could not append account mapping to sequence"); MappingNode *mapnode = static_cast<MappingNode*>(yamlNode); - Mapping *map = mapnode->getMapping(); - for (Mapping::const_iterator i = map->begin(); i != map->end(); ++i) - addMappingItem(id, i->first, i->second); + addMappingItems(id, mapnode->getMapping()); } } else throw YamlEmitterException("Unknown node type while adding mapping node"); diff --git a/daemon/src/config/yamlemitter.h b/daemon/src/config/yamlemitter.h index d3d7eedcdfa1cffae0eac5bf7a72afb563bff589..4f6234e63c878223dc220cb68b6718be669803ed 100644 --- a/daemon/src/config/yamlemitter.h +++ b/daemon/src/config/yamlemitter.h @@ -28,24 +28,26 @@ * as that of the covered work. */ -#ifndef __YAMLEMITTER_H__ -#define __YAMLEMITTER_H__ +#ifndef YAMLEMITTER_H__ +#define YAMLEMITTER_H__ #include <yaml.h> #include <stdexcept> #include <string> +#include <map> #include "noncopyable.h" -#include "yamlnode.h" namespace Conf { #define EMITTER_BUFFERSIZE 65536 #define EMITTER_MAXEVENT 1024 +class MappingNode; +class YamlNode; + class YamlEmitterException : public std::runtime_error { public: - YamlEmitterException(const std::string& str="") : - std::runtime_error("YamlEmitterException occured: " + str) {} + YamlEmitterException(const char *err) : std::runtime_error(err) {} }; class YamlEmitter { @@ -61,17 +63,7 @@ class YamlEmitter { void serializeAccount(MappingNode *map); - void serializePreference(MappingNode *map); - - void serializeVoipPreference(MappingNode *map); - - void serializeAddressbookPreference(MappingNode *map); - - void serializeHooksPreference(MappingNode *map); - - void serializeAudioPreference(MappingNode *map); - - void serializeShortcutPreference(MappingNode *map); + void serializePreference(MappingNode *map, const char *preference_str); void writeAudio(); @@ -84,6 +76,7 @@ class YamlEmitter { private: NON_COPYABLE(YamlEmitter); + void addMappingItems(int mappingid, std::map<std::string, YamlNode*> *mapping); void addMappingItem(int mappingid, const std::string &key, YamlNode *node); std::string filename_; @@ -126,4 +119,4 @@ class YamlEmitter { }; } -#endif +#endif // YAMLEMITTER_H__ diff --git a/daemon/src/config/yamlnode.cpp b/daemon/src/config/yamlnode.cpp index f4590a3611208b7c1103f8017952ed6637344581..32bdf2f27626648262d08d49a9d3683be2dea047 100644 --- a/daemon/src/config/yamlnode.cpp +++ b/daemon/src/config/yamlnode.cpp @@ -89,6 +89,8 @@ void MappingNode::addNode(YamlNode *node) setKeyValue(tmpKey_, node); } +typedef std::map<std::string, YamlNode*> Mapping; + void MappingNode::setKeyValue(const std::string &key, YamlNode *value) { Mapping::iterator it = map_.end(); diff --git a/daemon/src/config/yamlnode.h b/daemon/src/config/yamlnode.h index 36ca8d6e7f6d7fefa5a11634b94f8f75ad35e1f1..990b23733ccf4372fc7a96a4c22ec9437baa938a 100644 --- a/daemon/src/config/yamlnode.h +++ b/daemon/src/config/yamlnode.h @@ -42,7 +42,6 @@ namespace Conf { class YamlNode; typedef std::list<YamlNode *> Sequence; -typedef std::map<std::string, YamlNode *> Mapping; enum NodeType { DOCUMENT, SCALAR, MAPPING, SEQUENCE }; @@ -109,7 +108,8 @@ class MappingNode : public YamlNode { MappingNode(YamlNode *top) : YamlNode(MAPPING, top), map_(), tmpKey_() {} - Mapping *getMapping() { + std::map<std::string, YamlNode*> * + getMapping() { return &map_; } @@ -131,7 +131,7 @@ class MappingNode : public YamlNode { virtual void deleteChildNodes(); private: - Mapping map_; + std::map<std::string, YamlNode*> map_; std::string tmpKey_; }; diff --git a/daemon/src/config/yamlparser.cpp b/daemon/src/config/yamlparser.cpp index 19ba06ad57ba128d739509e657f0fdf8444ed1ec..97b0c18b71518f82f0381653521940650d5951dc 100644 --- a/daemon/src/config/yamlparser.cpp +++ b/daemon/src/config/yamlparser.cpp @@ -390,7 +390,7 @@ void YamlParser::constructNativeData() void YamlParser::mainNativeDataMapping(MappingNode *map) { - Mapping *mapping = map->getMapping(); + std::map<std::string, YamlNode*> *mapping = map->getMapping(); accountSequence_ = (SequenceNode*)(*mapping)["accounts"]; addressbookNode_ = (MappingNode*)(*mapping)["addressbook"]; diff --git a/daemon/src/preferences.cpp b/daemon/src/preferences.cpp index 79551680156b24409b3234384840909a5553bfc6..aa7afc70cb2fb17c6d09fe8520a1d1000aa7500d 100644 --- a/daemon/src/preferences.cpp +++ b/daemon/src/preferences.cpp @@ -160,7 +160,7 @@ void Preferences::serialize(Conf::YamlEmitter *emiter) preferencemap.setKeyValue(ZEROCONF_ENABLE_KEY, &zeroConfenable); preferencemap.setKeyValue(MD5_HASH_KEY, &md5Hash); - emiter->serializePreference(&preferencemap); + emiter->serializePreference(&preferencemap, "preferences"); } void Preferences::unserialize(const Conf::MappingNode *map) @@ -207,7 +207,7 @@ void VoipPreference::serialize(Conf::YamlEmitter *emitter) preferencemap.setKeyValue(SYMMETRIC_RTP_KEY, &symmetricRtp); preferencemap.setKeyValue(ZID_FILE_KEY, &zidFile); - emitter->serializeVoipPreference(&preferencemap); + emitter->serializePreference(&preferencemap, "voipPreferences"); } void VoipPreference::unserialize(const Conf::MappingNode *map) @@ -255,7 +255,7 @@ void AddressbookPreference::serialize(Conf::YamlEmitter *emitter) preferencemap.setKeyValue(HOME_KEY, &home); preferencemap.setKeyValue(MOBILE_KEY, &mobile); - emitter->serializeAddressbookPreference(&preferencemap); + emitter->serializePreference(&preferencemap, "addressbook"); } @@ -323,7 +323,7 @@ void HookPreference::serialize(Conf::YamlEmitter *emitter) preferencemap.setKeyValue(URL_COMMAND_KEY, &urlCommand); preferencemap.setKeyValue(URL_SIP_FIELD_KEY, &urlSipField); - emitter->serializeHooksPreference(&preferencemap); + emitter->serializePreference(&preferencemap, "hooks"); } void HookPreference::unserialize(const Conf::MappingNode *map) @@ -471,7 +471,7 @@ void AudioPreference::serialize(Conf::YamlEmitter *emitter) preferencemap.setKeyValue(ECHO_TAIL_KEY, &echotail); preferencemap.setKeyValue(ECHO_DELAY_KEY, &echodelay); - emitter->serializeAudioPreference(&preferencemap); + emitter->serializePreference(&preferencemap, "audio"); } void AudioPreference::unserialize(const Conf::MappingNode *map) @@ -548,7 +548,7 @@ void ShortcutPreferences::serialize(Conf::YamlEmitter *emitter) preferencemap.setKeyValue(TOGGLE_HOLD_SHORT_KEY, &toggleHold); preferencemap.setKeyValue(TOGGLE_PICKUP_HANGUP_SHORT_KEY, &togglePickupHangup); - emitter->serializeShortcutPreference(&preferencemap); + emitter->serializePreference(&preferencemap, "shortcuts"); } void ShortcutPreferences::unserialize(const Conf::MappingNode *map)