Module: Exerb::Utility

Defined in:
lib/exerb/utility.rb

Overview

#

Class Method Summary collapse

Class Method Details

.align_value(value, align) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/exerb/utility.rb', line 13

def self.align_value(value, align)
  if value % align == 0
    return value
  else
    return value + (align - (value % align))
  end
end

.alignment(bin, align) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/exerb/utility.rb', line 21

def self.alignment(bin, align)
  if bin.size % align == 0
    return bin
  else
    return bin + "\0" * (align - (bin.size % align))
  end
end

.alignment16(bin) ⇒ Object



29
30
31
# File 'lib/exerb/utility.rb', line 29

def self.alignment16(bin)
  return alignment(bin, 16)
end

.alignment4k(bin) ⇒ Object



33
34
35
# File 'lib/exerb/utility.rb', line 33

def self.alignment4k(bin)
  return alignment(bin, 1024 * 4)
end

.find_core_by_filename(filename) ⇒ Object



51
52
53
54
# File 'lib/exerb/utility.rb', line 51

def self.find_core_by_filename(filename)
  require 'exerb/config'
  return self.find_file_by_filename(filename, Exerb::CORE_PATH)
end

.find_core_by_filepath(filepath) ⇒ Object



47
48
49
# File 'lib/exerb/utility.rb', line 47

def self.find_core_by_filepath(filepath)
  return (File.exist?(filepath) ? filepath : nil)
end

.find_core_by_name(name, error = false) ⇒ Object

Raises:



56
57
58
59
60
61
# File 'lib/exerb/utility.rb', line 56

def self.find_core_by_name(name, error = false)
  require 'exerb/config'
  path = self.find_file_by_name(name, Exerb::CORE_NAME, Exerb::CORE_PATH)
  raise(Exerb::ExerbError, "unknown core name -- #{name}") if path.nil? && error
  return path
end

.find_file_by_filename(filename, path) ⇒ Object



37
38
39
40
# File 'lib/exerb/utility.rb', line 37

def self.find_file_by_filename(filename, path)
  return filename if File.expand_path(filename) == filename && File.exist?(filename)
  return path.collect { |dir| File.join(dir, filename) }.find { |filepath| File.exist?(filepath) }
end

.find_file_by_name(name, table, path) ⇒ Object



42
43
44
45
# File 'lib/exerb/utility.rb', line 42

def self.find_file_by_name(name, table, path)
  return nil unless table.has_key?(name)
  return self.find_file_by_filename(table[name], path)
end

.read(input) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/exerb/utility.rb', line 63

def self.read(input)
  case input
  when IO
    input.binmode
    return input.read
  when String
    return File.open(input, "rb") { |file| self.read(file) }
  else raise(ArgumentError, "input must be IO or String object")
  end
end

.write(output, binary, mode = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/exerb/utility.rb', line 74

def self.write(output, binary, mode = nil)
  case output
  when IO
    output.binmode
    output.write(binary)
  when String
    File.open(output, "wb") { |file| self.write(file, binary) }
    File.chmod(mode, output) if mode
  else raise(ArgumentError, "output must be IO or String object")
  end
  return nil
end