Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > splitting functions into files

Reply
Thread Tools

splitting functions into files

 
 
moumou
Guest
Posts: n/a
 
      01-18-2005
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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-18-2005
"moumou" <(E-Mail Removed)-strasbg.fr> wrote in message
news:csiseq$3t6$(E-Mail Removed)-strasbg.fr...
> 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


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-18-2005
moumou <(E-Mail Removed)-strasbg.fr> writes:
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-18-2005
moumou wrote:

> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-19-2005
"E. Robert Tisdale" <(E-Mail Removed)> writes:
> moumou wrote:
>
>> 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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Ira Baxter
Guest
Posts: n/a
 
      01-22-2005
"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> moumou <(E-Mail Removed)-strasbg.fr> writes:
> > 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 ?

>
> 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/Produ...MSToolkit.html.


--
Ira D. Baxter, Ph.D., CTO 512-250-1018
Semantic Designs, Inc. www.semdesigns.com


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
splitting common functions into a sepperate module jonkersbart@gmail.com Python 5 03-08-2007 10:55 AM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM
Re: Splitting big .mp3 file into smaller files? °Mike° Computer Support 0 06-27-2003 06:00 PM



Advertisments