View | Details | Raw Unified | Return to bug 36831
Collapse All | Expand All

(-)a/Koha/ERM/EHoldings/Title.pm (-1 / +46 lines)
Lines 100-105 sub resources { Link Here
100
    return Koha::ERM::EHoldings::Resources->_new_from_dbic($resources_rs);
100
    return Koha::ERM::EHoldings::Resources->_new_from_dbic($resources_rs);
101
}
101
}
102
102
103
=head3 _detect_delimiter_and_quote
104
105
Identifies the delimiter and the quote character used in the KBART file and returns both.
106
107
=cut
108
109
sub _detect_delimiter_and_quote {
110
    my ($file) = @_;
111
    my $sample_lines = 5;    # Number of lines to sample for detection
112
113
    open my $fh, '<', \$file or die "Could not open '$file': $!";
114
115
    my @lines;
116
    while (<$fh>) {
117
        push @lines, $_;
118
        last if $. >= $sample_lines;
119
    }
120
    close $fh;
121
122
    my %delimiter_count;
123
    my %quote_count;
124
125
    foreach my $line (@lines) {
126
        foreach my $char ( ',', '\t', ';', '|' ) {
127
            my $count = () = $line =~ /\Q$char\E/g;
128
            $delimiter_count{$char} += $count if $count;
129
        }
130
        foreach my $char ( '"', "'" ) {
131
            my $count = () = $line =~ /\Q$char\E/g;
132
            $quote_count{$char} += $count if $count;
133
        }
134
    }
135
136
    # Guess the delimiter with the highest count
137
    my ($delimiter) = sort { $delimiter_count{$b} <=> $delimiter_count{$a} } keys %delimiter_count;
138
139
    # Guess the quote character with the highest count
140
    my ($quote) = sort { $quote_count{$b} <=> $quote_count{$a} } keys %quote_count;
141
142
    # Fallback to common defaults if nothing is detected
143
    $delimiter //= ',';
144
    $quote     //= '"';
145
146
    return ( $delimiter, $quote );
147
}
148
103
=head2 Internal methods
149
=head2 Internal methods
104
150
105
=head3 _type
151
=head3 _type
106
- 

Return to bug 36831