看ligttpd源代码的一个疑惑

loseblue 2009-07-30 11:47:15

static server *server_init(void) {
int i;
FILE *frandom = NULL;

server *srv = calloc(1, sizeof(*srv));
assert(srv);
#define CLEAN(x) \
srv->x = buffer_init();

CLEAN(response_header);
CLEAN(parse_full_path);
CLEAN(ts_debug_str);
CLEAN(ts_date_str);
CLEAN(errorlog_buf);
CLEAN(response_range);
CLEAN(tmp_buf);
srv->empty_string = buffer_init_string("");
CLEAN(cond_check_buf);

CLEAN(srvconf.errorlog_file);
CLEAN(srvconf.groupname);
CLEAN(srvconf.username);
CLEAN(srvconf.changeroot);
CLEAN(srvconf.bindhost);
CLEAN(srvconf.event_handler);
CLEAN(srvconf.pid_file);

CLEAN(tmp_chunk_len);
#undef CLEAN

#define CLEAN(x) \
srv->x = array_init();

CLEAN(config_context);
CLEAN(config_touched);
CLEAN(status);
#undef CLEAN

/*后面还有*/
}/*main 结束*/
......


我没搞懂这里面的#define CLEAN(x)是什么?
如果是宏定义函数,那为什么在main()函数里面?
而且连着两个?

请问各位这是什么用法?
...全文
107 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
loseblue 2009-07-31
  • 打赏
  • 举报
回复
明白了,谢谢各位~~
mstlq 2009-07-31
  • 打赏
  • 举报
回复
等价于下面的
static server *server_init(void) {
int i;
FILE *frandom = NULL;

server *srv = calloc(1, sizeof(*srv));
assert(srv);
#define CLEAN(x) srv->x = buffer_init();

CLEAN(response_header); //等价于srv->response_header = buffer_init();
CLEAN(parse_full_path);
CLEAN(ts_debug_str);
CLEAN(ts_date_str);
CLEAN(errorlog_buf);
CLEAN(response_range);
CLEAN(tmp_buf);
srv->empty_string = buffer_init_string("");
CLEAN(cond_check_buf);

CLEAN(srvconf.errorlog_file);
CLEAN(srvconf.groupname);
CLEAN(srvconf.username);
CLEAN(srvconf.changeroot);
CLEAN(srvconf.bindhost);
CLEAN(srvconf.event_handler);
CLEAN(srvconf.pid_file);

CLEAN(tmp_chunk_len);
#undef CLEAN //原来的#define CLEAN(x) srv->x = buffer_init();到这里失效

#define CLEAN(x) srv->x = array_init();

CLEAN(config_context); //等价于srv-> config_context= array_init();
CLEAN(config_touched);
CLEAN(status);
#undef CLEAN //原来的#define CLEAN(x) srv->x = array_init();到这里失效

Nio96 2009-07-31
  • 打赏
  • 举报
回复
这有什么奇怪的啊。
没看到后面有一个反斜杠么? 下面那一行就是他展开后的语句了。

#define CLEAN(x) \
srv->x = buffer_init();

#undef
fox000002 2009-07-30
  • 打赏
  • 举报
回复
不是有 #undef CLEAN 吗

define 的作用范围只到 undef 之前,之后无效了

lz 可以把这种用法看成是 CLEAN 这个宏名称的回收再利用

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