/* 版权说明 */
#include <stdlib.h>//------------系统头文件 #include <stdio.h>
#include <osipparser2/osip_port.h>//---------自定义头文件
/* returns the content_type header as a string. *///------------函数功能 /* INPUT : osip_content_type_t *content_type | content_type header. */ //------------------------------应该输入那些参数 /* returns null on error. *///-------- - 返回值含义 int osip_accept_to_str(const osip_accept_t * accept, char **dest) { //---------------------------- - 对于所有不需要或不能改变的变量都应该使用const标识 char *buf; //------------------临时变量 char *tmp; size_t len;
*dest = NULL; if (accept == NULL)//--------------任何参数传入后都应该检查它的有效性 return -1;// -------------------- - 根据传入参数的有效性区别对待
if ((accept->type == NULL) && (accept->subtype == NULL)) { /* Empty header ! */ buf = (char *)osip_malloc(2); buf[0] = ' '; buf[1] = '\0'; *dest = buf; return 0; }
/* try to guess a long enough length *///----------- 段落注释 len = strlen(accept->type) + strlen(accept->subtype) + 4 /* for '/', ' ', ';' and '\0' *///------------------行注释 + 10 * osip_list_size(accept->gen_params);
buf = (char *)osip_malloc(len); //----------------所有的指针必须有其空间 tmp = buf;
sprintf(tmp, "%s/%s", accept->type, accept->subtype);
tmp = tmp + strlen(tmp); { int pos = 0; osip_generic_param_t *u_param;
#if 0//---------------------------对于不再使用但是又有必有保留以后使用的 if (!osip_list_eol(accept->gen_params, pos)) { /* needed for cannonical form! (authentication issue of rfc2543) */ sprintf(tmp, " "); tmp++; } #endif while (!osip_list_eol(accept->gen_params, pos)) { size_t tmp_len;
u_param = (osip_generic_param_t *)osip_list_get(accept->gen_params, pos); if (u_param->gvalue == NULL)//------------要为所有的函数调用判断是否成功 { osip_free(buf); return -1; } tmp_len = strlen(buf) + 4 + strlen(u_param->gname) + strlen(u_param->gvalue) + 1; if (len < tmp_len) { buf = osip_realloc(buf, tmp_len); len = tmp_len; tmp = buf + strlen(buf); } sprintf(tmp, "; %s=%s", u_param->gname, u_param->gvalue); tmp = tmp + strlen(tmp); pos++; } } *dest = buf; return 0; }
|