| 1 | #!perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # inidiff - generate diffs between two .ini files | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Getopt::Std; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use App::IniDiff::IniFile; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 8 | my $prog = $0; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 8 | $prog =~ s:.*\/::; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 8 | my $Usage = "Usage: $prog [-q] [-V] [-i] [-M] [-c] [-o outfile] file1 file2 | |||||||||||||||||||||||||||||||||||||||||
| 17 | -q don't generate comments indicating new/old values | ||||||||||||||||||||||||||||||||||||||||||
| 18 | -V Print version number and exit. | ||||||||||||||||||||||||||||||||||||||||||
| 19 | -i ignore the case of comparisons. | ||||||||||||||||||||||||||||||||||||||||||
| 20 | -M add ^M to output to support old (pre-NT) Windows/DOS systems | ||||||||||||||||||||||||||||||||||||||||||
| 21 | -c strip trailing inline comments after semicolon | ||||||||||||||||||||||||||||||||||||||||||
| 22 | -o file File to save result to (instead of -f file); \"-\" means write | ||||||||||||||||||||||||||||||||||||||||||
| 23 | to stdout. | ||||||||||||||||||||||||||||||||||||||||||
| 24 | Compare two .ini files and generate a differences file. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | "; | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # add support for --help and --version | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 8 | $Getopt::Std::STANDARD_HELP_VERSION = "true"; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 8 | my $VERSION = '0.19'; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub VERSION_MESSAGE { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
VERSION_MESSAGE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | print "$prog: version $VERSION\n"; | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub HELP_MESSAGE { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
HELP_MESSAGE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | print STDERR $Usage; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 8 | my %opt; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 8 | ▶ | if (!&getopts('o:qViMc', \%opt)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | print STDERR $Usage; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 8 | ▶ | my $outFile = defined $opt{'o'} ? $opt{'o'} : '-'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 8 | ▶ | if (defined $opt{'V'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | print "$prog: version $VERSION\n"; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | ▶ | my $genComments = defined $opt{'q'} ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 8 | ▶ | my $ignoreCase = defined $opt{'i'} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 8 | ▶ | my $addM = defined $opt{'M'} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 8 | ▶ | my $stripComments = defined $opt{'c'} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 8 | ▶ | if (@ARGV < 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | print STDERR "$prog: wrong number of arguments\n"; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | die $Usage; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 8 | my $file1 = $ARGV[0]; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 8 | my $file2 = $ARGV[1]; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 8 | ▶ | if ($file1 eq '-' && $file2 eq '-') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$file1 eq "-" and $file2 eq "-"
MC/DC0%0/2
$file1 eq "-" and $file2 eq "-"
$file1 eq "-"
$file2 eq "-"
Truth table33%1/3
A and B
A= $file1 eq "-"B= $file2 eq "-"
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | die "$prog: both files can't be from stdin!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 8 | my $f1 = new IO::File $file1, "r"; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | ▶ | if (!defined $f1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | die "$prog: can't open $file1 - $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 8 | my $f2 = new IO::File $file2, "r"; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | ▶ | if (!defined $f2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | die "$prog: can't open $file2 - $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 7 | my $f1Ini = new App::IniDiff::IniFile($f1, 0, $addM, $stripComments); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 7 | ▶ | die "$prog: $file1:$App::IniDiff::IniFile::errorString\n" if (!defined $f1Ini); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 7 | my $f2Ini = new App::IniDiff::IniFile($f2, 0, $addM, $stripComments); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 7 | ▶ | die "$prog: $file1:$App::IniDiff::IniFile::errorString\n" if (!defined $f2Ini); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # Generate the diffs... | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | # using default () would not allow ^M and stripcomments to be passed in | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 7 | my $dIni = new App::IniDiff::IniFile("", 0, $addM, $stripComments); | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 7 | my %doneKeys = (); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 7 | my $key1; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 7 | foreach $key1 (@{$f1Ini->keys}) { | |||||||||||||||||||||||||||||||||||||||||
| 89 | 57 | my $key2 = $f2Ini->findKey($key1->name); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 57 | my %doneFields = (); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 57 | ▶ | if (defined $key2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 53 | my $keyd = undef; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 53 | $doneKeys{$key2->name} = $key1->orderId; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 53 | my $field1; | |||||||||||||||||||||||||||||||||||||||||
| 95 | my $field2; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 53 | foreach $field1 (@{$key1->fields}) { | |||||||||||||||||||||||||||||||||||||||||
| 97 | # returns canonicalized (lc) name | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 121 | $field2 = $key2->findField($field1->name); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 121 | ▶ | if (defined $field2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 121 | $doneFields{$field2->name} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | # if case should be ignored (-i opt) and | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # they are equivalent otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # then do not find them unequal | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 121 | ▶ | if (($field1->value ne $field2->value) && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$field1->value ne $field2->value and not $ignoreCase && lc $field1->value eq lc $field2->value
$ignoreCase && lc $field1->value eq lc $field2->value
MC/DC0%0/3
$field1->value ne $field2->value and not $ignoreCase && lc $field1->value eq lc $field2->value
$field1->value ne $field2->value
$ignoreCase
lc $field1->value eq lc $field2->value
Truth table25%1/4
A and not(B && C)
A= $field1->value ne $field2->valueB= $ignoreCaseC= lc $field1->value eq lc $field2->value
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | !(($ignoreCase) and | ||||||||||||||||||||||||||||||||||||||||||
| 107 | (lc($field1->value) eq lc($field2->value)))) | ||||||||||||||||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $keyd = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | $dIni->addKey(new App::IniDiff::IniFile::Key($key2->name, 0, undef)) | ||||||||||||||||||||||||||||||||||||||||||
| 111 | if !defined $keyd; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | # Changed | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $keyd->addField( | |||||||||||||||||||||||||||||||||||||||||
| 115 | new App::IniDiff::IniFile::Field( | ||||||||||||||||||||||||||||||||||||||||||
| 116 | $field2->name, $field2->value, 0, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | &comment("Old value: " . $field1->value))); | ||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | $keyd = $dIni->addKey(new App::IniDiff::IniFile::Key($key2->name, 0, undef)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | if !defined $keyd; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | # Old - not in new version | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | $keyd->addField( | |||||||||||||||||||||||||||||||||||||||||
| 126 | new App::IniDiff::IniFile::Field( | ||||||||||||||||||||||||||||||||||||||||||
| 127 | $field1->name, $field1->value, 1, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | &comment("Old value: " . $field1->value))); | ||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 53 | foreach $field2 (@{$key2->fields}) { | |||||||||||||||||||||||||||||||||||||||||
| 132 | 125 | ▶ | if (!exists $doneFields{$field2->name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 4 | ▶ | $keyd = $dIni->addKey(new App::IniDiff::IniFile::Key($key2->name, 0, undef)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | if !defined $keyd; | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # New addition | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 4 | $keyd->addField( | |||||||||||||||||||||||||||||||||||||||||
| 138 | new App::IniDiff::IniFile::Field( | ||||||||||||||||||||||||||||||||||||||||||
| 139 | $field2->name, $field2->value, 0, &comment('New'))); | ||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | # Whole key is deleted | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | $dIni->addKey(new App::IniDiff::IniFile::Key($key1->name, 1, undef)); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 7 | my $key2; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 7 | my $lastOrderId = -1; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 7 | foreach $key2 (@{$f2Ini->keys}) { | |||||||||||||||||||||||||||||||||||||||||
| 153 | 57 | ▶ | if (!exists $doneKeys{$key2->name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | # New key (and contents) | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 4 | my $keyd = | |||||||||||||||||||||||||||||||||||||||||
| 157 | $dIni->addKey(new App::IniDiff::IniFile::Key($key2->name, 0, &comment('New'))); | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # Attempt to the preserve order in the file... | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 4 | $keyd->orderId($lastOrderId += 0.000001); | |||||||||||||||||||||||||||||||||||||||||
| 161 | 4 | my $field2; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 4 | foreach $field2 (@{$key2->fields}) { | |||||||||||||||||||||||||||||||||||||||||
| 163 | 16 | $keyd->addField( | |||||||||||||||||||||||||||||||||||||||||
| 164 | new App::IniDiff::IniFile::Field($field2->name, $field2->value, 0, undef)); | ||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 53 | $lastOrderId = $doneKeys{$key2->name}; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 7 | my $out; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 7 | ▶ | if ($outFile eq '-') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | $out = new IO::Handle; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | if (!$out->fdopen("STDOUT", "w")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | die "$prog: can't fdopen STDOUT - $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 7 | $out = new IO::File $outFile, 'w'; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 7 | ▶ | if (!defined $out) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | die "$prog: couldn't open $outFile for writing - $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 7 | $dIni->write($out); | |||||||||||||||||||||||||||||||||||||||||
| 187 | 7 | ▶ | if (!$out->close) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | die "$prog: error writing to STDOUT - $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 7 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | sub comment | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
comment: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 8 | ▶ | return $genComments ? $_[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | __END__ | ||||||||||||||||||||||||||||||||||||||||||