![]() |
| ||
| 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 } |
| ||
| 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. |
| ||
| 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 |
| ||
| 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. . |
| ||
| 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) |
| ||
| 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 |
| ||
| 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 |
| ||
| 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 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. |
| ||
| 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 =~ /^ ... $/))also, can you ever think of a function that will never match the regex /return/perhaps a function like this: void myStupidFunction (void)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!
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. |
| ||
| 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(($_ =~ /^\{/) || ($_ =~ /\{$/))
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