Class: Verilog::File

Inherits:
Object
  • Object
show all
Defined in:
lib/verilog/file.rb

Overview

< Class::File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}) ⇒ File

Returns a new instance of File.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/verilog/file.rb', line 8

def initialize( filename, options={} )
  @filename         = ::File.basename( filename )
  @options          = options

  options[:path]  ||= ''
  relative_path_added_with_file_name = filename.dup
  #Remove the basename section of the filename
  relative_path_added_with_file_name[@filename] = ''

  @options[:path] = ::File.join( options[:path], relative_path_added_with_file_name )
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



6
7
8
# File 'lib/verilog/file.rb', line 6

def contents
  @contents
end

#filenameObject

Returns the value of attribute filename.



6
7
8
# File 'lib/verilog/file.rb', line 6

def filename
  @filename
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/verilog/file.rb', line 5

def options
  @options
end

Instance Method Details

#absolute_filenameObject



47
48
49
50
51
52
# File 'lib/verilog/file.rb', line 47

def absolute_filename
  dir = ::File.join( @options[:path], @filename ) 
  dir = ::File.expand_path( dir )

  return dir
end

#includesObject



71
72
73
74
75
76
# File 'lib/verilog/file.rb', line 71

def includes
  inc = []
  @contents.scan(/(^\s*`include [\'\"])(.*)([\'\"])/i){ inc << $2 }

  return inc
end

#inspectObject



20
21
22
# File 'lib/verilog/file.rb', line 20

def inspect
  [":options => #{@options.inspect}", ":filename => #{@filename}", ":contents => #{@contents}"] 
end

#instantiationsObject



62
63
64
65
66
67
68
69
# File 'lib/verilog/file.rb', line 62

def instantiations
  inst = []
  @contents.scan(/(^\s*)(\w*)\s+\w+\s*(\([.,\(\)\w\s]*\))?;/mi){ inst << $2 }
  #Hack, module will also match the instatiation syntax, rempve via array subtraction
  inst = inst - ['module']

  return inst
end

#module_nameObject



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

def module_name
    if @contents.match(/(^\s*module *)(.*)(;|\s*\()/i)
      return $2
    else
      return ""
    end
end

#readObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/verilog/file.rb', line 29

def read
  if ::File.exist?( absolute_filename )
    ::File.open( absolute_filename, "rb") do |f| 
      @contents = f.read 
    end
  else
    #TODO raise correct exception here
    $stderr.puts "ERROR File Not Found #{absolute_filename}"
  end

end

#read_from_diskObject

Alias method



25
26
27
# File 'lib/verilog/file.rb', line 25

def read_from_disk
  read()
end

#saveObject



41
42
43
44
45
# File 'lib/verilog/file.rb', line 41

def save
  ::File.open( absolute_filename, "w" ) do |f|
    f.write @contents
  end
end