0

i want to generate block comment using eclipse-Indigo like this. I'm C++ programmer.

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

how can i do like this.

user881703
  • 1,111
  • 3
  • 19
  • 38
  • how do you mean?generateing C++ from a C++ program? doesn't this require writing a c++ parser and lexer, essentially a compiler? the syntax of functions varies widely. there are freind functions, they can be members of a class so they can have classname::, – Jim Michaels Apr 30 '12 at 07:17
  • they can be part of a namespace, so they could be std:: or whatever namespace, they can optionally return const, return pointers, return void, return void pointers, return function pointers (bleah, what a thought), arguments can have references or not, have const or not and all the previous features I mentioned, etc. IF you know the exact format of the functions you will be getting, then good for you, it's relatively easy to write a lexer. – Jim Michaels Apr 30 '12 at 07:21
  • sorry for mistake now i think you can understand what i mean. – user881703 Apr 30 '12 at 07:22
  • Can you check below similar post: http://stackoverflow.com/questions/4629671/jautodoc-like-plugin-for-c – Seshagiri Apr 30 '12 at 07:23
  • @Seshagiri thanks your feed back i were try with Doxygen but still can't get block comment form my C++ method – user881703 Apr 30 '12 at 07:48
  • Why do you want to do this? Your **code** tells you what the parameters and return values are. Write [Clean Code](http://tinyurl.com/CleanCodeBook), not comments. – johnsyweb May 02 '12 at 03:45

1 Answers1

0

IF your input is pretty much static, you can write a simplified lexer that will work, requires simple string mungeing. string has lots of nice editing capabilities in it with .substr() and .find() in it. all you have to do is figure out where the perens are. you know you can optionally process this as a stringstream, which makes this FAR easier (don't forget to use std::skipws to skip whitespace.

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;

this will handle any number of arguments EXCEPT ... the variable argument type. but you can put in your own detection code for that and the if statement that switches out between ... and the 2-keyword argument types. here I am only supporting 2 keywords in my struct. you can support more by using a while to search for all the spaces before the next , comma or ) right peren in inside the while loop add your variable number of strings to a vector<string> inside the struct you are going to replace - nah, just make a vector<vector<string> >. or, just one vector and do a va.clear() after every function is done.

I just noticed the eclipse tag. I don't know much about eclipse. I can't even get it to work. some program.

Jim Michaels
  • 669
  • 5
  • 9