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
17
# 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
   @meta_data={}
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

#meta_dataObject

Returns the value of attribute meta_data.



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

def 
  @meta_data
end

Class Method Details

.all_native_file_typesObject



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

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/NativeFileType.rb', line 94

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]}")
  native_file=best_candidate.new(file_system_image,filename,contents,file_type,aux_code)
  if GenericGIF.is_gif?(native_file) then
    RipXploreLog.debug(" #{filename} is a GIF")
    native_file.extend GenericGIF
  end
  
  if native_file.respond_to?(:files) then
  	return [native_file,native_file.files].flatten
  else 
  	return native_file
  end

end

.code_for_testsObject



147
148
149
# File 'lib/NativeFileType.rb', line 147

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

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



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/NativeFileType.rb', line 157

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|
      RipXploreLog.debug "test for #{self} : #{code_for_test}"     
     result = eval code_for_test,binding
     RipXploreLog.debug "RESULT: #{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)


127
128
129
130
131
132
133
134
135
# File 'lib/NativeFileType.rb', line 127

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



151
152
153
154
155
# File 'lib/NativeFileType.rb', line 151

def self.is_valid_file_if(code_for_test)
  #RipXploreLog.debug "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



50
51
52
# File 'lib/NativeFileType.rb', line 50

def NativeFileType.load_address(filebytes)
  0
end

.matching_scoreObject



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

def NativeFileType.matching_score
  ancestors.length
end

.native_file_types_possible_on_file_system(file_system) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/NativeFileType.rb', line 32

def NativeFileType.native_file_types_possible_on_file_system(file_system)
  candidates=[]
  NativeFileType.all_native_file_types.each do |native_file_type|
    candidates<<native_file_type if (native_file_type.file_system_file_types.keys.include?(file_system) ) && (native_file_type.file_system_file_types[file_system]!=:any)
  end
  candidates.sort {|a,b| a.to_s <=> b.to_s}
end

.non_matching_scoreObject



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

def NativeFileType.non_matching_score
  0
end

Instance Method Details

#<=>(o) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/NativeFileType.rb', line 54

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



82
83
84
85
86
87
88
89
90
# File 'lib/NativeFileType.rb', line 82

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



142
143
144
# File 'lib/NativeFileType.rb', line 142

def data_without_header
  (@contents.length>header_length) ?  @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



78
79
80
# File 'lib/NativeFileType.rb', line 78

def full_filename
  @filename
end

#header_lengthObject

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



138
139
140
# File 'lib/NativeFileType.rb', line 138

def header_length
  0
end

#load_addressObject



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

def load_address
  self.class.load_address(contents)    
end

#to_hex_dumpObject



63
64
65
# File 'lib/NativeFileType.rb', line 63

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

#to_info_dumpObject



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

def to_info_dump
	
	s="class: #{self.class}\n"
	s+="filename: #{self.filename}\n"
	@meta_data.keys.sort.each {|k| s<<"#{k}: #{@meta_data[k]}\n"}
	s
end

#type_descriptionObject

how should this file be described in a catalog list?



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

def type_description
  self.class
end