Libevent分解之数据结构- 字典表

14 篇文章 0 订阅
#ifndef _STUDENT_TEST_C_
#define  _STUDENT_TEST_C_
#include "queue.h"


#define ev_uint8_t unsigned char
#define ev_int8_t signed char


struct StudentKV
{
    TAILQ_ENTRY(StudentKV) next;
char *key;
char *value;
};
TAILQ_HEAD (StudentKVQ, StudentKV);


char EVUTIL_TOLOWER(char c);
// 对比字符串
int evutil_ascii_strcasecmp(const char *s1, const char *s2);
//查找字典表
const char * find_header(const struct StudentKVQ *headers, const char *key);
void clear_headers(struct StudentKVQ *headers);
int remove_header(struct StudentKVQ *headers, const char *key);
static int header_is_valid_value(const char *value);
int add_header(struct StudentKVQ *headers,const char *key, const char *value);
static int add_header_internal(struct StudentKVQ *headers,const char *key, const char *value);

#endif



#include "stdio.h"
#include "student.h"


static const unsigned char EVUTIL_TOLOWER_TABLE[256] = 
{
  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
  32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
  48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
  64,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
  112,113,114,115,116,117,118,119,120,121,122,91,92,93,94,95,
  96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
  112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
  224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
  240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,
};


char EVUTIL_TOLOWER(char c)
{
return ((char)EVUTIL_TOLOWER_TABLE[(ev_uint8_t)c]);
}


int evutil_ascii_strcasecmp(const char *s1, const char *s2)
{
char c1, c2;
while (1) {
c1 = EVUTIL_TOLOWER(*s1++);
c2 = EVUTIL_TOLOWER(*s2++);
if (c1 < c2)
return -1;
else if (c1 > c2)
return 1;
else if (c1 == 0)
return 0;
}
}


const char *
find_header(const struct StudentKVQ *headers, const char *key)
{
struct StudentKV *header;
TAILQ_FOREACH(header, headers, next) 
{
if (evutil_ascii_strcasecmp(header->key, key) == 0)
return (header->value);
}


return (NULL);
}
void clear_headers(struct StudentKVQ *headers)
{
struct StudentKV *header;


for (header = TAILQ_FIRST(headers);
   header != NULL;
   header = TAILQ_FIRST(headers)) {
TAILQ_REMOVE(headers, header, next);
free(header->key);
   free(header->value);
free(header);
}
}


int remove_header(struct StudentKVQ *headers, const char *key)
{
struct StudentKV *header;
TAILQ_FOREACH(header, headers, next) {
if (evutil_ascii_strcasecmp(header->key, key) == 0)
break;

}
if (header == NULL)
return (-1);
/* Free and remove the header that we found */
TAILQ_REMOVE(headers, header, next);
free(header->key);
free(header->value);
free(header);
return (0);
}
static int header_is_valid_value(const char *value)
{
const char *p = value;


while ((p = strpbrk(p, "\r\n")) != NULL) {
/* we really expect only one new line */
p += strspn(p, "\r\n");
/* we expect a space or tab for continuation */
if (*p != ' ' && *p != '\t')
return (0);
}
return (1);
}
int add_header(struct StudentKVQ *headers,const char *key, const char *value)
{
//event_debug(("%s: key: %s val: %s\n", __func__, key, value));


if (strchr(key, '\r') != NULL || strchr(key, '\n') != NULL) {
/* drop illegal headers */
// event_debug(("%s: dropping illegal header key\n", __func__));
return (-1);
}


if (!header_is_valid_value(value)) {
// event_debug(("%s: dropping illegal header value\n", __func__));
return (-1);
}


return (add_header_internal(headers, key, value));
}
static int add_header_internal(struct StudentKVQ *headers,const char *key, const char *value)
{
struct StudentKV *header = calloc(1, sizeof(struct StudentKV));
if (header == NULL) {
// event_warn("%s: calloc", __func__);
return (-1);
}
if ((header->key = strdup(key)) == NULL) {
free(header);
// event_warn("%s: strdup", __func__);
return (-1);
}
if ((header->value = strdup(value)) == NULL) {
free(header->key);
free(header);
// event_warn("%s: strdup", __func__);
return (-1);
}


TAILQ_INSERT_TAIL(headers, header, next);
return (0);
}


#include "stdio.h"


#include "student.h"
void main()
{
 int k;
 char* tem;
 struct StudentKVQ head;
 TAILQ_INIT(&head);
 add_header(&head,"1","abc");
 add_header(&head,"2","def");
 add_header(&head,"3","jiayp");
 tem=find_header(&head,"3");
 k=0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值