source: main/trunk/model-sites-dev/heritage-nz/collect/reports-2019/perllib/plugins/CSVFieldSeparator.pm@ 32812

Last change on this file since 32812 was 32812, checked in by davidb, 5 years ago

Refactored to use inheritance

File size: 2.9 KB
Line 
1##########################################################################
2#
3# CSVFieldSeparator -- helper plugin that 'auto' works out what the
4# comma-separated field character is
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 2019 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28package CSVFieldSeparator;
29
30use PrintInfo;
31
32use strict;
33no strict 'refs'; # make an exception so we can use variables as filehandles
34
35BEGIN {
36 @CSVFieldSeparator::ISA = ('PrintInfo');
37}
38
39my $arguments =
40 [
41 { 'name' => "separate_char",
42 'desc' => "{CSVFieldSeparator.separate_char}",
43 'type' => "string",
44 'deft' => "auto",
45 'reqd' => "no" }
46 ];
47
48my $options = { 'name' => "CSVFieldSeparator",
49 'desc' => "{CSVFieldSeparator.desc}",
50 'abstract' => "yes",
51 'inherits' => "yes",
52 'args' => $arguments };
53
54sub new {
55 my ($class) = shift (@_);
56 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
57 push(@$pluginlist, $class);
58
59 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
60 push(@{$hashArgOptLists->{"OptList"}},$options);
61
62 my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, 1);
63
64 return bless $self, $class;
65
66}
67
68
69sub resolve_auto
70{
71 my ($self) = shift @_;
72 my ($line,$plugin_name,$outhandle,$verbosity) = @_;
73
74
75 # count number of char matches for common separates such as ',' ';' '\t' and '|'
76 my $comma_count = () = ($line =~ m/,/g);
77 my $separate_char = ",";
78 my $max_count = $comma_count;
79
80 my $semicolon_count = () = ($line =~ m/\;/g);
81 if ($semicolon_count > $max_count) {
82 $separate_char = ";";
83 $max_count = $semicolon_count;
84 }
85
86 my $tab_count = () = ($line =~ m/\t/g);
87 if ($tab_count > $max_count) {
88 $separate_char = "\t";
89 $max_count = $tab_count;
90 }
91
92 my $pipe_count = () = ($line =~ m/\|/g);
93 if ($pipe_count > $max_count) {
94 $separate_char = "|";
95 $max_count = $pipe_count;
96 }
97
98 if ($outhandle) {
99 print $outhandle "$plugin_name: Auto selecting '$separate_char' as -separate_char\n" if ($verbosity) > 1;
100 }
101
102 return $separate_char;
103}
104
1051;
Note: See TracBrowser for help on using the repository browser.