Module: AddMagicComment

Defined in:
lib/magic_pragma.rb

Overview

Easily add “#pragma once” to multiple “.h” and “.hpp” and “.hxx” header files

Class Method Summary collapse

Class Method Details

.process(options) ⇒ Object

Options : 1 : Encoding 2 : Path TODO : check that the pragma specified is a valid pragma



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/magic_pragma.rb', line 9

def self.process(options)
  
  # defaults
  pragma  = options[0] || "once"
  directory = options[1] || Dir.pwd
  
  prefix = "#pragma #{pragma}\n"
  
  # TODO : add options for recursivity (and application of the script to a single file)
  rbfiles = File.join(directory ,"**", "*.{h,hpp,hxx}")
  Dir.glob(rbfiles).each do |filename|
    file = File.new(filename, "r+")
    
    lines = file.readlines
    
    # remove current pragma once(s)
    while lines[0] && (
              lines[0].starts_with?("#pragma #{pragma}") ||
lines[0].strip == ""
)
      lines.shift
    end

    # set new pragma once
    lines.insert(0,prefix)
    
    file.pos = 0
    file.puts(lines.join) 
    file.close
  end
  print "\"#pragma once\" set for #{Dir.glob(rbfiles).count} header files\n"
end