stuart: devel/kolab/Kolab Util.pm,1.2,1.3

cvs at intevation.de cvs at intevation.de
Wed May 12 10:46:07 CEST 2004


Author: stuart

Update of /kolabrepository/devel/kolab/Kolab
In directory doto:/tmp/cvs-serv8270

Modified Files:
	Util.pm 
Log Message:
Updated Util module for the new template code


Index: Util.pm
===================================================================
RCS file: /kolabrepository/devel/kolab/Kolab/Util.pm,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Util.pm	11 May 2004 15:09:29 -0000	1.2
+++ Util.pm	12 May 2004 08:46:04 -0000	1.3
@@ -1,23 +1,27 @@
 package Kolab::Util;
 
-##
-##  Copyright (c) 2003  Code Fusion cc
-##
-##    Writen by Stuart Bingë  <s.binge at codefusion.co.za>
-##
-##  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, 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 can view the  GNU General Public License, online, at the GNU
-##  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
-##
+##########################################################################
+##                                                                      ##
+##  The Kolab Groupware Server                                          ##
+##                                                                      ##
+##    See the file CONTRIBUTORS that was distributed with this program  ##
+##  for copyright information.                                          ##
+##                                                                      ##
+##    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 can view the GNU  General Public License, online, at the GNU  ##
+##  project's  web  site;  see <http://www.gnu.org/licenses/gpl.html>.  ##
+##  The full text of the license is also included in the file COPYING.  ##
+##                                                                      ##
+##########################################################################
 
 use 5.008;
 use strict;
@@ -39,83 +43,158 @@
 
 our @EXPORT = qw(
     &trim
+    &readHash
+    &readArray
+    &readMeta
+    &lerpVar
+    &writeHash
+    &writeArray
     &ldapDateToEpoch
-    &readConfig
-    &readList
 );
 
 our $VERSION = sprintf('%d.%02d', q$Revision$ =~ /(\d+)\.(\d+)/);
 
+# What delimits the template meta data
+sub KOLAB_META_END() { return "__END_KOLAB_META__"; }
+
 sub trim
 {
-    my $string = shift;
+    my $string = shift || '';
+    $string =~ s/^\s+//g;
+    $string =~ s/\s+$//g;
+    chomp $string;
+    return $string;
+}
 
-    if (defined $string) {
-        $string =~ s/^\s+//g;
-        $string =~ s/\s+$//g;
-        chomp $string;
+sub readHash
+{
+    my $file = shift;
+    my $ref = shift;
+    my %hash;
+    %hash = %$ref if ref($ref) eq 'HASH';
+    return %hash if !$file;
+
+    my $fd;
+    return %hash if !($fd = IO::File->new($file, "r"));
+
+    my $key;
+    while (<$fd>) {
+        if (/^([^:#]+):+([^#]*)/) {
+            $key = trim($1);
+            next if $key eq "";
+            $hash{$key} = trim($2);
+        }
     }
 
-    return $string;
+    return %hash;
 }
 
-sub ldapDateToEpoch
+sub readArray
 {
-    my $ldapdate = shift;
+    my $file = shift;
+    my $ref = shift;
+    my @array;
+    @array = @$ref if ref($ref) eq 'ARRAY';
+    return @array if !$file;
 
-    (my $y, my $m, my $d, my $h, my $mi, my $se) = unpack('A4A2A2A2A2A2', $ldapdate);
+    my $fd;
+    return @array if !($fd = IO::File->new($file, "r"));
 
-    return timelocal($se, $mi, $h, $d, $m, $y);
+    my $value;
+    while (<$fd>) {
+        if (/^([^#]+)/) {
+            $value = trim($1);
+            next if $value eq "";
+            push @array, $value;
+        }
+    }
+
+    return @array;
 }
 
-sub readConfig
+sub readMeta
 {
+    my $fd = shift;
     my $ref = shift;
-    my (%cfg, $file);
+    my %hash;
+    %hash = %$ref if ref($ref) eq 'HASH';
+    return %hash if !$fd;
 
-    if (ref($ref) eq 'HASH') {
-        %cfg = %$ref;
-        $file = shift || 0;
-    } else {
-        $file = $ref;
+    my $key;
+    my $has_meta = 0;
+    while (<$fd>) {
+        if (trim($_) eq KOLAB_META_END) {
+            $has_meta = 1;
+            last;
+        }
+        if (/^([^:#]+):+([^#]*)/) {
+            $key = trim($1);
+            next if $key eq "";
+            $hash{$key} = trim($2);
+        }
     }
 
-    if (!$file) { return %cfg; }
+    return () if !$has_meta;
 
-    my $sep = shift || ':';
-    $sep = '\s' if ($sep eq ' ' || $sep eq '#');
+    return %hash;
+}
 
-    my $fd;
-    if (!($fd = IO::File->new($file, 'r'))) { return %cfg; }
+sub lerpVar
+{
+    my $var = shift;
 
-    foreach (<$fd>) {
-        if (/^([^$sep#]+)$sep+([^#]*)/) {
-            $cfg{trim($1)} = trim($2);
+    while ($var =~ /\$\{(\S+)\}/) {
+        if ($Kolab::config{$1}) {
+            $var =~ s/\$\{(\S+)\}/$Kolab::config{$1}/;
+        } else {
+            $var =~ s/\$\{\S+\}//;
         }
     }
 
-    return %cfg;
+    return $var;
 }
 
-sub readList
+sub writeHash
 {
-    my @list;
+    my $file = shift || return;
+    my $ref = shift;
+    my %hash;
+    if (ref($ref) eq 'HASH') {
+        %hash = %$ref;
+    }
 
-    my $file = shift || 0;
-    if (!$file) { return @list; }
+    my $fd;
+    return if !($fd = IO::File->new($file, "w"));
+
+    foreach my $key (sort keys %hash) {
+        print $fd "$key : $hash{$key}\n";
+    }
+}
+
+sub writeArray
+{
+    my $file = shift || return ();
+    my $ref = shift;
+    my @array;
+    if (ref($ref) eq 'ARRAY') {
+        @array = @$ref;
+    }
 
     my $fd;
-    if (!($fd = IO::File->new($file, 'r'))) { return @list; }
+    return if !($fd = IO::File->new($file, "r"));
 
-    foreach (<$fd>) {
-        if (/^([^#]+)/) {
-            my $temp = trim($1);
-            next if $temp eq '';
-            push(@list, ($temp));
-        }
+    foreach my $item (sort @array) {
+        print $fd "$item\n";
     }
+}
 
-    return @list;
+sub ldapDateToEpoch
+{
+    my $ldapdate = shift;
+
+    (my $y, my $m, my $d, my $h, my $mi, my $se) = unpack('A4A2A2A2A2A2', $ldapdate);
+
+    return timelocal($se, $mi, $h, $d, $m, $y);
 }
 
 1;





More information about the commits mailing list