Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/helpers/hexdump.rb,
lib/helpers/hexdump.rb,
lib/id3/string_extensions.rb,
lib/helpers/ruby_1.8_1.9_compatibility.rb
Overview
EXTENSIONS to Class String
if you have a (partial) MP3-file stored in a String.. you can check if it contains ID3 tags
Instance Method Summary collapse
-
#getbyte(x) ⇒ Object
when accessing a string and selecting x-th byte to do calculations , as defined in Ruby 1.9.
- #hasID3tag? ⇒ Boolean
-
#hasID3v1tag? ⇒ Boolean
returns either nil or the version number – this can be used in a boolean comparison.
-
#hasID3v2tag? ⇒ Boolean
returns either nil or the version number – this can be used in a boolean comparison.
-
#hexdump(sparse = false) ⇒ Object
prints out a good’ol hexdump of the data contained in the string.
-
#id3_versions ⇒ Object
str = File.open(filename, ‘rb:binary’).read; 1 str.hasID3v2tag? str.hasID3v1tag?.
-
#ID3v2_tag_size ⇒ Object
we also need a method to return the size of the ID3v2 tag , e.g.
Instance Method Details
#getbyte(x) ⇒ Object
when accessing a string and selecting x-th byte to do calculations , as defined in Ruby 1.9
38 39 40 |
# File 'lib/helpers/hexdump.rb', line 38 def getbyte(x) # when accessing a string and selecting x-th byte to do calculations , as defined in Ruby 1.9 self[x] # returns an integer end |
#hasID3tag? ⇒ Boolean
15 16 17 |
# File 'lib/id3/string_extensions.rb', line 15 def hasID3tag? hasID3v2tag? || hasID3v1tag? ? true : false # returns true or false end |
#hasID3v1tag? ⇒ Boolean
returns either nil or the version number – this can be used in a boolean comparison
33 34 35 36 37 38 39 |
# File 'lib/id3/string_extensions.rb', line 33 def hasID3v1tag? # returns either nil or the version number -- this can be used in a boolean comparison return nil if size < ID3::ID3v1tagSize # if the String is too small to contain a tag size = self.bytesize tag = self[size-128,size] # get the last 128 bytes return nil if tag !~/^TAG/ return tag[ID3::ID3v1versionbyte] == ZEROBYTE ? "1.0" : "1.1" # return version number otherwise end |
#hasID3v2tag? ⇒ Boolean
returns either nil or the version number – this can be used in a boolean comparison
19 20 21 22 23 24 |
# File 'lib/id3/string_extensions.rb', line 19 def hasID3v2tag? # returns either nil or the version number -- this can be used in a boolean comparison return nil if self !~ /^ID3/ major = self.getbyte(ID3::ID3v2major) minor = self.getbyte(ID3::ID3v2minor) version = "2." + major.to_s + '.' + minor.to_s end |
#hexdump(sparse = false) ⇒ Object
prints out a good’ol hexdump of the data contained in the string
parameters: sparse: true / false do we want to print multiple lines with zero values?
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/helpers/hexdump.rb', line 53 def hexdump(sparse = false) selfsize = self.bytesize first = true print "\n index 0 1 2 3 4 5 6 7 8 9 A B C D E F\n\n" lines,rest = selfsize.divmod(16) address = 0; i = 0 # we count them independently for future extension. while lines > 0 str = self[i..i+15] # we don't print lines with all zeroes, unless it's the last line if str == ZEROBYTE * 16 # if the 16 bytes are all zero if (!sparse) || (sparse && lines == 1 && rest == 0) str.tr!("\000-\037\177-\377",'.') printf( "%08x %8s %8s %8s %8s %s\n", address, self[i..i+3].unpack('H8').first, self[i+4..i+7].unpack('H8').first, self[i+8..i+11].unpack('H8').first, self[i+12..i+15].unpack('H8').first, str) else print " .... 00 .. 00 00 .. 00 00 .. 00 00 .. 00 ................\n" if first first = false end else # print string which is not all zeros str.tr!("\000-\037\177-\377",'.') printf( "%08x %8s %8s %8s %8s %s\n", address, self[i..i+3].unpack('H8').first, self[i+4..i+7].unpack('H8').first, self[i+8..i+11].unpack('H8').first, self[i+12..i+15].unpack('H8').first, str) first = true end i += 16; address += 16; lines -= 1 end # now do the remaining bytes, which don't fit a full line.. # yikes - this is truly ugly! REWRITE THIS!! if rest > 0 chunks2,rest2 = rest.divmod(4) j = i; k = 0 if (i < selfsize) printf( "%08x ", address) while (i < selfsize) printf "%02x", self.getbyte(i) i += 1; k += 1 print " " if ((i % 4) == 0) end for i in (k..15) print " " end str = self[j..selfsize] str.tr!("\000-\037\177-\377",'.') print " " * (4 - chunks2+1) printf(" %s\n", str) end end end |
#id3_versions ⇒ Object
str = File.open(filename, ‘rb:binary’).read; 1 str.hasID3v2tag? str.hasID3v1tag?
11 12 13 |
# File 'lib/id3/string_extensions.rb', line 11 def id3_versions [ hasID3v1tag? ,hasID3v2tag? ].compact # returns an Array of version numbers end |
#ID3v2_tag_size ⇒ Object
we also need a method to return the size of the ID3v2 tag , e.g. needed when we need to determine the buffersize to read the tag from a file or from a remote location
28 29 30 31 |
# File 'lib/id3/string_extensions.rb', line 28 def ID3v2_tag_size return 0 if self !~ /^ID3/ return ID3::ID3v2headerSize + ID3.unmungeSize( self[ID3::ID3v2tagSize..ID3::ID3v2tagSize+4] ) end |