#!/usr/bin/perl -w my $RCS_Id = '$Id: csv2lsdb.pl,v 1.4 2003/09/13 09:50:19 jv Exp $ '; # Author : Johan Vromans # Created On : Tue Sep 1 15:59:04 2003 # Last Modified By: Johan Vromans # Last Modified On: Sat Sep 13 11:34:55 2003 # Update Count : 42 # Status : Unknown, Use with caution! ################ Common stuff ################ use strict; # Package name. my $my_package = 'Sciurix'; # Program name and version. my ($my_name, $my_version) = $RCS_Id =~ /: (.+).pl,v ([\d.]+)/; # Tack '*' if it is not checked in into RCS. $my_version .= '*' if length('$Locker: $ ') > 12; ################ Command line parameters ################ my $sep = ","; # separator my $notesep = 1; # change sep in note to newline my $verbose = 0; # more verbosity my $pdb = "-"; # output name # Development options (not shown with --help). my $debug = 0; # debugging my $trace = 0; # trace (show process) my $test = 0; # test mode. # Process command line options. app_options(); # Post-processing. my $sepp = quotemeta($sep); $trace |= ($debug || $test); ################ Presets ################ my $TMPDIR = $ENV{TMPDIR} || $ENV{TEMP} || '/usr/tmp'; ################ The Process ################ use Palm::ListDB::Writer; sub wmsg { my $msg = "@_"; $msg =~ s/$/, $ARGV line $./; print STDERR ($msg); } $SIG{__WARN__} = \&wmsg; $SIG{__DIE__} = sub { &wmsg; exit(-1) }; # First record contains DBname,Label1,Label2,Cat1,Cat2,,,,,, my $a = <>; chomp($a); $a =~ s/\\$sepp/\n/g; my ($db,$l1,$l2,@a) = map { s/\n/$sep/g; $_ } split(/$sepp/, $a); die("my_name: Missing database name, $ARGV line $.\n") unless $db; die("my_name: Missing 1st label, $ARGV line $.\n") unless $l1; die("my_name: Missing 2nd label, $ARGV line $.\n") unless $l2; die("my_name: Missing categories, $ARGV line $.\n") unless @a; my $x = new Palm::ListDB::Writer $db, label1 => $l1, label2 => $l2, cat => [ @a ]; # Subsequent records contain Cat,Field1,Field2,Note my $tally = 0; while ( <> ) { chomp; my @a; s/\\$sepp/\0/g; @a = split(/$sepp/,$_,4); $a[3] =~ s/$sepp/\n/g if $notesep; for ( @a ) { s/\0/$sep/g; } $a[0] = "Unfiled" if $a[0] eq ""; $x->add(@a) && $tally++; } $x->write($pdb); if ( $verbose ) { my $ncat = $x->categories; print STDERR ($my_name, ": Created LSdb $pdb with ", $ncat, " categor", ($ncat != 1 ? "ies" : "y"), " and $tally record", ($tally != 1 ? "s" : ""), "\n"); } exit 0; ################ Subroutines ################ ################ Command Line Options ################ use Getopt::Long 2.33; # will enable help/version sub app_options { GetOptions(ident => \&app_ident, 'verbose|v' => \$verbose, # application specific options go here "tab" => sub { $sep = "\t" }, "pdb=s" => \$pdb, # development options test => \$test, trace => \$trace, debug => \$debug) or Getopt::Long::HelpMessage(2); } sub app_ident { print STDOUT ("This is $my_package [$my_name $my_version]\n"); } __END__ =head1 NAME csv2lsdb - Generate a List database from a comma separated values (CSV) file =head1 SYNOPSIS csv2lsdb [options] [file ...] Options: --pdb=XXX the file to write the database to --tab use TAB characters for separators --[no]sepnote replace separators in note field by newlines --ident show identification --help brief help message --verbose verbose information =head1 OPTIONS =over 8 =item B<--pdb> I The name of the file to write the database to. If omitted, the database it written to standard output. =item B<--tab> The fields are separated by tabs instead of comma's. =item B<-->[B]B If enabled (default), separator characters that occur in the note field are translated into newlines. =item B<--verbose> More verbose information. =item B<--version> Print a version identification to standard output and exits. =item B<--help> Print a brief help message to standard output and exits. =item B<--ident> Prints a program identification. =item I Input file(s) with data. =back =head1 DESCRIPTION B will read the given input file(s) and generate a Palm List database from the data. The first line of the input must contain Database,Label1,Label2,Cat1,Cat2,,,,,, Subsequent lines contain Category,Field1,Field2,Note data Note that additional separators in the note data are usually translated into newlines. Use the B<--nosepnote> option to suppress this. =head1 SEE ALSO L. =head1 AUTHOR Johan Vromans =head1 COPYRIGHT This programs is Copyright 2003, Squirrel Consultancy. This program is free software; you can redistribute it and/or modify it under the terms of the Perl Artistic License or 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. =cut