0

我想像这样使用 eclipse-Indigo 生成块注释。我是 C++ 程序员。

/**
 * 
 * @param bar
 * @return
 */
int foo(int bar);

我怎么能这样。

4

1 回答 1

0

如果您的输入几乎是静态的,您可以编写一个可以工作的简化词法分析器,需要简单的字符串处理。string 具有许多不错的编辑功能,其中包含 .substr() 和 .find() 。你所要做的就是弄清楚perens在哪里。您知道您可以选择将其作为字符串流处理,这使得 FAR 更容易(不要忘记使用 std::skipws 来跳过空格。

http://www.cplusplus.com/reference/string/string/substr/

http://www.cplusplus.com/reference/string/string/find/

#include <vector>
#include <string>

typedef STRUCT arg_s {
string sVarArgDataType, sVarArg;
} arg_s ARG;
ARG a;
vector<ARG> va;
char line[65000];

filein.getline(line, 65000);
line[65000-1]='\0'; //force null termination if it hasn't happened
get line and store in string sline0
size_t firstSpacePos=sline.find(' ');
size_t nextSpacePos = sline.find(' ',firstSpacePos+1);
size_t prevCommaPos = string::npos;
size_t nextCommaPos = sline.find(',');
size_t openPerenPos=sline.find('(');
size_t closePerenPos=sline.find(");");
string sReturnDataType, sFuncName;
if (
    string::npos==firstSpacePos||
    string::npos==semicolonPos||
    string::npos==openPerenPos||
    string::npos==closePerenPos) {
    return false; //failure
}
while (string::npos != nextSpacePos) {
    if (string::npos != nextCommaPos) {
        //found another comma, a next argument. use next comma as a string terminator and prevCommaPos as an arg beginning.
        //assume all keywords are globs of text
        a.sVarArgDataType=sline.substr(prevCommaPos+1,nextSpacePos-(prevCommaPos+1));
        a.sVarArg=sline.substr(nextSpacePos+1,nextCommaPos-(nextSpacePos+1));
    } else {
        //didn't find another comma. use ) as a string terminator and prevCommaPos as an arg beginning.
        //assume all keywords are globs of text
        a.sVarArgDataType=sline.substr(prevCommaPos+1,nextSpacePos-(prevCommaPos+1));
        a.sVarArg=sline.substr(nextSpacePos+1,closePerenPos-(nextSpacePos+1));
    }
    va.push_back(a); //add structure to list
    //move indices to next argument
    nextCommaPos = sline.find(',', secondSpacePos+1);
    nextSpacePos = sline.find(' ', secondSpacePos+1);
}
int i;

fileout<<"/**
 * 
";
for (i=0; i < va.size(); i++) {
    fileout<<" * @param "<<va[i].sVarArg;
}
fileout<<"
 * @return
 */
"<<sReturnDataType<<" "<<sFuncName<<'(';
for (i=0; i < va.size(); i++) {
    fileout<<va[i].sArgDataType<<" "<<va[i].sVarArg;
    if (i != va.size()-1) {
        fileout<<", "; //don;t show a comma-space for the last item
    }
}

fileout<<");"<<std::endl;

这将处理任意数量的参数,除了 ... 变量参数类型。但是您可以为此输入自己的检测代码以及在 ... 和 2 关键字参数类型之间切换的 if 语句。在这里,我的结构中只支持 2 个关键字。您可以通过使用 while 搜索下一个之前的所有空格来支持更多,逗号或 ) 在 while 循环内将可变数量的字符串添加到vector<string>要替换的结构内 - 不,只需制作一个vector<vector<string> >. 或者,只有一个向量,然后va.clear()在每个函数完成后执行一个。

我刚注意到eclipse标签。我对日食了解不多。我什至无法让它工作。一些程序。

于 2012-04-30T08:37:19.407 回答