最近一直在将自己的程序进行重构,在重构过程中,有这样一个需求:对一个库文件里的所有函数名添加前缀。
在网络搜素了一番,在百度文章搜索到一篇,链接地址,它需要将整个工程目录中的文件整理到C:\\filelist.txt中之后,使用symbol收集,然后再执行替换操作完成。
经过半天的研读宏语言的一些简要语法,觉得上述所描述的方法有点复杂,于是想能不能直接在source insight工程里进行替换,前缀字符自己定义。
macro add_prefix_functionname_ifile()
{
szprefix = Ask("Enter prefix you want to add on function name in file:")
//获得前缀字符串
//if(szprefix == "")
//return
szprefixlim = strlen(szprefix) //得到前缀串的长度
hbuf = GetCurrentBuf() //获得当前打开文件的句柄
isym = 0
isymMax = GetBufSymCount(hbuf) //symbol的数量
lnlim = GetBufLineCount (hbuf) //用于搜索当前整个文件
while(isym < isymMax)
{
symbol = GetBufSymLocation(hbuf, isym)
//symbol一个为定义的函数,一个可能在H文件中声明的函数原型
if((symbol.Type == "Function Prototype") || (symbol.Type == "Function")
{
szold = GetBufSymName(hbuf, isym)
szcmp = strmid(szold, 0, szprefixlim)
if(szcmp != szprefix) //比较是否已经添加了所需的前缀
{
sznew = "@szprefix@@szold@"
ReplaceInBuf(hbuf, szold, sznew, 0, lnlim, TRUE, FALSE, TRUE, FALSE)
}
}
isym = isym + 1
}
SaveBuf(hbuf)
}
依据这种方式,可以对其进行延伸和拓展,在整个工程中函数名添加前缀,又或者在当前文件删除前缀和在整个工程中函数删除前缀。
将下列代码保存为em文件,可以直接将内容拷贝到source inight 中的base工程中的util.em文件,或者在base中添加这em文件均可。操作方法在文库中均有
提示。
macro add_prefix_functionname_ifile()
{
szprefix = Ask("Enter prefix you want to add on function name in file:")
//if(szprefix == "")
//return
szprefixlim = strlen(szprefix)
hbuf = GetCurrentBuf()
isym = 0
isymMax = GetBufSymCount(hbuf)
lnlim = GetBufLineCount (hbuf)
while(isym < isymMax)
{
symbol = GetBufSymLocation(hbuf, isym)
if((symbol.Type == "Function Prototype") || (symbol.Type == "Function")
{
szold = GetBufSymName(hbuf, isym)
szcmp = strmid(szold, 0, szprefixlim)
if(szcmp != szprefix)
{
sznew = "@szprefix@@szold@"
ReplaceInBuf(hbuf, szold, sznew, 0, lnlim, TRUE, FALSE, TRUE, FALSE)
}
}
isym = isym + 1
}
SaveBuf(hbuf)
}
macro add_prefix_functionname_iproj()
{
szprefix = Ask("Enter prefix you want to add on function name in project:")
//if(szprefix == "")
// return
szprefixlim = strlen(szprefix)
hprj = GetCurrentProj();
ifileMax = GetProjFileCount(hprj)
ifile = 0
while(ifile < ifileMax)
{
filename = GetProjFileName(hprj, ifile)
hbuf = OpenBuf(filename)
ifileOpen = FALSE
if(hbuf == hNil)
{
hbuf = GetBufHandle(filename)
ifileOpen = TRUE
}
isym = 0
isymMax = GetBufSymCount(hbuf)
lnlim = GetBufLineCount (hbuf)
while(isym < isymMax)
{
symbol = GetBufSymLocation(hbuf, isym)
if((symbol.Type == "Function Prototype") || (symbol.Type == "Function")
{
szold = GetBufSymName(hbuf, isym)
szcmp = strmid(szold, 0, szprefixlim)
if(szcmp != szprefix)
{
sznew = "@szprefix@@szold@"
ReplaceInBuf(hbuf, szold, sznew, 0, lnlim, TRUE, FALSE, TRUE, FALSE)
}
}
isym = isym + 1
}
SaveBuf(hbuf)
ifile = ifile + 1
}
}
macro del_prefix_functionname_ifile()
{
szprefix = Ask("Enter prefix you want to delete on function name in file:")
//if(szprefix == "")
// return
szprefixlim = strlen(szprefix)
hbuf = GetCurrentBuf()
isym = 0
isymMax = GetBufSymCount(hbuf)
lnlim = GetBufLineCount (hbuf)
while(isym < isymMax)
{
symbol = GetBufSymLocation(hbuf, isym)
if((symbol.Type == "Function Prototype") || (symbol.Type == "Function")
{
szold = GetBufSymName(hbuf, isym)
szcmp = strmid(szold, 0, szprefixlim)
if(szcmp == szprefix){
szoldlim = strlen(szold)
sznew = strmid(szold, szprefixlim, szoldlim)
ReplaceInBuf(hbuf, szold, sznew, 0, lnlim, TRUE, FALSE, TRUE, FALSE)
}
}
isym = isym + 1
}
SaveBuf(hbuf)
}
macro del_prefix_functionname_iproj()
{
szprefix = Ask("Enter prefix you want to delete on function name in project:")
//if(szprefix == "")
// return
szprefixlim = strlen(szprefix)
hprj = GetCurrentProj();
ifileMax = GetProjFileCount(hprj)
ifile = 0
while(ifile < ifileMax)
{
filename = GetProjFileName(hprj, ifile)
hbuf = OpenBuf(filename)
ifileOpen = FALSE
if(hbuf == hNil)
{
hbuf = GetBufHandle(filename)
ifileOpen = TRUE
}
isym = 0
isymMax = GetBufSymCount(hbuf)
lnlim = GetBufLineCount (hbuf)
while(isym < isymMax)
{
symbol = GetBufSymLocation(hbuf, isym)
if((symbol.Type == "Function Prototype") || (symbol.Type == "Function")
{
szold = GetBufSymName(hbuf, isym)
szcmp = strmid(szold, 0, szprefixlim)
if(szcmp == szprefix){
szoldlim = strlen(szold)
sznew = strmid(szold, szprefixlim, szoldlim)
ReplaceInBuf(hbuf, szold, sznew, 0, lnlim, TRUE, FALSE, TRUE, FALSE)
}
}
isym = isym + 1
}
SaveBuf(hbuf)
ifile = ifile + 1
}
}
希望对大家有点小小帮助。