Strip semicolons

This commit is contained in:
gingerBill
2021-09-19 11:59:44 +01:00
parent ceebd7b23c
commit ee876ad66b
20 changed files with 517 additions and 514 deletions

View File

@@ -5,8 +5,8 @@ package os2
// To distinguish between an empty value and an unset value, use lookup_env
// NOTE: the value will be allocated with the supplied allocator
get_env :: proc(key: string, allocator := context.allocator) -> string {
value, _ := lookup_env(key, allocator);
return value;
value, _ := lookup_env(key, allocator)
return value
}
// lookup_env gets the value of the environment variable named by the key
@@ -14,30 +14,30 @@ get_env :: proc(key: string, allocator := context.allocator) -> string {
// Otherwise the returned value will be empty and the boolean will be false
// NOTE: the value will be allocated with the supplied allocator
lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
return _lookup_env(key, allocator);
return _lookup_env(key, allocator)
}
// set_env sets the value of the environment variable named by the key
// Returns true on success, false on failure
set_env :: proc(key, value: string) -> bool {
return _set_env(key, value);
return _set_env(key, value)
}
// unset_env unsets a single environment variable
// Returns true on success, false on failure
unset_env :: proc(key: string) -> bool {
return _unset_env(key);
return _unset_env(key)
}
clear_env :: proc() {
_clear_env();
_clear_env()
}
// environ returns a copy of strings representing the environment, in the form "key=value"
// NOTE: the slice of strings and the strings with be allocated using the supplied allocator
environ :: proc(allocator := context.allocator) -> []string {
return _environ(allocator);
return _environ(allocator)
}

View File

@@ -6,75 +6,75 @@ import win32 "core:sys/windows"
_lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
if key == "" {
return;
return
}
wkey := win32.utf8_to_wstring(key);
b := make([dynamic]u16, 100, context.temp_allocator);
wkey := win32.utf8_to_wstring(key)
b := make([dynamic]u16, 100, context.temp_allocator)
for {
n := win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b)));
n := win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b)))
if n == 0 {
err := win32.GetLastError();
err := win32.GetLastError()
if err == win32.ERROR_ENVVAR_NOT_FOUND {
return "", false;
return "", false
}
}
if n <= u32(len(b)) {
value = win32.utf16_to_utf8(b[:n], allocator);
found = true;
return;
value = win32.utf16_to_utf8(b[:n], allocator)
found = true
return
}
resize(&b, len(b)*2);
resize(&b, len(b)*2)
}
}
_set_env :: proc(key, value: string) -> bool {
k := win32.utf8_to_wstring(key);
v := win32.utf8_to_wstring(value);
k := win32.utf8_to_wstring(key)
v := win32.utf8_to_wstring(value)
return bool(win32.SetEnvironmentVariableW(k, v));
return bool(win32.SetEnvironmentVariableW(k, v))
}
_unset_env :: proc(key: string) -> bool {
k := win32.utf8_to_wstring(key);
return bool(win32.SetEnvironmentVariableW(k, nil));
k := win32.utf8_to_wstring(key)
return bool(win32.SetEnvironmentVariableW(k, nil))
}
_clear_env :: proc() {
envs := environ(context.temp_allocator);
envs := environ(context.temp_allocator)
for env in envs {
for j in 1..<len(env) {
if env[j] == '=' {
unset_env(env[0:j]);
break;
unset_env(env[0:j])
break
}
}
}
}
_environ :: proc(allocator := context.allocator) -> []string {
envs := win32.GetEnvironmentStringsW();
envs := win32.GetEnvironmentStringsW()
if envs == nil {
return nil;
return nil
}
defer win32.FreeEnvironmentStringsW(envs);
defer win32.FreeEnvironmentStringsW(envs)
r := make([dynamic]string, 0, 50, allocator);
r := make([dynamic]string, 0, 50, allocator)
for from, i, p := 0, 0, envs; true; i += 1 {
c := (^u16)(uintptr(p) + uintptr(i*2))^;
c := (^u16)(uintptr(p) + uintptr(i*2))^
if c == 0 {
if i <= from {
break;
break
}
w := mem.slice_ptr(mem.ptr_offset(p, from), i-from);
w := mem.slice_ptr(mem.ptr_offset(p, from), i-from)
append(&r, win32.utf16_to_utf8(w, allocator));
from = i + 1;
append(&r, win32.utf16_to_utf8(w, allocator))
from = i + 1
}
}
return r[:];
return r[:]
}

View File

