Class: C::Preprocessor

Inherits:
Object
  • Object
show all
Defined in:
lib/casty/preprocessor.rb

Defined Under Namespace

Classes: Error

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePreprocessor

Returns a new instance of Preprocessor.



24
25
26
27
28
# File 'lib/casty/preprocessor.rb', line 24

def initialize
  @include_path = []
  @macros = {}
  @undef_macros = Set.new
end

Class Attribute Details

.commandObject

Returns the value of attribute command.



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

def command
  @command
end

Instance Attribute Details

#include_pathObject

Returns the value of attribute include_path.



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

def include_path
  @include_path
end

#macrosObject

Returns the value of attribute macros.



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

def macros
  @macros
end

#pwdObject

Returns the value of attribute pwd.



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

def pwd
  @pwd
end

#undef_macrosObject

Returns the value of attribute undef_macros.



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

def undef_macros
  @undef_macros
end

Instance Method Details

#preprocess(text) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/casty/preprocessor.rb', line 29

def preprocess(text)
  filename = nil
  temp_file = nil
  Tempfile.open(['cast-preprocessor-input.', '.c'], File.expand_path(pwd || '.')) do |file|
    temp_file = file
    filename = file.path
    file.puts text
  end
  return preprocess_file(filename);
ensure
  if temp_file
    temp_file.close
    temp_file.unlink
  end
end

#preprocess_file(file_name, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/casty/preprocessor.rb', line 44

def preprocess_file(file_name, options={})
  file_name = File.expand_path(file_name)
  dir = File.dirname(file_name)
  stderr_buf = Tempfile.new('cast-preprocessor-stderr.txt')
  FileUtils.cd(dir) do
    output = `#{full_command(file_name)} 2>#{shellquote(stderr_buf.path)}`
    if $? == 0 || options[:force] && !output.empty?
      return output
    else
      raise Error, stderr_buf.read
    end
  end
ensure
  stderr_buf.close
  stderr_buf.unlink
end