A friend of mine wanted a script to comment parts of code in a source file. We were chatting & i gave him this solution:
me: perl --i.bak -pe 's/^(.*)$/#$1/gi if ($. >=start_line_number && $. <= end_line_number)' filename
Try above command in terminal. Where start_line_number is the line number where u shud start commenting & end_line_number is where u want to stop commenting.But he wanted that the script should take a function name as input rather than line numbers ... this is what he says: (name not published ;))
myfrend: hey vikas, i need a script to start commenting the codes without using line numbers, but using function name....is it possible?
me: u mean to say u have function names ... & u want to comment the whole functions?
u mean to say u have function names ... & u want to comment the whole functions?
myfrend: yeah
s
me: it is possible ... if u can get correct start&end boundaries for the functions
myfrend: yeah..how to get it....its difficult
me: how big can the file be .. that is MAX big file size?
myfrend: say around 3000 lines
I was asking a lot of questions about the source files like do they follow coding standards, etc .. & all that. So he suggested what he wanted to do ... i mean the logic ... he said:
myfrend: my logic is : search for function name, then keep a counter to increment/decrement when { or } is found, when counter is zero, exit the prgm
me: this is holds good ... but u have to account for { } occuring on same line ... & account for comments, etc
myfrend: ok
Got some free time to think about it ... & wrote a script for this. This may not be a complete solution, but a good starting point i guess.
Here is the link to the script:
PS - myfrend :
If you permit I can publish your name ... with a hyperlink possibly to your blog ;)
0 comments:
Post a Comment