]> sjero.net Git - linphone/blobdiff - coreapi/lpconfig.c
Add the linphone_core_new_with_config() function to instantiate a LinphoneCore given...
[linphone] / coreapi / lpconfig.c
index d12166ec49209b4a1265596a198c680aa8cd17d9..ca65fd1ffcd5ca4158d8e5425df06b485a985fb5 100644 (file)
@@ -109,7 +109,7 @@ static bool_t is_first_char(const char *start, const char *pos){
        return TRUE;
 }
 
-LpSection *lp_config_find_section(LpConfig *lpconfig, const char *name){
+LpSection *lp_config_find_section(const LpConfig *lpconfig, const char *name){
        LpSection *sec;
        MSList *elem;
        /*printf("Looking for section %s\n",name);*/
@@ -123,7 +123,7 @@ LpSection *lp_config_find_section(LpConfig *lpconfig, const char *name){
        return NULL;
 }
 
-LpItem *lp_section_find_item(LpSection *sec, const char *name){
+LpItem *lp_section_find_item(const LpSection *sec, const char *name){
        MSList *elem;
        LpItem *item;
        /*printf("Looking for item %s\n",name);*/
@@ -184,12 +184,13 @@ void lp_config_parse(LpConfig *lpconfig, FILE *file){
                                        if (pos2==NULL) pos2=pos1+strlen(pos1);
                                        else {
                                                *pos2='\0'; /*replace the '\n' */
-                                               pos2--;
                                        }
                                        /* remove ending white spaces */
-                                       for (; pos2>pos1 && *pos2==' ';pos2--) *pos2='\0';
+                                       for (; pos2>pos1 && pos2[-1]==' ';pos2--) pos2[-1]='\0';
+
                                        if (pos2-pos1>=0){
                                                /* found a pair key,value */
+                                               
                                                if (cur!=NULL){
                                                        LpItem *item=lp_section_find_item(cur,key);
                                                        if (item==NULL){
@@ -198,7 +199,7 @@ void lp_config_parse(LpConfig *lpconfig, FILE *file){
                                                                ms_free(item->value);
                                                                item->value=strdup(pos1);
                                                        }
-                                                       /*printf("Found %s %s=%s\n",cur->name,key,pos1);*/
+                                                       /*ms_message("Found %s=%s",key,pos1);*/
                                                }else{
                                                        ms_warning("found key,item but no sections");
                                                }
@@ -210,30 +211,42 @@ void lp_config_parse(LpConfig *lpconfig, FILE *file){
 }
 
 LpConfig * lp_config_new(const char *filename){
+       return lp_config_new_with_factory(filename, NULL);
+}
+
+LpConfig *lp_config_new_with_factory(const char *config_filename, const char *factory_config_filename) {
        LpConfig *lpconfig=lp_new0(LpConfig,1);
-       if (filename!=NULL){
-               lpconfig->filename=ortp_strdup(filename);
-               lpconfig->file=fopen(filename,"rw");
+       if (config_filename!=NULL){
+               ms_message("Using (r/w) config information from %s", config_filename);
+               lpconfig->filename=ortp_strdup(config_filename);
+               lpconfig->file=fopen(config_filename,"r+");
                if (lpconfig->file!=NULL){
+                       struct stat fileStat;
                        lp_config_parse(lpconfig,lpconfig->file);
-                       fclose(lpconfig->file);                 
+                       fclose(lpconfig->file);
 #if !defined(_WIN32_WCE)
-                       /* make existing configuration files non-group/world-accessible */
-                       if (chmod(filename, S_IRUSR | S_IWUSR) == -1)
-                               ms_warning("unable to correct permissions on "
-                                         "configuration file: %s",
-                                          strerror(errno));
+                       if ((stat(config_filename,&fileStat) == 0) && (S_ISREG(fileStat.st_mode))) {
+                               /* make existing configuration files non-group/world-accessible */
+                               if (chmod(config_filename, S_IRUSR | S_IWUSR) == -1) {
+                                       ms_warning("unable to correct permissions on "
+                                       "configuration file: %s", strerror(errno));
+                               }
+                       }
 #endif /*_WIN32_WCE*/
                        lpconfig->file=NULL;
                        lpconfig->modified=0;
                }
        }
+       if (factory_config_filename != NULL) {
+               lp_config_read_file(lpconfig, factory_config_filename);
+       }
        return lpconfig;
 }
 
 int lp_config_read_file(LpConfig *lpconfig, const char *filename){
        FILE* f=fopen(filename,"r");
        if (f!=NULL){
+               ms_message("Reading config information from %s", filename);
                lp_config_parse(lpconfig,f);
                fclose(f);
                return 0;
@@ -260,7 +273,7 @@ void lp_section_remove_item(LpSection *sec, LpItem *item){
        lp_item_destroy(item);
 }
 
-const char *lp_config_get_string(LpConfig *lpconfig, const char *section, const char *key, const char *default_string){
+const char *lp_config_get_string(const LpConfig *lpconfig, const char *section, const char *key, const char *default_string){
        LpSection *sec;
        LpItem *item;
        sec=lp_config_find_section(lpconfig,section);
@@ -271,13 +284,50 @@ const char *lp_config_get_string(LpConfig *lpconfig, const char *section, const
        return default_string;
 }
 
-int lp_config_get_int(LpConfig *lpconfig,const char *section, const char *key, int default_value){
+bool_t lp_config_get_range(const LpConfig *lpconfig, const char *section, const char *key, int *min, int *max, int default_min, int default_max) {
+       const char *str = lp_config_get_string(lpconfig, section, key, NULL);
+       if (str != NULL) {
+               char *minusptr = strchr(str, '-');
+               if ((minusptr == NULL) || (minusptr == str)) {
+                       *min = default_min;
+                       *max = default_max;
+                       return FALSE;
+               }
+               *min = atoi(str);
+               *max = atoi(minusptr + 1);
+               return TRUE;
+       } else {
+               *min = default_min;
+               *max = default_max;
+               return TRUE;
+       }
+}
+
+int lp_config_get_int(const LpConfig *lpconfig,const char *section, const char *key, int default_value){
+       const char *str=lp_config_get_string(lpconfig,section,key,NULL);
+       if (str!=NULL) {
+               int ret=0;
+               if (strstr(str,"0x")==str){
+                       sscanf(str,"%x",&ret);
+               }else ret=atoi(str);
+               return ret;
+       }
+       else return default_value;
+}
+
+int64_t lp_config_get_int64(const LpConfig *lpconfig,const char *section, const char *key, int64_t default_value){
        const char *str=lp_config_get_string(lpconfig,section,key,NULL);
-       if (str!=NULL) return atoi(str);
+       if (str!=NULL) {
+#ifdef WIN32
+               return (int64_t)_atoi64(str);
+#else
+               return atoll(str);
+#endif
+       }
        else return default_value;
 }
 
-float lp_config_get_float(LpConfig *lpconfig,const char *section, const char *key, float default_value){
+float lp_config_get_float(const LpConfig *lpconfig,const char *section, const char *key, float default_value){
        const char *str=lp_config_get_string(lpconfig,section,key,NULL);
        float ret=default_value;
        if (str==NULL) return default_value;
@@ -306,12 +356,31 @@ void lp_config_set_string(LpConfig *lpconfig,const char *section, const char *ke
        lpconfig->modified++;
 }
 
+void lp_config_set_range(LpConfig *lpconfig, const char *section, const char *key, int min_value, int max_value) {
+       char tmp[30];
+       snprintf(tmp, sizeof(tmp), "%i-%i", min_value, max_value);
+       lp_config_set_string(lpconfig, section, key, tmp);
+}
+
 void lp_config_set_int(LpConfig *lpconfig,const char *section, const char *key, int value){
        char tmp[30];
        snprintf(tmp,sizeof(tmp),"%i",value);
        lp_config_set_string(lpconfig,section,key,tmp);
 }
 
+void lp_config_set_int_hex(LpConfig *lpconfig,const char *section, const char *key, int value){
+       char tmp[30];
+       snprintf(tmp,sizeof(tmp),"0x%x",value);
+       lp_config_set_string(lpconfig,section,key,tmp);
+}
+
+void lp_config_set_int64(LpConfig *lpconfig,const char *section, const char *key, int64_t value){
+       char tmp[30];
+       snprintf(tmp,sizeof(tmp),"%lli",(long long)value);
+       lp_config_set_string(lpconfig,section,key,tmp);
+}
+
+
 void lp_config_set_float(LpConfig *lpconfig,const char *section, const char *key, float value){
        char tmp[30];
        snprintf(tmp,sizeof(tmp),"%f",value);
@@ -348,11 +417,32 @@ int lp_config_sync(LpConfig *lpconfig){
        return 0;
 }
 
-int lp_config_has_section(LpConfig *lpconfig, const char *section){
+int lp_config_has_section(const LpConfig *lpconfig, const char *section){
        if (lp_config_find_section(lpconfig,section)!=NULL) return 1;
        return 0;
 }
 
+void lp_config_for_each_section(const LpConfig *lpconfig, void (*callback)(const char *section, void *ctx), void *ctx) {
+       LpSection *sec;
+       MSList *elem;
+       for (elem=lpconfig->sections;elem!=NULL;elem=ms_list_next(elem)){
+               sec=(LpSection*)elem->data;
+               callback(sec->name, ctx);
+       }
+}
+
+void lp_config_for_each_entry(const LpConfig *lpconfig, const char *section, void (*callback)(const char *entry, void *ctx), void *ctx) {
+       LpItem *item;
+       MSList *elem;
+       LpSection *sec=lp_config_find_section(lpconfig,section);
+       if (sec!=NULL){
+               for (elem=sec->items;elem!=NULL;elem=ms_list_next(elem)){
+                       item=(LpItem*)elem->data;
+                       callback(item->key, ctx);
+               }
+       }
+}
+
 void lp_config_clean_section(LpConfig *lpconfig, const char *section){
        LpSection *sec=lp_config_find_section(lpconfig,section);
        if (sec!=NULL){