比如:
#ifndef MSGTYPE_H
#define MSGTYPE_H
// login
const short MSG_LOGIN = 8;
// register
const short MSG_REGIS = 22;
// send message to friend
const short MSG_SENDTOFRIEND = 2;
// send message to group
const short MSG_SENDTOGROUP = 3;
// add friend
const short MSG_ADDFRIEND = 4;
const short MSG_ADDFRIEND_WAIT = 9;
const short MSG_ADDFRIEND_NONAME = 6;
// initial client's friend list
const short MSG_INITFRIEND_ONLINE = 7;
const short MSG_INITFRIEND_OFFLINE = 88;
const short MSG_INITGROUP = 11;
// 当本地没有检测到好友等初始化数据,那么服务器给予相关数据
const short MSG_BUILDGROUP = 10;
// invite member
const short MSG_GROUPINVIMEM = 12;
const short MSG_GROUPINVIMEM_WAIT = 18;
const short MSG_GROUPINVIMEM_SUCC = 13;
const short MSG_GROUPINVIMEM_FAIL = 14;
const short MSG_GROUPINVIMEM_NONAME = 15;
// request enter group
const short MSG_REQENTERGROUP = 16;
const short MSG_REQENTERGROUP_WAIT = 17;
const short MSG_REQENTERGROUP_SUCC = 1;
const short MSG_REQENTERGROUP_FAIL = 19;
const short MSG_REQENTERGROUP_NONAME = 20;
const short MSG_DOWNLOAD_FILE = 21;
const short MSG_UPLOAD_FILE = 64;
//
/reload控制信息//
const short MSG_RELOAD_FILE = 23;
//
const short MSG_DELETE_FILE = 24;
const short MSG_ECHO = 25;
const short MSG_OK = 26;
const short MSG_ERROR = 27;
const short MSG_NOT_CLEAR = 28;
#endif // MSGTYPE_H
有这么一个文件,这是一个例子。
因为编程过程中间又插入一个消息类型,那么此时又要修改后面的数据顺序化,非常繁琐,
python实现自动顺序化:
1 #!/usr/bin/python
2
3 import re
4 import sys
5 import os
6
7 def SerializeNumber():
8 fd = open(sys.argv[1], 'r')
9 fd_1 = open("swap", "w")
10 lines = fd.readlines()
11 ls = []
12 i = 0
13 j = 0
14 print lines
15 for line in lines:
16 if re.match("const short", line):
17 result = re.match("const short \w+_\w+_?\w* ?= ?", line).group();
18 lines[i] = result + str(j) + ";"
19 print lines[i]
20 j+=1
21 ls.append(lines[i].strip('\n') + '\n')
22 i += 1
23
24 fd_1.writelines(ls)
25 fd.close()
26 fd_1.close()
27 os.remove(sys.argv[1])
28 os.rename("swap", sys.argv[1])
29
30
31 if __name__ == '__main__':
32 SerializeNumber()
上面通过匹配每个消息,进行值顺序化,代码非常简单。
python是自动化的利器。