1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: tag some File methods that take voidptr as unsafe - write_bytes, write_bytes_at (#8985)

This commit is contained in:
Nick Treleaven 2021-02-27 08:11:17 +00:00 committed by GitHub
parent 0b3f209965
commit aed348fb80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 5 additions and 3 deletions

View File

@ -78,7 +78,7 @@ fn main() {
println('ERR: $err') println('ERR: $err')
return return
} }
wrote := file.write_bytes(data, size) wrote := unsafe { file.write_bytes(data, size) }
println('write_bytes: $wrote [./google.pdf]') println('write_bytes: $wrote [./google.pdf]')
file.flush() file.flush()
file.close() file.close()

View File

@ -79,10 +79,12 @@ pub fn (mut f File) write_to(pos int, buf []byte) ?int {
return res return res
} }
[unsafe]
pub fn (mut f File) write_bytes(data voidptr, size int) int { pub fn (mut f File) write_bytes(data voidptr, size int) int {
return int(C.fwrite(data, 1, size, f.cfile)) return int(C.fwrite(data, 1, size, f.cfile))
} }
[unsafe]
pub fn (mut f File) write_bytes_at(data voidptr, size int, pos int) int { pub fn (mut f File) write_bytes_at(data voidptr, size int, pos int) int {
C.fseek(f.cfile, pos, C.SEEK_SET) C.fseek(f.cfile, pos, C.SEEK_SET)
res := int(C.fwrite(data, 1, size, f.cfile)) res := int(C.fwrite(data, 1, size, f.cfile))

View File

@ -325,7 +325,7 @@ pub fn write_file(path string, text string) ? {
// write_file_array writes the data in `buffer` to a file in `path`. // write_file_array writes the data in `buffer` to a file in `path`.
pub fn write_file_array(path string, buffer array) ? { pub fn write_file_array(path string, buffer array) ? {
mut f := create(path) ? mut f := create(path) ?
f.write_bytes_at(buffer.data, (buffer.len * buffer.element_size), 0) unsafe { f.write_bytes_at(buffer.data, (buffer.len * buffer.element_size), 0) }
f.close() f.close()
} }

View File

@ -101,7 +101,7 @@ pub fn (mut g Gen) generate_elf_footer() {
// Create the binary // Create the binary
mut f := os.create(g.out_name) or { panic(err) } mut f := os.create(g.out_name) or { panic(err) }
os.chmod(g.out_name, 0o775) // make it an executable os.chmod(g.out_name, 0o775) // make it an executable
f.write_bytes(g.buf.data, g.buf.len) unsafe { f.write_bytes(g.buf.data, g.buf.len) }
f.close() f.close()
println('\nx64 elf binary has been successfully generated') println('\nx64 elf binary has been successfully generated')
} }