Module: CTypes::Importers::CastXML

Defined in:
lib/ctypes/importers/castxml.rb

Defined Under Namespace

Classes: CompilerError, Loader

Class Method Summary collapse

Class Method Details

.load_source(src) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/ctypes/importers/castxml.rb', line 41

def self.load_source(src)
  Tempfile.open(["", ".c"]) do |f|
    f.write(src)
    f.flush
    load_source_file(f.path)
  end
end

.load_source_file(path) ⇒ Object

Raises:



49
50
51
52
53
54
# File 'lib/ctypes/importers/castxml.rb', line 49

def self.load_source_file(path)
  stdout, stderr, status = Open3
    .capture3("castxml --castxml-output=1 #{path} -o -")
  raise CompilerError, stderr unless status.success?
  load_xml(stdout)
end

.load_xml(xml) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ctypes/importers/castxml.rb', line 21

def self.load_xml(xml)
  io = case xml
  when IO
    xml
  when ::String
    StringIO.new(xml)
  else
    raise Error, "arg must be IO or String: %p" % xml
  end

  l = Loader.new(io)
  l.load
end

.load_xml_file(path) ⇒ Object



35
36
37
38
39
# File 'lib/ctypes/importers/castxml.rb', line 35

def self.load_xml_file(path)
  File.open(path) do |f|
    load_xml(f)
  end
end