2020-01-13 19:54:18 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import os
|
|
|
|
import flag
|
2020-06-24 01:00:37 +03:00
|
|
|
import strings
|
2020-01-13 19:54:18 +03:00
|
|
|
|
|
|
|
const (
|
2020-10-15 16:17:52 +03:00
|
|
|
tool_version = '0.0.4'
|
2020-01-13 19:54:18 +03:00
|
|
|
tool_description = 'Converts a list of arbitrary files into a single v module file.'
|
|
|
|
)
|
|
|
|
|
|
|
|
struct Context {
|
|
|
|
mut:
|
|
|
|
files []string
|
|
|
|
prefix string
|
|
|
|
show_help bool
|
|
|
|
module_name string
|
2020-06-24 13:56:00 +03:00
|
|
|
write_file string
|
2020-01-13 19:54:18 +03:00
|
|
|
}
|
|
|
|
|
2020-06-24 01:00:37 +03:00
|
|
|
fn (context Context) header() string {
|
|
|
|
mut header_s := ''
|
2020-10-15 16:17:52 +03:00
|
|
|
header_s += 'module $context.module_name\n'
|
2020-06-24 01:00:37 +03:00
|
|
|
header_s += '\n'
|
2020-01-13 19:54:18 +03:00
|
|
|
allfiles := context.files.join(' ')
|
2020-04-26 14:49:31 +03:00
|
|
|
mut options := []string{}
|
2020-01-13 19:54:18 +03:00
|
|
|
if context.prefix.len > 0 {
|
2020-10-15 16:17:52 +03:00
|
|
|
options << '-p $context.prefix'
|
2020-01-13 19:54:18 +03:00
|
|
|
}
|
|
|
|
if context.module_name.len > 0 {
|
2020-10-15 16:17:52 +03:00
|
|
|
options << '-m $context.module_name'
|
2020-01-13 19:54:18 +03:00
|
|
|
}
|
2020-06-24 13:56:00 +03:00
|
|
|
if context.write_file.len > 0 {
|
2020-10-15 16:17:52 +03:00
|
|
|
options << '-w $context.write_file'
|
2020-06-24 01:00:37 +03:00
|
|
|
}
|
2020-01-13 19:54:18 +03:00
|
|
|
soptions := options.join(' ')
|
2020-06-24 01:00:37 +03:00
|
|
|
header_s += '// File generated by:\n'
|
2020-11-06 16:36:14 +03:00
|
|
|
header_s += '// v bin2v $allfiles $soptions\n'
|
2020-06-24 01:00:37 +03:00
|
|
|
header_s += '// Please, do not edit this file.\n'
|
|
|
|
header_s += '// Your changes may be overwritten.\n'
|
|
|
|
header_s += 'const (\n'
|
|
|
|
return header_s
|
2020-01-13 19:54:18 +03:00
|
|
|
}
|
|
|
|
|
2020-06-24 01:00:37 +03:00
|
|
|
fn (context Context) footer() string {
|
|
|
|
return ')\n'
|
2020-01-13 19:54:18 +03:00
|
|
|
}
|
|
|
|
|
2020-11-06 16:36:14 +03:00
|
|
|
fn (context Context) file2v(bname string, fbytes []byte, bn_max int) string {
|
2020-06-24 01:00:37 +03:00
|
|
|
mut sb := strings.new_builder(1000)
|
2020-11-06 16:36:14 +03:00
|
|
|
bn_diff_len := bn_max - bname.len
|
|
|
|
sb.write('\t${bname}_len' + ' '.repeat(bn_diff_len - 4) + ' = $fbytes.len\n')
|
|
|
|
fbyte := fbytes[0]
|
2021-01-18 08:02:29 +03:00
|
|
|
bnmae_line := '\t$bname' + ' '.repeat(bn_diff_len) + ' = [byte($fbyte), '
|
|
|
|
sb.write(bnmae_line)
|
|
|
|
mut line_len := bnmae_line.len + 3
|
2020-11-06 16:36:14 +03:00
|
|
|
for i := 1; i < fbytes.len; i++ {
|
|
|
|
b := int(fbytes[i]).str()
|
2021-01-18 08:02:29 +03:00
|
|
|
if line_len > 94 {
|
|
|
|
sb.go_back(1)
|
|
|
|
sb.write('\n\t\t')
|
|
|
|
line_len = 8
|
|
|
|
}
|
|
|
|
if i == fbytes.len - 1 {
|
2020-11-06 16:36:14 +03:00
|
|
|
sb.write(b)
|
2021-01-18 08:02:29 +03:00
|
|
|
line_len += b.len
|
2020-11-06 16:36:14 +03:00
|
|
|
} else {
|
|
|
|
sb.write('$b, ')
|
2021-01-18 08:02:29 +03:00
|
|
|
line_len += b.len + 2
|
2020-11-06 16:36:14 +03:00
|
|
|
}
|
2021-01-18 08:02:29 +03:00
|
|
|
|
2020-11-06 16:36:14 +03:00
|
|
|
}
|
2021-01-13 10:32:17 +03:00
|
|
|
sb.write(']!\n')
|
2020-11-06 16:36:14 +03:00
|
|
|
return sb.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (context Context) bname_and_bytes(file string) ?(string, []byte) {
|
2020-03-19 17:49:07 +03:00
|
|
|
fname := os.file_name(file)
|
2021-01-07 23:37:36 +03:00
|
|
|
fname_escpaed := fname.replace_each(['.', '_', '-', '_'])
|
|
|
|
byte_name := '$context.prefix$fname_escpaed'.to_lower()
|
|
|
|
fbytes := os.read_bytes(file) or { return error('Error: $err') }
|
2020-11-06 16:36:14 +03:00
|
|
|
return byte_name, fbytes
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (context Context) max_bname_len(bnames []string) int {
|
|
|
|
mut max := 0
|
|
|
|
for n in bnames {
|
|
|
|
if n.len > max {
|
|
|
|
max = n.len
|
2020-01-13 19:54:18 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-06 16:36:14 +03:00
|
|
|
// Add 4 to max due to "_len" suffix
|
|
|
|
return max + 4
|
2020-01-13 19:54:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
mut context := Context{}
|
|
|
|
mut fp := flag.new_flag_parser(os.args[1..])
|
|
|
|
fp.application('v bin2v')
|
|
|
|
fp.version(tool_version)
|
|
|
|
fp.description(tool_description)
|
|
|
|
fp.arguments_description('FILE [FILE]...')
|
2020-03-19 09:06:37 +03:00
|
|
|
context.show_help = fp.bool('help', `h`, false, 'Show this help screen.')
|
2020-06-24 01:00:37 +03:00
|
|
|
context.module_name = fp.string('module', `m`, 'binary', 'Name of the generated module.')
|
|
|
|
context.prefix = fp.string('prefix', `p`, '', 'A prefix put before each resource name.')
|
2020-06-24 13:56:00 +03:00
|
|
|
context.write_file = fp.string('write', `w`, '', 'Write directly to a file with the given name.')
|
2020-03-29 11:08:42 +03:00
|
|
|
if context.show_help {
|
2020-01-13 19:54:18 +03:00
|
|
|
println(fp.usage())
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
files := fp.finalize() or {
|
|
|
|
eprintln('Error: ' + err)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
real_files := files.filter(it != 'bin2v')
|
|
|
|
if real_files.len == 0 {
|
|
|
|
println(fp.usage())
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
context.files = real_files
|
2021-01-06 20:19:48 +03:00
|
|
|
if context.write_file != '' && os.file_ext(context.write_file) !in ['.vv', '.v'] {
|
2020-06-24 15:13:31 +03:00
|
|
|
context.write_file += '.v'
|
|
|
|
}
|
2020-11-06 16:36:14 +03:00
|
|
|
mut file_byte_map := map[string][]byte{}
|
|
|
|
for file in real_files {
|
|
|
|
bname, fbytes := context.bname_and_bytes(file) or {
|
|
|
|
eprintln(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
file_byte_map[bname] = fbytes
|
|
|
|
}
|
|
|
|
max_bname := context.max_bname_len(file_byte_map.keys())
|
2020-06-24 13:56:00 +03:00
|
|
|
if context.write_file.len > 0 {
|
2021-01-07 23:37:36 +03:00
|
|
|
mut out_file := os.create(context.write_file) or { panic(err) }
|
2020-11-15 23:54:47 +03:00
|
|
|
out_file.write_str(context.header())
|
2020-11-06 16:36:14 +03:00
|
|
|
for bname, fbytes in file_byte_map {
|
2020-11-15 23:54:47 +03:00
|
|
|
out_file.write_str(context.file2v(bname, fbytes, max_bname))
|
2020-06-24 01:00:37 +03:00
|
|
|
}
|
2020-11-15 23:54:47 +03:00
|
|
|
out_file.write_str(context.footer())
|
2020-10-15 16:17:52 +03:00
|
|
|
} else {
|
2021-01-06 20:19:48 +03:00
|
|
|
print(context.header())
|
2020-11-06 16:36:14 +03:00
|
|
|
for bname, fbytes in file_byte_map {
|
2021-01-06 20:19:48 +03:00
|
|
|
print(context.file2v(bname, fbytes, max_bname))
|
2020-06-24 01:00:37 +03:00
|
|
|
}
|
2021-01-06 20:19:48 +03:00
|
|
|
print(context.footer())
|
2020-01-13 19:54:18 +03:00
|
|
|
}
|
|
|
|
}
|