Browse Source

Merge pull request #1471 from Platin21/feature/fix-odin-fmt

Feature/fix odin fmt
Jeroen van Rijn 3 years ago
parent
commit
25769f139a
2 changed files with 65 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 62 0
      core/os/os_darwin.odin

+ 3 - 0
.gitignore

@@ -7,6 +7,9 @@
 # User-specific files (MonoDevelop/Xamarin Studio)
 *.userprefs
 
+# For macOS
+.DS_Store
+
 # Build results
 [Dd]ebug/
 [Dd]ebugPublic/

+ 62 - 0
core/os/os_darwin.odin

@@ -296,6 +296,9 @@ foreign libc {
 	@(link_name="readdir_r$INODE64") _unix_readdir_r    :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
 	@(link_name="fcntl")            _unix_fcntl         :: proc(fd: Handle, cmd: c.int, buf: ^byte) -> c.int ---
 
+	@(link_name="rename") _unix_rename :: proc(old: cstring, new: cstring) -> c.int ---
+	@(link_name="remove") _unix_remove :: proc(path: cstring) -> c.int ---
+
 	@(link_name="fchmod") _unix_fchmod :: proc(fildes: Handle, mode: u16) -> c.int ---
 
 	@(link_name="malloc")   _unix_malloc   :: proc(size: int) -> rawptr ---
@@ -412,6 +415,65 @@ is_path_separator :: proc(r: rune) -> bool {
 	return r == '/'
 }
 
+is_file_handle :: proc(fd: Handle) -> bool {
+	s, err := _fstat(fd)
+	if err != ERROR_NONE {
+		return false
+	}
+	return S_ISREG(cast(u32)s.mode)
+}
+
+is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
+	s: OS_Stat
+	err: Errno
+	if follow_links {
+		s, err = _stat(path)
+	} else {
+		s, err = _lstat(path)
+	}
+	if err != ERROR_NONE {
+		return false
+	}
+	return S_ISREG(cast(u32)s.mode)
+}
+
+
+is_dir_handle :: proc(fd: Handle) -> bool {
+	s, err := _fstat(fd)
+	if err != ERROR_NONE {
+		return false
+	}
+	return S_ISDIR(cast(u32)s.mode)
+}
+
+is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
+	s: OS_Stat
+	err: Errno
+	if follow_links {
+		s, err = _stat(path)
+	} else {
+		s, err = _lstat(path)
+	}
+	if err != ERROR_NONE {
+		return false
+	}
+	return S_ISDIR(cast(u32)s.mode)
+}
+
+is_file :: proc {is_file_path, is_file_handle}
+is_dir :: proc {is_dir_path, is_dir_handle}
+
+
+rename :: proc(old: string, new: string) -> bool {
+	old_cstr := strings.clone_to_cstring(old, context.temp_allocator)
+	new_cstr := strings.clone_to_cstring(new, context.temp_allocator)
+	return _unix_rename(old_cstr, new_cstr) != -1 
+}
+
+remove :: proc(path: string) -> bool {
+	path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
+	return _unix_remove(path_cstr) != -1 
+}
 
 @private
 _stat :: proc(path: string) -> (OS_Stat, Errno) {