mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v.embed_file: add .to_string() and .to_bytes() utility methods
This commit is contained in:
parent
e3c0f305b2
commit
a6ddd24f5c
@ -3558,12 +3558,10 @@ Full list of builtin options:
|
|||||||
#### $embed_file
|
#### $embed_file
|
||||||
|
|
||||||
```v ignore
|
```v ignore
|
||||||
module main
|
import os
|
||||||
fn main() {
|
fn main() {
|
||||||
embedded_file := $embed_file('v.png')
|
embedded_file := $embed_file('v.png')
|
||||||
mut fw := os.create('exported.png') or { panic(err.msg) }
|
os.write_file('exported.png', embedded_file.to_string()) ?
|
||||||
fw.write_bytes(embedded_file.data(), embedded_file.len)
|
|
||||||
fw.close()
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -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 {
|
pub fn (mut ed EmbedFileData) data() byteptr {
|
||||||
if !isnil(ed.uncompressed) {
|
if !isnil(ed.uncompressed) {
|
||||||
return ed.uncompressed
|
return ed.uncompressed
|
||||||
|
Loading…
Reference in New Issue
Block a user