Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | ##---------------------------------------------------------------------------## |
2 | ## File: | |
3 | ## $Id: ewhutil.pl,v 2.9 2002/09/25 03:51:13 ehood Exp $ | |
4 | ## Author: | |
5 | ## Earl Hood mhonarc@mhonarc.org | |
6 | ## Description: | |
7 | ## Generic utility routines | |
8 | ##---------------------------------------------------------------------------## | |
9 | ## Copyright (C) 1996-2001 Earl Hood, mhonarc@mhonarc.org | |
10 | ## | |
11 | ## This program is free software; you can redistribute it and/or modify | |
12 | ## it under the terms of the GNU General Public License as published by | |
13 | ## the Free Software Foundation; either version 2 of the License, or | |
14 | ## (at your option) any later version. | |
15 | ## | |
16 | ## This program is distributed in the hope that it will be useful, | |
17 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ## GNU General Public License for more details. | |
20 | ## | |
21 | ## You should have received a copy of the GNU General Public License | |
22 | ## along with this program; if not, write to the Free Software | |
23 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
24 | ## 02111-1307, USA | |
25 | ##---------------------------------------------------------------------------## | |
26 | ||
27 | package mhonarc; | |
28 | ||
29 | my %HTMLSpecials = ( | |
30 | '"' => '"', | |
31 | '&' => '&', | |
32 | '<' => '<', | |
33 | '>' => '>', | |
34 | ); | |
35 | ||
36 | ##--------------------------------------------------------------------------- | |
37 | ## Remove duplicates in an array. | |
38 | ## Returns list with duplicates removed. | |
39 | ## | |
40 | sub remove_dups { | |
41 | my $a = shift; | |
42 | return () unless scalar(@$a); | |
43 | my %dup = (); | |
44 | grep(!$dup{$_}++, @$a); | |
45 | } | |
46 | ||
47 | ##--------------------------------------------------------------------------- | |
48 | ## "Entify" special characters | |
49 | ||
50 | sub htmlize { # Older name | |
51 | return '' unless scalar(@_) && defined($_[0]); | |
52 | my($txt) = $_[0]; | |
53 | $txt =~ s/(["&<>])/$HTMLSpecials{$1}/g; | |
54 | $txt; | |
55 | } | |
56 | ||
57 | sub entify { # Newer name | |
58 | return '' unless scalar(@_) && defined($_[0]); | |
59 | my($txt) = $_[0]; | |
60 | $txt =~ s/(["&<>])/$HTMLSpecials{$1}/g; | |
61 | $txt; | |
62 | } | |
63 | ||
64 | ## commentize entifies certain characters to avoid problems when a | |
65 | ## string will be included in a comment declaration | |
66 | ||
67 | sub commentize { | |
68 | my($txt) = $_[0]; | |
69 | $txt =~ s/([\-&])/'&#'.unpack('C',$1).';'/ge; | |
70 | $txt; | |
71 | } | |
72 | ||
73 | sub uncommentize { | |
74 | my($txt) = $_[0]; | |
75 | $txt =~ s/&#(\d+);/pack("C",$1)/ge; | |
76 | $txt; | |
77 | } | |
78 | ||
79 | ##--------------------------------------------------------------------------- | |
80 | ## Copy a file. | |
81 | ## | |
82 | sub cp { | |
83 | my($src, $dst) = @_; | |
84 | open(SRC, $src) || die("ERROR: Unable to open $src\n"); | |
85 | open(DST, "> $dst") || die("ERROR: Unable to create $dst\n"); | |
86 | print DST <SRC>; | |
87 | close(SRC); | |
88 | close(DST); | |
89 | } | |
90 | ||
91 | ##--------------------------------------------------------------------------- | |
92 | ## Translate html string back to regular string | |
93 | ## | |
94 | sub dehtmlize { | |
95 | my($str) = shift; | |
96 | $str =~ s/\</</g; | |
97 | $str =~ s/\>/>/g; | |
98 | $str =~ s/\&/\&/g; | |
99 | $str; | |
100 | } | |
101 | ||
102 | ##--------------------------------------------------------------------------- | |
103 | ## Escape special characters in string for URL use. | |
104 | ## | |
105 | sub urlize { | |
106 | my($url) = shift || ""; | |
107 | $url =~ s/([^\w@\.\-])/sprintf("%%%X",unpack("C",$1))/ge; | |
108 | $url; | |
109 | } | |
110 | ||
111 | ##---------------------------------------------------------------------------## | |
112 | ## Perform a "modified" rot13 on a string. This version includes | |
113 | ## the '@' character so addresses can be munged a little better. | |
114 | ## | |
115 | sub mrot13 { | |
116 | my $str = shift; | |
117 | $str =~ tr/@A-Z[a-z/N-Z[@A-Mn-za-m/; | |
118 | $str; | |
119 | } | |
120 | ||
121 | ##---------------------------------------------------------------------------## | |
122 | 1; |