Class: SMPTool::VirtualVolume::Volume

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/smp_tool/virtual_volume/volume.rb

Overview

Ruby representation of the volume.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bootloader:, home_block:, volume_params:, volume_data: nil) ⇒ Volume

Create new virtual volume.

Parameters:

  • bootloader (Array<Integer>)
  • home_block (Array<Integer>)
  • volume_params (VolumeParams)
  • volume_data (VolumeData, nil) (defaults to: nil)

    Existing volume data (parsed from an existing VolumeIO obj) or nil. Nil indicates that an empty volume should be created, and empty data must be initialized.



35
36
37
38
39
40
41
# File 'lib/smp_tool/virtual_volume/volume.rb', line 35

def initialize(bootloader:, home_block:, volume_params:, volume_data: nil)
  @bootloader = bootloader
  @home_block = home_block

  @volume_params = volume_params
  @data = volume_data || Utils::EmptyVolDataInitializer.call(@volume_params)
end

Instance Attribute Details

#bootloaderObject (readonly)

Returns the value of attribute bootloader.



14
15
16
# File 'lib/smp_tool/virtual_volume/volume.rb', line 14

def bootloader
  @bootloader
end

#dataObject (readonly)

Returns the value of attribute data.



14
15
16
# File 'lib/smp_tool/virtual_volume/volume.rb', line 14

def data
  @data
end

#home_blockObject (readonly)

Returns the value of attribute home_block.



14
15
16
# File 'lib/smp_tool/virtual_volume/volume.rb', line 14

def home_block
  @home_block
end

#volume_paramsObject (readonly)

Returns the value of attribute volume_params.



14
15
16
# File 'lib/smp_tool/virtual_volume/volume.rb', line 14

def volume_params
  @volume_params
end

Class Method Details

.read_io(volume_io) ⇒ Object



20
21
22
# File 'lib/smp_tool/virtual_volume/volume.rb', line 20

def self.read_io(volume_io)
  Utils::ConverterFromVolumeIO.read_io(volume_io)
end

.read_volume_io(volume_io) ⇒ Object



16
17
18
# File 'lib/smp_tool/virtual_volume/volume.rb', line 16

def self.read_volume_io(volume_io)
  Utils::ConverterFromVolumeIO.read_volume_io(volume_io)
end

Instance Method Details

#f_delete(filename) ⇒ String

Delete a file.

Parameters:

  • filename (<String>)

Returns:

  • (String)

    ASCII filename of a deleted file.



188
189
190
# File 'lib/smp_tool/virtual_volume/volume.rb', line 188

def f_delete(filename)
  @data.f_delete(Filename.new(ascii: filename))
end

#f_extract_raw(filename) ⇒ FileInterface

Extract content of a file as a ‘raw’ string (as is).

Parameters:

  • filename (<String>)

    ASCII filename of the file to extract.

Returns:



149
150
151
152
153
# File 'lib/smp_tool/virtual_volume/volume.rb', line 149

def f_extract_raw(filename)
  Utils::FileExtracter.new(@data).f_extract_raw(
    Filename.new(ascii: filename)
  )
end

#f_extract_raw_allArray<FileInterface>

Extract all files as ‘raw’ strings.

Returns:



160
161
162
# File 'lib/smp_tool/virtual_volume/volume.rb', line 160

def f_extract_raw_all
  _all_filenames.map { |fn| f_extract_raw(fn) }
end

#f_extract_txt(filename) {|str| ... } ⇒ FileInterface

Extract content of a file as an array of strings.

Parameters:

  • filename (<String>)

    ASCII filename of the file to extract.

Yields:

  • (str)

    Each line of a file gets passed through this block. The default block decodes a string from the KOI-7 to the UTF-8, but a custom block allows to alter this behavior.

Returns:



123
124
125
126
127
128
129
130
# File 'lib/smp_tool/virtual_volume/volume.rb', line 123

def f_extract_txt(filename, &block)
  block = ->(str) { InjalidDejice.koi_to_utf(str) } unless block_given?

  Utils::FileExtracter.new(@data).f_extract_txt(
    Filename.new(ascii: filename),
    &block
  )
end

#f_extract_txt_allArray<FileInterface>

Extract all files as arrays of strings.

Returns:



137
138
139
# File 'lib/smp_tool/virtual_volume/volume.rb', line 137

def f_extract_txt_all
  _all_filenames.map { |fn| f_extract_txt(fn) }
end

#f_push(file_obj) {|str| ... } ⇒ String

Push a file to the volume.

Parameters:

Yields:

  • (str)

    Each line of a file gets passed through this block. The default block encodes a string from the UTF-8 to the KOI-7, but a custom block allows to alter this behavior (e.g. when the file is already in the KOI-7 encoding).

Returns:

  • (String)

    ASCII filename of the pushed file.



104
105
106
107
108
# File 'lib/smp_tool/virtual_volume/volume.rb', line 104

def f_push(file_obj, &block)
  block = ->(str) { InjalidDejice.utf_to_koi(str, forced_latin: "\"") } unless block_given?

  _f_push(file_obj, &block)
end

#f_rename(old_filename, new_filename) ⇒ Array<String>

Rename a file.

Parameters:

  • old_filename (<String>)
  • new_filename (<String>)

Returns:

  • (Array<String>)

    Old and new ASCII filenames of a renamed file.



173
174
175
176
177
178
# File 'lib/smp_tool/virtual_volume/volume.rb', line 173

def f_rename(old_filename, new_filename)
  @data.f_rename(
    Filename.new(ascii: old_filename),
    Filename.new(ascii: new_filename)
  )
end

#resize(n_clusters) ⇒ Integer

Allocate more clusters to the volume or trim free clusters.

Parameters:

  • n_clusters (Integer)

    Number of clusters to add (pos. int.) or to trim (neg. int.).

Returns:

  • (Integer)

    Number of clusters that were added/trimmed.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/smp_tool/virtual_volume/volume.rb', line 79

def resize(n_clusters)
  if n_clusters.positive?
    _resize_validate_pos_input(n_clusters)
  elsif n_clusters.negative?
    _resize_validate_neg_input(n_clusters)
  else
    return n_clusters
  end

  _resize(n_clusters)
end

#snapshotObject



43
44
45
46
47
48
49
# File 'lib/smp_tool/virtual_volume/volume.rb', line 43

def snapshot
  {
    volume_params: @volume_params.snapshot,
    volume_data: @data.snapshot,
    n_free_clusters: @data.calc_n_free_clusters
  }
end

#squeezeInteger

Consolidate all free space at the end ot the volume.

Returns:

  • (Integer)

    Number of free clusters that were joined.



198
199
200
# File 'lib/smp_tool/virtual_volume/volume.rb', line 198

def squeeze
  @data.squeeze
end

#to_binary_sString

Convert ‘self` to a binary string. Write this string to a binary file to get a MK90 volume that works on an emulator or on a real machine.

Returns:

  • (String)


66
67
68
# File 'lib/smp_tool/virtual_volume/volume.rb', line 66

def to_binary_s
  to_volume_io.to_binary_s
end

#to_volume_ioVolumeIO

Convert ‘self` to a VolumeIO object.

Returns:



56
57
58
# File 'lib/smp_tool/virtual_volume/volume.rb', line 56

def to_volume_io
  Utils::ConverterToVolumeIO.new(self).call
end