@@ -22,7 +22,7 @@ Error :: union {
io.Error,
Platform_Error,
}
#assert(size_of(Error) == size_of(u64));
#assert(size_of(Error) == size_of(u64))
Path_Error :: struct {
op: string,
@@ -39,56 +39,56 @@ Link_Error :: struct {
path_error_delete :: proc(perr: Maybe(Path_Error)) {
if err, ok := perr.?; ok {
context.allocator = error_allocator();
delete(err.op);
delete(err.path);
context.allocator = error_allocator()
delete(err.op)
delete(err.path)
}
}
link_error_delete :: proc(lerr: Maybe(Link_Error)) {
if err, ok := lerr.?; ok {
context.allocator = error_allocator();
delete(err.op);
delete(err.old);
delete(err.new);
context.allocator = error_allocator()
delete(err.op)
delete(err.old)
delete(err.new)
}
}
is_platform_error :: proc(ferr: Error) -> (err: i32, ok: bool) {
v := ferr.(Platform_Error) or_else {};
return v.err, v.err != 0;
v := ferr.(Platform_Error) or_else {}
return v.err, v.err != 0
}
error_string :: proc(ferr: Error) -> string {
switch ferr {
case nil: return "";
case .Invalid_Argument: return "invalid argument";
case .Permission_Denied: return "permission denied";
case .Exist: return "file already exists";
case .Not_Exist: return "file does not exist";
case .Closed: return "file already closed";
case .Timeout: return "i/o timeout";
case .EOF: return "eof";
case .Unexpected_EOF: return "unexpected eof";
case .Short_Write: return "short write";
case .Invalid_Write: return "invalid write result";
case .Short_Buffer: return "short buffer";
case .No_Progress: return "multiple read calls return no data or error";
case .Invalid_Whence: return "invalid whence";
case .Invalid_Offset: return "invalid offset";
case .Invalid_Unread: return "invalid unread";
case .Negative_Read: return "negative read";
case .Negative_Write: return "negative write";
case .Negative_Count: return "negative count";
case .Buffer_Full: return "buffer full";
case nil: return ""
case .Invalid_Argument: return "invalid argument"
case .Permission_Denied: return "permission denied"
case .Exist: return "file already exists"
case .Not_Exist: return "file does not exist"
case .Closed: return "file already closed"
case .Timeout: return "i/o timeout"
case .EOF: return "eof"
case .Unexpected_EOF: return "unexpected eof"
case .Short_Write: return "short write"
case .Invalid_Write: return "invalid write result"
case .Short_Buffer: return "short buffer"
case .No_Progress: return "multiple read calls return no data or error"
case .Invalid_Whence: return "invalid whence"
case .Invalid_Offset: return "invalid offset"
case .Invalid_Unread: return "invalid unread"
case .Negative_Read: return "negative read"
case .Negative_Write: return "negative write"
case .Negative_Count: return "negative count"
case .Buffer_Full: return "buffer full"
}
if errno, ok := is_platform_error(ferr); ok {
return _error_string(errno);
return _error_string(errno)
}
return "unknown error";
return "unknown error"
}

View File

@@ -4,11 +4,11 @@ package os2
import win32 "core:sys/windows"
_error_string :: proc(errno: i32) -> string {
e := win32.DWORD(errno);
e := win32.DWORD(errno)
if e == 0 {
return "";
return ""
}
// TODO(bill): _error_string for windows
// FormatMessageW
return "";
return ""
}

View File

@@ -3,7 +3,7 @@ package os2
import "core:io"
import "core:time"
Handle :: distinct uintptr;
Handle :: distinct uintptr
Seek_From :: enum {
Start = 0, // seek relative to the origin of the file
@@ -11,148 +11,148 @@ Seek_From :: enum {
End = 2, // seek relative to the end
}
File_Mode :: distinct u32;
File_Mode_Dir :: File_Mode(1<<16);
File_Mode_Named_Pipe :: File_Mode(1<<17);
File_Mode_Device :: File_Mode(1<<18);
File_Mode_Char_Device :: File_Mode(1<<19);
File_Mode_Sym_Link :: File_Mode(1<<20);
File_Mode :: distinct u32
File_Mode_Dir :: File_Mode(1<<16)
File_Mode_Named_Pipe :: File_Mode(1<<17)
File_Mode_Device :: File_Mode(1<<18)
File_Mode_Char_Device :: File_Mode(1<<19)
File_Mode_Sym_Link :: File_Mode(1<<20)
O_RDONLY :: int( 0);
O_WRONLY :: int( 1);
O_RDWR :: int( 2);
O_APPEND :: int( 4);
O_CREATE :: int( 8);
O_EXCL :: int(16);
O_SYNC :: int(32);
O_TRUNC :: int(64);
O_RDONLY :: int( 0)
O_WRONLY :: int( 1)
O_RDWR :: int( 2)
O_APPEND :: int( 4)
O_CREATE :: int( 8)
O_EXCL :: int(16)
O_SYNC :: int(32)
O_TRUNC :: int(64)
stdin: Handle = 0; // OS-Specific
stdout: Handle = 1; // OS-Specific
stderr: Handle = 2; // OS-Specific
stdin: Handle = 0 // OS-Specific
stdout: Handle = 1 // OS-Specific
stderr: Handle = 2 // OS-Specific
create :: proc(name: string) -> (Handle, Error) {
return _create(name);
return _create(name)
}
open :: proc(name: string) -> (Handle, Error) {
return _open(name);
return _open(name)
}
open_file :: proc(name: string, flag: int, perm: File_Mode) -> (Handle, Error) {
return _open_file(name, flag, perm);
return _open_file(name, flag, perm)
}
close :: proc(fd: Handle) -> Error {
return _close(fd);
return _close(fd)
}
name :: proc(fd: Handle, allocator := context.allocator) -> string {
return _name(fd);
return _name(fd)
}
seek :: proc(fd: Handle, offset: i64, whence: Seek_From) -> (ret: i64, err: Error) {
return _seek(fd, offset, whence);
return _seek(fd, offset, whence)
}
read :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) {
return _read(fd, p);
return _read(fd, p)
}
read_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) {
return _read_at(fd, p, offset);
return _read_at(fd, p, offset)
}
read_from :: proc(fd: Handle, r: io.Reader) -> (n: i64, err: Error) {
return _read_from(fd, r);
return _read_from(fd, r)
}
write :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) {
return _write(fd, p);
return _write(fd, p)
}
write_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) {
return _write_at(fd, p, offset);
return _write_at(fd, p, offset)
}
write_to :: proc(fd: Handle, w: io.Writer) -> (n: i64, err: Error) {
return _write_to(fd, w);
return _write_to(fd, w)
}
file_size :: proc(fd: Handle) -> (n: i64, err: Error) {
return _file_size(fd);
return _file_size(fd)
}
sync :: proc(fd: Handle) -> Error {
return _sync(fd);
return _sync(fd)
}
flush :: proc(fd: Handle) -> Error {
return _flush(fd);
return _flush(fd)
}
truncate :: proc(fd: Handle, size: i64) -> Maybe(Path_Error) {
return _truncate(fd, size);
return _truncate(fd, size)
}
remove :: proc(name: string) -> Maybe(Path_Error) {
return _remove(name);
return _remove(name)
}
rename :: proc(old_path, new_path: string) -> Maybe(Path_Error) {
return _rename(old_path, new_path);
return _rename(old_path, new_path)
}
link :: proc(old_name, new_name: string) -> Maybe(Link_Error) {
return _link(old_name, new_name);
return _link(old_name, new_name)
}
symlink :: proc(old_name, new_name: string) -> Maybe(Link_Error) {
return _symlink(old_name, new_name);
return _symlink(old_name, new_name)
}
read_link :: proc(name: string) -> (string, Maybe(Path_Error)) {
return _read_link(name);
return _read_link(name)
}
chdir :: proc(fd: Handle) -> Error {
return _chdir(fd);
return _chdir(fd)
}
chmod :: proc(fd: Handle, mode: File_Mode) -> Error {
return _chmod(fd, mode);
return _chmod(fd, mode)
}
chown :: proc(fd: Handle, uid, gid: int) -> Error {
return _chown(fd, uid, gid);
return _chown(fd, uid, gid)
}
lchown :: proc(name: string, uid, gid: int) -> Error {
return _lchown(name, uid, gid);
return _lchown(name, uid, gid)
}
chtimes :: proc(name: string, atime, mtime: time.Time) -> Maybe(Path_Error) {
return _chtimes(name, atime, mtime);
return _chtimes(name, atime, mtime)
}
exists :: proc(path: string) -> bool {
return _exists(path);
return _exists(path)
}
is_file :: proc(path: string) -> bool {
return _is_file(path);
return _is_file(path)
}
is_dir :: proc(path: string) -> bool {
return _is_dir(path);
return _is_dir(path)
}

View File

