I want my perl program to do the substitution of ‘{‘ – > ‘{function(‘.counter++.’)’ in all the files except the lines when there is a ‘{‘ and a ‘}’ in the same line, and except when the ‘{‘ appears one line under a ‘typedef’ substring.
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
use File::Find;
my $dir = "C:/test dir";
# fill up our argument list with file names:
find(sub { if (-f && /\.[hc]$/) { push @ARGV, $File::Find::name } }, $dir);
$^I = ".bak"; # supply backup string to enable in-place edit
my $counter = 0;
# now process our files
while (<>)
{
my @lines;
# copy each line from the text file to the string @lines and add a function call after every '{' '
tie @lines, 'Tie::File', $ARGV or die "Can't read file: $!\n"
foreach (@lines)
{
if (!( index (@lines,'}')!= -1 )) # if there is a '}' in the same line don't add the macro
{
s/{/'{function(' . $counter++ . ')'/ge;
print;
}
}
untie @lines; # free @lines
}
what I was trying to do is to go through all the files in @ARGV that i found in my dir and subdirs and for each *.c or *.h file I want to go line by line and check if this line contains ‘{‘. if it does the program won’t check if there is a ‘{‘ and won’t make the substitution, if it doesn’t the program will substitute ‘{‘ with ‘{function(‘.counter++.’);’
unfortunately this code does not work. I’m ashamed to say that I’m trying to make it work all day and still no go.I think that my problem is that I’m not really working with lines where I search for ‘{‘ but I don’t understand why. I would really appreciate some help.
I would also like to add that I am working in windows environment.
Thank You!!
Edit: so far with your help this is the code:
use strict;
use warnings;
use File::Find;
my $dir = "C:/projects/SW/fw/phy"; # use forward slashes in paths
# fill up our argument list with file names:
find(sub { if (-f && /\.[hc]$/) { push @ARGV, $File::Find::name } }, $dir);
$^I = ".bak"; # supply backup string to enable in-place edit
my $counter = 0;
# now process our files
while (<>) {
s/{/'{ function(' . $counter++ . ')'/ge unless /}/;
print;
}
The only thing that is left to do is to make it ignore ‘{‘ substitution when it is one line under ‘typedef’ substring like this:
typedef struct
{
}examp;
I would greatly appreciate your help! Thank you! 🙂
Edit #2: This is the final code:
use strict;
use warnings;
use File::Find;
my $dir = "C:/exmp";
# fill up our argument list with file names:
find(sub { if (-f && /\.[hc]$/) { push @ARGV, $File::Find::name } }, $dir);
$^I = ".bak"; # supply backup string to enable in-place edit
my $counter = 0;
my $td = 0;
# now process our files
while (<>) {
s/{/'{ function(' . $counter++ . ')'/ge if /{[^}]*$/ && $td == 0;
$td = do { (/typedef/ ? 1 : 0 ) || ( (/=/ ? 1 : 0 ) && (/if/ ? 0 : 1 ) && (/while/ ? 0 : 1 ) && (/for/ ? 0 : 1 ) && (/switch/ ? 0 : 1 ) )};
print;
}
The code does the substitution except when the line above the substitution place included ‘typedef’,
When the line above it included ‘=’ and no ‘if’, ‘while’, ‘for’ or ‘switch’ the substitiution will also not happen.
Thank you all for your help!
The
-iswith let you presise an extension for backup files.Using perl:
or (same result):
And for processing all files in sub-folder too, this could be simplier to use
find:Using
GNU sedlet you do the job very quicklyEdit For doing modification only if previous line don’t contain word
typedef:could be written;
or more readable as
Or as a perl script file:
cat >addFunction.plExplained:
BEGIN{...}command block to do at begin of program.s/// if // &&to replacement if current match match and $td=0$td=do{ aaa ? bbb : ccc }assing to td: if aaa then bbb else ccc.As perl run sequetialy,
$tdkeep his value until next assignement. So if test for replacement is doing before$tdassignement, the check will use previous value.And finaly, same using sed:
or more readable:
Some
sedtricks:hstore (backup) current line to the hold spacexexchange current working line with the hold spaces///well known replacement string command\o047octal tick:'/{[^}]*$/{ ... }Command block to do only on lines maching{and no}././{ ... }Command block to do only on lines containing at least 1 character