HELP: split up source file into segments automatically....

This is a discussion on HELP: split up source file into segments automatically.... within the Tech Board forums, part of the Community Boards category; This is not really language specific but more of a source code parsing. I have a large ASP source file ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    9

    HELP: split up source file into segments automatically....

    This is not really language specific but more of a source code parsing.

    I have a large ASP source file that I like to split up, resulting in separate ASP files, each containing one function and the filename named after that function.

    Does anyone know of a tool/script which does this?

    PS: I am too lazy to learn Perl/AWK/Python to do this, hence why I believe someone has already written a tool/script to do this. Possibly in C++/etc.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    C or C++ isn't really the right tool for this purpose.

    My other question is "why?". Splitting tiny functions into their own separate .C file isn't a particularly useful idea. If you have LARGE functions, yes, split the code into separate functions, and name them something appropriate.

    How large is your project?

    Even if you manage to quickly split your sources into a function per file, you still have the problem of manually inserting the right header includes into each file (unless of course you have one ginormous "include everything" mega-include at for ALL files). Likewise for any global variables that may be in the project.

    This is going to be much more of an issue, and it will be real tricky to teach some tool to do that - it would have to parse the entire file, with includes and all [and the right #defines to know which variant it may be]. Quite hard, I would say.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Do you have a couple of examples of such functions?
    Can you paste them and indicate where the split is supposed to take place?

    If they're reasonably well structured, then this should be just a few lines of perl, which won't take more than a few mins to lash together.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    Was hoping a C/C++ tool could split up a VBScript/ASP code file, for example:

    Function one(a, b, c)

    End Function

    Sub two(d, e, f)

    End Sub

    -------

    would create two new files, first one containing Function one, second file containing Sub two.

    I like to compare them using diff tools, because I got lots of drawing functions which follow a similar pattern.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aha - that was a part I didn't get - it's written in VB, rather than C/C++!

    Seems like you could just scan for "function" or "sub" at the beginning of a line, and then the corresponding end function/end sub.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    How about
    Code:
    #!/bin/perl -w
    use strict;
    my $fName;
    while ( <> ) {
        chomp;s/\r//;
        if ( /^(Function|Sub)\s+(\w+)/ ) {
            $fName = $2;
            open FH,">$fName.vb" or die "oops";
        }
        if ( /^(Function|Sub)\s+$fName/ .. /^End\s+(Function|Sub)/ ) {
            print FH "$_\n";
        }
    }
    
    $ cat foo.vb
    Function one(a, b, c)
    
    End Function
    
    Sub two(d, e, f)
    
    End Sub
    
    $ perl foo.pl foo.vb
    $ cat one.vb
    Function one(a, b, c)
    
    End Function
    $ cat two.vb
    Sub two(d, e, f)
    
    End Sub
    $
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 06-08-2009, 05:33 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Source file inclusion
    By sh3rpa in forum C++ Programming
    Replies: 7
    Last Post: 10-02-2007, 11:10 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21