| 1 | package Protocol::IR::Format::CSV; | ||||||||||||||||||||||||||||||||||
| 2 | 55 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 55 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | 55 | our $VERSION = '1.5'; | |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | # Parse a single CSV line with full RFC-style quoting: a field wrapped in | ||||||||||||||||||||||||||||||||||
| 8 | # double quotes may hold commas, quotes, and newlines, with a literal quote | ||||||||||||||||||||||||||||||||||
| 9 | # written as a doubled "" exactly as the reference TypeScript parser accepts | ||||||||||||||||||||||||||||||||||
| 10 | # (the web-ir-remote-tools port emits such files). Unquoted fields are | ||||||||||||||||||||||||||||||||||
| 11 | # trimmed of surrounding whitespace, again matching the TS port so the two | ||||||||||||||||||||||||||||||||||
| 12 | # agree on every file. A hand-rolled parser stays because IRDB files are not | ||||||||||||||||||||||||||||||||||
| 13 | # strict RFC 4180 - header fields can be tab-joined, rows arrive headerless, | ||||||||||||||||||||||||||||||||||
| 14 | # and quoted values may carry leading/trailing whitespace the strict library | ||||||||||||||||||||||||||||||||||
| 15 | # parsers reject - and Text::CSV_XS would add a compiled dependency for a | ||||||||||||||||||||||||||||||||||
| 16 | # format that mostly does not need it. | ||||||||||||||||||||||||||||||||||
| 17 | 209 | ▶ | sub _parse_csv_line { | ||||||||||||||||||||||||||||||||
|
Subroutine
_parse_csv_line: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||
| 18 | 209 | my ($line) = @_; | |||||||||||||||||||||||||||||||||
| 19 | 209 | ▶ | return () unless defined $line; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 20 | 209 | $line =~ s/[\r\n]+$//; | |||||||||||||||||||||||||||||||||
| 21 | 209 | my @fields; | |||||||||||||||||||||||||||||||||
| 22 | 209 | my ($field, $in_quotes, $quoted) = ('', 0, 0); | |||||||||||||||||||||||||||||||||
| 23 | 209 | ▶ | for (my $i = 0; $i < length $line; $i++) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 24 | 4845 | my $c = substr($line, $i, 1); | |||||||||||||||||||||||||||||||||
| 25 | 4845 | ▶ | if ($in_quotes) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 26 | 31 | ▶ | if ($c eq '"') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 27 | 7 | ▶ | if (substr($line, $i + 1, 1) eq '"') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 28 | 4 | $field .= '"'; | |||||||||||||||||||||||||||||||||
| 29 | 4 | $i++; | |||||||||||||||||||||||||||||||||
| 30 | } else { | ||||||||||||||||||||||||||||||||||
| 31 | 3 | $in_quotes = 0; | |||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||
| 33 | } else { | ||||||||||||||||||||||||||||||||||
| 34 | 24 | $field .= $c; | |||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||
| 36 | ▶ | } elsif ($c eq '"' && $field eq '') { | |||||||||||||||||||||||||||||||||
|
Condition66%2/3
$c eq "\"" and $field eq ""
MC/DC50%1/2
$c eq "\"" and $field eq ""
$c eq "\""
$field eq ""
Truth table66%2/3
A and B
A= $c eq "\""B= $field eq ""
| |||||||||||||||||||||||||||||||||||
| 37 | 3 | $in_quotes = 1; | |||||||||||||||||||||||||||||||||
| 38 | 3 | $quoted = 1; | |||||||||||||||||||||||||||||||||
| 39 | } elsif ($c eq ',') { | ||||||||||||||||||||||||||||||||||
| 40 | 829 | push @fields, _field_value($field, $quoted); | |||||||||||||||||||||||||||||||||
| 41 | 829 | $field = ''; | |||||||||||||||||||||||||||||||||
| 42 | 829 | $quoted = 0; | |||||||||||||||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||||||||||||||
| 44 | 3982 | $field .= $c; | |||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||
| 47 | 209 | push @fields, _field_value($field, $quoted); | |||||||||||||||||||||||||||||||||
| 48 | 209 | return @fields; | |||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | # Quoted fields keep their bytes verbatim; unquoted fields are trimmed to | ||||||||||||||||||||||||||||||||||
| 52 | # match the TypeScript parser's String.trim(). | ||||||||||||||||||||||||||||||||||
| 53 | 1038 | ▶ | sub _field_value { | ||||||||||||||||||||||||||||||||
|
Subroutine
_field_value: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 54 | 1038 | my ($value, $quoted) = @_; | |||||||||||||||||||||||||||||||||
| 55 | 1038 | ▶ | return $value if $quoted; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 56 | 1035 | $value =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||
| 57 | 1035 | return $value; | |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | # Normalize protocol aliases commonly found in IRDB. | ||||||||||||||||||||||||||||||||||
| 61 | # | ||||||||||||||||||||||||||||||||||
| 62 | # IRDB labels its NEC-family rows NEC, NEC1, nec, nec1 (all the standard | ||||||||||||||||||||||||||||||||||
| 63 | # NEC1 framing), NEC2 (whole-frame repeat), and NECx1/NECx2 (the extended, | ||||||||||||||||||||||||||||||||||
| 64 | # half-header 16-bit-address variants). Anything else is returned | ||||||||||||||||||||||||||||||||||
| 65 | # unchanged, so unknown protocols still reach the registry and are skipped | ||||||||||||||||||||||||||||||||||
| 66 | # as unregistered rather than being silently remapped. | ||||||||||||||||||||||||||||||||||
| 67 | 186 | ▶ | sub _normalize_protocol { | ||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_protocol: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||
| 68 | 186 | my ($proto) = @_; | |||||||||||||||||||||||||||||||||
| 69 | 186 | ▶ | return '' unless defined $proto; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 70 | 186 | $proto =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||
| 71 | 186 | $proto = uc $proto; | |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | 186 | ▶ | return 'NEC' if $proto =~ /^NEC1?$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 74 | 48 | ▶ | return 'NEC2' if $proto eq 'NEC2'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 75 | 45 | ▶ | return 'NECX1' if $proto eq 'NECX1'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 76 | 44 | ▶ | return 'NECX2' if $proto eq 'NECX2'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 77 | 34 | ▶ | return 'JVC' if $proto eq 'JVC'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 78 | 6 | return $proto; | |||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | 26 | ▶ | sub decode { | ||||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=32 SCAR=35.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 82 | 26 | my ($class, $input, $registry) = @_; | |||||||||||||||||||||||||||||||||
| 83 | 26 | ▶ | die "No CSV input provided\n" unless defined $input; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | 25 | my @lines; | |||||||||||||||||||||||||||||||||
| 86 | 25 | ▶ | if ($input !~ /[\r\n]/ && -e $input) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $input =~ /[\r\n]/ and -e $input
MC/DC100%2/2
not $input =~ /[\r\n]/ and -e $input
not $input =~ /[\r\n]/
-e $input
Truth table100%3/3
not(A) and B
A= not $input =~ /[\r\n]/B= -e $input
| |||||||||||||||||||||||||||||||||||
| 87 | 5 | ▶ | open my $fh, '<', $input or die "Cannot open CSV file '$input': $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 88 | 5 | @lines = <$fh>; | |||||||||||||||||||||||||||||||||
| 89 | 5 | close $fh; | |||||||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||||||||
| 91 | 20 | @lines = split(/\r?\n/, $input); | |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | # Filter empty lines | ||||||||||||||||||||||||||||||||||
| 95 | 25 | @lines = grep { /\S/ } @lines; | |||||||||||||||||||||||||||||||||
| 96 | 25 | ▶ | die "CSV input is empty\n" unless @lines; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | # Parse header row | ||||||||||||||||||||||||||||||||||
| 99 | 23 | my @headers = map { lc($_) } _parse_csv_line(shift @lines); | |||||||||||||||||||||||||||||||||
| 100 | 23 | $headers[0] =~ s/^\x{FEFF}//; # Strip UTF-8 BOM if present | |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | # Map column headers | ||||||||||||||||||||||||||||||||||
| 103 | 23 | my %col_map; | |||||||||||||||||||||||||||||||||
| 104 | 23 | for my $i (0 .. $#headers) { | |||||||||||||||||||||||||||||||||
| 105 | 112 | my $h = $headers[$i]; | |||||||||||||||||||||||||||||||||
| 106 | 112 | $h =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | 112 | ▶ | if ($h =~ /^(functionname|function_name|key|alias|label|name)$/) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 109 | 22 | $col_map{alias} = $i; | |||||||||||||||||||||||||||||||||
| 110 | } elsif ($h =~ /^(protocol|proto)$/) { | ||||||||||||||||||||||||||||||||||
| 111 | 23 | $col_map{protocol} = $i; | |||||||||||||||||||||||||||||||||
| 112 | } elsif ($h =~ /^(device|address|addr|dev)$/) { | ||||||||||||||||||||||||||||||||||
| 113 | 22 | $col_map{device} = $i; | |||||||||||||||||||||||||||||||||
| 114 | } elsif ($h =~ /^(subdevice|subaddress|subaddr|subdev)$/) { | ||||||||||||||||||||||||||||||||||
| 115 | 22 | $col_map{subdevice} = $i; | |||||||||||||||||||||||||||||||||
| 116 | ▶ | } elsif ($h =~ /^(function|command|cmd|code)$/ && !defined $col_map{alias}) { | |||||||||||||||||||||||||||||||||
|
Condition100%3/3
$h =~ /^(function|command|cmd|code)$/ and not defined $col_map{"alias"}
MC/DC100%2/2
$h =~ /^(function|command|cmd|code)$/ and not defined $col_map{"alias"}
$h =~ /^(function|command|cmd|code)$/
not defined $col_map{"alias"}
Truth table100%3/3
A and not(B)
A= $h =~ /^(function|command|cmd|code)$/B= not defined $col_map{"alias"}
| |||||||||||||||||||||||||||||||||||
| 117 | 1 | $col_map{command} = $i; | |||||||||||||||||||||||||||||||||
| 118 | } elsif ($h =~ /^(function|command|cmd|code)$/) { | ||||||||||||||||||||||||||||||||||
| 119 | 21 | ▶ | $col_map{command} //= $i; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$col_map{"command"} //= $i
MC/DC0%0/2
$col_map{"command"} //= $i
$col_map{"command"}
$i
Truth table33%1/3
A //= B
A= $col_map{"command"}B= $i
| |||||||||||||||||||||||||||||||||||
| 120 | } elsif ($h =~ /^(data|hex|raw)$/) { | ||||||||||||||||||||||||||||||||||
| 121 | 1 | $col_map{data} = $i; | |||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 23 | my @decoded_codes; | |||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | 23 | for my $line (@lines) { | |||||||||||||||||||||||||||||||||
| 128 | 186 | my @fields = _parse_csv_line($line); | |||||||||||||||||||||||||||||||||
| 129 | 186 | ▶ | next unless @fields; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | 186 | ▶ | my $proto_raw = defined $col_map{protocol} ? $fields[$col_map{protocol}] : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 132 | 186 | my $proto = _normalize_protocol($proto_raw); | |||||||||||||||||||||||||||||||||
| 133 | 186 | ▶ | next unless $proto; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||
| 135 | 186 | ▶ | my $alias = defined $col_map{alias} ? $fields[$col_map{alias}] : 'UNKNOWN'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 136 | 186 | ▶ | my $device = defined $col_map{device} ? $fields[$col_map{device}] : undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 137 | 186 | ▶ | my $subdevice = defined $col_map{subdevice} ? $fields[$col_map{subdevice}] : undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 138 | 186 | ▶ | my $command = defined $col_map{command} ? $fields[$col_map{command}] : undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 139 | 186 | ▶ | my $data = defined $col_map{data} ? $fields[$col_map{data}] : undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | 186 | my $ir_code; | |||||||||||||||||||||||||||||||||
| 142 | 186 | eval { | |||||||||||||||||||||||||||||||||
| 143 | 186 | ▶ | if (defined $data && $data ne '') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $data and $data ne ""
MC/DC50%1/2
defined $data and $data ne ""
defined $data
$data ne ""
Truth table66%2/3
A and B
A= defined $dataB= $data ne ""
| |||||||||||||||||||||||||||||||||||
| 144 | 1 | $ir_code = $registry->import_code($proto, $data); | |||||||||||||||||||||||||||||||||
| 145 | ▶ | } elsif (defined $device && defined $command) { | |||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $device and defined $command
MC/DC0%0/2
defined $device and defined $command
defined $device
defined $command
Truth table33%1/3
A and B
A= defined $deviceB= defined $command
| |||||||||||||||||||||||||||||||||||
| 146 | 185 | ▶ | $ir_code = $registry->import_code($proto, { | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$subdevice // -1
MC/DC0%0/1
$subdevice // -1
$subdevice
Truth table50%1/2
A
A= $subdevice
| |||||||||||||||||||||||||||||||||||
| 147 | device => $device, | ||||||||||||||||||||||||||||||||||
| 148 | subdevice => $subdevice // -1, | ||||||||||||||||||||||||||||||||||
| 149 | command => $command, | ||||||||||||||||||||||||||||||||||
| 150 | }); | ||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||
| 152 | }; | ||||||||||||||||||||||||||||||||||
| 153 | # Rows with an unregistered protocol or an unparseable value are | ||||||||||||||||||||||||||||||||||
| 154 | # skipped (as documented in the POD), not fatal: real IRDB files | ||||||||||||||||||||||||||||||||||
| 155 | # routinely mix protocols this distribution does not handle. | ||||||||||||||||||||||||||||||||||
| 156 | 186 | ▶ | next if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | 185 | ▶ | if ($ir_code) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 159 | 185 | $ir_code->alias($alias); | |||||||||||||||||||||||||||||||||
| 160 | 185 | push @decoded_codes, $ir_code; | |||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | 23 | return \@decoded_codes; | |||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | # Serialize decoded codes as canonical IRDB rows. Only addressable, | ||||||||||||||||||||||||||||||||||
| 168 | # registered-protocol codes can be represented: a code whose protocol is | ||||||||||||||||||||||||||||||||||
| 169 | # unknown has nothing to key a row on, so it is skipped (the same set the | ||||||||||||||||||||||||||||||||||
| 170 | # importer drops). Each row carries the decoded fields the importer turns | ||||||||||||||||||||||||||||||||||
| 171 | # back into a code, so CSV -> Code -> CSV is lossless for addressable | ||||||||||||||||||||||||||||||||||
| 172 | # codes; the aliased device/subdevice/function columns cannot express every | ||||||||||||||||||||||||||||||||||
| 173 | # raw word of every protocol (e.g. Samsung20), which is inherent to IRDB's | ||||||||||||||||||||||||||||||||||
| 174 | # keyed schema, not to this export. | ||||||||||||||||||||||||||||||||||
| 175 | 4 | ▶ | sub export { | ||||||||||||||||||||||||||||||||
|
Subroutine
export: called CC=11 SCAR=25.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 176 | 4 | my ($class, $codes, $registry, %opts) = @_; | |||||||||||||||||||||||||||||||||
| 177 | 4 | ▶ | $codes = [$codes] unless ref $codes eq 'ARRAY'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||
| 179 | 4 | my @lines = ('functionname,protocol,device,subdevice,function'); | |||||||||||||||||||||||||||||||||
| 180 | 4 | for my $code (@$codes) { | |||||||||||||||||||||||||||||||||
| 181 | 9 | ▶ | my $protocol = $code->protocol // 'UNKNOWN'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$code->protocol // "UNKNOWN"
MC/DC0%0/1
$code->protocol // "UNKNOWN"
$code->protocol
Truth table50%1/2
A
A= $code->protocol
| |||||||||||||||||||||||||||||||||||
| 182 | 9 | ▶ | next if $protocol eq 'UNKNOWN'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | 8 | ▶ | my $alias = (defined $code->alias && $code->alias =~ /\S/) ? $code->alias : 'UNKNOWN'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $code->alias and $code->alias =~ /\S/
MC/DC0%0/2
defined $code->alias and $code->alias =~ /\S/
defined $code->alias
$code->alias =~ /\S/
Truth table33%1/3
A and B
A= defined $code->aliasB= $code->alias =~ /\S/
| |||||||||||||||||||||||||||||||||||
| 185 | 8 | ▶ | if ($alias =~ /[",\n\r]/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 186 | 2 | $alias =~ s/"/""/g; | |||||||||||||||||||||||||||||||||
| 187 | 2 | $alias = qq{"$alias"}; | |||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | 8 | ▶ | push @lines, join(',', | ||||||||||||||||||||||||||||||||
|
Condition50%3/6
$code->address // 0
$code->subaddress // -1
$code->command // 0
MC/DC0%0/1
$code->address // 0
$code->address
MC/DC0%0/1
$code->subaddress // -1
$code->subaddress
MC/DC0%0/1
$code->command // 0
$code->command
Truth table50%1/2
A
A= $code->address
Truth table50%1/2
A
A= $code->subaddress
Truth table50%1/2
A
A= $code->command
| |||||||||||||||||||||||||||||||||||
| 191 | $alias, | ||||||||||||||||||||||||||||||||||
| 192 | $protocol, | ||||||||||||||||||||||||||||||||||
| 193 | $code->address // 0, | ||||||||||||||||||||||||||||||||||
| 194 | $code->subaddress // -1, | ||||||||||||||||||||||||||||||||||
| 195 | $code->command // 0, | ||||||||||||||||||||||||||||||||||
| 196 | ); | ||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||
| 198 | 4 | return join("\n", @lines) . "\n"; | |||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||
| 201 | 55 | 1; | |||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||
| 203 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||
| 205 | Protocol::IR::Format::CSV - IRDB CSV importer and exporter | ||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||
| 207 | =head1 VERSION | ||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||
| 209 | version 1.5 | ||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||
| 211 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||
| 213 | use Protocol::IR::Converter; | ||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||
| 215 | my $converter = Protocol::IR::Converter->new(); | ||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||
| 217 | # Parse an IRDB-style CSV string | ||||||||||||||||||||||||||||||||||
| 218 | my $csv = "functionname,protocol,device,subdevice,function\n" | ||||||||||||||||||||||||||||||||||
| 219 | . "KEY_POWER,NEC1,4,0,8\n"; | ||||||||||||||||||||||||||||||||||
| 220 | my $codes = $converter->import_format('CSV', $csv); | ||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||
| 222 | for my $code (@$codes) { | ||||||||||||||||||||||||||||||||||
| 223 | print $code->alias . "\n"; | ||||||||||||||||||||||||||||||||||
| 224 | print $converter->export_code($code, 'Pronto') . "\n"; | ||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||
| 227 | # Or load from a file path | ||||||||||||||||||||||||||||||||||
| 228 | my $codes = $converter->import_format('CSV', 'remote.csv'); | ||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||
| 230 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||
| 232 | C<Protocol::IR::Format::CSV> imports and exports IRDB-style CSV button listings as | ||||||||||||||||||||||||||||||||||
| 233 | L<Protocol::IR::Code> objects. It accepts either a CSV string or the path to a CSV file. | ||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||
| 235 | =over 4 | ||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||
| 237 | =item Example IRDB CSV (Samsung TV, NECx2 variant) | ||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||
| 239 | functionname,protocol,device,subdevice,function | ||||||||||||||||||||||||||||||||||
| 240 | POWER,NECx2,7,7,2 | ||||||||||||||||||||||||||||||||||
| 241 | 1,NECx2,7,7,4 | ||||||||||||||||||||||||||||||||||
| 242 | 2,NECx2,7,7,5 | ||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||
| 244 | =back | ||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | This is the delimited Remotec ZXT-120 style CSV used by the IRDB project | ||||||||||||||||||||||||||||||||||
| 247 | (L<https://github.com/probonopd/irdb>). | ||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||
| 249 | Column headers are detected automatically. Recognized header aliases: | ||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||
| 251 | =over 4 | ||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||
| 253 | =item * C<functionname>, C<function_name>, C<key>, C<alias>, C<label>, | ||||||||||||||||||||||||||||||||||
| 254 | C<name> -- button name (stored on the C<alias> accessor) | ||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||
| 256 | =item * C<protocol>, C<proto> -- protocol name | ||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||
| 258 | =item * C<device>, C<address>, C<addr>, C<dev> -- device (address) | ||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||
| 260 | =item * C<subdevice>, C<subaddress>, C<subaddr>, C<subdev> -- subdevice | ||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||
| 262 | =item * C<function>, C<command>, C<cmd>, C<code> -- command | ||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||
| 264 | =item * C<data>, C<hex>, C<raw> -- a raw value for the protocol | ||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||
| 266 | =back | ||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||
| 268 | A row is decoded either from a raw C<data> value or, when no C<data> column | ||||||||||||||||||||||||||||||||||
| 269 | is present, from the C<device>/C<subdevice>/C<command> columns. Protocol | ||||||||||||||||||||||||||||||||||
| 270 | aliases such as C<NEC1> and C<nec> are normalized to C<NEC>; the IRDB | ||||||||||||||||||||||||||||||||||
| 271 | NEC-family variants C<NEC2>, C<NECx1>, and C<NECx2> are kept as-is, so the | ||||||||||||||||||||||||||||||||||
| 272 | variant identity survives the conversion. Each decoded code gets its | ||||||||||||||||||||||||||||||||||
| 273 | C<alias> set from the button name column. | ||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||
| 275 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||
| 277 | =head2 decode | ||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||
| 279 | my $codes = $class->decode($input, $registry); | ||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||
| 281 | Parses a CSV string or file and returns an arrayref of L<Protocol::IR::Code> objects. | ||||||||||||||||||||||||||||||||||
| 282 | Rows with an unregistered or unrecognized protocol are skipped. | ||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||
| 284 | =head2 export | ||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||
| 286 | my $csv = $class->export($codes, $registry); | ||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||
| 288 | Serializes a L<Protocol::IR::Code> or an arrayref of codes as canonical IRDB | ||||||||||||||||||||||||||||||||||
| 289 | CSV: a C<functionname,protocol,device,subdevice,function> header followed by | ||||||||||||||||||||||||||||||||||
| 290 | one row per code. A code with no addressable decoded fields (an C<UNKNOWN> | ||||||||||||||||||||||||||||||||||
| 291 | protocol) is skipped. Button names are quoted - with embedded quotes doubled - | ||||||||||||||||||||||||||||||||||
| 292 | when they contain a comma, double quote, or newline. Round-tripping through | ||||||||||||||||||||||||||||||||||
| 293 | C<decode> reproduces the decoded fields. | ||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||
| 295 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||
| 297 | Source code: L<https://github.com/bwarden/perl-protocol-ir> | ||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||
| 299 | Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues> | ||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||
| 301 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||
| 303 | Brett T. Warden <bwarden@cpan.org> | ||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||
| 305 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||
| 307 | Copyright (c) 2026 Brett T. Warden | ||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||
| 309 | This library is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||
| 310 | under the terms of the GNU General Public License as published by the | ||||||||||||||||||||||||||||||||||
| 311 | Free Software Foundation; either version 2 of the License, or (at your | ||||||||||||||||||||||||||||||||||
| 312 | option) any later version. | ||||||||||||||||||||||||||||||||||