宏的使用

#define DEFINE_PARSE_BUILD_LINK_CSEQ(_name, _type, _param, _fmt) \
static int rtsp_msg_parse_##_name (rtsp_msg_s *msg, const char *line) \
{ \
rtsp_msg_hdr_s *hdrs = &msg->hdrs; \
if (hdrs->_name) { \
rtsp_mem_free(hdrs->_name); \
hdrs->_name = NULL; \
} \
hdrs->_name = (_type*)rtsp_mem_alloc(sizeof(_type)); \
if (!hdrs->_name) { \
err("rtsp_mem_alloc for %s failed\n", #_type); \
return -1; \
} \
if (sscanf(line, _fmt, &hdrs->_name->_param) != 1) { \
rtsp_mem_free(hdrs->_name); \
hdrs->_name = NULL; \
err("parse %s failed. line: %s\n", #_name, line); \
return -1; \
} \
return 0; \
} \
static int rtsp_msg_build_##_name (const rtsp_msg_s *msg, char *line, int size) \
{ \
if (msg->hdrs._name) { \
snprintf(line, size, _fmt "\r\n", msg->hdrs._name->_param); \
return strlen(line); \
} \
return 0; \
}


DEFINE_PARSE_BUILD_LINK_CSEQ(cseq, rtsp_msg_cseq_s, cseq, "CSeq: %u")
DEFINE_PARSE_BUILD_LINK_CSEQ(session, rtsp_msg_session_s, session, "Session: %08X")
DEFINE_PARSE_BUILD_LINK_CSEQ(content_length, rtsp_msg_content_length_s, length, "Content-Length: %u")

你可能感兴趣的:(C/C++)