@@ -3,83 +3,83 @@ package os2
import "core:io"
file_to_stream :: proc(fd: Handle) -> (s: io.Stream) {
s.stream_data = rawptr(uintptr(fd));
s.stream_vtable = _file_stream_vtable;
return;
s.stream_data = rawptr(uintptr(fd))
s.stream_vtable = _file_stream_vtable
return
}
@(private)
error_to_io_error :: proc(ferr: Error) -> io.Error {
if ferr == nil {
return .None;
return .None
}
return ferr.(io.Error) or_else .Unknown;
return ferr.(io.Error) or_else .Unknown
}
@(private)
_file_stream_vtable := &io.Stream_VTable{
impl_read = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
fd := Handle(uintptr(s.stream_data));
ferr: Error;
n, ferr = read(fd, p);
err = error_to_io_error(ferr);
return;
fd := Handle(uintptr(s.stream_data))
ferr: Error
n, ferr = read(fd, p)
err = error_to_io_error(ferr)
return
},
impl_read_at = proc(s: io.Stream, p: []byte, offset: i64) -> (n: int, err: io.Error) {
fd := Handle(uintptr(s.stream_data));
ferr: Error;
n, ferr = read_at(fd, p, offset);
err = error_to_io_error(ferr);
return;
fd := Handle(uintptr(s.stream_data))
ferr: Error
n, ferr = read_at(fd, p, offset)
err = error_to_io_error(ferr)
return
},
impl_write_to = proc(s: io.Stream, w: io.Writer) -> (n: i64, err: io.Error) {
fd := Handle(uintptr(s.stream_data));
ferr: Error;
n, ferr = write_to(fd, w);
err = error_to_io_error(ferr);
return;
fd := Handle(uintptr(s.stream_data))
ferr: Error
n, ferr = write_to(fd, w)
err = error_to_io_error(ferr)
return
},
impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
fd := Handle(uintptr(s.stream_data));
ferr: Error;
n, ferr = write(fd, p);
err = error_to_io_error(ferr);
return;
fd := Handle(uintptr(s.stream_data))
ferr: Error
n, ferr = write(fd, p)
err = error_to_io_error(ferr)
return
},
impl_write_at = proc(s: io.Stream, p: []byte, offset: i64) -> (n: int, err: io.Error) {
fd := Handle(uintptr(s.stream_data));
ferr: Error;
n, ferr = write_at(fd, p, offset);
err = error_to_io_error(ferr);
return;
fd := Handle(uintptr(s.stream_data))
ferr: Error
n, ferr = write_at(fd, p, offset)
err = error_to_io_error(ferr)
return
},
impl_read_from = proc(s: io.Stream, r: io.Reader) -> (n: i64, err: io.Error) {
fd := Handle(uintptr(s.stream_data));
ferr: Error;
n, ferr = read_from(fd, r);
err = error_to_io_error(ferr);
return;
fd := Handle(uintptr(s.stream_data))
ferr: Error
n, ferr = read_from(fd, r)
err = error_to_io_error(ferr)
return
},
impl_seek = proc(s: io.Stream, offset: i64, whence: io.Seek_From) -> (i64, io.Error) {
fd := Handle(uintptr(s.stream_data));
n, ferr := seek(fd, offset, Seek_From(whence));
err := error_to_io_error(ferr);
return n, err;
fd := Handle(uintptr(s.stream_data))
n, ferr := seek(fd, offset, Seek_From(whence))
err := error_to_io_error(ferr)
return n, err
},
impl_size = proc(s: io.Stream) -> i64 {
fd := Handle(uintptr(s.stream_data));
sz, _ := file_size(fd);
return sz;
fd := Handle(uintptr(s.stream_data))
sz, _ := file_size(fd)
return sz
},
impl_flush = proc(s: io.Stream) -> io.Error {
fd := Handle(uintptr(s.stream_data));
ferr := flush(fd);
return error_to_io_error(ferr);
fd := Handle(uintptr(s.stream_data))
ferr := flush(fd)
return error_to_io_error(ferr)
},
impl_close = proc(s: io.Stream) -> io.Error {
fd := Handle(uintptr(s.stream_data));
ferr := close(fd);
return error_to_io_error(ferr);
fd := Handle(uintptr(s.stream_data))
ferr := close(fd)
return error_to_io_error(ferr)
},
};
}

View File

@@ -6,118 +6,118 @@ import "core:strconv"
import "core:unicode/utf8"
write_string :: proc(fd: Handle, s: string) -> (n: int, err: Error) {
return write(fd, transmute([]byte)s);
return write(fd, transmute([]byte)s)
}
write_byte :: proc(fd: Handle, b: byte) -> (n: int, err: Error) {
return write(fd, []byte{b});
return write(fd, []byte{b})
}
write_rune :: proc(fd: Handle, r: rune) -> (n: int, err: Error) {
if r < utf8.RUNE_SELF {
return write_byte(fd, byte(r));
return write_byte(fd, byte(r))
}
b: [4]byte;
b, n = utf8.encode_rune(r);
return write(fd, b[:n]);
b: [4]byte
b, n = utf8.encode_rune(r)
return write(fd, b[:n])
}
write_encoded_rune :: proc(fd: Handle, r: rune) -> (n: int, err: Error) {
wrap :: proc(m: int, merr: Error, n: ^int, err: ^Error) -> bool {
n^ += m;
n^ += m
if merr != nil {
err^ = merr;
return true;
err^ = merr
return true
}
return false;
return false
}
if wrap(write_byte(fd, '\''), &n, &err) { return; }
if wrap(write_byte(fd, '\''), &n, &err) { return }
switch r {
case '\a': if wrap(write_string(fd, "\\a"), &n, &err) { return; }
case '\b': if wrap(write_string(fd, "\\b"), &n, &err) { return; }
case '\e': if wrap(write_string(fd, "\\e"), &n, &err) { return; }
case '\f': if wrap(write_string(fd, "\\f"), &n, &err) { return; }
case '\n': if wrap(write_string(fd, "\\n"), &n, &err) { return; }
case '\r': if wrap(write_string(fd, "\\r"), &n, &err) { return; }
case '\t': if wrap(write_string(fd, "\\t"), &n, &err) { return; }
case '\v': if wrap(write_string(fd, "\\v"), &n, &err) { return; }
case '\a': if wrap(write_string(fd, "\\a"), &n, &err) { return }
case '\b': if wrap(write_string(fd, "\\b"), &n, &err) { return }
case '\e': if wrap(write_string(fd, "\\e"), &n, &err) { return }
case '\f': if wrap(write_string(fd, "\\f"), &n, &err) { return }
case '\n': if wrap(write_string(fd, "\\n"), &n, &err) { return }
case '\r': if wrap(write_string(fd, "\\r"), &n, &err) { return }
case '\t': if wrap(write_string(fd, "\\t"), &n, &err) { return }
case '\v': if wrap(write_string(fd, "\\v"), &n, &err) { return }
case:
if r < 32 {
if wrap(write_string(fd, "\\x"), &n, &err) { return; }
b: [2]byte;
s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil);
if wrap(write_string(fd, "\\x"), &n, &err) { return }
b: [2]byte
s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil)
switch len(s) {
case 0: if wrap(write_string(fd, "00"), &n, &err) { return; }
case 1: if wrap(write_rune(fd, '0'), &n, &err) { return; }
case 2: if wrap(write_string(fd, s), &n, &err) { return; }
case 0: if wrap(write_string(fd, "00"), &n, &err) { return }
case 1: if wrap(write_rune(fd, '0'), &n, &err) { return }
case 2: if wrap(write_string(fd, s), &n, &err) { return }
}
} else {
if wrap(write_rune(fd, r), &n, &err) { return; }
if wrap(write_rune(fd, r), &n, &err) { return }
}
}
_ = wrap(write_byte(fd, '\''), &n, &err);
return;
_ = wrap(write_byte(fd, '\''), &n, &err)
return
}
write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (n: int, err: Error) {
s := transmute([]byte)mem.Raw_Slice{data, len};
return write(fd, s);
s := transmute([]byte)mem.Raw_Slice{data, len}
return write(fd, s)
}
read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (n: int, err: Error) {
s := transmute([]byte)mem.Raw_Slice{data, len};
return read(fd, s);
s := transmute([]byte)mem.Raw_Slice{data, len}
return read(fd, s)
}
read_entire_file :: proc(name: string, allocator := context.allocator) -> ([]byte, Error) {
f, ferr := open(name);
f, ferr := open(name)
if ferr != nil {
return nil, ferr;
return nil, ferr
}
defer close(f);
defer close(f)
size: int;
size: int
if size64, err := file_size(f); err == nil {
if i64(int(size64)) != size64 {
size = int(size64);
size = int(size64)
}
}
size += 1; // for EOF
size += 1 // for EOF
// TODO(bill): Is this correct logic?
total: int;
data := make([]byte, size, allocator);
total: int
data := make([]byte, size, allocator)
for {
n, err := read(f, data[total:]);
total += n;
n, err := read(f, data[total:])
total += n
if err != nil {
if err == .EOF {
err = nil;
err = nil
}
return data[:total], err;
return data[:total], err
}
}
}
write_entire_file :: proc(name: string, data: []byte, perm: File_Mode, truncate := true) -> Error {
flags := O_WRONLY|O_CREATE;
flags := O_WRONLY|O_CREATE
if truncate {
flags |= O_TRUNC;
flags |= O_TRUNC
}
f, err := open_file(name, flags, perm);
f, err := open_file(name, flags, perm)
if err != nil {
return err;
return err
}
_, err = write(f, data);
_, err = write(f, data)
if cerr := close(f); cerr != nil && err == nil {
err = cerr;
err = cerr
}
return err;
return err
}

