mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os.v: replace remaining C code with V
This commit is contained in:
parent
c83ebe2c66
commit
494010d2af
@ -71,6 +71,7 @@ mut:
|
||||
cur_fn *Fn
|
||||
returns bool
|
||||
vroot string
|
||||
is_c_struct_init bool
|
||||
}
|
||||
|
||||
const (
|
||||
@ -1273,7 +1274,7 @@ fn (p mut Parser) name_expr() string {
|
||||
// known type? int(4.5) or Color.green (enum)
|
||||
if p.table.known_type(name) {
|
||||
// float(5), byte(0), (*int)(ptr) etc
|
||||
if p.peek() == LPAR || (deref && p.peek() == RPAR) {
|
||||
if !is_c && ( p.peek() == LPAR || (deref && p.peek() == RPAR) ) {
|
||||
if deref {
|
||||
name += '*'
|
||||
}
|
||||
@ -1302,15 +1303,16 @@ fn (p mut Parser) name_expr() string {
|
||||
p.next()
|
||||
return enum_type.name
|
||||
}
|
||||
else {
|
||||
else if p.peek() == LCBR {
|
||||
// go back to name start (pkg.name)
|
||||
p.scanner.pos = hack_pos
|
||||
p.tok = hack_tok
|
||||
p.lit = hack_lit
|
||||
// TODO hack. If it's a C type, we may need to add struct before declaration:
|
||||
// a := &C.A{} ==> struct A* a = malloc(sizeof(struct A));
|
||||
if is_c_struct_init && name != 'tm' {
|
||||
p.cgen.insert_before('struct ')
|
||||
if is_c_struct_init {
|
||||
p.is_c_struct_init = true
|
||||
p.cgen.insert_before('struct /*c struct init*/')
|
||||
}
|
||||
return p.struct_init(is_c_struct_init)
|
||||
}
|
||||
@ -2351,8 +2353,9 @@ fn (p mut Parser) struct_init(is_c_struct_init bool) string {
|
||||
p.check(LCBR)
|
||||
// tmp := p.get_tmp()
|
||||
if !ptr {
|
||||
if typ == 'tm' {
|
||||
p.gen('(struct tm) {')// TODO struct tm hack, handle all C structs
|
||||
if p.is_c_struct_init {
|
||||
p.gen('(struct $typ){')
|
||||
p.is_c_struct_init = false
|
||||
}
|
||||
else {
|
||||
p.gen('($typ){')
|
||||
|
129
os/os.v
129
os/os.v
@ -24,22 +24,32 @@ struct FileInfo {
|
||||
import const (
|
||||
SEEK_SET
|
||||
SEEK_END
|
||||
SA_SIGINFO
|
||||
SIGSEGV
|
||||
)
|
||||
|
||||
struct C.stat {
|
||||
st_size int
|
||||
}
|
||||
|
||||
struct C.sigaction {
|
||||
mut:
|
||||
sa_mask int
|
||||
sa_sigaction int
|
||||
sa_flags int
|
||||
}
|
||||
|
||||
fn C.getline(voidptr, voidptr, voidptr) int
|
||||
fn C.ftell(fp voidptr) int
|
||||
fn C.getenv(byteptr) byteptr
|
||||
fn C.sigaction(int, voidptr, int)
|
||||
|
||||
fn todo_remove(){}
|
||||
|
||||
fn init_os_args(argc int, _argv *byteptr) []string {
|
||||
fn init_os_args(argc int, argv *byteptr) []string {
|
||||
mut args := []string
|
||||
# char** argv = (char**) _argv;
|
||||
for i := 0; i < argc; i++ {
|
||||
arg := ''
|
||||
//arg := tos(argv[i], strlen(argv[i]))
|
||||
# arg = tos((char**)(argv[i]), strlen((char**)(argv[i])));
|
||||
args << arg
|
||||
args << string(argv[i])
|
||||
}
|
||||
return args
|
||||
}
|
||||
@ -69,57 +79,17 @@ pub fn read_file(path string) ?string {
|
||||
return res
|
||||
}
|
||||
|
||||
/*
|
||||
// TODO
|
||||
fn (f File) read_rune() string {
|
||||
# if (!f.cfile) return tos("", 0);
|
||||
c := malloc(1)
|
||||
C.fread(c, 1, 1, f.cfile)
|
||||
return tos(c, 1)
|
||||
}
|
||||
*/
|
||||
|
||||
// file_size returns the size of the file located in `path`.
|
||||
pub fn file_size(path string) int {
|
||||
# struct stat s;
|
||||
# stat(path.str, &s);
|
||||
// # if (S_ISLNK(s.st_mode)) return -1;
|
||||
# return s.st_size;
|
||||
// //////////////////////
|
||||
# FILE *f = fopen(path.str, "r");
|
||||
# if (!f) return 0;
|
||||
# fseek(f, 0, SEEK_END);
|
||||
# long fsize = ftell(f);
|
||||
// # fseek(f, 0, SEEK_SET); //same as rewind(f);
|
||||
# rewind(f);
|
||||
# return fsize;
|
||||
return 0
|
||||
s := C.stat{}
|
||||
C.stat(path.str, &s)
|
||||
return s.st_size
|
||||
}
|
||||
|
||||
pub fn mv(old, new string) {
|
||||
C.rename(old.cstr(), new.cstr())
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn file_last_mod_unix(path string) int {
|
||||
# struct stat attr;
|
||||
# stat(path.str, &attr);
|
||||
# return attr.st_mtime ;
|
||||
return 0
|
||||
}
|
||||
|
||||
pub fn file_last_mod_time(path string) time.Time {
|
||||
return time.now()
|
||||
q := C.tm{}
|
||||
# struct stat attr;
|
||||
# stat(path.str, &attr);
|
||||
// # q = attr.st_mtime;
|
||||
# struct tm * now = localtime(&attr.st_mtime);
|
||||
# q = *now;
|
||||
# printf("Last modified time: %s", ctime(&attr.st_mtime));
|
||||
return time.convert_ctime(q)
|
||||
}
|
||||
*/
|
||||
// read_lines reads the file in `path` into an array of lines.
|
||||
// TODO return `?[]string` TODO implement `?[]` support
|
||||
pub fn read_lines(path string) []string {
|
||||
@ -157,13 +127,6 @@ fn read_ulines(path string) []ustring {
|
||||
return ulines
|
||||
}
|
||||
|
||||
/*
|
||||
struct Reader {
|
||||
fp *FILE
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
// fn open(file string) File? {
|
||||
// return open_file(file)
|
||||
// }
|
||||
@ -243,7 +206,9 @@ pub fn (f File) close() {
|
||||
fn close_file(fp *FILE) {
|
||||
$if windows {
|
||||
}
|
||||
# if (fp)
|
||||
if isnil(fp) {
|
||||
return
|
||||
}
|
||||
C.fclose(fp)
|
||||
}
|
||||
|
||||
@ -283,30 +248,6 @@ pub fn exec(cmd string) string {
|
||||
return res.trim_space()
|
||||
}
|
||||
|
||||
/*
|
||||
// TODO
|
||||
fn system_into_lines(s string) []string {
|
||||
mut res := []string
|
||||
cmd := '$s 2>&1'
|
||||
max := 5000
|
||||
$if windows {
|
||||
# FILE* f = _popen(cmd.str, "r");
|
||||
}
|
||||
$else {
|
||||
# FILE* f = popen(cmd.str, "r");
|
||||
}
|
||||
# char * buf = malloc(sizeof(char) * max);
|
||||
# while (fgets(buf, max, f) != NULL)
|
||||
{
|
||||
val := ''
|
||||
# buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores
|
||||
# val=tos_clone(buf);
|
||||
res << val
|
||||
}
|
||||
return res
|
||||
}
|
||||
*/
|
||||
|
||||
// `getenv` returns the value of the environment variable named by the key.
|
||||
pub fn getenv(key string) string {
|
||||
s := C.getenv(key.cstr())
|
||||
@ -316,7 +257,6 @@ pub fn getenv(key string) string {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
|
||||
pub fn setenv(name string, value string, overwrite bool) int {
|
||||
return C.setenv(name.cstr(), value.cstr(), overwrite)
|
||||
}
|
||||
@ -325,21 +265,12 @@ pub fn unsetenv(name string) int {
|
||||
return C.unsetenv(name.cstr())
|
||||
}
|
||||
|
||||
fn exit(code int) {
|
||||
C.exit(code)
|
||||
}
|
||||
|
||||
|
||||
// `file_exists` returns true if `path` exists.
|
||||
pub fn file_exists(path string) bool {
|
||||
res := false
|
||||
$if windows {
|
||||
# res = _access( path.str, 0 ) != -1 ;
|
||||
return C._access( path.str, 0 ) != -1
|
||||
}
|
||||
$else {
|
||||
# res = access( path.str, 0 ) != -1 ;
|
||||
}
|
||||
return res
|
||||
return C.access( path.str, 0 ) != -1
|
||||
}
|
||||
|
||||
pub fn dir_exists(path string) bool {
|
||||
@ -480,11 +411,11 @@ fn on_segfault(f voidptr) {
|
||||
return
|
||||
}
|
||||
$if mac {
|
||||
# struct sigaction sa;
|
||||
# memset(&sa, 0, sizeof(struct sigaction));
|
||||
# sigemptyset(&sa.sa_mask);
|
||||
# sa.sa_sigaction = f;
|
||||
# sa.sa_flags = SA_SIGINFO;
|
||||
# sigaction(SIGSEGV, &sa, 0);
|
||||
mut sa := C.sigaction{}
|
||||
C.memset(&sa, 0, sizeof(sigaction))
|
||||
C.sigemptyset(&sa.sa_mask)
|
||||
sa.sa_sigaction = f
|
||||
sa.sa_flags = SA_SIGINFO
|
||||
C.sigaction(SIGSEGV, &sa, 0)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user