splitting functions into files

M

moumou

Hello world !

This may be a naive question, so I apologize in advance !

I'm wonderting if it is possible to find a program that takes a C source
code file and split it into multiple files, one per each function
included in the source code ?

Many thanks in advance !

Luc Moulinier, Ph.D
IGBMC, Illkirch
France
 
M

Mike Wahler

moumou said:
Hello world !

This may be a naive question, so I apologize in advance !

I'm wonderting if it is possible to find a program that takes a C source
code file and split it into multiple files, one per each function
included in the source code ?

If such a program exists, I suspect it's possible to find it.
I'd start with www.google.com

If I needed but couldn't find one, I try to write one. Of
course if you have control over the source you're going to parse,
this can make things much easier.

But are you sure you want to do this? I think it
would make much more sense to do this by hand, because a
program's design can often indicate that a set of functions
should stay together in the same file. Also, depending upon
the program, doing this could cause it to fail to build.

IOW if you find a source file too large, I recommend breaking
it up logically, not 'mechanically'. Computers are great
at automating menial work, but they can't do your thinking
for you.

-Mike
 
K

Keith Thompson

moumou said:
I'm wonderting if it is possible to find a program that takes a C
source code file and split it into multiple files, one per each
function included in the source code ?

I don't know of one (unless you count text editors, but I presume you
want a program that does this automatically).

It probably wouldn't be too difficult to write a program that does
this naively. The job is simpler if you make some assumptions about
how the code is formatted, particularly about the placement of curly
braces. For full generality, the program would have to incorporate a
C parser.

There are programs (such as ctags) that generate lists of symbols
declared in a source file. You might be able to use something like
that as a basis for what you want.

But if the file contains declarations other than function definitions,
each split file won't be able to see those declarations. You might be
able to automatically generate a header containing any global
declarations (not definitions!), but that gets tricky.
 
E

E. Robert Tisdale

moumou said:
I'm wonderting if it is possible to find a program
that takes a C source code file and split it into multiple files,
one per each function included in the source code?

Most [UNIX] programmers use csplit to do this.
Just type

man csplit

or

info csplit

at your UNIX [Linux] shell prompt.
 
K

Keith Thompson

E. Robert Tisdale said:
moumou said:
I'm wonderting if it is possible to find a program
that takes a C source code file and split it into multiple files,
one per each function included in the source code?

Most [UNIX] programmers use csplit to do this.
Just type

man csplit

or

info csplit

at your UNIX [Linux] shell prompt.

The 'c' in csplit stands for "context", not C. To use it to split a C
source file into functions, you'd probably have to construct regular
expressions matching the beginnings and ends of functions. I don't
believe that's possible.

If you have a convention of marking function boundaries in your code
(say, by a standard comment), csplit might be helpful -- but then
you've done most of the work already.
 
I

Ira Baxter

Keith Thompson said:
It probably wouldn't be too difficult to write a program that does
this naively. The job is simpler if you make some assumptions about
how the code is formatted, particularly about the placement of curly
braces. For full generality, the program would have to incorporate a
C parser.

But if the file contains declarations other than function definitions,
each split file won't be able to see those declarations. You might be
able to automatically generate a header containing any global
declarations (not definitions!), but that gets tricky.

The DMS Software Reengineering Toolkit is designed to help
one build custom tools like this. It does have a full C parser
(and just as important, full name and type resolution) so
that one could identify, for each "split" function, the set
of definitions on which it depends. The parser also
builds trees, and can reconstruct the source from
those trees (comments included).
Using that information, one could write a tool
that wrote individual functions to individual files,
and constructed appropriate externs, .h files
and #includes to ensure the program still compiled
and ran.

See http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top