Creating new files based on pattern matching with regex, sprintf() and fopen()

215 views Asked by At

I have a large text file containing only function definitions in a pseudo programming language. A function definition looks like this (They can be as complex as they want though (deep bracketing etc.).):

foo1 = FUNCTION() {

    body
}

I use a C-program with regex.h and grouped pattern matching. fgets() reads in new lines in a while-loop until the end of the file. Every time regexec() matches foo1 = FUNCTION in a line fgets() read in I calculate foo1 from regmatch_t, pass it to sprintf() and open a new file with fopen(foo1, "wx"). All the following lines will then be written into the newly opened file until regexec() matches a new foon = FUNCTION() {} and a new file is opened via sprintf() and fopen(). This is the part of the solution I would like to keep!

But the author of the file was sloppy in some parts and instead of putting the name of the function and it's definition-keyword FUNCTION on the same line, he sometimes wrote:

foo2 = 
FUNCTION() {
    body
}

Currently I treat such functions as unnamed and assign the file they get written into a name of the form m_func where n is a running index. Now, the file previous to m_func (m-1_func) will also contain as the last line the actual name of m_func. Given that m = 50 at least, copying and pasting seems a nuisance. So here is the question: Is there a way to achieve the same I achieved with functions defined on one line such that I can match the definition, then somehow get hold of the name one or two lines before and create a new file based upon the name of the function via fopen() and sprintf()?

0

There are 0 answers