summaryrefslogtreecommitdiff
path: root/lib/Tools.pm
diff options
context:
space:
mode:
authorAndreas Brachold <vdr07@deltab.de>2008-04-07 16:11:08 +0000
committerAndreas Brachold <vdr07@deltab.de>2008-04-07 16:11:08 +0000
commit4661c74cdb300f64e3058cbc384148d76c703d56 (patch)
treee9415a41777b589a44bc2bac854e5694f98b925c /lib/Tools.pm
parent00bd3aff2a832d6b7379fc02ccbd5c884acc6d7c (diff)
downloadxxv-4661c74cdb300f64e3058cbc384148d76c703d56.tar.gz
xxv-4661c74cdb300f64e3058cbc384148d76c703d56.tar.bz2
* Remove String::Escape, use now regular expression to remove unwanted part of unicode as last character
* Reformat message 'Couldn't load perl module...' * different ways to load binary/text files * RECORDS: Prepare list of commands at perl code and not into template code * RECORDS: Load images less strictness * Reorder locale routines
Diffstat (limited to 'lib/Tools.pm')
-rw-r--r--lib/Tools.pm48
1 files changed, 28 insertions, 20 deletions
diff --git a/lib/Tools.pm b/lib/Tools.pm
index f7d1864..e4a1b5d 100644
--- a/lib/Tools.pm
+++ b/lib/Tools.pm
@@ -352,12 +352,16 @@ or use the script contrib/update-xxv to upgrade the database!
sub load_file {
#--------------------------------------------------------
my $file = shift || return error('No file defined!');
+ my $binmode = shift || 'text';
- lg sprintf('Load file "%s"',
- $file,
- );
+ lg sprintf('Load file "%s" (%s)',$file, $binmode);
+
+ my $mode = '<';
+# if($binmode ne 'binary') {
+# $mode .= ':utf8' if($CHARSET && $CHARSET eq 'UTF-8');
+# }
- my $fh = IO::File->new("< $file")
+ my $fh = IO::File->new($file,$mode)
or return error(sprintf("Couldn't open %s : %s!",$file,$!));
my $data;
while ( defined (my $l = <$fh>) ) {
@@ -370,22 +374,26 @@ sub load_file {
#--------------------------------------------------------
sub save_file {
#--------------------------------------------------------
- my ($file, $data) = @_;
- return unless($file);
+ my $file = shift || return error('No file defined!');
+ my $data = shift || '';
+ my $binmode = shift || 'text';
- $data =~ s/\r\n/\n/sig;
+ return unless($file);
- lg sprintf('Save file %s(%s)',
- $file,
- convert(length($data))
- );
+ lg sprintf('Save file %s(%s)',$file,convert(length($data)));
+ my $mode = '>';
- my $fhi = new IO::File("> $file")
- or return error(sprintf("Couldn't write %s : %s!",$file,$!));
- print $fhi $data;
- $fhi->close;
+ if($binmode ne 'binary') {
+ $data =~ s/\r\n/\n/sig;
+# $mode .= ':utf8' if($CHARSET && $CHARSET eq 'UTF-8');
+ }
+
+ my $fh = IO::File->new($file,$mode)
+ or return error(sprintf("Couldn't write %s : %s!",$file,$!));
+ print $fh $data;
+ $fh->close;
- return $file
+ return $file
}
@@ -629,7 +637,9 @@ sub entities {
$s =~ s/>/&gt;/g;
$s =~ s/</&lt;/g;
$s =~ s/\"/&quot;/g;
- if($CHARSET ne 'UTF-8') {
+ if($CHARSET eq 'UTF-8') {
+ $s =~ s/(\~)/sprintf("&#x%02x;",ord($1))/eg;
+ } else {
$s =~ s/([^a-zA-Z0-9&%;:,\.\!\?\(\)\_\|\'\r\n ])/sprintf("&#x%02x;",ord($1))/eg;
}
$s =~ s/\r\n/<br \/>/g;
@@ -642,9 +652,7 @@ sub reentities {
# ------------------
my $s = shift || return '';
- if($CHARSET ne 'UTF-8') {
- $s =~ s/\&\#x([a-fA-F0-9][a-fA-F0-9])\;/pack("C", hex($1))/eg;
- }
+ $s =~ s/\&\#x([a-fA-F0-9][a-fA-F0-9])\;/pack("C", hex($1))/eg;
$s =~ s/&amp;/&/g;
$s =~ s/&gt;/>/g;
$s =~ s/&lt;/</g;