| 1 | #!perl | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # iniedit - edit (or patch) an .ini file | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Getopt::Std; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use App::IniDiff::IniFile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | my $prog = $0; | |||||||||||||||||||||||||||
| 14 | 5 | $prog =~ s:.*\/::; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | my $Usage = "Usage: $prog [-V] [-o outfile] -i patchfile -f inoutfile | |||||||||||||||||||||||||||
| 17 | -f file File to patch; if -o not given, file is overwritten with | ||||||||||||||||||||||||||||
| 18 | new file; \"-\" means read (write) from stdin (stdout). | ||||||||||||||||||||||||||||
| 19 | -i file File containing patches; \"-\" means read from stdin. | ||||||||||||||||||||||||||||
| 20 | -o file File to save result to (instead of -f file); \"-\" means write | ||||||||||||||||||||||||||||
| 21 | to stdout. | ||||||||||||||||||||||||||||
| 22 | -V Print version number and exit. | ||||||||||||||||||||||||||||
| 23 | Reads .ini file patches and applies them to the specified file. | ||||||||||||||||||||||||||||
| 24 | "; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # add support for --help and --version | ||||||||||||||||||||||||||||
| 27 | 5 | $Getopt::Std::STANDARD_HELP_VERSION = "true"; | |||||||||||||||||||||||||||
| 28 | 5 | my $VERSION = '0.19'; | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub VERSION_MESSAGE { | ||||||||||||||||||||||||||
|
Subroutine
VERSION_MESSAGE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 30 | 0 | print "$prog: version $VERSION\n"; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub HELP_MESSAGE { | ||||||||||||||||||||||||||
|
Subroutine
HELP_MESSAGE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 0 | print STDERR $Usage; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 5 | my %opt; | |||||||||||||||||||||||||||
| 37 | 5 | ▶ | if (!getopts('f:i:o:V', \%opt)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | print STDERR $Usage; | |||||||||||||||||||||||||||
| 39 | 0 | exit 1; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 5 | ▶ | if (defined $opt{'V'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | print "$prog: version $VERSION\n"; | |||||||||||||||||||||||||||
| 43 | 0 | exit 0; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 5 | ▶ | if (!defined $opt{'f'} || !defined $opt{'i'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opt{"f"} and defined $opt{"i"}
MC/DC0%0/2
defined $opt{"f"} and defined $opt{"i"}
defined $opt{"f"}
defined $opt{"i"}
Truth table33%1/3
A and B
A= defined $opt{"f"}B= defined $opt{"i"}
| |||||||||||||||||||||||||||||
| 47 | 0 | die "$prog: -f and -i options are required\n$Usage"; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 5 | my $stdinUsed = 0; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 5 | my $inFile = $opt{'f'}; | |||||||||||||||||||||||||||
| 53 | 5 | my $patchFile = $opt{'i'}; | |||||||||||||||||||||||||||
| 54 | 5 | ▶ | my $outFile = defined $opt{'o'} ? $opt{'o'} : $inFile; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 5 | ▶ | my $saveOrig = !defined $opt{'o'} && $inFile ne '-'; | ||||||||||||||||||||||||||
|
Condition33%1/3
!defined($opt{'o'}) && $inFile ne '-'
MC/DC0%0/2
!defined($opt{'o'}) && $inFile ne '-'
!defined($opt{'o'})
$inFile ne '-'
Truth table33%1/3
not(A) && B
A= !defined($opt{'o'})B= $inFile ne '-'
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 5 | ▶ | if (@ARGV != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | print STDERR "$prog: don't know what to do with command line arguments\n"; | |||||||||||||||||||||||||||
| 59 | 0 | die $Usage; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 5 | my $fIn; | |||||||||||||||||||||||||||
| 63 | 5 | ▶ | if ($inFile eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ($stdinUsed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | die "$prog: stdin (-) used for multiple options\n"; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 0 | $stdinUsed = 1; | |||||||||||||||||||||||||||
| 68 | 0 | $fIn = new IO::Handle; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if (!$fIn->fdopen('STDIN', 'r')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | die "$prog: can't fdopen STDIN - $!\n"; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 0 | $inFile = 'stdin'; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||||||||
| 75 | 5 | $fIn = new IO::File $inFile, 'r'; | |||||||||||||||||||||||||||
| 76 | 5 | ▶ | if (!defined $fIn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | die "$prog: can't open $inFile - $!\n"; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 5 | my $pIn; | |||||||||||||||||||||||||||
| 81 | 5 | ▶ | if ($patchFile eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ($stdinUsed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | die "$prog: stdin (-) used for multiple options\n"; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 0 | $stdinUsed = 1; | |||||||||||||||||||||||||||
| 86 | 0 | $patchFile = 'stdin'; | |||||||||||||||||||||||||||
| 87 | 0 | $pIn = new IO::Handle; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (!$pIn->fdopen('STDIN', 'r')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | die "$prog: can't fdopen STDIN - $!\n"; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 5 | $pIn = new IO::File $patchFile, 'r'; | |||||||||||||||||||||||||||
| 94 | 5 | ▶ | if (!defined $pIn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | die "$prog: can't open $patchFile - $!\n"; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 5 | my $fIni = new App::IniDiff::IniFile($fIn, 0); | |||||||||||||||||||||||||||
| 100 | 5 | ▶ | die "$prog: Line", __LINE__, ": $inFile:$App::IniDiff::IniFile::errorString\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | if (!defined $fIni); | ||||||||||||||||||||||||||||
| 102 | 5 | my $pIni = new App::IniDiff::IniFile($pIn, 1); | |||||||||||||||||||||||||||
| 103 | 5 | ▶ | die "$prog: Line ", __LINE__, ": $patchFile:$App::IniDiff::IniFile::errorString\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | if (!defined $pIni); | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Apply the patch... | ||||||||||||||||||||||||||||
| 107 | 5 | my $pKey; | |||||||||||||||||||||||||||
| 108 | 5 | my $lastOrderId = -1; | |||||||||||||||||||||||||||
| 109 | 5 | foreach $pKey (@{$pIni->keys}) { | |||||||||||||||||||||||||||
| 110 | 10 | ▶ | if ($pKey->deleted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 2 | $fIni->removeKey($pKey->name); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | else { | ||||||||||||||||||||||||||||
| 114 | 8 | my $fKey = $fIni->findKey($pKey->name); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 8 | ▶ | $lastOrderId = $fKey->orderId if (defined $fKey); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 8 | my $field; | |||||||||||||||||||||||||||
| 118 | 8 | foreach $field (@{$pKey->fields}) { | |||||||||||||||||||||||||||
| 119 | 14 | ▶ | if ($field->deleted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 2 | ▶ | $fKey->removeField($field->name) if defined $fKey; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||
| 123 | 12 | ▶ | if (!defined $fKey) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 2 | $fKey = | |||||||||||||||||||||||||||
| 125 | $fIni->addKey(new App::IniDiff::IniFile::Key($pKey->name, 0, undef)); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Attempt to preserve order of keys in file | ||||||||||||||||||||||||||||
| 128 | 2 | $fKey->orderId($lastOrderId += 0.0000001); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Replaces existing field if there is one... | ||||||||||||||||||||||||||||
| 132 | $fKey->addField( | ||||||||||||||||||||||||||||
| 133 | 12 | new App::IniDiff::IniFile::Field($field->name, $field->value, 0, undef)); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 5 | ▶ | if ($outFile eq '-') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 140 | 0 | my $out = new IO::Handle; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (!$out->fdopen('STDOUT', 'w')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | die "$prog: can't fdopen STDOUT - $!\n"; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 0 | $fIni->write($out); | |||||||||||||||||||||||||||
| 145 | 0 | ▶ | if (!$out->close) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | print STDERR "$prog: error writing to stdout - $!\n"; | |||||||||||||||||||||||||||
| 147 | 0 | exit(1); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | elsif (!$saveOrig) { | ||||||||||||||||||||||||||||
| 151 | 5 | my $out = new IO::File $outFile, 'w'; | |||||||||||||||||||||||||||
| 152 | 5 | ▶ | if (!defined $out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | die "$prog: couldn't open $outFile for writing - $!\n"; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 5 | $fIni->write($out); | |||||||||||||||||||||||||||
| 156 | 5 | ▶ | if (!$out->close) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | die "$prog: error writing to $outFile - $!\n"; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||
| 161 | 0 | my $tmp = "$outFile.$$"; | |||||||||||||||||||||||||||
| 162 | 0 | my $out = new IO::File $tmp, 'w'; | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | if (!defined $out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | die "$prog: couldn't open $tmp for writing - $!\n"; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 0 | $fIni->write($out); | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | if (!$out->close) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | print STDERR "$prog: error writing to $tmp - $!\n"; | |||||||||||||||||||||||||||
| 169 | 0 | unlink($tmp); | |||||||||||||||||||||||||||
| 170 | 0 | exit(1); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | if (!rename($outFile, "$outFile.orig")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | print STDERR "$prog: couldn't rename $outFile to $outFile.orig - $!\n"; | |||||||||||||||||||||||||||
| 174 | 0 | unlink($tmp); | |||||||||||||||||||||||||||
| 175 | 0 | exit(1); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if (!rename($tmp, $outFile)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | print STDERR | |||||||||||||||||||||||||||
| 179 | "$prog: couldn't rename $tmp to $outFile - $! (will attemp to restore orig)\n"; | ||||||||||||||||||||||||||||
| 180 | 0 | rename("$outFile.orig", $outFile); | |||||||||||||||||||||||||||
| 181 | 0 | exit(1); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 5 | exit(0); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||||||||||