| 1 | package DB::Color::Config; | ||||||||||||
| 2 | |||||||||||||
| 3 | # If you thought Config::Simple was small... | ||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | BEGIN { | ||||||||||||
| 7 | 1 | ▶ | require 5.004; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | $DB::Color::Config::VERSION = '0.10'; | |||||||||||
| 9 | 1 | $DB::Color::Config::errstr = ''; | |||||||||||
| 10 | } | ||||||||||||
| 11 | |||||||||||||
| 12 | # Create an empty object | ||||||||||||
| 13 | 0 | ▶ | sub new { bless {}, shift } | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 14 | |||||||||||||
| 15 | # Create an object from a file | ||||||||||||
| 16 | sub read { | ||||||||||||
| 17 | 1 | ▶ | my $class = ref $_[0] ? ref shift : shift; | ||||||||||
|
Subroutine
read: called CC=7 SCAR=32.5
Pod
documented
Branch: 1 branch
| |||||||||||||
| 18 | |||||||||||||
| 19 | # Check the file | ||||||||||||
| 20 | 1 | ▶ | my $file = shift or return $class->_error( 'You did not specify a file name' ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | 1 | ▶ | return $class->_error( "File '$file' does not exist" ) unless -e $file; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 0 | ▶ | return $class->_error( "'$file' is a directory, not a file" ) unless -f _; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | 0 | ▶ | return $class->_error( "Insufficient permissions to read '$file'" ) unless -r _; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 24 | |||||||||||||
| 25 | # Slurp in the file | ||||||||||||
| 26 | 0 | local $/ = undef; | |||||||||||
| 27 | 0 | ▶ | open( CFG, $file ) or return $class->_error( "Failed to open file '$file': $!" ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 0 | my $contents = <CFG>; | |||||||||||
| 29 | 0 | close( CFG ); | |||||||||||
| 30 | |||||||||||||
| 31 | 0 | $class->read_string( $contents ); | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | # Create an object from a string | ||||||||||||
| 35 | sub read_string { | ||||||||||||
| 36 | 0 | ▶ | my $class = ref $_[0] ? ref shift : shift; | ||||||||||
|
Subroutine
read_string: not called CC=8 SCAR=42.8
Pod
documented
Branch: 1 branch
| |||||||||||||
| 37 | 0 | my $self = bless {}, $class; | |||||||||||
| 38 | 0 | ▶ | return undef unless defined $_[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | |||||||||||||
| 40 | # Parse the file | ||||||||||||
| 41 | 0 | my $ns = '_'; | |||||||||||
| 42 | 0 | my $counter = 0; | |||||||||||
| 43 | 0 | foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) { | |||||||||||
| 44 | 0 | $counter++; | |||||||||||
| 45 | |||||||||||||
| 46 | # Skip comments and empty lines | ||||||||||||
| 47 | 0 | ▶ | next if /^\s*(?:\#|\;|$)/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | |||||||||||||
| 49 | # Remove inline comments | ||||||||||||
| 50 | 0 | s/\s\;\s.+$//g; | |||||||||||
| 51 | |||||||||||||
| 52 | # Handle section headers | ||||||||||||
| 53 | 0 | ▶ | if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | # Create the sub-hash if it doesn't exist. | ||||||||||||
| 55 | # Without this sections without keys will not | ||||||||||||
| 56 | # appear at all in the completed struct. | ||||||||||||
| 57 | 0 | ▶ | $self->{$ns = $1} ||= {}; | ||||||||||
|
Condition0%0/2
$$self{$ns = $1} ||= {}
MC/DC0%0/1
$$self{$ns = $1} ||= {}
$$self{$ns = $1}
Truth table0%0/2
A
A= $$self{$ns = $1}
| |||||||||||||
| 58 | 0 | next; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | # Handle properties | ||||||||||||
| 62 | 0 | ▶ | if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 0 | $self->{$ns}->{$1} = $2; | |||||||||||
| 64 | 0 | next; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 0 | return $self->_error( "Syntax error at line $counter: '$_'" ); | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 0 | $self; | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | # Save an object to a file | ||||||||||||
| 74 | sub write { | ||||||||||||
| 75 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
write: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 76 | 0 | ▶ | my $file = shift or return $self->_error( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 'No file name provided' | ||||||||||||
| 78 | ); | ||||||||||||
| 79 | |||||||||||||
| 80 | # Write it to the file | ||||||||||||
| 81 | 0 | my $string = $self->write_string; | |||||||||||
| 82 | 0 | ▶ | return undef unless defined $string; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 0 | ▶ | open( CFG, '>' . $file ) or return $self->_error( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | "Failed to open file '$file' for writing: $!" | ||||||||||||
| 85 | ); | ||||||||||||
| 86 | 0 | print CFG $string; | |||||||||||
| 87 | 0 | close CFG; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | # Save an object to a string | ||||||||||||
| 91 | sub write_string { | ||||||||||||
| 92 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
write_string: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||
| 93 | |||||||||||||
| 94 | 0 | my $contents = ''; | |||||||||||
| 95 | 0 | ▶ | foreach my $section ( sort { (($b eq '_') <=> ($a eq '_')) || ($a cmp $b) } keys %$self ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | # Check for several known-bad situations with the section | ||||||||||||
| 97 | # 1. Leading whitespace | ||||||||||||
| 98 | # 2. Trailing whitespace | ||||||||||||
| 99 | # 3. Newlines in section name | ||||||||||||
| 100 | 0 | ▶ | return $self->_error( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | "Illegal whitespace in section name '$section'" | ||||||||||||
| 102 | ) if $section =~ /(?:^\s|\n|\s$)/s; | ||||||||||||
| 103 | 0 | my $block = $self->{$section}; | |||||||||||
| 104 | 0 | ▶ | $contents .= "\n" if length $contents; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 105 | 0 | ▶ | $contents .= "[$section]\n" unless $section eq '_'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | 0 | foreach my $property ( sort keys %$block ) { | |||||||||||
| 107 | return $self->_error( | ||||||||||||
| 108 | "Illegal newlines in property '$section.$property'" | ||||||||||||
| 109 | 0 | ▶ | ) if $block->{$property} =~ /(?:\012|\015)/s; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 0 | $contents .= "$property=$block->{$property}\n"; | |||||||||||
| 111 | } | ||||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | 0 | $contents; | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | # Error handling | ||||||||||||
| 118 | 0 | ▶ | sub errstr { $DB::Color::Config::errstr } | ||||||||||
|
Subroutine
errstr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 119 | 1 | ▶ | sub _error { $DB::Color::Config::errstr = $_[1]; undef } | ||||||||||
|
Subroutine
_error: called CC=1 SCAR=0.0
| |||||||||||||
| 120 | |||||||||||||
| 121 | 1 | 1; | |||||||||||
| 122 | |||||||||||||
| 123 | __END__ | ||||||||||||