DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Perl (http://www.daniweb.com/forums/forum112.html)
-   -   add text in the c functions.plz help me (http://www.daniweb.com/forums/thread113712.html)

rayscosmic Mar 13th, 2008 7:55 am
add text in the c functions.plz help me
 
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
}

katharnakh Mar 13th, 2008 11:03 pm
Re: add text in the c functions.plz help me
 
You have to read .c file and write to some other file, while checking the requirements.

rayscosmic Mar 17th, 2008 2:18 am
Re: add text in the c functions.plz help me
 
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

jephthah Mar 17th, 2008 11:19 am
Re: add text in the c functions.plz help me
 
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.


.

jephthah Mar 17th, 2008 11:29 am
Re: add text in the c functions.plz help me
 
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:

void myExample_Function (int argument, char * value)
{
    blah;
    blah;
    blah;
}

rayscosmic Mar 18th, 2008 6:41 am
Re: add text in the c functions.plz help me
 
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

jephthah Mar 18th, 2008 9:14 am
Re: add text in the c functions.plz help me
 
it isn't "necessarily" always at the first spot.

but the regex should be correct.

post more code. use [code=c] tags, please

rayscosmic Mar 25th, 2008 8:02 am
Re: add text in the c functions.plz help me
 
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

 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.

jephthah Mar 25th, 2008 7:35 pm
Re: add text in the c functions.plz help me
 
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
/^  ...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
if (($var =~ /^  ...  $/) || ($var =~ /^ ... $/))
{
    do stuff
}
also, can you ever think of a function that will never match the regex
/return/
perhaps a function like this:
void myStupidFunction (void) 

    printf("hi world!\n");
    foo = bar;
}
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:
for (a=1; a<10; a++) {     // only call 9 times!
    myFunction(a);
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.

rayscosmic Mar 26th, 2008 1:35 am
Re: add text in the c functions.plz help me
 
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(($_ =~ /^\{/) || ($_ =~ /\{$/))
    {
    $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.


All times are GMT -4. The time now is 3:51 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC