[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-unattended/var/se3/unattended/install/lib/ -> conf-csv.pl (source)

   1  # unattend.csv
   2  # ============
   3  # "Lookup","Property","Value"
   4  # "0003473ECA3C","ComputerName","Test Computer"
   5  #
   6  # The lookup_value function will query the above file when
   7  # passed a Lookup, Property pair and return Value.  If Value
   8  # is 'Empty' then '' is returned.  All errors return undef.
   9  
  10  use strict;
  11  use warnings;
  12  
  13  # Must be named CONFIG
  14  package CONFIG;
  15  use Carp;
  16  
  17  my $config_hash;
  18  
  19  # Parse a single line from a CSV file and return an array of its
  20  # items.  Unlikely to be called externally.
  21  sub parse_line ($) {
  22      my ($line) = @_;
  23                                                                                                                             
  24      my @ret;
  25                                                                                                                             
  26      # Clobber line ending, if any.
  27      chomp $line;
  28      $line =~ s/\r\z//;
  29                                                                                                                             
  30      # Hack to make every field start with comma delimiter
  31      $line = ",$line";
  32                                                                                                                             
  33      while ($line ne '') {
  34          if ($line =~ /^,\"((?:\"\"|[^\"])*)\"((?:,|\z).*)/) {
  35              my ($val, $rest) = ($1, $2);
  36              # Replace double-quote with single-quote
  37              $val =~ s/\"\"/\"/g;
  38              push @ret, $val;
  39              $line = $rest;
  40          }
  41          elsif ($line =~ /^,([^,]*)((?:,|\z).*)/) {
  42              push @ret, $1;
  43              $line = $2;
  44          }
  45          else {
  46              die "Unparsable line:\n$line";
  47          }
  48      }
  49                                                                                                                             
  50      return @ret;
  51  }
  52  
  53  # Setup connection to database
  54  sub setup ($$$) {
  55      my $self = shift;
  56      my $filename = shift;
  57      my $dummy1 = shift;
  58      my $dummy2 = shift;
  59  
  60      open CSV_FILE, $filename
  61          or croak "Unable to open $filename for reading: $^E";
  62  
  63      # Read first line to get field names.
  64      my $first_line = <CSV_FILE>;
  65      my @field_names = parse_line ($first_line);
  66  
  67      # Check for duplicate field names.
  68      my %names;
  69      foreach my $name (@field_names) {
  70          (exists $names{$name})
  71              and die "Duplicate field name \"$name\" in $filename";
  72          $names{$name} = undef;
  73      }
  74                                                                                                                             
  75      my $num_fields = scalar @field_names;
  76      while (my $line = <CSV_FILE>) {
  77          next if $line =~ /^\s*$/;
  78          my @fields = parse_line ($line);
  79          $num_fields == scalar @fields
  80              or die "Wrong number of items (expected $num_fields):\n$line\n ";
  81          (exists $config_hash->{$fields[0]}->{$fields[1]})
  82              and die "Duplicate key found: $fields[0]/$fields[1] in $filename";
  83          $config_hash->{$fields[0]}->{$fields[1]} = $fields[2];
  84      }
  85  
  86      close CSV_FILE
  87          or croak "Unable to close $filename: $^E";
  88  }
  89  
  90  # Query database looking up value for Lookup, Property pair
  91  sub lookup_value ($$) {
  92      my $self = shift;
  93      my $lookup = shift;
  94      my $property = shift;
  95  
  96      return undef
  97          unless defined $lookup && defined $property;
  98      (exists $config_hash->{$lookup}->{$property})
  99          or return undef;
 100      my $value = $config_hash->{$lookup}->{$property}
 101          or return undef;
 102      $value =~ /\S/
 103          or return undef;
 104      print "Found $property for $lookup: $value\n";
 105      $value =~ /Empty/i
 106          and return '';
 107      return $value;
 108  }
 109  
 110  # Make this file evaluate to "true".
 111  1;


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1