mirror of
https://github.com/odin-lang/Odin.git
synced 2026-01-18 02:27:00 +00:00
Update package os for package path/filepath support on macOS
This commit is contained in:
70
core/os/dir_darwin.odin
Normal file
70
core/os/dir_darwin.odin
Normal file
@@ -0,0 +1,70 @@
|
||||
package os
|
||||
|
||||
import "core:strings"
|
||||
import "core:mem"
|
||||
|
||||
read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Errno) {
|
||||
dirp: Dir;
|
||||
dirp, err = _fdopendir(fd);
|
||||
if err != ERROR_NONE {
|
||||
return;
|
||||
}
|
||||
|
||||
defer _closedir(dirp);
|
||||
|
||||
dirpath: string;
|
||||
dirpath, err = absolute_path_from_handle(fd);
|
||||
|
||||
if err != ERROR_NONE {
|
||||
return;
|
||||
}
|
||||
|
||||
defer delete(dirpath);
|
||||
|
||||
n := n;
|
||||
size := n;
|
||||
if n <= 0 {
|
||||
n = -1;
|
||||
size = 100;
|
||||
}
|
||||
|
||||
dfi := make([dynamic]File_Info, 0, size, allocator);
|
||||
|
||||
for {
|
||||
entry: Dirent;
|
||||
end_of_stream: bool;
|
||||
entry, err, end_of_stream = _readdir(dirp);
|
||||
if err != ERROR_NONE {
|
||||
for fi_ in dfi {
|
||||
file_info_delete(fi_, allocator);
|
||||
}
|
||||
delete(dfi);
|
||||
return;
|
||||
} else if end_of_stream {
|
||||
break;
|
||||
}
|
||||
|
||||
fi_: File_Info;
|
||||
filename := cast(string)(transmute(cstring)mem.Raw_Cstring{ data = &entry.name[0] });
|
||||
|
||||
if filename == "." || filename == ".." {
|
||||
continue;
|
||||
}
|
||||
|
||||
fullpath := strings.join( []string{ dirpath, filename }, "/", context.temp_allocator);
|
||||
defer delete(fullpath, context.temp_allocator);
|
||||
|
||||
fi_, err = stat(fullpath, allocator);
|
||||
if err != ERROR_NONE {
|
||||
for fi__ in dfi {
|
||||
file_info_delete(fi__, allocator);
|
||||
}
|
||||
delete(dfi);
|
||||
return;
|
||||
}
|
||||
|
||||
append(&dfi, fi_);
|
||||
}
|
||||
|
||||
return dfi[:], ERROR_NONE;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ foreign import pthread "System.framework"
|
||||
|
||||
import "core:runtime"
|
||||
import "core:strings"
|
||||
import "core:strconv"
|
||||
import "core:c"
|
||||
|
||||
Handle :: distinct i32;
|
||||
@@ -191,7 +192,7 @@ Unix_File_Time :: struct {
|
||||
nanoseconds: i64,
|
||||
}
|
||||
|
||||
Stat :: struct {
|
||||
OS_Stat :: struct {
|
||||
device_id: i32, // ID of device containing file
|
||||
mode: u16, // Mode of the file
|
||||
nlink: u16, // Number of hard links
|
||||
@@ -215,6 +216,17 @@ Stat :: struct {
|
||||
_reserve2: i64, // RESERVED
|
||||
};
|
||||
|
||||
// NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
|
||||
Dirent :: struct {
|
||||
ino: u64,
|
||||
off: u64,
|
||||
reclen: u16,
|
||||
type: u8,
|
||||
name: [256]byte,
|
||||
};
|
||||
|
||||
Dir :: distinct rawptr; // DIR*
|
||||
|
||||
// File type
|
||||
S_IFMT :: 0o170000; // Type of file mask
|
||||
S_IFIFO :: 0o010000; // Named pipe (fifo)
|
||||
@@ -264,25 +276,33 @@ F_OK :: 0; // Test for file existance
|
||||
foreign libc {
|
||||
@(link_name="__error") __error :: proc() -> ^int ---;
|
||||
|
||||
@(link_name="open") _unix_open :: proc(path: cstring, flags: i32, mode: u16) -> Handle ---;
|
||||
@(link_name="close") _unix_close :: proc(handle: Handle) ---;
|
||||
@(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
|
||||
@(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
|
||||
@(link_name="lseek") _unix_lseek :: proc(fs: Handle, offset: int, whence: int) -> int ---;
|
||||
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
|
||||
@(link_name="getpagesize") _unix_getpagesize :: proc() -> i32 ---;
|
||||
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^Stat) -> int ---;
|
||||
@(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---;
|
||||
@(link_name="open") _unix_open :: proc(path: cstring, flags: i32, mode: u16) -> Handle ---;
|
||||
@(link_name="close") _unix_close :: proc(handle: Handle) ---;
|
||||
@(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
|
||||
@(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
|
||||
@(link_name="lseek") _unix_lseek :: proc(fs: Handle, offset: int, whence: int) -> int ---;
|
||||
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
|
||||
@(link_name="getpagesize") _unix_getpagesize :: proc() -> i32 ---;
|
||||
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
|
||||
@(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
|
||||
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---;
|
||||
@(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---;
|
||||
@(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---;
|
||||
@(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---;
|
||||
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---;
|
||||
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---;
|
||||
@(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---;
|
||||
|
||||
@(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---;
|
||||
@(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---;
|
||||
@(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
|
||||
@(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
|
||||
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
|
||||
@(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---;
|
||||
@(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---;
|
||||
@(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---;
|
||||
@(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---;
|
||||
@(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
|
||||
@(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
|
||||
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
|
||||
@(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---;
|
||||
@(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---;
|
||||
@(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---;
|
||||
|
||||
@(link_name="exit") _unix_exit :: proc(status: int) ---;
|
||||
@(link_name="exit") _unix_exit :: proc(status: int) ---;
|
||||
}
|
||||
|
||||
foreign dl {
|
||||
@@ -366,17 +386,138 @@ is_path_separator :: proc(r: rune) -> bool {
|
||||
return r == '/';
|
||||
}
|
||||
|
||||
stat :: proc(path: string) -> (Stat, Errno) {
|
||||
s: Stat;
|
||||
cstr := strings.clone_to_cstring(path);
|
||||
defer delete(cstr);
|
||||
ret_int := _unix_stat(cstr, &s);
|
||||
return s, Errno(ret_int);
|
||||
|
||||
@private
|
||||
_stat :: proc(path: string) -> (OS_Stat, Errno) {
|
||||
cstr := strings.clone_to_cstring(path, context.temp_allocator);
|
||||
|
||||
s: OS_Stat;
|
||||
result := _unix_stat(cstr, &s);
|
||||
if result == -1 {
|
||||
return s, Errno(get_last_error());
|
||||
}
|
||||
return s, ERROR_NONE;
|
||||
}
|
||||
|
||||
@private
|
||||
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
||||
cstr := strings.clone_to_cstring(path, context.temp_allocator);
|
||||
|
||||
s: OS_Stat;
|
||||
result := _unix_lstat(cstr, &s);
|
||||
if result == -1 {
|
||||
return s, Errno(get_last_error());
|
||||
}
|
||||
return s, ERROR_NONE;
|
||||
}
|
||||
|
||||
@private
|
||||
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
||||
s: OS_Stat;
|
||||
result := _unix_fstat(fd, &s);
|
||||
if result == -1 {
|
||||
return s, Errno(get_last_error());
|
||||
}
|
||||
return s, ERROR_NONE;
|
||||
}
|
||||
|
||||
@private
|
||||
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
|
||||
dirp := _unix_fdopendir(fd);
|
||||
if dirp == cast(Dir)nil {
|
||||
return nil, Errno(get_last_error());
|
||||
}
|
||||
return dirp, ERROR_NONE;
|
||||
}
|
||||
|
||||
@private
|
||||
_closedir :: proc(dirp: Dir) -> Errno {
|
||||
rc := _unix_closedir(dirp);
|
||||
if rc != 0 {
|
||||
return Errno(get_last_error());
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
@private
|
||||
_rewinddir :: proc(dirp: Dir) {
|
||||
_unix_rewinddir(dirp);
|
||||
}
|
||||
|
||||
@private
|
||||
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
|
||||
result: ^Dirent;
|
||||
rc := _unix_readdir_r(dirp, &entry, &result);
|
||||
|
||||
if rc != 0 {
|
||||
err = Errno(get_last_error());
|
||||
return;
|
||||
}
|
||||
err = ERROR_NONE;
|
||||
|
||||
if result == nil {
|
||||
end_of_stream = true;
|
||||
return;
|
||||
}
|
||||
end_of_stream = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@private
|
||||
_readlink :: proc(path: string) -> (string, Errno) {
|
||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator);
|
||||
|
||||
bufsz : uint = 256;
|
||||
buf := make([]byte, bufsz);
|
||||
for {
|
||||
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz);
|
||||
if rc == -1 {
|
||||
delete(buf);
|
||||
return "", Errno(get_last_error());
|
||||
} else if rc == int(bufsz) {
|
||||
// NOTE(laleksic, 2021-01-21): Any cleaner way to resize the slice?
|
||||
bufsz *= 2;
|
||||
delete(buf);
|
||||
buf = make([]byte, bufsz);
|
||||
} else {
|
||||
return strings.string_from_ptr(&buf[0], rc), ERROR_NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
|
||||
buf : [256]byte;
|
||||
fd_str := strconv.itoa( buf[:], cast(int)fd );
|
||||
|
||||
procfs_path := strings.concatenate( []string{ "/proc/self/fd/", fd_str } );
|
||||
defer delete(procfs_path);
|
||||
|
||||
return _readlink(procfs_path);
|
||||
}
|
||||
|
||||
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
||||
rel := rel;
|
||||
if rel == "" {
|
||||
rel = ".";
|
||||
}
|
||||
|
||||
rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator);
|
||||
|
||||
path_ptr := _unix_realpath(rel_cstr, nil);
|
||||
if path_ptr == nil {
|
||||
return "", Errno(get_last_error());
|
||||
}
|
||||
defer _unix_free(path_ptr);
|
||||
|
||||
path_cstr := transmute(cstring)path_ptr;
|
||||
path = strings.clone( string(path_cstr) );
|
||||
|
||||
return path, ERROR_NONE;
|
||||
}
|
||||
|
||||
access :: proc(path: string, mask: int) -> bool {
|
||||
cstr := strings.clone_to_cstring(path);
|
||||
defer delete(cstr);
|
||||
cstr := strings.clone_to_cstring(path, context.temp_allocator);
|
||||
return _unix_access(cstr, mask) == 0;
|
||||
}
|
||||
|
||||
@@ -392,8 +533,7 @@ heap_free :: proc(ptr: rawptr) {
|
||||
}
|
||||
|
||||
getenv :: proc(name: string) -> (string, bool) {
|
||||
path_str := strings.clone_to_cstring(name);
|
||||
defer delete(path_str);
|
||||
path_str := strings.clone_to_cstring(name, context.temp_allocator);
|
||||
cstr := _unix_getenv(path_str);
|
||||
if cstr == nil {
|
||||
return "", false;
|
||||
@@ -441,15 +581,13 @@ current_thread_id :: proc "contextless" () -> int {
|
||||
}
|
||||
|
||||
dlopen :: proc(filename: string, flags: int) -> rawptr {
|
||||
cstr := strings.clone_to_cstring(filename);
|
||||
defer delete(cstr);
|
||||
cstr := strings.clone_to_cstring(filename, context.temp_allocator);
|
||||
handle := _unix_dlopen(cstr, flags);
|
||||
return handle;
|
||||
}
|
||||
dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
|
||||
assert(handle != nil);
|
||||
cstr := strings.clone_to_cstring(symbol);
|
||||
defer delete(cstr);
|
||||
cstr := strings.clone_to_cstring(symbol, context.temp_allocator);
|
||||
proc_handle := _unix_dlsym(handle, cstr);
|
||||
return proc_handle;
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ Unix_File_Time :: struct {
|
||||
|
||||
pid_t :: u32;
|
||||
|
||||
Stat :: struct {
|
||||
OS_Stat :: struct {
|
||||
device_id: u64,
|
||||
serial: u64,
|
||||
nlink: u64,
|
||||
@@ -235,8 +235,8 @@ foreign libc {
|
||||
@(link_name="lseek64") _unix_seek :: proc(fd: Handle, offset: i64, whence: c.int) -> i64 ---;
|
||||
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
|
||||
@(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---;
|
||||
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^Stat) -> c.int ---;
|
||||
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^Stat) -> c.int ---;
|
||||
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
|
||||
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---;
|
||||
@(link_name="access") _unix_access :: proc(path: cstring, mask: c.int) -> c.int ---;
|
||||
|
||||
@(link_name="malloc") _unix_malloc :: proc(size: c.size_t) -> rawptr ---;
|
||||
@@ -341,11 +341,11 @@ last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
|
||||
return File_Time(modified), ERROR_NONE;
|
||||
}
|
||||
|
||||
stat :: proc(path: string) -> (Stat, Errno) {
|
||||
stat :: proc(path: string) -> (OS_Stat, Errno) {
|
||||
cstr := strings.clone_to_cstring(path);
|
||||
defer delete(cstr);
|
||||
|
||||
s: Stat;
|
||||
s: OS_Stat;
|
||||
result := _unix_stat(cstr, &s);
|
||||
if result == -1 {
|
||||
return s, Errno(get_last_error());
|
||||
@@ -353,8 +353,8 @@ stat :: proc(path: string) -> (Stat, Errno) {
|
||||
return s, ERROR_NONE;
|
||||
}
|
||||
|
||||
fstat :: proc(fd: Handle) -> (Stat, Errno) {
|
||||
s: Stat;
|
||||
fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
||||
s: OS_Stat;
|
||||
result := _unix_fstat(fd, &s);
|
||||
if result == -1 {
|
||||
return s, Errno(get_last_error());
|
||||
|
||||
@@ -185,7 +185,7 @@ Unix_File_Time :: struct {
|
||||
nanoseconds: i64,
|
||||
}
|
||||
|
||||
Stat :: struct {
|
||||
OS_Stat :: struct {
|
||||
device_id: u64, // ID of device containing file
|
||||
serial: u64, // File serial number
|
||||
nlink: u64, // Number of hard links
|
||||
@@ -207,7 +207,7 @@ Stat :: struct {
|
||||
_reserve3: i64,
|
||||
};
|
||||
|
||||
// NOTE(laleksic, 2021-01-21): Comment and rename these to match Stat above
|
||||
// NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
|
||||
Dirent :: struct {
|
||||
ino: u64,
|
||||
off: u64,
|
||||
@@ -278,9 +278,9 @@ foreign libc {
|
||||
@(link_name="lseek64") _unix_seek :: proc(fd: Handle, offset: i64, whence: c.int) -> i64 ---;
|
||||
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
|
||||
@(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---;
|
||||
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^Stat) -> c.int ---;
|
||||
@(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^Stat) -> c.int ---;
|
||||
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^Stat) -> c.int ---;
|
||||
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
|
||||
@(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
|
||||
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---;
|
||||
@(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---;
|
||||
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---;
|
||||
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---;
|
||||
@@ -397,11 +397,11 @@ last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
|
||||
}
|
||||
|
||||
@private
|
||||
_stat :: proc(path: string) -> (Stat, Errno) {
|
||||
_stat :: proc(path: string) -> (OS_Stat, Errno) {
|
||||
cstr := strings.clone_to_cstring(path);
|
||||
defer delete(cstr);
|
||||
|
||||
s: Stat;
|
||||
s: OS_Stat;
|
||||
result := _unix_stat(cstr, &s);
|
||||
if result == -1 {
|
||||
return s, Errno(get_last_error());
|
||||
@@ -410,11 +410,11 @@ _stat :: proc(path: string) -> (Stat, Errno) {
|
||||
}
|
||||
|
||||
@private
|
||||
_lstat :: proc(path: string) -> (Stat, Errno) {
|
||||
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
||||
cstr := strings.clone_to_cstring(path);
|
||||
defer delete(cstr);
|
||||
|
||||
s: Stat;
|
||||
s: OS_Stat;
|
||||
result := _unix_lstat(cstr, &s);
|
||||
if result == -1 {
|
||||
return s, Errno(get_last_error());
|
||||
@@ -423,8 +423,8 @@ _lstat :: proc(path: string) -> (Stat, Errno) {
|
||||
}
|
||||
|
||||
@private
|
||||
_fstat :: proc(fd: Handle) -> (Stat, Errno) {
|
||||
s: Stat;
|
||||
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
||||
s: OS_Stat;
|
||||
result := _unix_fstat(fd, &s);
|
||||
if result == -1 {
|
||||
return s, Errno(get_last_error());
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//+build linux, darwin, freebsd
|
||||
package os
|
||||
|
||||
import "core:time"
|
||||
@@ -58,10 +59,10 @@ _make_time_from_unix_file_time :: proc(uft: Unix_File_Time) -> time.Time {
|
||||
}
|
||||
|
||||
@private
|
||||
_fill_file_info_from_stat :: proc(fi: ^File_Info, s: Stat) {
|
||||
_fill_file_info_from_stat :: proc(fi: ^File_Info, s: OS_Stat) {
|
||||
fi.size = s.size;
|
||||
fi.mode = cast(File_Mode)s.mode;
|
||||
fi.is_dir = S_ISDIR(s.mode);
|
||||
fi.is_dir = S_ISDIR(auto_cast s.mode);
|
||||
|
||||
// NOTE(laleksic, 2021-01-21): Not really creation time, but closest we can get (maybe better to leave it 0?)
|
||||
fi.creation_time = _make_time_from_unix_file_time(s.status_change);
|
||||
@@ -74,7 +75,7 @@ lstat :: proc(name: string, allocator := context.allocator) -> (fi: File_Info, e
|
||||
|
||||
context.allocator = allocator;
|
||||
|
||||
s: Stat;
|
||||
s: OS_Stat;
|
||||
s, err = _lstat(name);
|
||||
if err != ERROR_NONE {
|
||||
return fi, err;
|
||||
@@ -92,7 +93,7 @@ stat :: proc(name: string, allocator := context.allocator) -> (fi: File_Info, er
|
||||
|
||||
context.allocator = allocator;
|
||||
|
||||
s: Stat;
|
||||
s: OS_Stat;
|
||||
s, err = _stat(name);
|
||||
if err != ERROR_NONE {
|
||||
return fi, err;
|
||||
@@ -110,7 +111,7 @@ fstat :: proc(fd: Handle, allocator := context.allocator) -> (fi: File_Info, err
|
||||
|
||||
context.allocator = allocator;
|
||||
|
||||
s: Stat;
|
||||
s: OS_Stat;
|
||||
s, err = _fstat(fd);
|
||||
if err != ERROR_NONE {
|
||||
return fi, err;
|
||||
@@ -32,36 +32,38 @@ volume_name :: proc(path: string) -> string {
|
||||
}
|
||||
|
||||
volume_name_len :: proc(path: string) -> int {
|
||||
if len(path) < 2 {
|
||||
return 0;
|
||||
}
|
||||
c := path[0];
|
||||
if path[1] == ':' {
|
||||
switch c {
|
||||
case 'a'..'z', 'A'..'Z':
|
||||
return 2;
|
||||
if ODIN_OS == "windows" {
|
||||
if len(path) < 2 {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// URL: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
|
||||
if l := len(path); l >= 5 && is_slash(path[0]) && is_slash(path[1]) &&
|
||||
!is_slash(path[2]) && path[2] != '.' {
|
||||
for n := 3; n < l-1; n += 1 {
|
||||
if is_slash(path[n]) {
|
||||
n += 1;
|
||||
if !is_slash(path[n]) {
|
||||
if path[n] == '.' {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for ; n < l; n += 1 {
|
||||
if is_slash(path[n]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
c := path[0];
|
||||
if path[1] == ':' {
|
||||
switch c {
|
||||
case 'a'..'z', 'A'..'Z':
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
// URL: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
|
||||
if l := len(path); l >= 5 && is_slash(path[0]) && is_slash(path[1]) &&
|
||||
!is_slash(path[2]) && path[2] != '.' {
|
||||
for n := 3; n < l-1; n += 1 {
|
||||
if is_slash(path[n]) {
|
||||
n += 1;
|
||||
if !is_slash(path[n]) {
|
||||
if path[n] == '.' {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for ; n < l; n += 1 {
|
||||
if is_slash(path[n]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -296,6 +298,57 @@ dir :: proc(path: string, allocator := context.allocator) -> string {
|
||||
|
||||
|
||||
|
||||
split_list :: proc(path: string, allocator := context.allocator) -> []string {
|
||||
if path == "" {
|
||||
return nil;
|
||||
}
|
||||
|
||||
start: int;
|
||||
quote: bool;
|
||||
|
||||
start, quote = 0, false;
|
||||
count := 0;
|
||||
|
||||
for i := 0; i < len(path); i += 1 {
|
||||
c := path[i];
|
||||
switch {
|
||||
case c == '"':
|
||||
quote = !quote;
|
||||
case c == LIST_SEPARATOR && !quote:
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
start, quote = 0, false;
|
||||
list := make([]string, count, allocator);
|
||||
index := 0;
|
||||
for i := 0; i < len(path); i += 1 {
|
||||
c := path[i];
|
||||
switch {
|
||||
case c == '"':
|
||||
quote = !quote;
|
||||
case c == LIST_SEPARATOR && !quote:
|
||||
list[index] = path[start:i];
|
||||
index += 1;
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
assert(index == count);
|
||||
|
||||
for s0, i in list {
|
||||
s, new := strings.replace_all(s0, `"`, ``, allocator);
|
||||
if !new {
|
||||
s = strings.clone(s, allocator);
|
||||
}
|
||||
list[i] = s;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
Lazy_Buffer is a lazily made path buffer
|
||||
When it does allocate, it uses the context.allocator
|
||||
|
||||
@@ -8,6 +8,14 @@ SEPARATOR :: '/';
|
||||
SEPARATOR_STRING :: `/`;
|
||||
LIST_SEPARATOR :: ':';
|
||||
|
||||
is_reserved_name :: proc(path: string) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
is_abs :: proc(path: string) -> bool {
|
||||
return strings.has_prefix(path, "/");
|
||||
}
|
||||
|
||||
abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
|
||||
full_path, err := os.absolute_path_from_relative(path);
|
||||
if err != os.ERROR_NONE {
|
||||
@@ -17,10 +25,11 @@ abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
|
||||
}
|
||||
|
||||
join :: proc(elems: ..string, allocator := context.allocator) -> string {
|
||||
s := strings.join(elems, SEPARATOR_STRING);
|
||||
return s;
|
||||
}
|
||||
|
||||
is_abs :: proc(path: string) -> bool {
|
||||
return (path[0] == '/');
|
||||
}
|
||||
for e, i in elems {
|
||||
if e != "" {
|
||||
p := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator);
|
||||
return clean(p, allocator);
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@@ -87,56 +87,6 @@ abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
|
||||
return p, true;
|
||||
}
|
||||
|
||||
split_list :: proc(path: string, allocator := context.allocator) -> []string {
|
||||
if path == "" {
|
||||
return nil;
|
||||
}
|
||||
|
||||
start: int;
|
||||
quote: bool;
|
||||
|
||||
start, quote = 0, false;
|
||||
count := 0;
|
||||
|
||||
for i := 0; i < len(path); i += 1 {
|
||||
c := path[i];
|
||||
switch {
|
||||
case c == '"':
|
||||
quote = !quote;
|
||||
case c == LIST_SEPARATOR && !quote:
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
start, quote = 0, false;
|
||||
list := make([]string, count, allocator);
|
||||
index := 0;
|
||||
for i := 0; i < len(path); i += 1 {
|
||||
c := path[i];
|
||||
switch {
|
||||
case c == '"':
|
||||
quote = !quote;
|
||||
case c == LIST_SEPARATOR && !quote:
|
||||
list[index] = path[start:i];
|
||||
index += 1;
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
assert(index == count);
|
||||
|
||||
for s0, i in list {
|
||||
s, new := strings.replace_all(s0, `"`, ``, allocator);
|
||||
if !new {
|
||||
s = strings.clone(s, allocator);
|
||||
}
|
||||
list[i] = s;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
join :: proc(elems: ..string, allocator := context.allocator) -> string {
|
||||
for e, i in elems {
|
||||
|
||||
Reference in New Issue
Block a user