User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Mar 2008
Posts: 11
Reputation: rayscosmic is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rayscosmic rayscosmic is offline Offline
Newbie Poster

Help add text in the c functions.plz help me

  #1  
Mar 13th, 2008
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
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: add text in the c functions.plz help me

  #2  
Mar 13th, 2008
You have to read .c file and write to some other file, while checking the requirements.
challenge the limits
Reply With Quote  
Join Date: Mar 2008
Posts: 11
Reputation: rayscosmic is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rayscosmic rayscosmic is offline Offline
Newbie Poster

Re: add text in the c functions.plz help me

  #3  
Mar 17th, 2008
i have done till there. i am reading .c files in the folder but i am not sure how to check if it a function or not becoz there is noo regular pattern for the functions in the .c file. Do u know any method or so to check it out

Thanks & Regards
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: add text in the c functions.plz help me

  #4  
Mar 17th, 2008
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.


.
Last edited by jephthah : Mar 17th, 2008 at 11:30 am.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: add text in the c functions.plz help me

  #5  
Mar 17th, 2008
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:

  1. void myExample_Function (int argument, char * value)
  2. {
  3. blah;
  4. blah;
  5. blah;
  6. }
Last edited by jephthah : Mar 17th, 2008 at 11:36 am.
Reply With Quote  
Join Date: Mar 2008
Posts: 11
Reputation: rayscosmic is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rayscosmic rayscosmic is offline Offline
Newbie Poster

Re: add text in the c functions.plz help me

  #6  
Mar 18th, 2008
Thanks for the help
I am doing the same but then i am not able to match for "{" for the function.
I am doing it as below.
if ($_ =~ /^\{/)
{
}
Some thing is wrong with it.Plz let me know
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: add text in the c functions.plz help me

  #7  
Mar 18th, 2008
it isn't "necessarily" always at the first spot.

but the regex should be correct.

post more code. use [code=c] tags, please
Reply With Quote  
Join Date: Mar 2008
Posts: 11
Reputation: rayscosmic is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rayscosmic rayscosmic is offline Offline
Newbie Poster

Re: add text in the c functions.plz help me

  #8  
Mar 25th, 2008
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

  1. do
  2. {
  3. # print "inside do while\n";
  4. if ($_ =~ /return/)
  5. {
  6. print OUT "$fotter";
  7. print "hey am inside return if\n";
  8. $foundReturn=1;
  9. $funInside=0;
  10. return;
  11. }
  12. if($_ =~ /^\{ || \{$/)
  13. {
  14. $openCount++;
  15. print "openCount1=", $openCount ,"\n";
  16. }#End of if($_ =~ /[^\{]/)
  17.  
  18. elsif($_ =~ /(^\}) || (\}$)/ )
  19. {
  20. $closeCount++;
  21. print "closeCount=", $closeCount,"\n";
  22.  
  23. }# End of else if($_ =~ /[^\}] | (\}$)/ )
  24.  
  25. else
  26. {
  27. #print "inside else \n";
  28. return;
  29. }#End of else
  30.  
  31. if($openCount=$closeCount)
  32.  
  33. {
  34. print "its the end of the function";
  35. }#End of if($openCount=$closeCount)
  36.  
  37. } #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.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: add text in the c functions.plz help me

  #9  
Mar 25th, 2008
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
  1. /^ ...regex... $/
for another thing, you can't put the LOGICAL OR inside a regex. you need to break it down into separate expressions like this
  1. if (($var =~ /^ ... $/) || ($var =~ /^ ... $/))
  2. {
  3. do stuff
  4. }
also, can you ever think of a function that will never match the regex
  1. /return/
perhaps a function like this:
  1. void myStupidFunction (void)
  2. {
  3. printf("hi world!\n");
  4. foo = bar;
  5. }
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:
  1. for (a=1; a<10; a++) { // only call 9 times!
  2. myFunction(a);
  3. }
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.
Last edited by jephthah : Mar 25th, 2008 at 8:05 pm.
Reply With Quote  
Join Date: Mar 2008
Posts: 11
Reputation: rayscosmic is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rayscosmic rayscosmic is offline Offline
Newbie Poster

Re: add text in the c functions.plz help me

  #10  
Mar 26th, 2008
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.


  1. if(($_ =~ /^\{/) || ($_ =~ /\{$/))
  2. {
  3. $openCount++;
  4. print "openCount1=", $openCount ,"\n";
  5. }
  6. elsif(($_ =~ /^\}/) || ($_=~ /\}$/ ))
  7.  
  8. {
  9. $closeCount++;
  10. print "closeCount=", $closeCount,"\n";
  11.  
  12. }# 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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Perl Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 4:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC