v.embed_file: add .to_string() and .to_bytes() utility methods

pull/9369/head
Delyan Angelov 2021-03-20 09:30:38 +02:00
parent e3c0f305b2
commit a6ddd24f5c
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 18 additions and 4 deletions

View File

@ -3558,12 +3558,10 @@ Full list of builtin options:
#### $embed_file
```v ignore
module main
import os
fn main() {
embedded_file := $embed_file('v.png')
mut fw := os.create('exported.png') or { panic(err.msg) }
fw.write_bytes(embedded_file.data(), embedded_file.len)
fw.close()
os.write_file('exported.png', embedded_file.to_string()) ?
}
```

View File

@ -36,6 +36,22 @@ pub fn (mut ed EmbedFileData) free() {
}
}
pub fn (original &EmbedFileData) to_string() string {
unsafe {
mut ed := &EmbedFileData(original)
the_copy := memdup(ed.data(), ed.len)
return byteptr(the_copy).vstring_with_len(ed.len)
}
}
pub fn (original &EmbedFileData) to_bytes() []byte {
unsafe {
mut ed := &EmbedFileData(original)
the_copy := memdup(ed.data(), ed.len)
return the_copy.vbytes(ed.len)
}
}
pub fn (mut ed EmbedFileData) data() byteptr {
if !isnil(ed.uncompressed) {
return ed.uncompressed