View File

@@ -5,125 +5,125 @@ import "core:io"
import "core:time"
_create :: proc(name: string) -> (Handle, Error) {
return 0, nil;
return 0, nil
}
_open :: proc(name: string) -> (Handle, Error) {
return 0, nil;
return 0, nil
}
_open_file :: proc(name: string, flag: int, perm: File_Mode) -> (Handle, Error) {
return 0, nil;
return 0, nil
}
_close :: proc(fd: Handle) -> Error {
return nil;
return nil
}
_name :: proc(fd: Handle, allocator := context.allocator) -> string {
return "";
return ""
}
_seek :: proc(fd: Handle, offset: i64, whence: Seek_From) -> (ret: i64, err: Error) {
return;
return
}
_read :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) {
return;
return
}
_read_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) {
return;
return
}
_read_from :: proc(fd: Handle, r: io.Reader) -> (n: i64, err: Error) {
return;
return
}
_write :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) {
return;
return
}
_write_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) {
return;
return
}
_write_to :: proc(fd: Handle, w: io.Writer) -> (n: i64, err: Error) {
return;
return
}
_file_size :: proc(fd: Handle) -> (n: i64, err: Error) {
return;
return
}
_sync :: proc(fd: Handle) -> Error {
return nil;
return nil
}
_flush :: proc(fd: Handle) -> Error {
return nil;
return nil
}
_truncate :: proc(fd: Handle, size: i64) -> Maybe(Path_Error) {
return nil;
return nil
}
_remove :: proc(name: string) -> Maybe(Path_Error) {
return nil;
return nil
}
_rename :: proc(old_path, new_path: string) -> Maybe(Path_Error) {
return nil;
return nil
}
_link :: proc(old_name, new_name: string) -> Maybe(Link_Error) {
return nil;
return nil
}
_symlink :: proc(old_name, new_name: string) -> Maybe(Link_Error) {
return nil;
return nil
}
_read_link :: proc(name: string) -> (string, Maybe(Path_Error)) {
return "", nil;
return "", nil
}
_chdir :: proc(fd: Handle) -> Error {
return nil;
return nil
}
_chmod :: proc(fd: Handle, mode: File_Mode) -> Error {
return nil;
return nil
}
_chown :: proc(fd: Handle, uid, gid: int) -> Error {
return nil;
return nil
}
_lchown :: proc(name: string, uid, gid: int) -> Error {
return nil;
return nil
}
_chtimes :: proc(name: string, atime, mtime: time.Time) -> Maybe(Path_Error) {
return nil;
return nil
}
_exists :: proc(path: string) -> bool {
return false;
return false
}
_is_file :: proc(path: string) -> bool {
return false;
return false
}
_is_dir :: proc(path: string) -> bool {
return false;
return false
}

View File

@@ -6,16 +6,16 @@ heap_allocator :: proc() -> runtime.Allocator {
return runtime.Allocator{
procedure = heap_allocator_proc,
data = nil,
};
}
}
heap_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, runtime.Allocator_Error) {
return _heap_allocator_proc(allocator_data, mode, size, alignment, old_memory, old_size, loc);
return _heap_allocator_proc(allocator_data, mode, size, alignment, old_memory, old_size, loc)
}
@(private)
error_allocator := heap_allocator;
error_allocator := heap_allocator

View File

