vfmt: fix `sizeof(C.Type)`

pull/5917/head
Delyan Angelov 2020-07-21 16:28:30 +03:00
parent e1eb9c4ff1
commit 4cb9e651a6
2 changed files with 35 additions and 20 deletions

View File

@ -6,14 +6,13 @@ import strings
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/utsname.h> #include <sys/utsname.h>
pub const ( pub const (
path_separator = '/' path_separator = '/'
path_delimiter = ':' path_delimiter = ':'
) )
const ( const (
stdin_value = 0 stdin_value = 0
stdout_value = 1 stdout_value = 1
stderr_value = 2 stderr_value = 2
) )
@ -28,11 +27,14 @@ mut:
} }
fn C.uname(name voidptr) int fn C.uname(name voidptr) int
fn C.symlink(charptr, charptr) int
fn C.symlink(arg_1, arg_2 charptr) int
pub fn uname() Uname { pub fn uname() Uname {
mut u := Uname{} mut u := Uname{}
d := &C.utsname( malloc(int(sizeof(C.utsname))) ) utsize := sizeof(C.utsname)
x := malloc(int(utsize))
d := &C.utsname(x)
if C.uname(d) == 0 { if C.uname(d) == 0 {
u.sysname = cstring_to_vstring(byteptr(d.sysname)) u.sysname = cstring_to_vstring(byteptr(d.sysname))
u.nodename = cstring_to_vstring(byteptr(d.nodename)) u.nodename = cstring_to_vstring(byteptr(d.nodename))
@ -46,11 +48,10 @@ pub fn uname() Uname {
fn init_os_args(argc int, argv &&byte) []string { fn init_os_args(argc int, argv &&byte) []string {
mut args := []string{} mut args := []string{}
//mut args := []string(make(0, argc, sizeof(string))) // mut args := []string(make(0, argc, sizeof(string)))
//mut args := []string{len:argc} // mut args := []string{len:argc}
for i in 0 .. argc { for i in 0 .. argc {
// args [i] = string(argv[i])
//args [i] = string(argv[i])
args << string(argv[i]) args << string(argv[i])
} }
return args return args
@ -91,13 +92,10 @@ pub fn is_dir(path string) bool {
return res return res
} }
*/ */
/* /*
pub fn (mut f File) fseek(pos, mode int) { pub fn (mut f File) fseek(pos, mode int) {
} }
*/ */
// mkdir creates a new directory with the specified path. // mkdir creates a new directory with the specified path.
pub fn mkdir(path string) ?bool { pub fn mkdir(path string) ?bool {
if path == '.' { if path == '.' {
@ -109,8 +107,8 @@ pub fn mkdir(path string) ?bool {
k = 1 k = 1
} }
*/ */
apath := os.real_path(path) apath := real_path(path)
/* /*
$if linux { $if linux {
$if !android { $if !android {
ret := C.syscall(sys_mkdir, apath.str, 511) ret := C.syscall(sys_mkdir, apath.str, 511)
@ -120,8 +118,11 @@ pub fn mkdir(path string) ?bool {
return true return true
} }
} }
*/ */
r := unsafe {C.mkdir(charptr(apath.str), 511)} r := unsafe {
C.mkdir(charptr(apath.str), 511)
}
if r == -1 { if r == -1 {
return error(posix_get_error_msg(C.errno)) return error(posix_get_error_msg(C.errno))
} }
@ -142,10 +143,10 @@ pub fn exec(cmd string) ?Result {
mut res := strings.new_builder(1024) mut res := strings.new_builder(1024)
for C.fgets(charptr(buf), 4096, f) != 0 { for C.fgets(charptr(buf), 4096, f) != 0 {
bufbp := byteptr(buf) bufbp := byteptr(buf)
res.write_bytes( bufbp, vstrlen(bufbp) ) res.write_bytes(bufbp, vstrlen(bufbp))
} }
soutput := res.str() soutput := res.str()
//res.free() // res.free()
exit_code := vpclose(f) exit_code := vpclose(f)
// if exit_code != 0 { // if exit_code != 0 {
// return error(res) // return error(res)
@ -174,14 +175,14 @@ pub fn (mut f File) close() {
return return
} }
f.opened = false f.opened = false
/* /*
$if linux { $if linux {
$if !android { $if !android {
C.syscall(sys_close, f.fd) C.syscall(sys_close, f.fd)
return return
} }
} }
*/ */
C.fflush(f.cfile) C.fflush(f.cfile)
C.fclose(f.cfile) C.fclose(f.cfile)
} }

View File

@ -914,7 +914,11 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
if node.is_type { if node.is_type {
f.write('sizeof(') f.write('sizeof(')
if node.type_name != '' { if node.type_name != '' {
f.write(f.short_module(node.type_name)) if f.is_external_name(node.type_name) {
f.write(node.type_name)
} else {
f.write(f.short_module(node.type_name))
}
} else { } else {
f.write(f.type_to_str(node.typ)) f.write(f.type_to_str(node.typ))
} }
@ -1732,3 +1736,13 @@ pub fn (mut f Fmt) const_decl(it ast.ConstDecl) {
f.indent-- f.indent--
f.writeln(')\n') f.writeln(')\n')
} }
fn (mut f Fmt) is_external_name(name string) bool {
if name.len > 2 && name[0]==`C` && name[1]==`.` {
return true
}
if name.len > 3 && name[0]==`J` && name[1]==`S` && name[2] == `.` {
return true
}
return false
}