Class: Ccfg::Handler

Inherits:
Cmdlib::Command
  • Object
show all
Includes:
Ccfg
Defined in:
lib/ccfg/handler.rb

Overview

CLI command handler.

Constant Summary

Constants included from Ccfg

CCONFIGURE_DEFINE_DIRECTIVE, VERSION

Instance Method Summary collapse

Instance Method Details

#handler(global_options, args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ccfg/handler.rb', line 45

def handler ( global_options, args )
  if args.size == 0 then
    puts "Error: to few arguments for command."
    exit
  end
  input = args[0]
  args.delete_at(0)
  cconfigure_definition_list = {}
  # Get options from STDIN.
  args.each do |a|
    keyval = []
    if a.include? '=' then
      keyval = a.split('=')
    else
      keyval << a
      keyval << ""
    end
    cconfigure_definition_list[keyval[0]] = keyval[1]
  end
  # Set output file from option or STDOUT.
  if global_options[:output].value != nil then
    fout = File.new( global_options[:output].value, 'w' )
  end
  # Start to parse lines.
  File.open( input, 'r' ).readlines.each do |line|
    line = parse_line line, cconfigure_definition_list
    if global_options[:output].value != nil
      fout.puts line
    else
      puts line
    end
  end
  # Close file if option will be set.
  if global_options[:output].value != nil then
    fout.close
  end
end

#parse_line(line, deflist) ⇒ Object



7
8
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
41
42
43
# File 'lib/ccfg/handler.rb', line 7

def parse_line(line, deflist)
  if line.length > 0 then
 if line.include? Ccfg::CCONFIGURE_DEFINE_DIRECTIVE then
      line.strip!
      lineary = line.split(' ')
      if lineary.size >=2 and lineary[0] == Ccfg::CCONFIGURE_DEFINE_DIRECTIVE then
        deflist.each do |defname, defval|
          if defname == lineary[1] then
            line = "#define #{defname} #{defval}"
            if lineary.size > 2 then
              comment = ''
              tail = ''
              lineary.each do |e|
                tail += "#{e} "
              end
              if tail.include? "//" then
                comment = tail[tail.index("//") + "//".length, tail.length]
                comment.strip!
                comment = " //#{comment}"
              elsif tail.include? "/*" then
                start_index = tail.index("/*") + "/*".length
                len = tail.index("*/") - start_index
                comment = tail[start_index, len]
                comment.strip!
                comment = " /*#{comment} */"
              end
              line += comment
            end
            return line
          end
        end
        line = "/* #undef #{lineary[1]} */"
      end
    end
  end
  return line
end