Class: Rnp::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/rnp/output.rb

Overview

Note:

When dealing with very large data, prefer Output.to_path which should be the most efficient. Output.to_io is likely to have more overhead.

Class used to feed data out of RNP.

Examples:

output to a string

output = Rnp::Input.to_string('my data')
# ... after performing operations
output.string

output to a file

Rnp::Input.to_path('/path/to/my/file')

output to a Ruby IO object

Rnp::Input.to_io(File.open('/path/to/file', 'wb'))

Constant Summary collapse

WRITER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

lambda do |writer, _ctx, buf, buf_len|
  begin
    data = buf.read_bytes(buf_len)
    written = writer.call(data)
    return written == data.bytesize
  rescue
    puts $ERROR_INFO
    return false
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr, writer = nil) ⇒ Output

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Output.

Raises:


34
35
36
37
38
# File 'lib/rnp/output.rb', line 34

def initialize(ptr, writer = nil)
  raise Rnp::Error, 'NULL pointer' if ptr.null?
  @ptr = FFI::AutoPointer.new(ptr, self.class.method(:destroy))
  @writer = writer
end

Instance Attribute Details

#ptrObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


31
32
33
# File 'lib/rnp/output.rb', line 31

def ptr
  @ptr
end

Class Method Details

.default(output) {|output| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields:

  • (output)

121
122
123
124
125
126
# File 'lib/rnp/output.rb', line 121

def self.default(output)
  to_str = output.nil?
  output = Output.to_string if to_str
  yield output
  output.string if to_str
end

.destroy(ptr) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


41
42
43
# File 'lib/rnp/output.rb', line 41

def self.destroy(ptr)
  LibRnp.rnp_output_destroy(ptr)
end

.to_callback(writer) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


113
114
115
116
117
118
# File 'lib/rnp/output.rb', line 113

def self.to_callback(writer)
  pptr = FFI::MemoryPointer.new(:pointer)
  writercb = WRITER.curry[writer]
  Rnp.call_ffi(:rnp_output_to_callback, pptr, writercb, nil, nil)
  Output.new(pptr.read_pointer, writercb)
end

.to_io(io) ⇒ Output

Create an Output to write to an IO object.

Parameters:

  • io (IO, #write)

    the IO object

Returns:


85
86
87
# File 'lib/rnp/output.rb', line 85

def self.to_io(io)
  to_callback(io.method(:write))
end

.to_nullOutput

Create an Output to discard all writes.

Returns:


75
76
77
78
79
# File 'lib/rnp/output.rb', line 75

def self.to_null
  pptr = FFI::MemoryPointer.new(:pointer)
  Rnp.call_ffi(:rnp_output_to_null, pptr)
  Output.new(pptr.read_pointer)
end

.to_path(path) ⇒ Output

Create an Output to write to a path.

Parameters:

  • path (String)

    the path

Returns:


66
67
68
69
70
# File 'lib/rnp/output.rb', line 66

def self.to_path(path)
  pptr = FFI::MemoryPointer.new(:pointer)
  Rnp.call_ffi(:rnp_output_to_path, pptr, path)
  Output.new(pptr.read_pointer)
end

.to_string(max_alloc = 0) ⇒ Output

Create an Output to write to a string.

The resulting string can later be retrieved with #string.

Parameters:

  • max_alloc (Integer) (defaults to: 0)

    the maximum amount of memory to allocate, or 0 for unlimited

Returns:


56
57
58
59
60
# File 'lib/rnp/output.rb', line 56

def self.to_string(max_alloc = 0)
  pptr = FFI::MemoryPointer.new(:pointer)
  Rnp.call_ffi(:rnp_output_to_memory, pptr, max_alloc)
  Output.new(pptr.read_pointer)
end

Instance Method Details

#inspectObject


45
46
47
# File 'lib/rnp/output.rb', line 45

def inspect
  Rnp.inspect_ptr(self)
end

#stringString?

Retrieve the data written. Only valid for #to_string.

Returns:

  • (String, nil)

92
93
94
95
96
97
98
# File 'lib/rnp/output.rb', line 92

def string
  pptr = FFI::MemoryPointer.new(:pointer)
  len = FFI::MemoryPointer.new(:size_t)
  Rnp.call_ffi(:rnp_output_memory_get_buf, @ptr, pptr, len, false)
  buf = pptr.read_pointer
  buf.read_bytes(len.read(:size_t)) unless buf.null?
end