Class: NativeFileType

Inherits:
Object
  • Object
show all
Extended by:
SubclassTracking
Defined in:
lib/NativeFileType.rb

Constant Summary collapse

@@code_for_tests =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SubclassTracking

extended

Constructor Details

#initialize(file_system_image, filename, contents, file_type, aux_code) ⇒ NativeFileType

Returns a new instance of NativeFileType.



10
11
12
13
14
15
16
# File 'lib/NativeFileType.rb', line 10

def initialize(file_system_image,filename,contents,file_type,aux_code)
   @file_system_image=file_system_image
	@filename=filename
	@contents=contents
   @file_type=file_type
   @aux_code=aux_code
end

Instance Attribute Details

#aux_codeObject

Returns the value of attribute aux_code.



9
10
11
# File 'lib/NativeFileType.rb', line 9

def aux_code
  @aux_code
end

#contentsObject

Returns the value of attribute contents.



9
10
11
# File 'lib/NativeFileType.rb', line 9

def contents
  @contents
end

#file_system_imageObject

Returns the value of attribute file_system_image.



9
10
11
# File 'lib/NativeFileType.rb', line 9

def file_system_image
  @file_system_image
end

#file_typeObject

Returns the value of attribute file_type.



9
10
11
# File 'lib/NativeFileType.rb', line 9

def file_type
  @file_type
end

#filenameObject

Returns the value of attribute filename.



9
10
11
# File 'lib/NativeFileType.rb', line 9

def filename
  @filename
end

Class Method Details

.all_native_file_typesObject



27
28
29
# File 'lib/NativeFileType.rb', line 27

def NativeFileType.all_native_file_types
  NativeFileType.subclasses
end

.best_fit(file_system_image, filename, contents, file_type = nil, aux_code = nil) ⇒ Object

given metadata and contents of a file from a file system image, return an instance of the NativeFileType subclass that is the closest match



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/NativeFileType.rb', line 76

def NativeFileType.best_fit(file_system_image,filename,contents,file_type=nil,aux_code=nil)
  candidates={}
  NativeFileType.all_native_file_types.each do |native_file_type|
    if !native_file_type.file_system_file_types.keys.include?(file_system_image.file_system) then
      RipXploreLog.debug("skipping #{native_file_type} - can't reside on #{file_system_image.file_system}")
    else
      RipXploreLog.debug("checking native file type #{native_file_type}")
      candidate_score=native_file_type.compatability_score(file_system_image,filename,contents,file_type,aux_code)
      RipXploreLog.debug("score - #{candidate_score}")
      candidates[native_file_type]=candidate_score
    end    
  end
  if candidates.length==0 then
    RipXploreLog.debug( 'no valid combination of file system and native file types found')
    return nil
  end
  best_candidate=candidates.keys.sort{|a,b| candidates[b]<=>candidates[a]}[0]
  RipXploreLog.debug("best candidate for #{filename} = #{best_candidate} score: #{candidates[best_candidate]}")
  best_candidate.new(file_system_image,filename,contents,file_type,aux_code)
end

.code_for_testsObject



118
119
120
# File 'lib/NativeFileType.rb', line 118

def self.code_for_tests
  @@code_for_tests[self] || []
end

.compatability_score(file_system_image, filename, contents, file_type, aux_code) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/NativeFileType.rb', line 128

def self.compatability_score(file_system_image,filename,contents,file_type,aux_code)
  return non_matching_score  unless (file_type_matches?(file_system_image,file_type))
    load_address=self.load_address(contents)
    passed_tests=0
   code_for_tests.each do |code_for_test|
     result = eval code_for_test,binding
     RipXploreLog.debug "test for #{self} : #{code_for_test} : #{result}"     
    return non_matching_score  unless (result)
    passed_tests+=1
  end
  return matching_score+passed_tests
end

.file_type_matches?(file_system_image, file_type) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
# File 'lib/NativeFileType.rb', line 98

def NativeFileType.file_type_matches?(file_system_image,file_type)
  result=false
  [file_system_file_types[file_system_image.file_system]].flatten.each do |target_file_type|
    result=true if target_file_type==:any
    result=true if(file_type.to_s==target_file_type.to_s)     
    RipXploreLog.debug "test for #{file_type} == #{target_file_type} : #{result}"
  end
  result
end

.is_valid_file_if(code_for_test) ⇒ Object



122
123
124
125
126
# File 'lib/NativeFileType.rb', line 122

def self.is_valid_file_if(code_for_test)
  #puts "adding test for #{self}: #{code_for_test.source}"
  @@code_for_tests[self] ||=[]
  @@code_for_tests[self]<<code_for_test.source
end

.load_address(filebytes) ⇒ Object



40
41
42
# File 'lib/NativeFileType.rb', line 40

def NativeFileType.load_address(filebytes)
  0
end

.matching_scoreObject



18
19
20
# File 'lib/NativeFileType.rb', line 18

def NativeFileType.matching_score
  ancestors.length
end

.non_matching_scoreObject



22
23
24
# File 'lib/NativeFileType.rb', line 22

def NativeFileType.non_matching_score
  0
end

Instance Method Details

#<=>(o) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/NativeFileType.rb', line 44

def <=>(o)
  return -1 unless o.respond_to?(:filename)
  if filename==o.filename then
    return self.to_s<=>o.to_s
  else
    return (filename<=>o.filename)
  end
end

#==(other_object) ⇒ Object



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

def ==(other_object)
  if  !other_object.kind_of? NativeFileType then
    return false
  end
   if self.filename!=other_object.filename then
     return false
   end
  return self.to_s==other_object.to_s
end

#data_without_headerObject



113
114
115
# File 'lib/NativeFileType.rb', line 113

def data_without_header
  @contents[header_length,@contents.length-header_length]
end

#full_filenameObject

some filesystems differentiate between full and partial filenames. e.g. in ProDOS the ‘full’ filename includes the full path by default, full_filename is the same as filename, but can be overridden for those filesystems that need this



60
61
62
# File 'lib/NativeFileType.rb', line 60

def full_filename
  @filename
end

#header_lengthObject

how many bytes should be skipped to get to the file data?



109
110
111
# File 'lib/NativeFileType.rb', line 109

def header_length
  0
end

#load_addressObject



36
37
38
# File 'lib/NativeFileType.rb', line 36

def load_address
  self.class.load_address(contents)    
end

#to_hex_dumpObject



53
54
55
# File 'lib/NativeFileType.rb', line 53

def to_hex_dump
  file_system_image.file_system.host_system.hex_dump(contents)
end

#type_descriptionObject

how should this file be described in a catalog list?



32
33
34
# File 'lib/NativeFileType.rb', line 32

def type_description
  self.class
end