#define PARALENGTH 50
typedef struct Content_Type_Para{
char paraName[PARALENGTH];
char paraValue[PARALENGTH];
}ContentTypePara_t;
void setContentType(const char *content)
{
//content参数形如"application/bear;params1=XXXYYY,params2=ZZZZSSS"
char contentTypeName[PARALENGTH];
char contentTypeValue[PARALENGTH];
memset(contentTypeName, 0, PARALENGTH);
memset(contentTypeValue, 0, PARALENGTH);
int ContentTypeParasNumber = 0; //参数个数
ContentTypePara_t contentTypeParas[5]; //参数数组
for(int k=0; k< 5; k++)
{
memset(contentTypeParas[k].paraName, 0, PARALENGTH);
memset(contentTypeParas[k].paraValue, 0, PARALENGTH);
}
//在临时空间里操作字符串
char *p;
char temp[1024];
memset(temp, 0, sizeof(temp));
strncpy(temp, content, strlen(content));
p = temp;
//临时子串
char *tp;
char subtemp[PARALENGTH];
memset(subtemp, 0, PARALENGTH);
tp = subtemp;
char flag; //用来标识字符串的分割点
//字符串查找
for(; *p != '\0'; p++)
{
if((*p != '/')&&(*p != ';')&&(*p != '=')&&(*p != ',')) //连续的字符串,复制
{
*tp = *p;
tp++;
}
else if(*p =='/') //'/'之前的字符串是contentTypeName
{
strncpy(contentTypeName, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp)); //清空临时子串
tp = subtemp;
flag = '/'; //标识一个子串的结束
}
else if(*p == ';') //如果参数之间使用',',那么这个头字段只有一个';'
{
if(flag == '/') //';'之前的flag一定是'/'
{
strncpy(contentTypeValue, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp));
tp = subtemp;
flag = ';';
}
}
else if(*p == '=') //参数的赋值使用'='
{
if((flag == ';') || (flag == ','))
{
strncpy(contentTypeParas[ContentTypeParasNumber].paraName, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp));
tp = subtemp;
flag = '=';
}
}
else if(*p == ',')
{
if(flag == '=')
{
strncpy(contentTypeParas[ContentTypeParasNumber].paraValue, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp));
tp = subtemp;
flag = ',';
ContentTypeParasNumber++;
}
}
}
//最后一个子串
if(flag == '=')
{
strncpy(contentTypeParas[ContentTypeParasNumber].paraValue, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp));
tp = subtemp;
ContentTypeParasNumber++;
}
else if(flag = '/') //没有参数的情况
{
strncpy(contentTypeValue, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp));
tp = subtemp;
}
printf("|*|*|***********************************************\n");
printf("|*|*|contentTypeName : %s \n", contentTypeName);
printf("|*|*|contentTypeValue : %s \n", contentTypeValue);
for(int i = 0; i