與VS2008系列的IDE相比Qt Creator開發代碼插件比較簡單
閒話不說,看我的步驟:
(1)Qt Creator中“菜單”-“工具”-“選項”-“環境”-“外部工具”-“文本”
(2)接下來說明一下如何寫一個工具處理輸入的文本
這個輸入文本通過控制檯的管道傳入到工具程序中也就是stdout ,從工具那邊就是stdin可以讀取到使用如下代碼進行讀取輸入文本:
void _FormatCode(constCStringA& IN strSelectCode, CStringA& OUT strOutput);
#define MAX_CODE_LEN (3*1024*1024)
int _tmain(int argc, _TCHAR*argv[])
{
#ifdef _DEBUG
MessageBox(NULL,L"srcFormater Attach", L"please Attach", 0);
#endif
//通過stdin讀取輸入的代碼
//
char*pbuf = new(nothrow) char[MAX_CODE_LEN];
if(pbuf)
{
intp = 0;
while(TRUE)
{
intc = fgetc(stdin);
if(c== EOF)
{
break;
}
pbuf[p++]= (char)c;
}
pbuf[p]= 0;
intcodeLen = p;
CStringAstrInput = pbuf;
//將pbuf的字符串進行astyle格式化
//
//
CStringAstrOutput;
_FormatCode(strInput,strOutput);
//輸出格式化後的代碼
//
if(!strOutput.IsEmpty())
{
printf(strOutput);
}
else
{
printf(strInput);//沒有輸入就把輸入輸出,當作沒有變化
}
delete[]pbuf;
}
return0;
}
(3)_FormatCode 這個函數大家去自己實現一個就行了,可以調用astyle等工具進行處理,最後通過printf打印出來,注意QtCreator的換行符是"\n"如果你指定"\r\n"則會產生多餘一個空行
astyle格式化工具的VS2008插件的代碼在me的資源中有