By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,198 Members | 963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,198 IT Pros & Developers. It's quick & easy.

Extract a function from C code?

P: n/a
I have a C/C++ file that I simply want to 'extract' a function from.
Something like: extract <function name> <c or cpp file>

I want it to return from the beginning of the function, to the end.

I tried cscope, and that will find the function, but it won't tell me
how many lines it is or extract it for me.

Any ideas on what I could use?

It would also be awesome if I could choose to extract struct
definitions, multi-line macro defs, etc.

se*******@gmail.com
Nov 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Robert Schultz wrote:
I have a C/C++ file that I simply want to 'extract' a function from.
Something like: extract <function name> <c or cpp file>

I want it to return from the beginning of the function, to the end.

I tried cscope, and that will find the function, but it won't tell me
how many lines it is or extract it for me.

Any ideas on what I could use?

It would also be awesome if I could choose to extract struct
definitions, multi-line macro defs, etc.

se*******@gmail.com


If you're on a *nix system, look into "csplit".

We're OT now, so I won't say any more.
Nov 14 '05 #2

P: n/a
Robert Schultz wrote:
I have a C/C++ file that I simply want to 'extract' a function from.
Something like: extract <function name> <c or cpp file>

I want it to return from the beginning of the function, to the end.

I tried cscope, and that will find the function, but it won't tell me
how many lines it is or extract it for me.

Any ideas on what I could use?

It would also be awesome if I could choose to extract struct
definitions, multi-line macro defs, etc.


If by "extract a function" you mean "move the
function's source lines out of one file and into
another," an editor is your best bet. Be warned
that the source lines of a function may not be
useful when separated from the #include's and other
declarations of their original file.

As for "extracting" struct definitions and macros,
once again an editor seems the best bet. What are
you trying to do? Break up an everything-thrown-
together source file into manageable pieces? If so,
purely mechanical tools -- tools that operate without
understanding of the relationships between the various
pieces -- won't do a very good job.

--
Er*********@sun.com

Nov 14 '05 #3

P: n/a
Robert Schultz wrote:

I have a C/C++ file that I simply want to 'extract' a function from.
Something like: extract <function name> <c or cpp file>

I want it to return from the beginning of the function, to the end.

I tried cscope, and that will find the function, but it won't tell me
how many lines it is or extract it for me.

Any ideas on what I could use?

It would also be awesome if I could choose to extract struct
definitions, multi-line macro defs, etc.

se*******@gmail.com


Use an editor. But it won't work on a C/C++ file, because no such
language exists. There are C source files, and there are C++
source files, and there are AWK source files, but there are NO
C/C++ source files, alas.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #4

P: n/a

On Thu, 28 Oct 2004, Robert Schultz wrote:

I have a C/C++ file that I simply want to 'extract' a function from.
Something like: extract <function name> <c or cpp file>

I want it to return from the beginning of the function, to the end.

I tried cscope, and that will find the function, but it won't tell me
how many lines it is or extract it for me.
I don't know what "cscope" does, but if it will tell you where the
function begins, then you can just take everything from that line,
to the next '{' character, to the matching '}' character; that will
give you all the lines you want, barring evil preprocessor tricks
such as

