This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
/
Import.pm
146 lines (124 loc) · 3.93 KB
/
Import.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
138
139
140
141
142
143
144
145
146
# Table/Import.pm: import a table
#
# $Id: Import.pm,v 1.10 1998/01/31 05:27:12 mike Exp $
#
# Copyright 1995 by Andrew M. Wilcox <awilcox@world.std.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
package Vend::Table::Import;
$VERSION = substr(q$Revision: 1.10 $, 10);
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(import_csv import_quoted import_ascii_delimited);
use strict;
use Vend::Table::Quoted qw(read_quoted_fields);
use Vend::Util;
sub import_csv {
my ($source, $create) = @_;
die "The source file '$source' does not exist\n" unless -e $source;
open(Vend::Table::Import::IN, "+<$source")
or die "Can't open '$source' for reading: $!\n";
lockfile(\*Vend::Table::Import::IN, 1, 1) or die "lock\n";
my @columns = read_quoted_fields(\*IN);
die "$source is empty\n" unless @columns;
$Vend::FIRST_COLUMN_NAME = shift @columns;
my $out = &$create(@columns);
my (@fields,$key);
while (@fields = read_quoted_fields(\*IN)) {
$out->set_row(@fields);
}
unlockfile(\*Vend::Table::Import::IN) or die "unlock\n";
close(Vend::Table::Import::IN);
return $out;
}
sub import_quoted { return import_csv(@_) }
sub import_ascii_delimited {
my ($infile, $delimiter, $create) = @_;
my ($options, $format);
if(ref $delimiter) {
$options = $delimiter;
$delimiter = quotemeta($options->{'delimiter'});
$format = uc ($options->{CONTINUE} || 'NONE');
}
else {
$delimiter = quotemeta($delimiter);
$format = 'STANDARD';
}
open(Vend::Table::Import::IN, "+<$infile")
or die "Couldn't open '$infile': $!\n";
lockfile(\*Vend::Table::Import::IN, 1, 1) or die "lock\n";
my $field_names = <IN>;
chomp $field_names;
$field_names =~ s/\s+$//;
my @field_names = split(/$delimiter/, $field_names);
my $field_count = scalar @field_names;
# HACK!
$Vend::FIRST_COLUMN_NAME = shift @field_names;
my $out = &$create(@field_names);
my $fields;
my (@fields, $key);
my @addl;
my %format = (
NONE => <<EndOfRoutine,
while (<IN>) {
chomp;
\$fields = \@fields = split(/$delimiter/, \$_, 99999);
push (\@fields, '') until \$fields++ >= $field_count;
\$key = shift \@fields;
\$out->set_row(\$key, \@fields);
}
EndOfRoutine
UNIX => <<EndOfRoutine,
while (<IN>) {
chomp;
if(s/\\\\\$//) {
\$_ .= <IN>;
redo;
}
\$fields = \@fields = split(/$delimiter/, \$_, 99999);
push (\@fields, '') until \$fields++ >= $field_count;
\$key = shift \@fields;
\$out->set_row(\$key, \@fields);
}
EndOfRoutine
DITTO => <<EndOfRoutine,
while (<IN>) {
chomp;
if(/^$delimiter/) {
\$fields = \@addl = split /$delimiter/, \$_, 99999;
shift \@addl;
my \$i;
for(\$i = 0; \$i < \@addl; \$i++) {
\$fields[\$i] .= "\n\$addl[\$i]"
if \$addl[\$i] ne '';
}
}
else {
\$fields = \@fields = split(/$delimiter/, \$_, 99999);
push (\@fields, '') until \$fields++ >= $field_count;
\$key = shift \@fields;
}
\$out->set_row(\$key, \@fields);
}
EndOfRoutine
);
eval $format{$format};
die $@ if $@;
unlockfile(\*Vend::Table::Import::IN) or die "unlock\n";
close(Vend::Table::Import::IN);
return $out;
}
sub version { $Vend::Table::Import::VERSION }
1;