@@ -6,25 +6,25 @@ import "core:mem"
import win32 "core:sys/windows"
heap_alloc :: proc(size: int) -> rawptr {
return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, uint(size));
return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, uint(size))
}
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
if new_size == 0 {
heap_free(ptr);
return nil;
heap_free(ptr)
return nil
}
if ptr == nil {
return heap_alloc(new_size);
return heap_alloc(new_size)
}
return win32.HeapReAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, ptr, uint(new_size));
return win32.HeapReAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, ptr, uint(new_size))
}
heap_free :: proc(ptr: rawptr) {
if ptr == nil {
return;
return
}
win32.HeapFree(win32.GetProcessHeap(), 0, ptr);
win32.HeapFree(win32.GetProcessHeap(), 0, ptr)
}
_heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
@@ -38,70 +38,70 @@ _heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
//
aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, mem.Allocator_Error) {
a := max(alignment, align_of(rawptr));
space := size + a - 1;
a := max(alignment, align_of(rawptr))
space := size + a - 1
allocated_mem: rawptr;
allocated_mem: rawptr
if old_ptr != nil {
original_old_ptr := mem.ptr_offset((^rawptr)(old_ptr), -1)^;
allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr));
original_old_ptr := mem.ptr_offset((^rawptr)(old_ptr), -1)^
allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr))
} else {
allocated_mem = heap_alloc(space+size_of(rawptr));
allocated_mem = heap_alloc(space+size_of(rawptr))
}
aligned_mem := rawptr(mem.ptr_offset((^u8)(allocated_mem), size_of(rawptr)));
aligned_mem := rawptr(mem.ptr_offset((^u8)(allocated_mem), size_of(rawptr)))
ptr := uintptr(aligned_mem);
aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a);
diff := int(aligned_ptr - ptr);
ptr := uintptr(aligned_mem)
aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a)
diff := int(aligned_ptr - ptr)
if (size + diff) > space {
return nil, .Out_Of_Memory;
return nil, .Out_Of_Memory
}
aligned_mem = rawptr(aligned_ptr);
mem.ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem;
aligned_mem = rawptr(aligned_ptr)
mem.ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem
return mem.byte_slice(aligned_mem, size), nil;
return mem.byte_slice(aligned_mem, size), nil
}
aligned_free :: proc(p: rawptr) {
if p != nil {
heap_free(mem.ptr_offset((^rawptr)(p), -1)^);
heap_free(mem.ptr_offset((^rawptr)(p), -1)^)
}
}
aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int) -> ([]byte, mem.Allocator_Error) {
if p == nil {
return nil, nil;
return nil, nil
}
return aligned_alloc(new_size, new_alignment, p);
return aligned_alloc(new_size, new_alignment, p)
}
switch mode {
case .Alloc:
return aligned_alloc(size, alignment);
return aligned_alloc(size, alignment)
case .Free:
aligned_free(old_memory);
aligned_free(old_memory)
case .Free_All:
return nil, .Mode_Not_Implemented;
return nil, .Mode_Not_Implemented
case .Resize:
if old_memory == nil {
return aligned_alloc(size, alignment);
return aligned_alloc(size, alignment)
}
return aligned_resize(old_memory, old_size, size, alignment);
return aligned_resize(old_memory, old_size, size, alignment)
case .Query_Features:
set := (^mem.Allocator_Mode_Set)(old_memory);
set := (^mem.Allocator_Mode_Set)(old_memory)
if set != nil {
set^ = {.Alloc, .Free, .Resize, .Query_Features};
set^ = {.Alloc, .Free, .Resize, .Query_Features}
}
return nil, nil;
return nil, nil
case .Query_Info:
return nil, nil;
return nil, nil
}
return nil, nil;
return nil, nil
}

View File

@@ -1,29 +1,29 @@
package os2
Path_Separator :: _Path_Separator; // OS-Specific
Path_List_Separator :: _Path_List_Separator; // OS-Specific
Path_Separator :: _Path_Separator // OS-Specific
Path_List_Separator :: _Path_List_Separator // OS-Specific
is_path_separator :: proc(c: byte) -> bool {
return _is_path_separator(c);
return _is_path_separator(c)
}
mkdir :: proc(name: string, perm: File_Mode) -> Maybe(Path_Error) {
return _mkdir(name, perm);
return _mkdir(name, perm)
}
mkdir_all :: proc(path: string, perm: File_Mode) -> Maybe(Path_Error) {
return _mkdir_all(path, perm);
return _mkdir_all(path, perm)
}
remove_all :: proc(path: string) -> Maybe(Path_Error) {
return _remove_all(path);
return _remove_all(path)
}
getwd :: proc(allocator := context.allocator) -> (dir: string, err: Error) {
return _getwd(allocator);
return _getwd(allocator)
}
setwd :: proc(dir: string) -> (err: Error) {
return _setwd(dir);
return _setwd(dir)
}

View File

@@ -1,31 +1,31 @@
//+private
package os2
_Path_Separator :: '\\';
_Path_List_Separator :: ';';
_Path_Separator :: '\\'
_Path_List_Separator :: ';'
_is_path_separator :: proc(c: byte) -> bool {
return c == '\\' || c == '/';
return c == '\\' || c == '/'
}
_mkdir :: proc(name: string, perm: File_Mode) -> Maybe(Path_Error) {
return nil;
return nil
}
_mkdir_all :: proc(path: string, perm: File_Mode) -> Maybe(Path_Error) {
// TODO(bill): _mkdir_all for windows
return nil;
return nil
}
_remove_all :: proc(path: string) -> Maybe(Path_Error) {
// TODO(bill): _remove_all for windows
return nil;
return nil
}
_getwd :: proc(allocator := context.allocator) -> (dir: string, err: Error) {
return "", nil;
return "", nil
}
_setwd :: proc(dir: string) -> (err: Error) {
return nil;
return nil
}

View File

@@ -1,5 +1,5 @@
package os2
pipe :: proc() -> (r, w: Handle, err: Error) {
return _pipe();
return _pipe()
}

View File

@@ -4,10 +4,10 @@ package os2
import win32 "core:sys/windows"
_pipe :: proc() -> (r, w: Handle, err: Error) {
p: [2]win32.HANDLE;
p: [2]win32.HANDLE
if !win32.CreatePipe(&p[0], &p[1], nil, 0) {
return 0, 0, Platform_Error{i32(win32.GetLastError())};
return 0, 0, Platform_Error{i32(win32.GetLastError())}
}
return Handle(p[0]), Handle(p[1]), nil;
return Handle(p[0]), Handle(p[1]), nil
}

View File

@@ -2,35 +2,36 @@ package os2
import sync "core:sync/sync2"
import "core:time"
import "core:runtime"
args: []string;
args: []string
exit :: proc "contextless" (code: int) -> ! {
//
runtime.trap()
}
get_uid :: proc() -> int {
return -1;
return -1
}
get_euid :: proc() -> int {
return -1;
return -1
}
get_gid :: proc() -> int {
return -1;
return -1
}
get_egid :: proc() -> int {
return -1;
return -1
}
get_pid :: proc() -> int {
return -1;
return -1
}
get_ppid :: proc() -> int {
return -1;
return -1
}
@@ -49,7 +50,7 @@ Process_Attributes :: struct {
sys: ^Process_Attributes_OS_Specific,
}
Process_Attributes_OS_Specific :: struct{};
Process_Attributes_OS_Specific :: struct{}
Process_Error :: enum {
None,
@@ -65,35 +66,35 @@ Process_State :: struct {
sys: rawptr,
}
Signal :: #type proc();
Signal :: #type proc()
Kill: Signal = nil;
Interrupt: Signal = nil;
Kill: Signal = nil
Interrupt: Signal = nil
find_process :: proc(pid: int) -> (^Process, Process_Error) {
return nil, .None;
return nil, .None
}
process_start :: proc(name: string, argv: []string, attr: ^Process_Attributes) -> (^Process, Process_Error) {
return nil, .None;
return nil, .None
}
process_release :: proc(p: ^Process) -> Process_Error {
return .None;
return .None
}
process_kill :: proc(p: ^Process) -> Process_Error {
return .None;
return .None
}
process_signal :: proc(p: ^Process, sig: Signal) -> Process_Error {
return .None;
return .None
}
process_wait :: proc(p: ^Process) -> (Process_State, Process_Error) {
return {}, .None;
return {}, .None
}

View File

@@ -15,28 +15,28 @@ File_Info :: struct {
file_info_slice_delete :: proc(infos: []File_Info, allocator := context.allocator) {
for i := len(infos)-1; i >= 0; i -= 1 {
file_info_delete(infos[i], allocator);
file_info_delete(infos[i], allocator)
}
delete(infos, allocator);
delete(infos, allocator)
}
file_info_delete :: proc(fi: File_Info, allocator := context.allocator) {
delete(fi.fullpath, allocator);
delete(fi.fullpath, allocator)
}
fstat :: proc(fd: Handle, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
return _fstat(fd, allocator);
return _fstat(fd, allocator)
}
stat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
return _stat(name, allocator);
return _stat(name, allocator)
}
lstat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
return _lstat(name, allocator);
return _lstat(name, allocator)
}
same_file :: proc(fi1, fi2: File_Info) -> bool {
return _same_file(fi1, fi2);
return _same_file(fi1, fi2)
}

