Remove broken cctv support and test
[clive.git] / lib / clive / Util.pm
blob5375aabf58d7e599a7ecbfbd732831074a17c8a0
1 # -*- coding: ascii -*-
2 ###########################################################################
3 # clive, command line video extraction utility.
5 # Copyright 2009 Toni Gundogdu.
7 # This file is part of clive.
9 # clive is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU General Public License as published by the Free
11 # Software Foundation, either version 3 of the License, or (at your option)
12 # any later version.
14 # clive is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 # details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program. If not, see <http://www.gnu.org/licenses/>.
21 ###########################################################################
22 package clive::Util;
24 use warnings;
25 use strict;
27 eval("use Term::ReadKey");
28 my $TermReadKey = int( !$@ );
30 use clive::Error qw(CLIVE_REGEXP);
32 use constant DEFAULT_TERM_WIDTH => 80;
33 use constant MBDIV => 0x100000;
35 sub termWidth {
36 return DEFAULT_TERM_WIDTH
37 unless $TermReadKey;
38 my ($width) = Term::ReadKey::GetTerminalSize();
39 return $width;
42 sub toMB {
43 my $bytes = shift;
44 return $bytes / MBDIV;
47 sub fileExists {
48 my ($path) = @_;
49 return -s $path || 0;
52 sub timeToStr {
53 my $secs = shift;
55 my $str;
56 if ( $secs < 100 ) {
57 $str = sprintf( "%02ds", $secs );
59 elsif ( $secs < 100 * 60 ) {
60 $str = sprintf( "%02dm%02ds", $secs / 60, $secs % 60 );
62 elsif ( $secs < 48 * 3600 ) {
63 $str = sprintf( "%02dh%02dm", $secs / 3600, ( $secs / 60 ) % 60 );
65 elsif ( $secs < 100 * 86400 ) {
66 $str = sprintf( "%dd%02dh", $secs / 86400, ( $secs / 3600 ) % 60 );
68 else {
69 $str = sprintf( "%dd", $secs / 86400 );
71 return $str;
74 sub toUnits {
75 my $rate = shift;
76 my $units = "K/s";
77 if ($rate >= 1024*1024*1024) {
78 $rate /= 1024*1024*1024;
79 $units = "G/s";
81 elsif ($rate >= 1024*1024) {
82 $rate /= 1024*1024;
83 $units = "M/s";
85 else
86 { $rate /= 1024; }
87 ($units, $rate);
90 sub matchRegExps {
91 my ( $regexps, $results, $content ) = @_;
92 while ( my ( $key, $re ) = each( %{$regexps} ) ) {
93 $$results->{$key} = $1 if $$content =~ /$re/;
94 if ( !$$results->{$key} ) {
95 clive::Log->instance->err( CLIVE_REGEXP, "no match: `$re'" );
96 return (1);
99 return (0);
102 sub toDomain {
103 my $uri = shift;
105 my ( $scheme, $authority, $path, $query, $fragment )
106 = $uri
107 =~ m{(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?}o;
109 return split( /\./, $authority );
112 sub prompt {
113 print shift;
114 chomp( my $ln = <STDIN> );
115 return $ln;
118 sub fromEntities {
119 my %conv_table = (
120 "&quot;" => "\"",
121 "&#34;" => "\"",
122 "&amp;" => "&",
123 "&#38;" => "&",
124 "&apos;" => "'",
125 "&#39;" => "'",
126 "&lt;" => "<",
127 "&#60;" => "<",
128 "&gt;" => ">",
129 "&#62;" => ">",
131 my $str = shift;
132 while ( my ( $from, $to ) = each(%conv_table) ) {
133 $str =~ s/$from/$to/g;
135 return $str;
140 # While all the women came and went.