"Fossies" - the Fresh Open Source Software Archive

Member "absence-v2.1/cgi-bin/old/AbsenceTypes.pm" (21 Aug 2008, 2818 Bytes) of package /linux/www/web-absence-2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbsenceTypes.pm" see the Fossies "Dox" file reference documentation.

    1 package AbsenceTypes;
    2 
    3 use FileHandle;
    4 
    5 use AbsenceConfig;
    6 
    7 my $TYPE_FILE   = AbsenceConfig::fetch('type_file');
    8 my %DB          = ();
    9 
   10 my $INIT_RUN    = 0;
   11 my $DEFAULT     = '';
   12 my $VERSION     = '1.2';
   13 
   14 sub readFile
   15 {
   16     my $fh = FileHandle->new($TYPE_FILE);
   17     defined($fh) || die "open $TYPE_FILE for reading";
   18     my ($type, $color);
   19     while(<$fh>) {
   20         if (/^\s*#/) { next; }
   21         if (/^\s*$/) { next; }
   22         if (/^Type:\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*(#.*)?$/) {
   23             $type = $1;
   24             @cols = ($2, $3, $4);
   25             $color = [colcon(@cols)];   # rgb
   26             $DB{$type} = $color;
   27         } elsif (/^\s*Default: (\S+)\s*$/) {
   28             $DEFAULT = $1;
   29         }
   30     }
   31     $fh->close;
   32 
   33     if ($DEFAULT && !exists($DB{$DEFAULT})) {
   34         $DEFAULT = '';
   35     }
   36 
   37     $INIT_RUN = 1;
   38 }
   39 
   40 sub colcon
   41 {
   42     my (@list) = @_;
   43 
   44     my @new;
   45     foreach my $col (@list) {
   46         ($col =~ /^0x/i) ? push(@new, hex($col)) : push(@new, $col);
   47     }
   48 
   49     @new;
   50 }
   51 
   52 sub getTypes
   53 {
   54     if (!$INIT_RUN) { readFile(); }
   55     return keys(%DB);
   56 }
   57 
   58 sub getColor
   59 {
   60     my $type = shift;
   61 
   62     if (!$INIT_RUN) { readFile(); }
   63 
   64     if (!exists($DB{$type})) {
   65         return undef;
   66     }
   67 
   68     return $DB{$type};
   69 }
   70 
   71 sub getHash
   72 {
   73     if (!$INIT_RUN) { readFile(); }
   74     return \%DB;
   75 }
   76 
   77 sub getDefault
   78 {
   79     if ($DEFAULT) {
   80         return $DEFAULT;
   81     }
   82 
   83     return undef;
   84 }
   85 
   86 sub modTime
   87 {
   88     return (stat($TYPE_FILE))[9];
   89 }
   90 
   91 1;
   92 
   93 __END__
   94 
   95 =head1 NAME
   96 
   97 AbsenceTypes.pm - primitive interface absence types
   98 
   99 =head1 SYNOPSIS
  100 
  101   use AbsenceTypes;
  102 
  103   @types = AbsenceTypes::getTypes();
  104   $color_hash = AbsenceTypes::getColor('type-string');
  105   $href = AbsenceTypes::getHash();
  106   $default_type = AbsenceTypes::getDefault();
  107   $modification_time = AbsenceTypes::modTime();
  108 
  109 =head1 DESCRIPTION
  110 
  111 B<AbsenceTypes> provides a simple interface to a text file
  112 containing type absence types (i.e., 'busy', 'vacation', etc).
  113 The format of the file is:
  114 
  115   Type: <type-name1> Red Green Blue
  116   Type: <type-name2> Red Green Blue
  117   Type: <type-nameN> Red Green Blue
  118   Default: <type-name>
  119 
  120 for example:
  121 
  122   Type: Vacation 255 00 00
  123   Type: Customer 00 255 00
  124   Type: Busy 90 90 255
  125   Type: Other 255 165 0
  126   
  127   Default: Customer
  128 
  129 The <type-name> should not have any white-space in it. Red, Green,
  130 and Blue are the corresponding color components.
  131 Blank lines and lines beginning with hash ('#') are ignored.
  132 
  133 =head1 FUNCTIONS
  134 
  135 =over 4
  136 
  137 =item B<getTypes()>
  138 
  139 returns a list of type-names.
  140 
  141 =item B<getColor()>
  142 
  143 returns a reference to a list containing the red, green, and blue components.
  144 
  145 =item B<getHash()>
  146 
  147 returns a reference to a hash of the entire absence-type data-structure.
  148 pretty much a hack.
  149 
  150 =item B<getDefault()>
  151 
  152 returns the default type-name.
  153 
  154 =item B<modTime()>
  155 
  156 returns the modification time of the 'database' file.  This allows scripts
  157 to determine if images generated based on outdated information need to
  158 be re-generated.
  159 
  160 =head1 AUTHOR
  161 
  162 Robert Urban <urban@tru64.org>
  163 
  164 Copyright (C) Robert Urban 2003
  165 
  166 =cut