-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeadedTSV.pm
137 lines (108 loc) · 3.35 KB
/
HeadedTSV.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package HeadedTSV;
use strict;
use warnings;
use Carp;
our $VERSION = '1.00';
use base 'Exporter';
our @EXPORT = qw(set_htsv_callbacks read_htsv write_htsv);
my ($read_encoder, $write_encoder, $write_complete);
sub set_htsv_callbacks
{
($read_encoder, $write_encoder, $write_complete) = @_;
return;
}
sub dedup_headings {
my $hds = $_[0];
my %seen;
$seen{$_}++ foreach (@$hds);
return if scalar keys %seen == scalar @$hds;
my %count;
foreach (@$hds) {
$_ = "${_}__HTSV" . $count{$_}++ . '__' if $seen{$_} > 1;
}
}
sub read_htsv
{
my ($file, $nexist_ok, $to_undef, $hdg_key) = @_;
local $_;
$hdg_key //= 'Headings';
my $fh;
my %content;
my $in_header = 1;
my @defs;
if ($nexist_ok) {
open ($fh, '<', $file) or return %content;
} else {
open ($fh, '<', $file) or confess "$file: $!";
}
while (<$fh>) {
chomp; # no newline
s/^#.*//; # no leading comments
s/\s#.*//; # no whitespace prefixed comments
s/^[ \r\n\v\f]+//; # no leading white, except tab
s/\s+$//; # no trailing white
next unless length; # anything left?
($_) = (/^(.*)$/); # forcably untaint file input. if it's bad it shouldn't have got there.
$in_header = 0 if m/^===/;
if ($in_header) {
my ($key, $value) = split (' ', $_, 2);
$content{$key} = $value;
} else {
next if m/^===/;
unless ($content{$hdg_key}) {
$content{$hdg_key} = [ split ];
dedup_headings(\@{$content{$hdg_key}});
foreach my $i (0 .. $#{$content{$hdg_key}}) {
$defs[$i] = 0 unless (!ref $to_undef || grep ($_ eq $content{$hdg_key}[$i], @$to_undef));
}
@{$content{$_}} = () foreach @{$content{$hdg_key}};
} else {
my @line = split (' ', $_, scalar(@{$content{$hdg_key}}));
foreach my $i (0 .. $#{$content{$hdg_key}}) {
push (@{$content{$content{$hdg_key}[$i]}}, (defined $line[$i] && length $line[$i]) ? $line[$i] : $defs[$i]);
}
}
}
}
close $fh or die;
$read_encoder->(\%content) if ($read_encoder);
return %content;
}
sub write_htsv
{
my ($file, $content, $given_ts, $hdg_key, $max_rows) = @_;
my $ts = $given_ts ? $given_ts : 8;
$hdg_key //= 'Headings';
my $heading_only = not exists $content->{$hdg_key};
$write_encoder->($content) if ($write_encoder);
open (my $fh, '>', "$file.new") or confess "$file.new: $!";
foreach my $key (sort keys %$content) {
# check if non-white exists (since trailing white killed on read anyway)
say $fh ((defined $content->{$key} and $content->{$key} =~ /\S/) ? "$key $content->{$key}" : "$key") unless ref $content->{$key};
}
unless ($heading_only) {
$max_rows //= scalar @{$content->{$content->{$hdg_key}[0]}};
my $col_line = '=' x ($ts - 1) . ' ';
my $cols = scalar(@{$content->{$hdg_key}});
print $fh $col_line foreach (2 .. $cols);
say $fh '==================================================';
foreach my $key (@{$content->{$hdg_key}}) {
print $fh "$key ";
}
print $fh "\n";
print $fh $col_line foreach (2 .. $cols);
say $fh '==================================================';
foreach my $row (0 .. ($max_rows - 1)) {
foreach my $key (@{$content->{$hdg_key}}) {
print $fh $content->{$key}[$row] ? "$content->{$key}[$row] " : ' ';
}
print $fh "\n";
}
}
say $fh "\n# vim: ts=$ts" unless ($heading_only and not defined $given_ts);
close $fh or die;
rename ("$file.new", $file) or confess "$file: $!";
$write_complete->($file) if $write_complete;
return;
}
1;