Class: C0::Container

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/c0/document.rb

Overview

A file or message: one or more documents and an optional raw tail after EM. Parsing applies the whole grammar to every document. Enumerates the documents.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buf) ⇒ Container

Returns a new instance of Container.



199
200
201
202
203
204
# File 'lib/c0/document.rb', line 199

def initialize(buf)
  @buf = C0.to_bin(buf)
  descs, @has_em, em_pos = Ext.documents(@buf)
  @documents = descs.map { |d| Document.from_ext(@buf, d) }
  @tail = @has_em ? @buf.byteslice(em_pos + 1, @buf.bytesize - em_pos - 1) : "".b
end

Instance Attribute Details

#buf ⇒ Object (readonly)

Returns the value of attribute buf.



197
198
199
# File 'lib/c0/document.rb', line 197

def buf
  @buf
end

#documents ⇒ Object (readonly)

Returns the value of attribute documents.



197
198
199
# File 'lib/c0/document.rb', line 197

def documents
  @documents
end

#tail ⇒ Object (readonly)

Returns the value of attribute tail.



197
198
199
# File 'lib/c0/document.rb', line 197

def tail
  @tail
end

Instance Method Details

#blob(name = nil) ⇒ Object

Bytes of a named blob from the tail (columns matched by header name, last row wins), or nil. With no name, the blob table itself.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/c0/document.rb', line 241

def blob(name = nil)
  return blob_table if name.nil?

  table = blob_table or return nil
  g = table.groups.first or return nil
  ni = g.column("name") || 0
  oi = g.column("offset") || 1
  li = g.column("length") || 2
  needle = C0.to_bin(name)
  found = nil
  g.records.each do |r|
    next unless r.value(ni) == needle

    off = r.value(oi)
    len = r.value(li)
    next unless off&.match?(/\A\d+\z/) && len&.match?(/\A\d+\z/)

    off = off.to_i
    len = len.to_i
    next if off > @tail.bytesize || len > @tail.bytesize - off

    found = @tail.byteslice(off, len)
  end
  found
end

#blob_table ⇒ Object

The blob table from the first document's heading, as a Body, or nil.



237
# File 'lib/c0/document.rb', line 237

def blob_table = first&.heading&.blob

#each ⇒ Object



210
# File 'lib/c0/document.rb', line 210

def each(&) = @documents.each(&)

#first ⇒ Object



212
# File 'lib/c0/document.rb', line 212

def first = @documents[0]

#has_em? ⇒ Boolean Also known as: em?

Returns:

  • (Boolean)


206
# File 'lib/c0/document.rb', line 206

def has_em? = @has_em

#size ⇒ Object



214
# File 'lib/c0/document.rb', line 214

def size = @documents.size

#warnings ⇒ Object

Loud warnings about the container as a whole; each document carries its own.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/c0/document.rb', line 217

def warnings
  @warnings ||= begin
    w = []
    @documents.each_with_index do |doc, i|
      next unless doc.heading.key?("blob")

      if i.positive?
        w << "blob table in a later heading is ignored"
      elsif doc.heading.blob.nil?
        w << "blob is not a level: declares nothing"
      elsif !doc.heading.blob.groups.first&.header?
        w << "blob table has no header row: declares nothing"
      end
    end
    w << "EM with no blob table: the region is unaddressable" if @has_em && blob_table.nil?
    w
  end
end