os.dir(); fix vroot on Windows

pull/1170/head
Alexander Medvednikov 2019-07-16 01:57:03 +02:00
parent 9177256726
commit cc06fe7ae6
5 changed files with 28 additions and 1 deletions

View File

@ -933,7 +933,7 @@ fn new_v(args[]string) *V {
'option.v', 'option.v',
] ]
// Location of all vlib files // Location of all vlib files
vroot := os.executable().all_before_last('/') vroot := os.dir(os.executable())
println('VROOT=$vroot') println('VROOT=$vroot')
// v.exe's parent directory should contain vlib // v.exe's parent directory should contain vlib
if os.dir_exists(vroot) && os.dir_exists(vroot + '/vlib/builtin') { if os.dir_exists(vroot) && os.dir_exists(vroot + '/vlib/builtin') {

View File

@ -382,6 +382,16 @@ pub fn ext(path string) string {
return path.right(pos) return path.right(pos)
} }
// dir returns all but the last element of path, typically the path's directory.
pub fn dir(path string) string {
pos := path.last_index(PathSeparator)
if pos == -1 {
return '.'
}
return path.left(pos)
}
fn path_sans_ext(path string) string { fn path_sans_ext(path string) string {
pos := path.last_index('.') pos := path.last_index('.')
if pos == -1 { if pos == -1 {

View File

@ -1,3 +1,7 @@
module os module os
#include <dirent.h> #include <dirent.h>
const (
PathSeparator = '/'
)

View File

@ -34,6 +34,15 @@ fn test_write_and_read_string_to_file() {
os.rm(filename) os.rm(filename)
} }
fn test_dir() {
$if windows {
assert os.dir('C:\a\b\c') == 'C:\a\b'
} $else {
assert os.dir('/var/tmp/foo') == '/var/tmp'
}
}
//fn test_fork() { //fn test_fork() {
// pid := os.fork() // pid := os.fork()
// if pid == 0 { // if pid == 0 {

View File

@ -1,5 +1,9 @@
module os module os
const (
PathSeparator = '/'
)
// Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types // Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types
// A handle to an object. // A handle to an object.
type HANDLE voidptr type HANDLE voidptr