Class: Ragweed::Sbuf

Inherits:
Object show all
Defined in:
lib/ragweed/sbuf.rb

Overview

Encapsulate a buffer of data with structured data accessors

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Sbuf

Returns a new instance of Sbuf.



45
46
47
48
# File 'lib/ragweed/sbuf.rb', line 45

def initialize(opts={})
  @content = opts[:content] || ""
  @position = 0
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



13
14
15
# File 'lib/ragweed/sbuf.rb', line 13

def content
  @content
end

#positionObject

Returns the value of attribute position.



14
15
16
# File 'lib/ragweed/sbuf.rb', line 14

def position
  @position
end

Class Method Details

.from(*args) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ragweed/sbuf.rb', line 16

def self.from(*args)
  raise ArgumentError, "odd argument count" if args.length.odd?

  b = self.new
  while not args.empty?
    t = args.shift; v = args.shift
    if t == :raw
      b.straw(v)
    elsif v.kind_of? Array
      b.st t, *v
    else
      b.st t, v
    end
  end

  return b
end

.sz(t, *args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ragweed/sbuf.rb', line 88

def self.sz(t, *args)
  begin
    send "sz#{ t }", *args
  rescue => e
    case t.to_s
    when /^strz(\d+)/
      $1.to_i
    when /^strs(\d+)/
      $1.to_i
    when /^str.*/
      raise Exception, "can't take size of unbounded string"
    else
      raise e
    end
  end
end

.szb16Object



168
# File 'lib/ragweed/sbuf.rb', line 168

def self.szb16; 2; end

.szb32Object



156
# File 'lib/ragweed/sbuf.rb', line 156

def self.szb32; 4; end

.szb64Object



144
# File 'lib/ragweed/sbuf.rb', line 144

def self.szb64; 8; end

.szb8Object



180
# File 'lib/ragweed/sbuf.rb', line 180

def self.szb8; 1; end

.szl16Object



167
# File 'lib/ragweed/sbuf.rb', line 167

def self.szl16; 2; end

.szl32Object



155
# File 'lib/ragweed/sbuf.rb', line 155

def self.szl32; 4; end

.szl64Object



143
# File 'lib/ragweed/sbuf.rb', line 143

def self.szl64; 8; end

.szl8Object



179
# File 'lib/ragweed/sbuf.rb', line 179

def self.szl8; 1; end

.szn16Object



169
# File 'lib/ragweed/sbuf.rb', line 169

def self.szn16; 2; end

.szn32Object



157
# File 'lib/ragweed/sbuf.rb', line 157

def self.szn32; 4; end

.szn64Object



145
# File 'lib/ragweed/sbuf.rb', line 145

def self.szn64; 8; end

.szn8Object



181
# File 'lib/ragweed/sbuf.rb', line 181

def self.szn8; 1; end

Instance Method Details

#==(b) ⇒ Object



136
# File 'lib/ragweed/sbuf.rb', line 136

def ==(b); to_s == b.to_s; end

#availableObject Also known as: remaining



132
# File 'lib/ragweed/sbuf.rb', line 132

def available; length - position; end

#clear!Object



139
# File 'lib/ragweed/sbuf.rb', line 139

def clear!; @content = "" and reset; end

#consume!(n = position) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/ragweed/sbuf.rb', line 50

def consume!(n=position)
  if(n >= length); clear!
  elsif n > 0
    @content = remainder(n)
    @position -= n
    @position = 0 if @position < 0
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


130
# File 'lib/ragweed/sbuf.rb', line 130

def empty?; @content.empty?; end

#eof?Boolean

Returns:

  • (Boolean)


138
# File 'lib/ragweed/sbuf.rb', line 138

def eof?; @position >= length; end

#ld(t, *args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ragweed/sbuf.rb', line 66

def ld(t, *args)
  return ldraw(t) if t.kind_of? Numeric

  begin
    send "ld#{ t }", *args
  rescue => e
    case t.to_s
    when /^strz(\d+)?/
      n = $1.to_i if not $1.empty?
      n ||= 0
      ldsz(n)
    when /^strs(\d+)?/
      n = $1.to_i if not $1.empty?
      n ||= 0
      ldss(n)
    else
      raise e
    end
  end
end

#ldb16Object Also known as: ldn16



172
# File 'lib/ragweed/sbuf.rb', line 172

def ldb16; ld(2).unpack("n").first; end

#ldb32Object Also known as: ldn32



160
# File 'lib/ragweed/sbuf.rb', line 160

def ldb32; ld(4).unpack("N").first; end

#ldb64Object Also known as: ldn64



148
# File 'lib/ragweed/sbuf.rb', line 148

def ldb64; ld(8).reverse.unpack("Q").first; end

#ldb8Object Also known as: ldn8



184
# File 'lib/ragweed/sbuf.rb', line 184

def ldb8; ldl8; end

#ldl16Object



171
# File 'lib/ragweed/sbuf.rb', line 171

def ldl16; ld(2).unpack("v").first; end

#ldl32Object



159
# File 'lib/ragweed/sbuf.rb', line 159

def ldl32; ld(4).unpack("L").first; end

#ldl64Object



147
# File 'lib/ragweed/sbuf.rb', line 147

def ldl64; ld(8).unpack("Q").first; end

#ldl8Object



183
# File 'lib/ragweed/sbuf.rb', line 183

def ldl8; ld(1)[0]; end

#ldraw(n = length) ⇒ Object



60
61
62
63
64
# File 'lib/ragweed/sbuf.rb', line 60

def ldraw(n=length)
  n = ((self.length) - position) if(position + n > length)
  @position += n
  @content[position-n, n]
end

#ldss(n = 0) ⇒ Object



123
124
125
126
# File 'lib/ragweed/sbuf.rb', line 123

def ldss(n=0)
  n = @content.size - @position if n == 0
  ld(n).unpack("A#{ n }").first
end

#ldsz(n = 0) ⇒ Object



118
119
120
121
# File 'lib/ragweed/sbuf.rb', line 118

def ldsz(n=0)
  n = @content.size - @position if n == 0
  ld(n).unpack("a#{ n }").first
end

#lengthObject



128
# File 'lib/ragweed/sbuf.rb', line 128

def length; @content.length; end

#remainder(n = position) ⇒ Object



140
# File 'lib/ragweed/sbuf.rb', line 140

def remainder(n = position); @content[n..-1] || ""; end

#remainder_as_buffer(t = Ragweed::Sbuf) ⇒ Object



141
# File 'lib/ragweed/sbuf.rb', line 141

def remainder_as_buffer(t=Ragweed::Sbuf); t.new(:content => remainder); end

#resetObject



137
# File 'lib/ragweed/sbuf.rb', line 137

def reset; @position = 0; end

#sh(t, *args) ⇒ Object



111
112
113
114
115
116
# File 'lib/ragweed/sbuf.rb', line 111

def sh(t, *args)
  return shraw(t) if t.kind_of? Numeric
  ret = send "ld#{ t }", *args
  consume!
  return ret
end

#shraw(n = length) ⇒ Object



105
106
107
108
109
# File 'lib/ragweed/sbuf.rb', line 105

def shraw(n=length)
  ret = ldraw(n)
  consume!
  return ret
end

#sizeObject



129
# File 'lib/ragweed/sbuf.rb', line 129

def size; @content.size; end

#st(t, *args) ⇒ Object



34
35
36
# File 'lib/ragweed/sbuf.rb', line 34

def st(t, *args)
  send "st#{ t }", *args
end

#stb16(v) ⇒ Object Also known as: stn16



176
# File 'lib/ragweed/sbuf.rb', line 176

def stb16(v); straw([v].pack("n")); end

#stb32(v) ⇒ Object Also known as: stn32



164
# File 'lib/ragweed/sbuf.rb', line 164

def stb32(v); straw([v].pack("N")); end

#stb64(v) ⇒ Object Also known as: stn64



152
# File 'lib/ragweed/sbuf.rb', line 152

def stb64(v); straw([v].pack("Q").reverse); end

#stb8(v) ⇒ Object Also known as: stn8



195
# File 'lib/ragweed/sbuf.rb', line 195

def stb8(v); stl8(v); end

#stl16(v) ⇒ Object



175
# File 'lib/ragweed/sbuf.rb', line 175

def stl16(v); straw([v].pack("v")); end

#stl32(v) ⇒ Object



163
# File 'lib/ragweed/sbuf.rb', line 163

def stl32(v); straw([v].pack("L")); end

#stl64(v) ⇒ Object



151
# File 'lib/ragweed/sbuf.rb', line 151

def stl64(v); straw([v].pack("Q")); end

#stl8(v) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/ragweed/sbuf.rb', line 187

def stl8(v)
  if v.kind_of? String
    straw(v[0].chr)
  else
    straw([v].pack("c"))
  end
end

#straw(*args) ⇒ Object



38
39
40
41
42
43
# File 'lib/ragweed/sbuf.rb', line 38

def straw(*args)
  args.each do |raw|
    @content << raw.to_s
  end
  self
end

#sz(t, *args) ⇒ Object



87
# File 'lib/ragweed/sbuf.rb', line 87

def sz(t, *args); self.class.sz(t, *args); end

#to_sObject



135
# File 'lib/ragweed/sbuf.rb', line 135

def to_s; @content.dup; end