class ECStr

Usage:

ruby -I . ecstr.rb a.cpp.erb b.c.erb c.cpp.erb

Result:

create a.cpp, b.c, c.cpp, ecstrdef.data

In the erb file,when using a string, write as:

<%= _ "the string which will be encrypted" %>

Remember to <% require ‘ecstr’ %> at begining of the erb file.

Constants

DATA_FILE_NAME

Public Instance Methods

run() click to toggle source
# File lib/ecstr.rb, line 25
def run
  @dict = Hash.new
  b = binding

  @files.each do |infile|
    outfile = infile[0..-5]  # "a.cpp.erb" => "a.cpp"
    erbfile = ERB.new(File.read infile)
    File.write outfile, erbfile.result(b)
    print "#{infile} --> #{outfile}\n"
  end
  
  ids = "enum {\n  __BEGIN = -1,\n"
  strs = "static const char *estrs[] = {\n"
  
  @dict.each do |key, value|
    ids += "  #{key},\n"
    strs += "  \"#{encrypt value}\",\n"
  end
  
  ids += "  ENCRYPTED_STR_TOTAL_NUM\n};\n"
  strs += "  NULL\n};\n"
  File.write DATA_FILE_NAME, "#ifndef __#{DATA_FILE_NAME.gsub('.','_').upcase}__\n#define __#{DATA_FILE_NAME.gsub('.','_').upcase}__\n\n#{ids}\n\n#{strs}\n\n#endif\n"
  print "Data file #{DATA_FILE_NAME} created.\n"
end