File Coverage

blib/lib/Colon/Config.pm
Criterion Covered Total %
statement 57 59 96.6
branch 34 36 94.4
condition 9 9 100.0
subroutine 7 7 100.0
pod 2 2 100.0
total 109 113 96.4


line stmt bran cond sub pod time code
1             # Copyright (c) 2018, cPanel, LLC.
2             # All rights reserved.
3             # http://cpanel.net
4             #
5             # This is free software; you can redistribute it and/or modify it under the
6             # same terms as Perl itself. See L.
7              
8             package Colon::Config;
9              
10 15     15   3079709 use 5.010;
  15         61  
11 15     15   93 use strict;
  15         69  
  15         529  
12 15     15   82 use warnings;
  15         43  
  15         2449  
13              
14             # ABSTRACT: XS helper to read a configuration file using ':' as separator
15              
16              
17             our $BACKEND;
18              
19             BEGIN {
20              
21 15     15   83 our $VERSION = '0.005'; # VERSION: generated by DZP::OurPkgVersion
22              
23 15         41 eval {
24 15         80 require XSLoader;
25 15         7667 XSLoader::load(__PACKAGE__);
26             };
27 15 50       91 if ($@) {
28 0         0 *read = \&read_pp;
29 0         0 $BACKEND = 'pp';
30             }
31             else {
32 15         3306 $BACKEND = 'xs';
33             }
34             }
35              
36             sub read_pp {
37 140     140 1 2204683 my ( $config, $field, $sep ) = @_;
38              
39 140 100       451 if ( defined $field ) {
40 118 100       1061 die "Colon::Config::read_pp - Second argument must be one integer.\n"
41             unless $field =~ /\A\s*[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?\s*\z/;
42 15     15   111 $field = do { no warnings; int($field) };
  15         28  
  15         10982  
  117         200  
  117         242  
43 117 100       304 die "Colon::Config::read_pp - field must be >= 0\n"
44             if $field < 0;
45             }
46             else {
47 22         56 $field = 0;
48             }
49              
50 138 100       262 if ( defined $sep ) {
51 22 100       48 die "Colon::Config::read_pp - Third argument must be a string.\n"
52             unless !ref($sep);
53 21 100       82 die "Colon::Config::read_pp - separator must be a single character.\n"
54             unless length($sep) == 1;
55 18 100 100     125 die "Colon::Config::read_pp - separator cannot be a newline, carriage return, or null character.\n"
      100        
56             if $sep eq "\n" || $sep eq "\r" || $sep eq "\0";
57             }
58             else {
59 116         214 $sep = ':';
60             }
61              
62 130         262 my $sep_re = quotemeta($sep);
63              
64 130         214 my @result;
65 130         584 for my $line ( split( m{\n}, $config ) ) {
66 360         682 $line =~ s/\0//g;
67 360         592 $line =~ s/\r+$//;
68 360         811 $line =~ s/^[ \t\r\0]+//;
69 360 100       796 next if $line eq '';
70 343 100       821 next if $line =~ /^#/;
71              
72 308         1551 my @parts = split( /$sep_re/, $line, -1 );
73 308 100       777 next unless @parts > 1;
74              
75 286         495 my $key = $parts[0];
76 286 100       597 next unless length $key;
77 282         416 my $value;
78              
79 282 100       656 if ( $field == 0 ) {
    100          
80 200         535 $value = join( $sep, @parts[ 1 .. $#parts ] );
81 200         503 $value =~ s/^[ \t\r\0]+//;
82 200         486 $value =~ s/[ \t\r]+$//;
83             }
84             elsif ( $field <= $#parts ) {
85 59         102 $value = $parts[$field];
86 59         175 $value =~ s/^[ \t\r\0]+//;
87 59         131 $value =~ s/[ \t\r]+$//;
88             }
89              
90 282 100 100     1107 $value = undef if defined $value && !length $value;
91              
92 282         1037 push @result, $key, $value;
93             }
94              
95 130         854 return \@result;
96             }
97              
98             sub read_as_hash {
99 26     26 1 1964502 my ( $config, $field, $sep ) = @_;
100              
101 26 100       96 $field = 0 unless defined $field;
102              
103 26 100       358 my $av = defined $sep
104             ? Colon::Config::read($config, $field, $sep)
105             : Colon::Config::read($config, $field);
106 26 50       73 return {} unless $av;
107              
108 26         270 return { @$av };
109             }
110              
111              
112             1;
113              
114             __END__