| 1 | package IOLayer::Base64; | ||||||||||
| 2 | |||||||||||
| 3 | # Make sure we do things by the book | ||||||||||
| 4 | # Set the version info | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | $IOLayer::Base64::VERSION = 0.03; | |||||||||
| 8 | |||||||||||
| 9 | # Make sure the encoding/decoding stuff is available | ||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use MIME::Base64 (); # no need to pollute this namespace | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | # Set the default setting for the end of line character | ||||||||||
| 14 | |||||||||||
| 15 | 1 | my $eol = "\n"; | |||||||||
| 16 | |||||||||||
| 17 | #----------------------------------------------------------------------- | ||||||||||
| 18 | |||||||||||
| 19 | # Methods for settings that will be used by the objects | ||||||||||
| 20 | |||||||||||
| 21 | #----------------------------------------------------------------------- | ||||||||||
| 22 | |||||||||||
| 23 | # IN: 1 class (ignored) | ||||||||||
| 24 | # 2 new setting for eol (default: no change) | ||||||||||
| 25 | # OUT: 1 current setting for eol | ||||||||||
| 26 | |||||||||||
| 27 | sub eol { | ||||||||||
| 28 | |||||||||||
| 29 | # Lose the class | ||||||||||
| 30 | # Set the new value if one specified | ||||||||||
| 31 | # Return whatever we have now | ||||||||||
| 32 | |||||||||||
| 33 | 3 | ▶ | shift; | ||||||||
|
Subroutine
eol: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 34 | 3 | ▶ | $eol = shift if @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 3 | $eol; | |||||||||
| 36 | } #eol | ||||||||||
| 37 | |||||||||||
| 38 | #----------------------------------------------------------------------- | ||||||||||
| 39 | |||||||||||
| 40 | # Methods for the actual layer implementation | ||||||||||
| 41 | |||||||||||
| 42 | #----------------------------------------------------------------------- | ||||||||||
| 43 | # IN: 1 class | ||||||||||
| 44 | # 2 mode string (ignored) | ||||||||||
| 45 | # 3 file handle of PerlIO layer below (ignored) | ||||||||||
| 46 | # OUT: 1 blessed object | ||||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | sub PUSHED { bless ['',$eol],$_[0] } #PUSHED | ||||||||
|
Subroutine
PUSHED: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 49 | |||||||||||
| 50 | #----------------------------------------------------------------------- | ||||||||||
| 51 | # IN: 1 instantiated object (ignored) | ||||||||||
| 52 | # 2 handle to read from | ||||||||||
| 53 | # OUT: 1 decoded string | ||||||||||
| 54 | |||||||||||
| 55 | sub FILL { | ||||||||||
| 56 | |||||||||||
| 57 | # Make sure we slurp everything we can in one go | ||||||||||
| 58 | # Read the line from the handle | ||||||||||
| 59 | # Decode if there is something decode and return result or signal eof | ||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | local $/ = undef; | ||||||||
|
Subroutine
FILL: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 62 | 0 | my $line = readline( $_[1] ); | |||||||||
| 63 | 0 | ▶ | (defined $line) ? MIME::Base64::decode_base64( $line ) : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | } #FILL | ||||||||||
| 65 | |||||||||||
| 66 | #----------------------------------------------------------------------- | ||||||||||
| 67 | # IN: 1 instantiated object (reference to buffer) | ||||||||||
| 68 | # 2 buffer to be written | ||||||||||
| 69 | # 3 handle to write to (ignored) | ||||||||||
| 70 | # OUT: 1 number of bytes "written" | ||||||||||
| 71 | |||||||||||
| 72 | sub WRITE { | ||||||||||
| 73 | |||||||||||
| 74 | # Add to the buffer (encoding will take place on FLUSH) | ||||||||||
| 75 | # Return indicating we read the entire buffer | ||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | $_[0]->[0] .= $_[1]; | ||||||||
|
Subroutine
WRITE: not called CC=1 SCAR=6.9
| |||||||||||
| 78 | 0 | length( $_[1] ); | |||||||||
| 79 | } #WRITE | ||||||||||
| 80 | |||||||||||
| 81 | #----------------------------------------------------------------------- | ||||||||||
| 82 | # IN: 1 instantiated object (reference to buffer) | ||||||||||
| 83 | # 2 handle to write to | ||||||||||
| 84 | # OUT: 1 flag indicating error | ||||||||||
| 85 | |||||||||||
| 86 | sub FLUSH { | ||||||||||
| 87 | |||||||||||
| 88 | # If there is something in the buffer to be handled | ||||||||||
| 89 | # Write out what we have in the buffer, encoding it on the fly | ||||||||||
| 90 | # Reset the buffer | ||||||||||
| 91 | # Return indicating success | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | if ($_[0]->[0]) { | ||||||||
|
Subroutine
FLUSH: not called CC=3 SCAR=24.8
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 94 | 0 | ▶ | print {$_[1]} MIME::Base64::encode_base64( $_[0]->[0],$_[0]->[1] ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | or return -1; | ||||||||||
| 96 | 0 | $_[0]->[0] = ''; | |||||||||
| 97 | } | ||||||||||
| 98 | 0 | 0; | |||||||||
| 99 | } #FLUSH | ||||||||||
| 100 | |||||||||||
| 101 | # Satisfy -require- | ||||||||||
| 102 | |||||||||||
| 103 | 1 | 1; | |||||||||
| 104 | |||||||||||
| 105 | __END__ | ||||||||||