Skip to content

Instantly share code, notes, and snippets.

@michaelfward
Last active August 29, 2015 14:18
Show Gist options
  • Save michaelfward/99280f293d1e7f9aa787 to your computer and use it in GitHub Desktop.
Save michaelfward/99280f293d1e7f9aa787 to your computer and use it in GitHub Desktop.
Load a file and write it into a string declaration in your language of choice
#!/bin/usr/ruby
#reads a file and parses into a single string declaration in language of choice
#another little snippet to make my job easier when writing lots of code
#programmed by michael ward
# h3xc0ntr0l@gmail.com | gists.github.com/michaelfward
# ***************************************
# example scrips
# with readfiles
# | writefiles [file with paths] [file to write*]
# | makestring [file to write* (actually is read, but same as above)] [lang]
#****************************************
def readFile(path)
str = File.read(path).gsub("\n", '\n')
str = str.gsub('"', '\"')
str
end
def usage
puts "makestring [file to read] [language output]"
exit
end
langs = {"rb"=>"str =", "js" => "var str =", "c"=> "char str[] ="}
usage unless ARGV.length == 2
lang = ARGV[1]
path = ARGV[0]
str = readFile(path)
if langs[lang] == nil
if lang == "c++" || lang == "cpp" || lang == "c#"
puts "#{lang[c]}\"#{str}\""
else
puts "unrecognized language found. supported languages are"
langs.each_key {|k| puts " #{k}"}
exit
end
else
puts "#{langs[lang]}\" #{str}\""
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment