Class: Hsss::COutput

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

Direct Known Subclasses

CSplit, CWhole

Constant Summary collapse

EXT =
"lua"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, opt = {}) ⇒ COutput

Returns a new instance of COutput.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hsss.rb', line 31

def initialize(files, opt={})
  @scripts={}
  names = { struct_name: DEFAULT_STRUCT_NAME, 
    row_struct_name: DEFAULT_ROW_STRUCT_NAME,
    hashes_struct: DEFAULT_HASHES_NAME,
    names_struct: DEFAULT_NAMES_NAME,
    scripts_struct: DEFAULT_SCRIPTS_NAME,
    count_name: DEFAULT_COUNT_NAME,
    count_macro_name: DEFAULT_COUNT_MACRO_NAME,
    iter_macro_name: DEFAULT_ITER_MACRO_NAME,
    all_hashes_string_name: DEFAULT_ALL_HASHES_STRING_NAME,
          }
  
  names.each do |var, default|
    send "#{var}=", opt[var]!=false ? opt[var] || cased_prefix(opt[:prefix], default) : false
  end
  @static=opt[:no_static] ? "" : "static "
  @header_only = opt[:header_only]
  @data_only = opt[:data_only]
  @include_count = !opt[:skip_count]
  @include_count_macro = !opt[:skip_count_macro]
  @include_iter_macro = !opt[:skip_each]
  @include_all_hashes_macro = !opt[:skip_all_hashes_string]
  @header_guard = opt[:header_guard] || "LUA_SCRIPTS_H"
  @header_guard = false if @header_guard.length == 0
  @include_hash = !!hashes_struct
  
  (Array === files ? files : [ files ]).each do |f|
    begin
      file_contents = IO.read f
      if opt[:no_luac] or check_script(f)
        @scripts[File.basename(f, ".#{EXT}") .to_sym]=file_contents
      end
    rescue Errno::ENOENT => e
      STDERR.puts "Failed to open file #{f}"
      @failed = true
    end
  end
  
  @struct=[]
  @name_table=[]
  @script_table=[]
  @hashed_table=[]
  
  @scripts.sort_by {|k,v| k}.each do |v| 
    name=v.first
    script=v.last
    
    @name_table << name
    
    str=[]
    for l in script.lines do
      cmt=l.match /^--(.*)/
      break unless cmt
      str << "  //#{cmt[1]}"
    end
    str << "  #{script_name_line(name)}\n"
    @struct << str.join("\n")
    
    @script_table << script_string(name, script)
    
    @hashed_table << Digest::SHA1.hexdigest(script) if @include_hash
  end
end

Instance Attribute Details

#all_hashes_string_nameObject

Returns the value of attribute all_hashes_string_name.



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

def all_hashes_string_name
  @all_hashes_string_name
end

#count_macro_nameObject

Returns the value of attribute count_macro_name.



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

def count_macro_name
  @count_macro_name
end

#count_nameObject

Returns the value of attribute count_name.



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

def count_name
  @count_name
end

#hashes_structObject

Returns the value of attribute hashes_struct.



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

def hashes_struct
  @hashes_struct
end

#iter_macro_nameObject

Returns the value of attribute iter_macro_name.



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

def iter_macro_name
  @iter_macro_name
end

#names_structObject

Returns the value of attribute names_struct.



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

def names_struct
  @names_struct
end

#row_struct_nameObject

Returns the value of attribute row_struct_name.



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

def row_struct_name
  @row_struct_name
end

#scripts_structObject

Returns the value of attribute scripts_struct.



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

def scripts_struct
  @scripts_struct
end

#struct_nameObject

Returns the value of attribute struct_name.



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

def struct_name
  @struct_name
end

Instance Method Details

#all_hashes_string_macroObject



101
102
103
104
# File 'lib/hsss.rb', line 101

def all_hashes_string_macro
  return "" unless @include_all_hashes_macro
  "#define #{all_hashes_string_name} \"#{@hashed_table.join " "}\"\n"
end

#cased_prefix(prefix, name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/hsss.rb', line 20

def cased_prefix(prefix, name)
  if name
    if name.upcase == name and name.downcase != name
      name = "#{(prefix || DEFAULT_PREFIX).upcase}#{name}"
    else
      name = "#{(prefix || DEFAULT_PREFIX)}#{name}"
    end
  end
  name
end

#check_script(path) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hsss.rb', line 106

def check_script(path)
  luac_exists_ret = system "command -v luac &>/dev/null"
  unless luac_exists_ret
    @failed = true
    STDERR.puts "luac is missing, cannot check script #{path}"
    return false
  end
  ret = system "luac -p #{path}"
  @failed = true unless ret
  ret
end

#count_macroObject



96
97
98
99
# File 'lib/hsss.rb', line 96

def count_macro
  return "" unless @include_count_macro
  "#define #{count_macro_name} #{@scripts.count}\n"
end

#cquote(str, line_start = "") ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/hsss.rb', line 118

def cquote(str, line_start="")
  out=[]
  if str.length == 0
    return "#{line_start}\"\"" #empty string
  end
  str.each_line do |l|
    l.gsub!("\\", '\\\\\\') #escape backslashes
    l.sub! "\n", "\\n"
    l.gsub! '"', '\"'
    l.gsub! /^(.*)$/, "#{line_start}\"\\1\""
    out << l
  end
  out.join "\n"
end

#failed?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/hsss.rb', line 133

def failed?
  @failed
end