diff --git a/examples/tetris/screenshot.png b/examples/tetris/screenshot.png index c6c908d8a2..67013c1011 100644 Binary files a/examples/tetris/screenshot.png and b/examples/tetris/screenshot.png differ diff --git a/vlib/os/os.v b/vlib/os/os.v index 8d644e20b8..318747c0a3 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -404,7 +404,7 @@ pub fn unsetenv(name string) int { } } -// `file_exists` returns true if `path` exists. +// file_exists returns true if `path` exists. pub fn file_exists(_path string) bool { $if windows { path := _path.replace('/', '\\') @@ -414,44 +414,6 @@ pub fn file_exists(_path string) bool { } } -pub fn dir_exists(path string) bool { - $if windows { - _path := path.replace('/', '\\') - attr := int(C.GetFileAttributes(_path.to_wide())) - if attr == C.INVALID_FILE_ATTRIBUTES { - return false - } - if (attr & C.FILE_ATTRIBUTE_DIRECTORY) != 0 { - return true - } - return false - } - $else { - dir := C.opendir(path.str) - res := !isnil(dir) - if res { - C.closedir(dir) - } - return res - } -} - -// mkdir creates a new directory with the specified path. -pub fn mkdir(path string) { - $if windows { - _path := path.replace('/', '\\') - // Windows doesnt recursively create the folders - // so we need to help it out here - if _path.last_index('\\') != -1 { - mkdir(_path.all_before_last('\\')) - } - C.CreateDirectory(_path.to_wide(), 0) - } - $else { - C.mkdir(path.str, 511)// S_IRWXU | S_IRWXG | S_IRWXO - } -} - // rm removes file in `path`. pub fn rm(path string) { $if windows { @@ -766,33 +728,6 @@ pub fn getwd() string { } } -// win: FILETIME -// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime -struct filetime { - dwLowDateTime u32 - dwHighDateTime u32 -} - -// win: WIN32_FIND_DATA -// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-_win32_find_dataw -struct win32finddata { -mut: - dwFileAttributes u32 - ftCreationTime filetime - ftLastAccessTime filetime - ftLastWriteTime filetime - nFileSizeHigh u32 - nFileSizeLow u32 - dwReserved0 u32 - dwReserved1 u32 - cFileName [260]u16 // MAX_PATH = 260 - cAlternateFileName [14]u16 // 14 - dwFileType u32 - dwCreatorType u32 - wFinderFlags u16 -} - - // walk_ext returns a recursive list of all file paths ending with `ext`. pub fn walk_ext(path, ext string) []string { if !os.is_dir(path) { diff --git a/vlib/os/os_nix.v b/vlib/os/os_nix.v index 6eac20da2b..3a18382d4e 100644 --- a/vlib/os/os_nix.v +++ b/vlib/os/os_nix.v @@ -39,3 +39,19 @@ pub fn ls(path string) []string { C.closedir(dir) return res } + +pub fn dir_exists(path string) bool { + dir := C.opendir(path.str) + res := !isnil(dir) + if res { + C.closedir(dir) + } + return res +} + +// mkdir creates a new directory with the specified path. +pub fn mkdir(path string) { + C.mkdir(path.str, 511)// S_IRWXU | S_IRWXG | S_IRWXO +} + + diff --git a/vlib/os/os_win.v b/vlib/os/os_win.v index 6a8c03f5d3..042711217f 100644 --- a/vlib/os/os_win.v +++ b/vlib/os/os_win.v @@ -11,6 +11,34 @@ const ( // A handle to an object. type HANDLE voidptr +// win: FILETIME +// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime +struct filetime { + dwLowDateTime u32 + dwHighDateTime u32 +} + +// win: WIN32_FIND_DATA +// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-_win32_find_dataw +struct win32finddata { +mut: + dwFileAttributes u32 + ftCreationTime filetime + ftLastAccessTime filetime + ftLastWriteTime filetime + nFileSizeHigh u32 + nFileSizeLow u32 + dwReserved0 u32 + dwReserved1 u32 + cFileName [260]u16 // MAX_PATH = 260 + cAlternateFileName [14]u16 // 14 + dwFileType u32 + dwCreatorType u32 + wFinderFlags u16 +} + + + pub fn ls(path string) []string { mut find_file_data := win32finddata{} mut dir_files := []string @@ -44,6 +72,29 @@ pub fn ls(path string) []string { return dir_files } +pub fn dir_exists(path string) bool { + _path := path.replace('/', '\\') + attr := int(C.GetFileAttributes(_path.to_wide())) + if attr == C.INVALID_FILE_ATTRIBUTES { + return false + } + if (attr & C.FILE_ATTRIBUTE_DIRECTORY) != 0 { + return true + } + return false +} + +// mkdir creates a new directory with the specified path. +pub fn mkdir(path string) { + _path := path.replace('/', '\\') + // Windows doesnt recursively create the folders + // so we need to help it out here + if _path.last_index('\\') != -1 { + mkdir(_path.all_before_last('\\')) + } + C.CreateDirectory(_path.to_wide(), 0) +} + // Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019 // get_file_handle retrieves the operating-system file handle that is associated with the specified file descriptor. pub fn get_file_handle(path string) HANDLE {