belle_sip_parameters.c 6.14 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    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
7
    the Free Software Foundation, either version 2 of the License, or
jehan's avatar
jehan committed
8 9 10 11 12 13 14 15 16 17 18 19 20
    (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, see <http://www.gnu.org/licenses/>.
*/
#include "belle-sip/belle-sip.h"
#include "belle-sip/parameters.h"
#include "belle_sip_internal.h"
jehan's avatar
jehan committed
21
#include "belle-sip/headers.h"
jehan's avatar
jehan committed
22

Simon Morlat's avatar
Simon Morlat committed
23 24 25
void belle_sip_parameters_init(belle_sip_parameters_t *obj){
}

jehan's avatar
jehan committed
26
void belle_sip_parameters_clean(belle_sip_parameters_t* params) {
jehan's avatar
jehan committed
27 28
	if (params->param_list) belle_sip_list_free_with_data (params->param_list, (void (*)(void*))belle_sip_param_pair_destroy);
	if (params->paramnames_list) belle_sip_list_free(params->paramnames_list);
jehan's avatar
jehan committed
29 30 31
	params->paramnames_list=NULL;
	params->param_list=NULL;
}
jehan's avatar
jehan committed
32 33

static void belle_sip_parameters_destroy(belle_sip_parameters_t* params) {
jehan's avatar
jehan committed
34
	belle_sip_parameters_clean(params);
jehan's avatar
jehan committed
35 36
}

37
void belle_sip_parameters_copy_parameters_from(belle_sip_parameters_t *params, const belle_sip_parameters_t *orig){
38 39 40 41 42
	belle_sip_list_t* list=orig->param_list;
	for(;list!=NULL;list=list->next){
		belle_sip_param_pair_t* container = (belle_sip_param_pair_t* )(list->data);
		belle_sip_parameters_set_parameter( params,container->name,container->value);
	}
jehan's avatar
jehan committed
43
}
Simon Morlat's avatar
Simon Morlat committed
44

45 46 47 48 49
static void belle_sip_parameters_clone(belle_sip_parameters_t *params, const belle_sip_parameters_t *orig){
	belle_sip_parameters_copy_parameters_from(params,orig);
}


50
belle_sip_error_code belle_sip_parameters_marshal(const belle_sip_parameters_t* params, char* buff, size_t buff_size, size_t *offset) {
jehan's avatar
jehan committed
51
	belle_sip_list_t* list=params->param_list;
Ghislain MARY's avatar
Ghislain MARY committed
52
	belle_sip_error_code error=BELLE_SIP_OK;
jehan's avatar
jehan committed
53 54 55
	for(;list!=NULL;list=list->next){
		belle_sip_param_pair_t* container = (belle_sip_param_pair_t* )(list->data);
		if (container->value) {
56
			error=belle_sip_snprintf(buff,buff_size,offset,";%s=%s", container->name, container->value);
jehan's avatar
jehan committed
57
		} else {
58
			error=belle_sip_snprintf(buff,buff_size,offset,";%s", container->name);
jehan's avatar
jehan committed
59
		}
Ghislain MARY's avatar
Ghislain MARY committed
60
		if (error!=BELLE_SIP_OK) return error;
jehan's avatar
jehan committed
61
	}
Ghislain MARY's avatar
Ghislain MARY committed
62
	return error;
jehan's avatar
jehan committed
63
}
64
BELLE_SIP_NEW_HEADER(parameters,header,"parameters")
65
const belle_sip_list_t *belle_sip_parameters_get_parameters(const belle_sip_parameters_t* obj) {
jehan's avatar
jehan committed
66 67
	return obj->param_list;
}
jehan's avatar
jehan committed
68

69
const char* belle_sip_parameters_get_parameter_base(const belle_sip_parameters_t* params,const char* name,belle_sip_compare_func func) {
jehan's avatar
jehan committed
70
	belle_sip_list_t *  lResult = belle_sip_list_find_custom(params->param_list, func, name);
jehan's avatar
jehan committed
71 72 73 74 75 76 77
	if (lResult) {
		return ((belle_sip_param_pair_t*)(lResult->data))->value;
	}
	else {
		return NULL;
	}
}
78
const char* belle_sip_parameters_get_parameter(const belle_sip_parameters_t* params,const char* name) {
jehan's avatar
jehan committed
79 80
	return belle_sip_parameters_get_parameter_base(params,name,(belle_sip_compare_func)belle_sip_param_pair_comp_func);
}
81
const char* belle_sip_parameters_get_case_parameter(const belle_sip_parameters_t* params,const char* name) {
jehan's avatar
jehan committed
82 83 84
	return belle_sip_parameters_get_parameter_base(params,name,(belle_sip_compare_func)belle_sip_param_pair_case_comp_func);
}

85
unsigned int belle_sip_parameters_has_parameter(const belle_sip_parameters_t* params,const char* name) {
jehan's avatar
jehan committed
86 87
	return belle_sip_list_find_custom(params->param_list, (belle_sip_compare_func)belle_sip_param_pair_comp_func, name) != NULL;
}
88 89

void belle_sip_parameters_set_parameter(belle_sip_parameters_t* params,const char* name,const char* value) {
jehan's avatar
jehan committed
90
	/*1 check if present*/
91
	belle_sip_param_pair_t* lNewpair;
jehan's avatar
jehan committed
92 93 94
	belle_sip_list_t *  lResult = belle_sip_list_find_custom(params->paramnames_list, (belle_sip_compare_func)strcmp, name);
	/* first remove from header names list*/
	if (lResult) {
Simon Morlat's avatar
Simon Morlat committed
95
		params->paramnames_list=belle_sip_list_delete_link(params->paramnames_list,lResult);
jehan's avatar
jehan committed
96 97 98 99 100
	}
	/* next from header list*/
	lResult = belle_sip_list_find_custom(params->param_list, (belle_sip_compare_func)belle_sip_param_pair_comp_func, name);
	if (lResult) {
		belle_sip_param_pair_destroy(lResult->data);
Simon Morlat's avatar
Simon Morlat committed
101
		params->param_list=belle_sip_list_delete_link(params->param_list,lResult);
jehan's avatar
jehan committed
102 103
	}
	/* 2 insert*/
104
	lNewpair = belle_sip_param_pair_new(name,value);
jehan's avatar
jehan committed
105 106 107 108
	params->param_list=belle_sip_list_append(params->param_list,lNewpair);
	params->paramnames_list=belle_sip_list_append(params->paramnames_list,lNewpair->name);
}

109 110 111 112 113 114 115 116
void belle_sip_parameters_set(belle_sip_parameters_t *parameters, const char* params){
	belle_sip_parameters_clean(parameters);
	if (params && *params!='\0'){
		char *tmp=belle_sip_strdup(params);
		char *end_of_param;
		char *current=tmp;
		char *equal;
		char *next;
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		do{
			end_of_param=strchr(current,';');
			equal=strchr(current,'=');
			if (!end_of_param) {
				end_of_param=current+strlen(current);
				next=end_of_param;
			}else{
				*end_of_param='\0';
				next=end_of_param+1;
			}
			if (equal && equal<end_of_param){
				*equal='\0';
				belle_sip_parameters_set_parameter(parameters,current,equal+1);
			}else{
				belle_sip_parameters_set_parameter(parameters,current,NULL);
			}
			current=next;
		}while(*current!='\0');
		belle_sip_free(tmp);
	}
}

jehan's avatar
jehan committed
140
const belle_sip_list_t*	belle_sip_parameters_get_parameter_names(const belle_sip_parameters_t* params) {
jehan's avatar
jehan committed
141
	return params?params->paramnames_list:NULL;
jehan's avatar
jehan committed
142
}
143

144
void belle_sip_parameters_remove_parameter(belle_sip_parameters_t* params,const char* name) {
jehan's avatar
jehan committed
145 146 147 148
	/*1 check if present*/
	belle_sip_list_t *  lResult = belle_sip_list_find_custom(params->paramnames_list, (belle_sip_compare_func)strcmp, name);
	/* first remove from header names list*/
	if (lResult) {
Simon Morlat's avatar
Simon Morlat committed
149
		params->paramnames_list=belle_sip_list_delete_link(params->paramnames_list,lResult);
jehan's avatar
jehan committed
150 151 152 153 154 155
		/*next remove node*/
		lResult = belle_sip_list_find_custom(params->param_list, (belle_sip_compare_func)belle_sip_param_pair_comp_func, name);
		if (lResult) {
			belle_sip_param_pair_destroy(lResult->data);
			params->param_list=belle_sip_list_delete_link(params->param_list,lResult);
		}
jehan's avatar
jehan committed
156 157 158
	}
}