#define RBRACE }
void foo {
RBRACE
It would also be awesome if I could choose to extract struct
definitions, multi-line macro defs, etc.


Sounds like you're looking for a C parser. ;-) In the worst case,
there are lex/yacc grammars for C90 and maybe C99 available; you can
learn lex/yacc and use those grammars to extract the stuff you want.

HTH,
-Arthur
Nov 14 '05 #5

P: n/a
se*******@gmail.com (Robert Schultz) wrote in message news:<bb***********************@posting.google.com >...
I have a C/C++ file that I simply want to 'extract' a function from.
Something like: extract <function name> <c or cpp file>

I want it to return from the beginning of the function, to the end.

I tried cscope, and that will find the function, but it won't tell me
how many lines it is or extract it for me.

Any ideas on what I could use? You could use CodeWorker, a parsing tool and a source code generator.

Copy the following script into a file called "function_extractor.cwp":
function_extractor ::=
=> if _ARGS.empty() error("function name expected on the command
line");
#ignore(C++)
->[
// from here, copy what is parsed to the output file
#implicitCopy
// name of the function
#readIdentifier:sFunctionName
#nextStep
// function we are looking for?
#check(sFunctionName == _ARGS#front)
// in C++, the function could be template
['<' ignore_template_clause '>']?
'('
// read parameters
ignore_parenthesis
')'
// in C++, ignore 'throw' or 'const' ...
[#readIdentifier | '(' ignore_parenthesis ')']*
// read the block of instructions
'{'
block
'}'
]
;

ignore_template_clause ::= [#readCString | #readCChar | '<'
template_clause '>' | ~'>']*;
ignore_parenthesis ::= [#readCString | #readCChar | '('
ignore_parenthesis ')' | ~')']*;
block ::= [#readCString | #readCChar | '{' block '}' | ~'}']*;

Then, type the command:
codeworker -translate function_extractor.cwp <your-C-file>
<your-output-file> -args <function-name>

It doesn't copy the return type of the function. If you need it, and
if we can suppose that the return type occupies the space between the
beginning of the line and the name of the function, it doesn't require
to parse the type seriously, so I can improve the script very easily.
It would also be awesome if I could choose to extract struct
definitions, multi-line macro defs, etc.


It is simpler to extract a macro definition or a 'struct' declaration.
If you don't want to write them by yourself, because you don't want to
invest time on CodeWorker, I can write them for you.
Nov 14 '05 #6

P: n/a

In article <Pi***********************************@unix45.andr ew.cmu.edu>, "Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:

I don't know what "cscope" does, but if it will tell you where the
function begins, then you can just take everything from that line,
to the next '{' character, to the matching '}' character;
Provided they're not in comments, or string literals, or character
literals, or #define directives, or sections of code that are
conditionally-compiled out...
that will
give you all the lines you want, barring evil preprocessor tricks
such as

#define RBRACE }
void foo {
RBRACE


Yeah, or those, too.
It would also be awesome if I could choose to extract struct
definitions, multi-line macro defs, etc.


Sounds like you're looking for a C parser.


I think so. And it needs to handle preprocessing (and, to be fully
correct, translations phases 1 (mapping to source character set and
trigraph translation) and 2 (source line splicing), first), with
the same environmental conditions (eg predefined macros) in effect
as when the program is translated by the actual implementation.

Sure, in most cases, just matching {} characters works fine. Whether
it sufficies in this case is something the OP will have to judge. It
isn't the sort of thing I'd be comfortable automating; I'd do it by
hand, in vim, using brace-matching as a first approximation but
checking to make sure I had selected the lines I really wanted.

--
Michael Wojcik mi************@microfocus.com

You have Sun saying, "Who needs Linux? We have Solaris." You have
Microsoft saying, "Who needs Linux? We have Windows 2000." Then you
have IBM saying, "I think we all need Linux." Only the greatest
sinners know how to really repent. -- John Patrick, IBM VP
Nov 14 '05 #7

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi***********************************@unix45 .andrew.cmu.edu>...
On Thu, 28 Oct 2004, Robert Schultz wrote:

I have a C/C++ file that I simply want to 'extract' a function from.
Something like: extract <function name> <c or cpp file>

I want it to return from the beginning of the function, to the end.

I tried cscope, and that will find the function, but it won't tell me
how many lines it is or extract it for me.


I don't know what "cscope" does, but if it will tell you where the
function begins, then you can just take everything from that line,
to the next '{' character, to the matching '}' character; that will
give you all the lines you want, barring evil preprocessor tricks
such as

#define RBRACE }
void foo {
RBRACE
It would also be awesome if I could choose to extract struct
definitions, multi-line macro defs, etc.


Sounds like you're looking for a C parser. ;-) In the worst case,
there are lex/yacc grammars for C90 and maybe C99 available; you can
learn lex/yacc and use those grammars to extract the stuff you want.

HTH,
-Arthur


Yeah, something like this basically screams for a compiler front-end
to recognize individual translation units, struct and union
definitions, typedefs, etc. (at least to do it right).
Nov 14 '05 #8

P: n/a
se*******@gmail.com (Robert Schultz) wrote in message news:<bb************************@posting.google.co m>...
Well see, I've written some C functions of my own over the past 10
years.
In numerous projects, on numerous platforms.
The functions have evolved and more have been added, features have
been added, bugs have been fixed.
But silly me, I didn't do all my updates to a single file.

So now I have about 300 files, all named Utility.c containing more or
less the same functions, but each in a different degree of 'being
right'.

I wanted to avoid going through each file by hand, looking at each
function by hand.
So I was hoping to be able to automate a process so it would extract
function XYZ from every one of those files.
Then I would use diff to compare the extracted functions to see what
sort of changes are where.
I can't use diff on the original 300 because the files as a whole vary
file to much line by line (functions arranged in different orders).

So now you see why I wanted an automated method because a little
automation could reduce the amount of manual labor required by a large
large amount.

Any ideas?


It seems not so hard :
* first, to establish the name of all functions automatically,
considering the 300 'Utility.c' files,
* second, to suppress the function names, which aren't interesting
for you, by hand of course,
* third, to extract the implementations function by function, from
the 300 files:
* to retain all different implementations,
* to put them in different output files,
* to make a diff on each of them (good luck!),

These tasks are easy to implement with CodeWorker (see the extraction
further, for instance). If you are interested, I can help you.
Nov 14 '05 #9

P: n/a
co********@free.fr (Cedric LEMAIRE) wrote in message news:<db**************************@posting.google. com>...
se*******@gmail.com (Robert Schultz) wrote in message news:<bb***********************@posting.google.com >...
I have a C/C++ file that I simply want to 'extract' a function from.
Something like: extract <function name> <c or cpp file>

I want it to return from the beginning of the function, to the end.

I tried cscope, and that will find the function, but it won't tell me
how many lines it is or extract it for me.

Any ideas on what I could use?

You could use CodeWorker, a parsing tool and a source code generator.

Copy the following script into a file called "function_extractor.cwp":
[code]

Then, type the command:
codeworker -translate function_extractor.cwp <your-C-file>
<your-output-file> -args <function-name>

It doesn't copy the return type of the function. If you need it, and
if we can suppose that the return type occupies the space between the
beginning of the line and the name of the function, it doesn't require
to parse the type seriously, so I can improve the script very easily.
It would also be awesome if I could choose to extract struct
definitions, multi-line macro defs, etc.


It is simpler to extract a macro definition or a 'struct' declaration.
If you don't want to write them by yourself, because you don't want to
invest time on CodeWorker, I can write them for you.


AWESOME!!!! THANK YOU SO MUCH!
This is great.
codeworker is EXACTLY what I was looking for!
Thanks again!
Nov 14 '05 #10

P: n/a
se*******@gmail.com (Robert Schultz) wrote in message news:<bb************************@posting.google.co m>...
co********@free.fr (Cedric LEMAIRE) wrote in message news:<db**************************@posting.google. com>...
It would also be awesome if I could choose to extract struct
definitions, multi-line macro defs, etc.


It is simpler to extract a macro definition or a 'struct' declaration.
If you don't want to write them by yourself, because you don't want to
invest time on CodeWorker, I can write them for you.


AWESOME!!!! THANK YOU SO MUCH!
This is great.
codeworker is EXACTLY what I was looking for!
Thanks again!


You are welcome.

If you need some help to implement the extractor for 'struct' and
'#define', do not hesitate to contact me on my email.
Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.