[Anjuta-list] Re: Symbol Name generation from file for autocomplete/calltip



Here is a faster version which does not use too many UNIX commands. The temp. 
file name is hardcoded and not deleted after the call - We'll probably need 
to use mkstemp or something. This takes around .15 secs (redirecting the 
output to /dev/null) per file.

int getSymbolList(const char *fileName, const char *cFlags)
{
	static char *command = "cpp -P %s -dD %s >%s; echo %s | ctags --filter 
--c++-types=+px-fm -x";
	static char *tmpFile = "/tmp/tmp.c"; /* Need to use mkstemp or tmpnam() */
	char buf[BUFSIZ];
	char symbol[255];
	char type[20];
	char file[255];
	char line[10];
	char def[BUFSIZ];
	FILE *symbolList;
	sprintf(buf, command, cFlags, fileName, tmpFile, tmpFile);
	_(printf("%s\n", buf);)
	if (NULL == (symbolList = popen(buf, "r")))
		exit(1);
	while (4 == fscanf(symbolList, "%s %s %s %s", symbol, type, file, line))
	{
		fgets(def, BUFSIZ, symbolList);
		def[strlen(def) -1] = '\0';
		printf("%s: %s(%s)\n", symbol, type, def);
	}
	fclose(symbolList);
	return 0;
}

Hope this is useful.
- Biswa.

On Tuesday 11 September 2001 05:06 pm, Biswapesh Chattopadhyay wrote:
> Hi all
>
> Working on a suggestion posted on the list, here is a function which takes
> any C/C++ file name as input and extracts for each symbol the symbol name,
> symbol type, file name, line number and the symbol definition. Tested on
> Linux but should work on all *nix AFAICS. Takes two arguments, the name of
> the file to analyze and the C and C++ flags to use with the pre-processor
> (basically the same as the compiler flags). You need ctags installed.
>
> int analyzeSymbols(const char *fileName, const char *cFlags)
> {
> 	char buf[BUFSIZ];
> 	char symbol[255];
> 	char type[20];
> 	char file[255];
> 	char line[10];
> 	char def[BUFSIZ];
> 	FILE *symbolList;
> 	sprintf(buf, "cpp %s -MG -MM -H %s 2>&1 | grep \"^\\.\\.* \" | awk '{print
> $2}'  | sort -u | xargs ctags --filter -x --c++-types=+px", cFlags,
> fileName); printf("%s\n", buf);
> 	if (NULL == (symbolList = popen(buf, "r")))
> 		exit(1);
> 	while (4 == fscanf(symbolList, "%s %s %s %s", symbol, type, file, line))
> 	{
> 		fgets(def, BUFSIZ, symbolList);
> 		def[strlen(def) -1] = '\0';
> 		printf("%s (%s at %s %s): %s\n", symbol, type, file, line, def);
> 	}
> 	fclose(symbolList);
> 	return 0;
> }
>
> CAVEAT: This is currently a bit slow (~ .6 secs per file - doesn't seem to
> vary too much with size), probably because it uses a lot of piped UNIX
> utils, namely, grep, sort, xargs and awk. Probably can be made much faster
> if written in plain C.
>
> Hope this helps.
> - Biswa.

-- 
- Biswapesh.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]