IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 5248


Ignore:
Timestamp:
Oct 7, 2005, 3:20:12 PM (21 years ago)
Author:
jhoblitt
Message:

fwv

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/PS-IPP-PSFTP/lib/PS/IPP/PSFTP/Parser.pm

    r5233 r5248  
    11# Copyright (C) 2005  Joshua Hoblitt
    22#
    3 # $Id: Parser.pm,v 1.1 2005-10-07 00:12:32 jhoblitt Exp $
     3# $Id: Parser.pm,v 1.2 2005-10-08 01:20:12 jhoblitt Exp $
    44
    55package PS::IPP::PSFTP::Parser;
    66
    77use strict;
     8
     9use Carp qw( carp );
     10
     11use vars qw( @FIELDS @REQUIRED_FIELDS );
     12
     13@FIELDS = qw( date time id type attributes size md5 url );
     14@REQUIRED_FIELDS = qw( date time id type size md5 url );
    815
    916sub new
     
    1623}
    1724
     25sub parse
     26{
     27    my ($self, $doc) = @_;
     28
     29    # pack data into an HoH
     30    my @data;
     31    foreach my $line (split /\n/, $doc) {
     32        # convert string to an array of lines
     33        my %record;
     34        @record{@FIELDS} = map { $_ ? $_ : undef } split /\|/, $line;
     35
     36        # check for the proper number of fields (null or not)
     37        unless (scalar keys %record == scalar @FIELDS) {
     38            carp "record: $line does not have the proper number of fields\n";
     39            return undef;
     40        }
     41
     42        # check that all required fields are defined
     43        foreach my $key (@REQUIRED_FIELDS) {
     44           unless (defined $record{$key}) {
     45                carp "record: $line is missing required field: $key\n";
     46                return undef;
     47           }
     48        }
     49
     50        # convert attributes into a hash
     51        if (defined $record{attributes}) {
     52            my @pairs = split /:/, $record{attributes};
     53           
     54            my %attr;
     55            foreach my $pair (@pairs) {
     56                my ($key, $value) = split /=/, $pair;
     57
     58                unless (defined $key) {
     59                    carp "record: $line has an attribute: $pair "
     60                       . " with no key";
     61                    return undef;
     62                }
     63
     64                unless (defined $value) {
     65                    carp "record: $line has an attribute: $pair"
     66                       . " with no value";
     67                    return undef;
     68                }
     69
     70                # pack pairs into a hash
     71                $attr{$key} = $value;
     72            }
     73       
     74            # replace attributes string with a hashref
     75            if (%attr) {
     76                $record{attributes} = \%attr;
     77            } else {
     78                delete $record{attributes};
     79            }
     80        }
     81
     82        # pack date & time into stamp
     83        $record{stamp} = $record{date} . "T" . $record{time};
     84        delete $record{date};
     85        delete $record{time};
     86
     87        my $obj = bless \%record, "PS::IPP::PSFTP::Record";
     88
     89        # pack as a FIFO
     90        unshift @data, $obj;
     91    }
     92
     93    return @data ? \@data : undef;
     94}
     95
     96package PS::IPP::PSFTP::Record;
     97
     98use base qw( Class::Accessor::Fast );
     99
     100use vars qw( @FIELDS );
     101
     102@FIELDS = qw( stamp id type attributes size md5 url );
     103
     104__PACKAGE__->mk_accessors( @FIELDS );
     105
    181061;
    19107
Note: See TracChangeset for help on using the changeset viewer.