1
0
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:
Alexander Medvednikov 2019-06-27 17:48:49 +02:00
parent c83ebe2c66
commit 494010d2af
2 changed files with 39 additions and 105 deletions

View File

@ -71,6 +71,7 @@ mut:
cur_fn *Fn cur_fn *Fn
returns bool returns bool
vroot string vroot string
is_c_struct_init bool
} }
const ( const (
@ -1273,7 +1274,7 @@ fn (p mut Parser) name_expr() string {
// known type? int(4.5) or Color.green (enum) // known type? int(4.5) or Color.green (enum)
if p.table.known_type(name) { if p.table.known_type(name) {
// float(5), byte(0), (*int)(ptr) etc // 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 { if deref {
name += '*' name += '*'
} }
@ -1302,15 +1303,16 @@ fn (p mut Parser) name_expr() string {
p.next() p.next()
return enum_type.name return enum_type.name
} }
else { else if p.peek() == LCBR {
// go back to name start (pkg.name) // go back to name start (pkg.name)
p.scanner.pos = hack_pos p.scanner.pos = hack_pos
p.tok = hack_tok p.tok = hack_tok
p.lit = hack_lit p.lit = hack_lit
// TODO hack. If it's a C type, we may need to add struct before declaration: // 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)); // a := &C.A{} ==> struct A* a = malloc(sizeof(struct A));
if is_c_struct_init && name != 'tm' { if is_c_struct_init {
p.cgen.insert_before('struct ') p.is_c_struct_init = true
p.cgen.insert_before('struct /*c struct init*/')
} }
return p.struct_init(is_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) p.check(LCBR)
// tmp := p.get_tmp() // tmp := p.get_tmp()
if !ptr { if !ptr {
if typ == 'tm' { if p.is_c_struct_init {
p.gen('(struct tm) {')// TODO struct tm hack, handle all C structs p.gen('(struct $typ){')
p.is_c_struct_init = false
} }
else { else {
p.gen('($typ){') p.gen('($typ){')

129
os/os.v
View File

@ -24,22 +24,32 @@ struct FileInfo {
import const ( import const (
SEEK_SET SEEK_SET
SEEK_END 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.getline(voidptr, voidptr, voidptr) int
fn C.ftell(fp voidptr) int fn C.ftell(fp voidptr) int
fn C.getenv(byteptr) byteptr fn C.getenv(byteptr) byteptr
fn C.sigaction(int, voidptr, int)
fn todo_remove(){} fn todo_remove(){}
fn init_os_args(argc int, _argv *byteptr) []string { fn init_os_args(argc int, argv *byteptr) []string {
mut args := []string mut args := []string
# char** argv = (char**) _argv;
for i := 0; i < argc; i++ { for i := 0; i < argc; i++ {
arg := '' args << string(argv[i])
//arg := tos(argv[i], strlen(argv[i]))
# arg = tos((char**)(argv[i]), strlen((char**)(argv[i])));
args << arg
} }
return args return args
} }
@ -69,57 +79,17 @@ pub fn read_file(path string) ?string {
return res 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`. // file_size returns the size of the file located in `path`.
pub fn file_size(path string) int { pub fn file_size(path string) int {
# struct stat s; s := C.stat{}
# stat(path.str, &s); C.stat(path.str, &s)
// # if (S_ISLNK(s.st_mode)) return -1; return s.st_size
# 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
} }
pub fn mv(old, new string) { pub fn mv(old, new string) {
C.rename(old.cstr(), new.cstr()) 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. // read_lines reads the file in `path` into an array of lines.
// TODO return `?[]string` TODO implement `?[]` support // TODO return `?[]string` TODO implement `?[]` support
pub fn read_lines(path string) []string { pub fn read_lines(path string) []string {
@ -157,13 +127,6 @@ fn read_ulines(path string) []ustring {
return ulines return ulines
} }
/*
struct Reader {
fp *FILE
}
*/
// fn open(file string) File? { // fn open(file string) File? {
// return open_file(file) // return open_file(file)
// } // }
@ -243,7 +206,9 @@ pub fn (f File) close() {
fn close_file(fp *FILE) { fn close_file(fp *FILE) {
$if windows { $if windows {
} }
# if (fp) if isnil(fp) {
return
}
C.fclose(fp) C.fclose(fp)
} }
@ -283,30 +248,6 @@ pub fn exec(cmd string) string {
return res.trim_space() 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. // `getenv` returns the value of the environment variable named by the key.
pub fn getenv(key string) string { pub fn getenv(key string) string {
s := C.getenv(key.cstr()) s := C.getenv(key.cstr())
@ -316,7 +257,6 @@ pub fn getenv(key string) string {
return string(s) return string(s)
} }
pub fn setenv(name string, value string, overwrite bool) int { pub fn setenv(name string, value string, overwrite bool) int {
return C.setenv(name.cstr(), value.cstr(), overwrite) return C.setenv(name.cstr(), value.cstr(), overwrite)
} }
@ -325,21 +265,12 @@ pub fn unsetenv(name string) int {
return C.unsetenv(name.cstr()) return C.unsetenv(name.cstr())
} }
fn exit(code int) {
C.exit(code)
}
// `file_exists` returns true if `path` exists. // `file_exists` returns true if `path` exists.
pub fn file_exists(path string) bool { pub fn file_exists(path string) bool {
res := false
$if windows { $if windows {
# res = _access( path.str, 0 ) != -1 ; return C._access( path.str, 0 ) != -1
} }
$else { return C.access( path.str, 0 ) != -1
# res = access( path.str, 0 ) != -1 ;
}
return res
} }
pub fn dir_exists(path string) bool { pub fn dir_exists(path string) bool {
@ -480,11 +411,11 @@ fn on_segfault(f voidptr) {
return return
} }
$if mac { $if mac {
# struct sigaction sa; mut sa := C.sigaction{}
# memset(&sa, 0, sizeof(struct sigaction)); C.memset(&sa, 0, sizeof(sigaction))
# sigemptyset(&sa.sa_mask); C.sigemptyset(&sa.sa_mask)
# sa.sa_sigaction = f; sa.sa_sigaction = f
# sa.sa_flags = SA_SIGINFO; sa.sa_flags = SA_SIGINFO
# sigaction(SIGSEGV, &sa, 0); C.sigaction(SIGSEGV, &sa, 0)
} }
} }