码迷,mamicode.com
首页 > 其他好文 > 详细

消息码和消息描述字符串关联的一种实现

时间:2017-04-23 23:10:05      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:name   size   res   printf   family   ret   class   cat   command   

若定义JMESSAGE(code,string)code ,宏,就表示enum list of message codes.

若定义JMESSAGE(code,string)string ,宏,就可表示a message string table.

==========================================================================

<jerror.h>

 1 #ifndef JMESSAGE
 2 #ifndef JERROR_H
 3 /* First time through, define the enum list */
 4 #define JMAKE_ENUM_LIST
 5 #else
 6 /* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
 7 #define JMESSAGE(code,string)
 8 #endif /* JERROR_H */
 9 #endif /* JMESSAGE */
10 
11 #ifdef JMAKE_ENUM_LIST
12 
13 typedef enum {
14 
15 #define JMESSAGE(code,string)    code ,
16 
17 #endif /* JMAKE_ENUM_LIST */
18 
19 JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */
20 
21 /* For maintenance convenience, list is alphabetical by message code name */
22 JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
23 JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
24 ......
25 ......
26 ......
27 JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG")
28 JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")
29 
30 #ifdef JMAKE_ENUM_LIST
31 
32   JMSG_LASTMSGCODE
33 } J_MESSAGE_CODE;
34 
35 #undef JMAKE_ENUM_LIST
36 #endif /* JMAKE_ENUM_LIST */
37 
38 /* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
39 #undef JMESSAGE

那么,.c文件中怎样使用message codes和对应的message desription string?

 

1 #include “jerror.h”//这一次是为了定义enum list of message codes 
2 #define JMESSAGE(code,string)    string ,
3 
4 const char * const jpeg_std_message_table[] = {
5 #include "jerror.h"//这一次是为了定义message string table
6   NULL
7 };

 

上述的代码复用率就非常高。

 

预编译后的形式如下:

 1 # 1 "main.c"
 2 # 1 "<built-in>"
 3 # 1 "<command-line>"
 4 # 1 "/usr/include/stdc-predef.h" 1 3 4
 5 # 1 "<command-line>" 2
 6 # 1 "main.c"
 7 
 8 # 1 "jerror.h" 1
 9 # 19 "jerror.h"
10 typedef enum {
11 
12 JMSG_NOMESSAGE ,
13 
14 
15 JERR_ARITH_NOTIMPL ,
16 
17 JERR_BAD_ALIGN_TYPE ,
18 JERR_BAD_ALLOC_CHUNK ,
19 JWRN_JPEG_EOF ,
20 JWRN_MUST_RESYNC ,
21 
22 JWRN_NOT_SEQUENTIAL ,
23 JWRN_TOO_MUCH_DATA ,
24 
25 
26 
27   JMSG_LASTMSGCODE
28 } J_MESSAGE_CODE;
29 # 3 "main.c" 2
30 
31 
32 
33 const char * const jpeg_std_message_table[] = {
34 # 1 "jerror.h" 1
35 # 25 "jerror.h"
36 "Bogus message code %d" ,
37 
38 
39 "Sorry, there are legal restrictions on arithmetic coding" ,
40 
41 "ALIGN_TYPE is wrong, please fix" ,
42 "MAX_ALLOC_CHUNK is wrong, please fix" ,
43 "Premature end of JPEG file" ,
44 "Corrupt JPEG data: found marker 0x%02x instead of RST%d" ,
45 
46 "Invalid SOS parameters for sequential JPEG" ,
47 "Application transferred too many scanlines" ,
48 # 8 "main.c" 2
49  NULL
50 };
51 
52 int main(int argc, char** argv)
53 {
54 
55  printf("%s\n", jpeg_std_message_table[JERR_ARITH_NOTIMPL]);
56 
57  return 0;
58 }

 

 

若定义JMESSAGE(code,string)code ,宏,就表示enum list of message codes.

若定义JMESSAGE(code,string)string ,宏,就可表示a message string table.

消息码和消息描述字符串关联的一种实现

标签:name   size   res   printf   family   ret   class   cat   command   

原文地址:http://www.cnblogs.com/black-mamba/p/6754493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!