diff --git a/coreapi/Makefile.am b/coreapi/Makefile.am index 928ffb084..228b7698b 100644 --- a/coreapi/Makefile.am +++ b/coreapi/Makefile.am @@ -49,6 +49,7 @@ liblinphone_la_SOURCES=\ info.c \ event.c event.h \ contactprovider.c contactprovider.h \ + dict.c \ $(GITVERSION_FILE) if BUILD_UPNP diff --git a/coreapi/dict.c b/coreapi/dict.c new file mode 100644 index 000000000..69b2bb4ee --- /dev/null +++ b/coreapi/dict.c @@ -0,0 +1,159 @@ +/* +linphone +Copyright (C) 2009 Simon MORLAT (simon.morlat@linphone.org) + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "linphonecore.h" +#include "lpconfig.h" +#include "private.h" + +#include +#include +#include + + +/** + * @addtogroup linphone_dict + * @{ +**/ + + +LinphoneDictionary* linphone_dictionary_new() +{ + return belle_sip_dict_create(); +} + +LinphoneDictionary* linphone_dictionary_clone(const LinphoneDictionary* src) +{ + LinphoneDictionary* cloned = linphone_dictionary_new(); + if( cloned ){ + belle_sip_dict_clone(src, cloned); + } + return cloned; +} + +LinphoneDictionary* linphone_dictionary_ref(LinphoneDictionary* obj) +{ + return BELLE_SIP_DICT(belle_sip_object_ref(obj)); +} + +void linphone_dictionary_unref(LinphoneDictionary *obj) +{ + belle_sip_object_unref(obj); +} + +void linphone_dictionary_set_int(LinphoneDictionary* obj, const char* key, int value) +{ + belle_sip_dict_set_int(obj, key, value); +} + +int linphone_dictionary_get_int(LinphoneDictionary* obj, const char* key, int default_value) +{ + return belle_sip_dict_get_int(obj, key, default_value); +} + +void linphone_dictionary_set_string(LinphoneDictionary* obj, const char* key, const char*value) +{ + belle_sip_dict_set_string(obj, key, value); +} + +const char* linphone_dictionary_get_string(LinphoneDictionary* obj, const char* key, const char* default_value) +{ + return belle_sip_dict_get_string(obj, key, default_value); +} + +void linphone_dictionary_set_int64(LinphoneDictionary* obj, const char* key, int64_t value) +{ + belle_sip_dict_set_int64(obj, key, value); +} + +int64_t linphone_dictionary_get_int64(LinphoneDictionary* obj, const char* key, int64_t default_value) +{ + return belle_sip_dict_get_int64(obj, key, default_value); +} + +int linphone_dictionary_remove(LinphoneDictionary* obj, const char* key) +{ + return belle_sip_dict_remove(obj, key); +} + +void linphone_dictionary_clear(LinphoneDictionary* obj) +{ + belle_sip_dict_clear(obj); +} + +int linphone_dictionary_haskey(LinphoneDictionary* obj, const char* key) +{ + return belle_sip_dict_haskey(obj, key); +} + +void linphone_dictionary_foreach(const LinphoneDictionary* obj, void (*apply_func)(const char*, void*, void*), void* userdata) +{ + return belle_sip_dict_foreach(obj, apply_func, userdata); +} + +struct lp_config_to_dict { + const char* section; + const LpConfig* config; + LinphoneDictionary* dict; +}; + +static void lp_config_section_to_dict_cb(const char*key, struct lp_config_to_dict* userdata) +{ + const char* value = lp_config_get_string(userdata->config, userdata->section, key, ""); + linphone_dictionary_set_string(userdata->dict, key, value); +} + +LinphoneDictionary* lp_config_section_to_dict(const LpConfig* lpconfig, const char* section) +{ + LinphoneDictionary* dict = NULL; + struct lp_config_to_dict fd; + fd.config = lpconfig; + fd.section = section; + + dict = linphone_dictionary_new(); + fd.dict = dict; + + lp_config_for_each_entry(lpconfig, section, + (void (*)(const char*, void*))lp_config_section_to_dict_cb, + &fd); + + return dict; +} + +struct lp_config_from_dict { + const char* section; + LpConfig* config; +}; + +static void lp_config_dict_dump_cb( const char* key, void* value, void* userdata) +{ + struct lp_config_from_dict* fd= (struct lp_config_from_dict*)userdata; + lp_config_set_string(fd->config, fd->section, key, (const char*)value); +} + +void lp_config_load_dict_to_section(LpConfig* lpconfig, const char* section, const LinphoneDictionary* dict) +{ + struct lp_config_from_dict pvdata = { section, lpconfig }; + linphone_dictionary_foreach(dict,lp_config_dict_dump_cb, &pvdata); +} + + + +/** + * @} +**/ diff --git a/coreapi/linphonecore.h b/coreapi/linphonecore.h index 657cd0de6..d45ad7f85 100644 --- a/coreapi/linphonecore.h +++ b/coreapi/linphonecore.h @@ -33,6 +33,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "lpconfig.h" #include +#include #define LINPHONE_IPADDR_SIZE 64 #define LINPHONE_HOSTNAME_SIZE 128 @@ -120,6 +121,8 @@ typedef enum _LinphoneTransportType LinphoneTransportType; */ typedef struct SalAddress LinphoneAddress; +typedef struct belle_sip_dict LinphoneDictionary; + /** * The LinphoneContent struct holds data that can be embedded in a signaling message. * @ingroup misc @@ -183,6 +186,37 @@ typedef enum _LinphoneReason LinphoneReason; **/ const char *linphone_reason_to_string(LinphoneReason err); + +/* linphone dictionary */ +LINPHONE_PUBLIC LinphoneDictionary* linphone_dictionary_new(); +LinphoneDictionary * linphone_dictionary_clone(const LinphoneDictionary* src); +LinphoneDictionary * linphone_dictionary_ref(LinphoneDictionary* obj); +void linphone_dictionary_unref(LinphoneDictionary* obj); +LINPHONE_PUBLIC void linphone_dictionary_set_int(LinphoneDictionary* obj, const char* key, int value); +LINPHONE_PUBLIC int linphone_dictionary_get_int(LinphoneDictionary* obj, const char* key, int default_value); +LINPHONE_PUBLIC void linphone_dictionary_set_string(LinphoneDictionary* obj, const char* key, const char*value); +LINPHONE_PUBLIC const char* linphone_dictionary_get_string(LinphoneDictionary* obj, const char* key, const char* default_value); +LINPHONE_PUBLIC void linphone_dictionary_set_int64(LinphoneDictionary* obj, const char* key, int64_t value); +LINPHONE_PUBLIC int64_t linphone_dictionary_get_int64(LinphoneDictionary* obj, const char* key, int64_t default_value); +LINPHONE_PUBLIC int linphone_dictionary_remove(LinphoneDictionary* obj, const char* key); +LINPHONE_PUBLIC void linphone_dictionary_clear(LinphoneDictionary* obj); +LINPHONE_PUBLIC int linphone_dictionary_haskey(LinphoneDictionary* obj, const char* key); +LINPHONE_PUBLIC void linphone_dictionary_foreach( const LinphoneDictionary* obj, void (*apply_func)(const char*key, void* value, void* userdata), void* userdata); +/** + * Converts a config section into a dictionary. + * @return a #LinphoneDictionary with all the keys from a section, or NULL if the section doesn't exist + * @ingroup misc + */ +LinphoneDictionary* lp_config_section_to_dict( const LpConfig* lpconfig, const char* section ); + +/** + * Loads a dictionary into a section of the lpconfig. If the section doesn't exist it is created. + * Overwrites existing keys, creates non-existing keys. + * @ingroup misc + */ +void lp_config_load_dict_to_section( LpConfig* lpconfig, const char* section, const LinphoneDictionary* dict); + + #ifdef IN_LINPHONE #include "linphonefriend.h" #include "event.h"