Refactored AMD64 code generator
[voodoo-lang.git] / lib / voodoo / generators / command_postprocessor.rb
blobf31045f96beeebbd6f9338d22bf7501b17f492c8
1 require 'delegate'
2 require 'tempfile'
4 module Voodoo
5   # Utility functions for classes that use commands to postprocess
6   # generator output.
7   module CommandPostProcessor
8     module_function
10     # Encodes a string so that it is safe for use as a shell argument
11     def shell_encode string
12       '"' + string.gsub(/([\\`"$\n])/, "\\\\\\1") + '"'
13     end
15     # Creates a temporary file and returns its name
16     def tempfile extension, base = nil
17       base = self.class.name unless base
18       file = Tempfile.open(basename + extension)
19       name = file.path
20       file.close
21       name
22     end
24     # Writes the contents of the named file to an IO handle
25     def write_file_to_io filename, io
26       File.open(filename) { |file| io.write(file.read) }
27     end
29   end
30 end