View File

@@ -6,368 +6,370 @@ import win32 "core:sys/windows"
_fstat :: proc(fd: Handle, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
if fd == 0 {
return {}, Path_Error{err = .Invalid_Argument};
return {}, Path_Error{err = .Invalid_Argument}
}
context.allocator = allocator;
context.allocator = allocator
path, err := _cleanpath_from_handle(fd);
path, err := _cleanpath_from_handle(fd)
if err != nil {
return {}, err;
return {}, err
}
h := win32.HANDLE(fd);
h := win32.HANDLE(fd)
switch win32.GetFileType(h) {
case win32.FILE_TYPE_PIPE, win32.FILE_TYPE_CHAR:
fi: File_Info;
fi.fullpath = path;
fi.name = basename(path);
fi.mode |= file_type_mode(h);
return fi, nil;
fi: File_Info
fi.fullpath = path
fi.name = basename(path)
fi.mode |= file_type_mode(h)
return fi, nil
}
return _file_info_from_get_file_information_by_handle(path, h);
return _file_info_from_get_file_information_by_handle(path, h)
}
_stat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS);
return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS)
}
_lstat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS|win32.FILE_FLAG_OPEN_REPARSE_POINT);
return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS|win32.FILE_FLAG_OPEN_REPARSE_POINT)
}
_same_file :: proc(fi1, fi2: File_Info) -> bool {
return fi1.fullpath == fi2.fullpath;
return fi1.fullpath == fi2.fullpath
}
_stat_errno :: proc(errno: win32.DWORD) -> Path_Error {
return Path_Error{err = Platform_Error{i32(errno)}};
return Path_Error{err = Platform_Error{i32(errno)}}
}
full_path_from_name :: proc(name: string, allocator := context.allocator) -> (path: string, err: Maybe(Path_Error)) {
name := name;
context.allocator = allocator
name := name
if name == "" {
name = ".";
name = "."
}
p := win32.utf8_to_utf16(name, context.temp_allocator);
buf := make([dynamic]u16, 100, allocator);
p := win32.utf8_to_utf16(name, context.temp_allocator)
buf := make([dynamic]u16, 100)
for {
n := win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil);
n := win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil)
if n == 0 {
delete(buf);
return "", _stat_errno(win32.GetLastError());
delete(buf)
return "", _stat_errno(win32.GetLastError())
}
if n <= u32(len(buf)) {
return win32.utf16_to_utf8(buf[:n]), nil;
return win32.utf16_to_utf8(buf[:n]), nil
}
resize(&buf, len(buf)*2);
resize(&buf, len(buf)*2)
}
return;
return
}
internal_stat :: proc(name: string, create_file_attributes: u32, allocator := context.allocator) -> (fi: File_Info, e: Maybe(Path_Error)) {
if len(name) == 0 {
return {}, Path_Error{err = .Not_Exist};
return {}, Path_Error{err = .Not_Exist}
}
context.allocator = allocator;
context.allocator = allocator
wname := win32.utf8_to_wstring(_fix_long_path(name), context.temp_allocator);
fa: win32.WIN32_FILE_ATTRIBUTE_DATA;
ok := win32.GetFileAttributesExW(wname, win32.GetFileExInfoStandard, &fa);
wname := win32.utf8_to_wstring(_fix_long_path(name), context.temp_allocator)
fa: win32.WIN32_FILE_ATTRIBUTE_DATA
ok := win32.GetFileAttributesExW(wname, win32.GetFileExInfoStandard, &fa)
if ok && fa.dwFileAttributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
// Not a symlink
return _file_info_from_win32_file_attribute_data(&fa, name);
return _file_info_from_win32_file_attribute_data(&fa, name)
}
err := 0 if ok else win32.GetLastError();
err := 0 if ok else win32.GetLastError()
if err == win32.ERROR_SHARING_VIOLATION {
fd: win32.WIN32_FIND_DATAW;
sh := win32.FindFirstFileW(wname, &fd);
fd: win32.WIN32_FIND_DATAW
sh := win32.FindFirstFileW(wname, &fd)
if sh == win32.INVALID_HANDLE_VALUE {
e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}};
return;
e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}}
return
}
win32.FindClose(sh);
win32.FindClose(sh)
return _file_info_from_win32_find_data(&fd, name);
return _file_info_from_win32_find_data(&fd, name)
}
h := win32.CreateFileW(wname, 0, 0, nil, win32.OPEN_EXISTING, create_file_attributes, nil);
h := win32.CreateFileW(wname, 0, 0, nil, win32.OPEN_EXISTING, create_file_attributes, nil)
if h == win32.INVALID_HANDLE_VALUE {
e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}};
return;
e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}}
return
}
defer win32.CloseHandle(h);
return _file_info_from_get_file_information_by_handle(name, h);
defer win32.CloseHandle(h)
return _file_info_from_get_file_information_by_handle(name, h)
}
_cleanpath_strip_prefix :: proc(buf: []u16) -> []u16 {
buf := buf;
N := 0;
buf := buf
N := 0
for c, i in buf {
if c == 0 { break; }
N = i+1;
if c == 0 { break }
N = i+1
}
buf = buf[:N];
buf = buf[:N]
if len(buf) >= 4 {
if buf[0] == '\\' &&
buf[1] == '\\' &&
buf[2] == '?' &&
buf[3] == '\\' {
buf = buf[4:];
buf = buf[4:]
}
}
return buf;
return buf
}
_cleanpath_from_handle :: proc(fd: Handle) -> (string, Maybe(Path_Error)) {
if fd == 0 {
return "", Path_Error{err = .Invalid_Argument};
return "", Path_Error{err = .Invalid_Argument}
}
h := win32.HANDLE(fd);
h := win32.HANDLE(fd)
MAX_PATH := win32.DWORD(260) + 1;
buf: []u16;
MAX_PATH := win32.DWORD(260) + 1
buf: []u16
for {
buf = make([]u16, MAX_PATH, context.temp_allocator);
err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0);
buf = make([]u16, MAX_PATH, context.temp_allocator)
err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0)
switch err {
case win32.ERROR_PATH_NOT_FOUND, win32.ERROR_INVALID_PARAMETER:
return "", _stat_errno(err);
return "", _stat_errno(err)
case win32.ERROR_NOT_ENOUGH_MEMORY:
MAX_PATH = MAX_PATH*2 + 1;
continue;
MAX_PATH = MAX_PATH*2 + 1
continue
}
break;
break
}
return _cleanpath_from_buf(buf), nil;
return _cleanpath_from_buf(buf), nil
}
_cleanpath_from_handle_u16 :: proc(fd: Handle) -> ([]u16, Maybe(Path_Error)) {
if fd == 0 {
return nil, Path_Error{err = .Invalid_Argument};
return nil, Path_Error{err = .Invalid_Argument}
}
h := win32.HANDLE(fd);
h := win32.HANDLE(fd)
MAX_PATH := win32.DWORD(260) + 1;
buf: []u16;
MAX_PATH := win32.DWORD(260) + 1
buf: []u16
for {
buf = make([]u16, MAX_PATH, context.temp_allocator);
err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0);
buf = make([]u16, MAX_PATH, context.temp_allocator)
err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0)
switch err {
case win32.ERROR_PATH_NOT_FOUND, win32.ERROR_INVALID_PARAMETER:
return nil, _stat_errno(err);
return nil, _stat_errno(err)
case win32.ERROR_NOT_ENOUGH_MEMORY:
MAX_PATH = MAX_PATH*2 + 1;
continue;
MAX_PATH = MAX_PATH*2 + 1
continue
}
break;
break
}
return _cleanpath_strip_prefix(buf), nil;
return _cleanpath_strip_prefix(buf), nil
}
_cleanpath_from_buf :: proc(buf: []u16) -> string {
buf := buf;
buf = _cleanpath_strip_prefix(buf);
return win32.utf16_to_utf8(buf, context.allocator);
buf := buf
buf = _cleanpath_strip_prefix(buf)
return win32.utf16_to_utf8(buf, context.allocator)
}
basename :: proc(name: string) -> (base: string) {
name := name;
name := name
if len(name) > 3 && name[:3] == `\\?` {
name = name[3:];
name = name[3:]
}
if len(name) == 2 && name[1] == ':' {
return ".";
return "."
} else if len(name) > 2 && name[1] == ':' {
name = name[2:];
name = name[2:]
}
i := len(name)-1;
i := len(name)-1
for ; i > 0 && (name[i] == '/' || name[i] == '\\'); i -= 1 {
name = name[:i];
name = name[:i]
}
for i -= 1; i >= 0; i -= 1 {
if name[i] == '/' || name[i] == '\\' {
name = name[i+1:];
break;
name = name[i+1:]
break
}
}
return name;
return name
}
file_type_mode :: proc(h: win32.HANDLE) -> File_Mode {
switch win32.GetFileType(h) {
case win32.FILE_TYPE_PIPE:
return File_Mode_Named_Pipe;
return File_Mode_Named_Pipe
case win32.FILE_TYPE_CHAR:
return File_Mode_Device | File_Mode_Char_Device;
return File_Mode_Device | File_Mode_Char_Device
}
return 0;
return 0
}
_file_mode_from_file_attributes :: proc(FileAttributes: win32.DWORD, h: win32.HANDLE, ReparseTag: win32.DWORD) -> (mode: File_Mode) {
if FileAttributes & win32.FILE_ATTRIBUTE_READONLY != 0 {
mode |= 0o444;
mode |= 0o444
} else {
mode |= 0o666;
mode |= 0o666
}
is_sym := false;
is_sym := false
if FileAttributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
is_sym = false;
is_sym = false
} else {
is_sym = ReparseTag == win32.IO_REPARSE_TAG_SYMLINK || ReparseTag == win32.IO_REPARSE_TAG_MOUNT_POINT;
is_sym = ReparseTag == win32.IO_REPARSE_TAG_SYMLINK || ReparseTag == win32.IO_REPARSE_TAG_MOUNT_POINT
}
if is_sym {
mode |= File_Mode_Sym_Link;
mode |= File_Mode_Sym_Link
} else {
if FileAttributes & win32.FILE_ATTRIBUTE_DIRECTORY != 0 {
mode |= 0o111 | File_Mode_Dir;
mode |= 0o111 | File_Mode_Dir
}
if h != nil {
mode |= file_type_mode(h);
mode |= file_type_mode(h)
}
}
return;
return
}
_file_info_from_win32_file_attribute_data :: proc(d: ^win32.WIN32_FILE_ATTRIBUTE_DATA, name: string) -> (fi: File_Info, e: Maybe(Path_Error)) {
fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow);
fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
fi.mode |= _file_mode_from_file_attributes(d.dwFileAttributes, nil, 0);
fi.is_dir = fi.mode & File_Mode_Dir != 0;
fi.mode |= _file_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
fi.is_dir = fi.mode & File_Mode_Dir != 0
fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime));
fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime));
fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime));
fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
fi.fullpath, e = full_path_from_name(name);
fi.name = basename(fi.fullpath);
fi.fullpath, e = full_path_from_name(name)
fi.name = basename(fi.fullpath)
return;
return
}
_file_info_from_win32_find_data :: proc(d: ^win32.WIN32_FIND_DATAW, name: string) -> (fi: File_Info, e: Maybe(Path_Error)) {
fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow);
fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
fi.mode |= _file_mode_from_file_attributes(d.dwFileAttributes, nil, 0);
fi.is_dir = fi.mode & File_Mode_Dir != 0;
fi.mode |= _file_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
fi.is_dir = fi.mode & File_Mode_Dir != 0
fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime));
fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime));
fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime));
fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
fi.fullpath, e = full_path_from_name(name);
fi.name = basename(fi.fullpath);
fi.fullpath, e = full_path_from_name(name)
fi.name = basename(fi.fullpath)
return;
return
}
_file_info_from_get_file_information_by_handle :: proc(path: string, h: win32.HANDLE) -> (File_Info, Maybe(Path_Error)) {
d: win32.BY_HANDLE_FILE_INFORMATION;
d: win32.BY_HANDLE_FILE_INFORMATION
if !win32.GetFileInformationByHandle(h, &d) {
return {}, _stat_errno(win32.GetLastError());
return {}, _stat_errno(win32.GetLastError())
}
ti: win32.FILE_ATTRIBUTE_TAG_INFO;
ti: win32.FILE_ATTRIBUTE_TAG_INFO
if !win32.GetFileInformationByHandleEx(h, .FileAttributeTagInfo, &ti, size_of(ti)) {
err := win32.GetLastError();
err := win32.GetLastError()
if err != win32.ERROR_INVALID_PARAMETER {
return {}, _stat_errno(err);
return {}, _stat_errno(err)
}
// Indicate this is a symlink on FAT file systems
ti.ReparseTag = 0;
ti.ReparseTag = 0
}
fi: File_Info;
fi: File_Info
fi.fullpath = path;
fi.name = basename(path);
fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow);
fi.fullpath = path
fi.name = basename(path)
fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
fi.mode |= _file_mode_from_file_attributes(ti.FileAttributes, h, ti.ReparseTag);
fi.is_dir = fi.mode & File_Mode_Dir != 0;
fi.mode |= _file_mode_from_file_attributes(ti.FileAttributes, h, ti.ReparseTag)
fi.is_dir = fi.mode & File_Mode_Dir != 0
fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime));
fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime));
fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime));
fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
return fi, nil;
return fi, nil
}
_is_abs :: proc(path: string) -> bool {
if len(path) > 0 && path[0] == '/' {
return true;
return true
}
if len(path) > 2 {
switch path[0] {
case 'A'..='Z', 'a'..='z':
return path[1] == ':' && is_path_separator(path[2]);
return path[1] == ':' && is_path_separator(path[2])
}
}
return false;
return false
}
_fix_long_path :: proc(path: string) -> string {
if len(path) < 248 {
return path;
return path
}
if len(path) >= 2 && path[:2] == `\\` {
return path;
return path
}
if !_is_abs(path) {
return path;
return path
}
prefix :: `\\?`;
prefix :: `\\?`
path_buf := make([]byte, len(prefix)+len(path)+len(`\`), context.temp_allocator);
copy(path_buf, prefix);
n := len(path);
r, w := 0, len(prefix);
path_buf := make([]byte, len(prefix)+len(path)+len(`\`), context.temp_allocator)
copy(path_buf, prefix)
n := len(path)
r, w := 0, len(prefix)
for r < n {
switch {
case is_path_separator(path[r]):
r += 1;
r += 1
case path[r] == '.' && (r+1 == n || is_path_separator(path[r+1])):
r += 1;
r += 1
case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_path_separator(path[r+2])):
return path;
return path
case:
path_buf[w] = '\\';
w += 1;
path_buf[w] = '\\'
w += 1
for ; r < n && !is_path_separator(path[r]); r += 1 {
path_buf[w] = path[r];
w += 1;
path_buf[w] = path[r]
w += 1
}
}
}
if w == len(`\\?\c:`) {
path_buf[w] = '\\';
w += 1;
path_buf[w] = '\\'
w += 1
}
return string(path_buf[:w]);
return string(path_buf[:w])
}

View File

@@ -2,13 +2,13 @@ package os2
create_temp :: proc(dir, pattern: string) -> (Handle, Error) {
return _create_temp(dir, pattern);
return _create_temp(dir, pattern)
}
mkdir_temp :: proc(dir, pattern: string, allocator := context.allocator) -> (string, Error) {
return _mkdir_temp(dir, pattern);
return _mkdir_temp(dir, pattern)
}
temp_dir :: proc(allocator := context.allocator) -> string {
return _temp_dir(allocator);
return _temp_dir(allocator)
}

View File

@@ -4,26 +4,26 @@ package os2
import win32 "core:sys/windows"
_create_temp :: proc(dir, pattern: string) -> (Handle, Error) {
return 0, nil;
return 0, nil
}
_mkdir_temp :: proc(dir, pattern: string, allocator := context.allocator) -> (string, Error) {
return "", nil;
return "", nil
}
_temp_dir :: proc(allocator := context.allocator) -> string {
b := make([dynamic]u16, u32(win32.MAX_PATH), context.temp_allocator);
b := make([dynamic]u16, u32(win32.MAX_PATH), context.temp_allocator)
for {
n := win32.GetTempPathW(u32(len(b)), raw_data(b));
n := win32.GetTempPathW(u32(len(b)), raw_data(b))
if n > u32(len(b)) {
resize(&b, int(n));
continue;
resize(&b, int(n))
continue
}
if n == 3 && b[1] == ':' && b[2] == '\\' {
} else if n > 0 && b[n-1] == '\\' {
n -= 1;
n -= 1
}
return win32.utf16_to_utf8(b[:n], allocator);
return win32.utf16_to_utf8(b[:n], allocator)
}
}

View File

@@ -5,64 +5,64 @@ import "core:strings"
user_cache_dir :: proc(allocator := context.allocator) -> (dir: string, is_defined: bool) {
switch ODIN_OS {
case "windows":
dir = get_env("LocalAppData");
dir = get_env("LocalAppData")
if dir != "" {
dir = strings.clone(dir, allocator);
dir = strings.clone(dir, allocator)
}
case "darwin":
dir = get_env("HOME");
dir = get_env("HOME")
if dir != "" {
dir = strings.concatenate({dir, "/Library/Caches"}, allocator);
dir = strings.concatenate({dir, "/Library/Caches"}, allocator)
}
case: // All other UNIX systems
dir = get_env("XDG_CACHE_HOME");
dir = get_env("XDG_CACHE_HOME")
if dir == "" {
dir = get_env("HOME");
dir = get_env("HOME")
if dir == "" {
return;
return
}
dir = strings.concatenate({dir, "/.cache"}, allocator);
dir = strings.concatenate({dir, "/.cache"}, allocator)
}
}
is_defined = dir != "";
return;
is_defined = dir != ""
return
}
user_config_dir :: proc(allocator := context.allocator) -> (dir: string, is_defined: bool) {
switch ODIN_OS {
case "windows":
dir = get_env("AppData");
dir = get_env("AppData")
if dir != "" {
dir = strings.clone(dir, allocator);
dir = strings.clone(dir, allocator)
}
case "darwin":
dir = get_env("HOME");
dir = get_env("HOME")
if dir != "" {
dir = strings.concatenate({dir, "/Library/Application Support"}, allocator);
dir = strings.concatenate({dir, "/Library/Application Support"}, allocator)
}
case: // All other UNIX systems
dir = get_env("XDG_CACHE_HOME");
dir = get_env("XDG_CACHE_HOME")
if dir == "" {
dir = get_env("HOME");
dir = get_env("HOME")
if dir == "" {
return;
return
}
dir = strings.concatenate({dir, "/.config"}, allocator);
dir = strings.concatenate({dir, "/.config"}, allocator)
}
}
is_defined = dir != "";
return;
is_defined = dir != ""
return
}
user_home_dir :: proc() -> (dir: string, is_defined: bool) {
env := "HOME";
env := "HOME"
switch ODIN_OS {
case "windows":
env = "USERPROFILE";
env = "USERPROFILE"
}
if v := get_env(env); v != "" {
return v, true;
return v, true
}
return "", false;
return "", false
}