splitting functions into files

Discussion in 'C Programming' started by moumou, Jan 18, 2005.

  1. moumou

    moumou Guest

    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
    moumou, Jan 18, 2005
    #1
    1. Advertising

  2. moumou

    Mike Wahler Guest

    "moumou" <-strasbg.fr> wrote in message
    news:csiseq$3t6$-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
    Mike Wahler, Jan 18, 2005
    #2
    1. Advertising

  3. moumou <-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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 18, 2005
    #3
  4. 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.
    E. Robert Tisdale, Jan 18, 2005
    #4
  5. "E. Robert Tisdale" <> 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) <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.
    Keith Thompson, Jan 19, 2005
    #5
  6. moumou

    Ira Baxter Guest

    "Keith Thompson" <> wrote in message
    news:...
    > moumou <-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/Products/DMS/DMSToolkit.html.


    --
    Ira D. Baxter, Ph.D., CTO 512-250-1018
    Semantic Designs, Inc. www.semdesigns.com
    Ira Baxter, Jan 22, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Ericson
    Replies:
    0
    Views:
    424
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    440
  3. John Dibling
    Replies:
    0
    Views:
    413
    John Dibling
    Jul 19, 2003
  4. just.an.imbecile
    Replies:
    0
    Views:
    637
    just.an.imbecile
    Jun 6, 2006
  5. Replies:
    5
    Views:
    303
    Bruno Desthuilliers
    Mar 8, 2007
Loading...

Share This Page