•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 374,179 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,420 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser:
Views: 1566 | Replies: 23 | Solved
![]() |
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
Hello i am new in perl scripting.I have to write a script to check for .c file and then go in to the ,c file and find all function and insert line1 after "{" and line2 before "}" or before return( if return is present). Can any1 plz help me in it.
Ex: Function(.........)
{
insert line1;
..................
...................
insert line2;
# if return is present add before it
}
Ex: Function(.........)
{
insert line1;
..................
...................
insert line2;
# if return is present add before it
}
easiest way is to
(1) do a dir (ls) listing of directory contents, parse each one for a .c extention. start with the first .c file...
(2) open the .c file for READ, to a file handle.
(3) open a new .c file to WRITE to a different file handle and make it's filename similar to the original one being read from in step 2 (like using same name, but adding a "-mod" after the filename)
(4) read each line of original file.
(5) search for each function, and keep strict count of number of opening brackets, and number of closing brackets ( '{' and '}' )
(6) write each of the the lines read from the original file to the new file, exactly as read.
(7) write an additional "extra line 1" only after the first opening bracket '{', and write teh additional "extra line 2" right before the last closing bracket '}'
(8) repeat steps 5 - 7 until all functions are completed and end of file is reached
(9) close both the read file and write file, and then move to the next .c file in your directory.
(10) repeat steps #2 - #10 for the next .c file, until all .c files are completed.
.
(1) do a dir (ls) listing of directory contents, parse each one for a .c extention. start with the first .c file...
(2) open the .c file for READ, to a file handle.
(3) open a new .c file to WRITE to a different file handle and make it's filename similar to the original one being read from in step 2 (like using same name, but adding a "-mod" after the filename)
(4) read each line of original file.
(5) search for each function, and keep strict count of number of opening brackets, and number of closing brackets ( '{' and '}' )
(6) write each of the the lines read from the original file to the new file, exactly as read.
(7) write an additional "extra line 1" only after the first opening bracket '{', and write teh additional "extra line 2" right before the last closing bracket '}'
(8) repeat steps 5 - 7 until all functions are completed and end of file is reached
(9) close both the read file and write file, and then move to the next .c file in your directory.
(10) repeat steps #2 - #10 for the next .c file, until all .c files are completed.
.
Last edited by jephthah : Mar 17th, 2008 at 11:30 am.
as for checking whether or not it is a function... each subroutine in C starts with one or more return type:
int, long, short, double, float, void, char, unsigned ...
then it has a single word for the function name (simply: \w+ )
then it will have an open parenthesis '(' followed eventually by a closed parenthesis ')' ....
and then there will be the opening bracket '{'
thats all you have to parse on to find that it is a function... of course be sure to ignore function "prototypes"... they will be distinguished by a semicolon ';' after the closing parenthesis
the problem for you will be that a function declaration can spread across multiple lines... very often that first opening bracket is on a line by itself. like so:
int, long, short, double, float, void, char, unsigned ...
then it has a single word for the function name (simply: \w+ )
then it will have an open parenthesis '(' followed eventually by a closed parenthesis ')' ....
and then there will be the opening bracket '{'
thats all you have to parse on to find that it is a function... of course be sure to ignore function "prototypes"... they will be distinguished by a semicolon ';' after the closing parenthesis
the problem for you will be that a function declaration can spread across multiple lines... very often that first opening bracket is on a line by itself. like so:
c Syntax (Toggle Plain Text)
void myExample_Function (int argument, char * value) { blah; blah; blah; }
Last edited by jephthah : Mar 17th, 2008 at 11:36 am.
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
hey i am trying to put line2 at the end of the function .But am not able to take up the " {" and "}" count. Plz let me know how to check for that
Sorry i can not put entair code becoz of security issues.I have tried till here.
am not able to match for "{". can u plz correct where i have gone wrong.
perl Syntax (Toggle Plain Text)
do { # print "inside do while\n"; if ($_ =~ /return/) { print OUT "$fotter"; print "hey am inside return if\n"; $foundReturn=1; $funInside=0; return; } if($_ =~ /^\{ || \{$/) { $openCount++; print "openCount1=", $openCount ,"\n"; }#End of if($_ =~ /[^\{]/) elsif($_ =~ /(^\}) || (\}$)/ ) { $closeCount++; print "closeCount=", $closeCount,"\n"; }# End of else if($_ =~ /[^\}] | (\}$)/ ) else { #print "inside else \n"; return; }#End of else if($openCount=$closeCount) { print "its the end of the function"; }#End of if($openCount=$closeCount) } #End of do while ($openCount=$closeCount)
Sorry i can not put entair code becoz of security issues.I have tried till here.
am not able to match for "{". can u plz correct where i have gone wrong.
Last edited by WolfPack : Mar 25th, 2008 at 10:23 am. Reason: Added code tags. Use them when you post code.
for one thing, you cant put the begin/end of line operators (^ and $) inside parentheses. they must be immediately adjacent to the match operator like this
for another thing, you can't put the LOGICAL OR inside a regex. you need to break it down into separate expressions like this
also, can you ever think of a function that will never match the regex
perhaps a function like this:
finally, you can't guarantee that the open or close braces are ALWAYS going to be either at the beginning or end of a line. that would then fail to find any indented brace that had spurious whitespace afterwards, much less a brace that was preceded and followed by more code. like this:
your method (as I interpret it from the apparent intent of your code) will fail to find the opening brace, and only count the closing brace. thereby getting your counting algorithm hopelessly lost.
perl Syntax (Toggle Plain Text)
/^ ...regex... $/
perl Syntax (Toggle Plain Text)
if (($var =~ /^ ... $/) || ($var =~ /^ ... $/)) { do stuff }
perl Syntax (Toggle Plain Text)
/return/
c Syntax (Toggle Plain Text)
void myStupidFunction (void) { printf("hi world!\n"); foo = bar; }
c Syntax (Toggle Plain Text)
for (a=1; a<10; a++) { // only call 9 times! myFunction(a); }
Last edited by jephthah : Mar 25th, 2008 at 8:05 pm.
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
Thank you very much for the suggestion. I am taking the brace count to check the end of the function for functions which does not have return value. I have tried with the below code but elsif(($_ =~ /^\}/) || ($_=~ /\}$/ )) part is going to infinate loop and is not giving the count.
If u have any other algorithm to check for the end of the function plz let me know.I am stuck up with this from 3 days now and dont know what to do next.
perl Syntax (Toggle Plain Text)
if(($_ =~ /^\{/) || ($_ =~ /\{$/)) { $openCount++; print "openCount1=", $openCount ,"\n"; } elsif(($_ =~ /^\}/) || ($_=~ /\}$/ )) { $closeCount++; print "closeCount=", $closeCount,"\n"; }# End of else if($_ =~ /[^\}] | (\}$)/ )
If u have any other algorithm to check for the end of the function plz let me know.I am stuck up with this from 3 days now and dont know what to do next.
Last edited by WolfPack : Mar 26th, 2008 at 8:36 am. Reason: Added code tags. USe them when you post code.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Perl Marketplace
Similar Threads
Other Threads in the Perl Forum
- Previous Thread: getting 401 Unauthorized from an xml feed...
- Next Thread: count characters in a string


Linear Mode