Import ospaths instead of include (#5233)
* Import ospaths instead of include * searchExtPos made public
This commit is contained in:
parent
05dec08cea
commit
fe0291f127
2 changed files with 14 additions and 12 deletions
|
|
@ -26,7 +26,8 @@ elif defined(posix):
|
||||||
else:
|
else:
|
||||||
{.error: "OS module not ported to your operating system!".}
|
{.error: "OS module not ported to your operating system!".}
|
||||||
|
|
||||||
include ospaths
|
import ospaths
|
||||||
|
export ospaths
|
||||||
|
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
when NoFakeVars:
|
when NoFakeVars:
|
||||||
|
|
@ -848,12 +849,12 @@ template walkCommon(pattern: string, filter) =
|
||||||
res = findFirstFile(pattern, f)
|
res = findFirstFile(pattern, f)
|
||||||
if res != -1:
|
if res != -1:
|
||||||
defer: findClose(res)
|
defer: findClose(res)
|
||||||
|
let dotPos = searchExtPos(pattern)
|
||||||
while true:
|
while true:
|
||||||
if not skipFindData(f) and filter(f):
|
if not skipFindData(f) and filter(f):
|
||||||
# Windows bug/gotcha: 't*.nim' matches 'tfoo.nims' -.- so we check
|
# Windows bug/gotcha: 't*.nim' matches 'tfoo.nims' -.- so we check
|
||||||
# that the file extensions have the same length ...
|
# that the file extensions have the same length ...
|
||||||
let ff = getFilename(f)
|
let ff = getFilename(f)
|
||||||
let dotPos = searchExtPos(pattern)
|
|
||||||
let idx = ff.len - pattern.len + dotPos
|
let idx = ff.len - pattern.len + dotPos
|
||||||
if dotPos < 0 or idx >= ff.len or ff[idx] == '.' or
|
if dotPos < 0 or idx >= ff.len or ff[idx] == '.' or
|
||||||
pattern[dotPos+1] == '*':
|
pattern[dotPos+1] == '*':
|
||||||
|
|
@ -1109,7 +1110,7 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1",
|
||||||
## fail if the directory already exists because for most usages this does not
|
## fail if the directory already exists because for most usages this does not
|
||||||
## indicate an error.
|
## indicate an error.
|
||||||
var omitNext = false
|
var omitNext = false
|
||||||
when doslike:
|
when doslikeFileSystem:
|
||||||
omitNext = isAbsolute(dir)
|
omitNext = isAbsolute(dir)
|
||||||
for i in 1.. dir.len-1:
|
for i in 1.. dir.len-1:
|
||||||
if dir[i] in {DirSep, AltSep}:
|
if dir[i] in {DirSep, AltSep}:
|
||||||
|
|
|
||||||
|
|
@ -40,8 +40,7 @@ when not declared(getEnv) or defined(nimscript):
|
||||||
TOSErrorCode: OSErrorCode
|
TOSErrorCode: OSErrorCode
|
||||||
].}
|
].}
|
||||||
const
|
const
|
||||||
doslike = defined(windows) or defined(OS2) or defined(DOS)
|
doslikeFileSystem* = defined(windows) or defined(OS2) or defined(DOS)
|
||||||
# DOS-like filesystem
|
|
||||||
|
|
||||||
when defined(Nimdoc): # only for proper documentation:
|
when defined(Nimdoc): # only for proper documentation:
|
||||||
const
|
const
|
||||||
|
|
@ -120,7 +119,7 @@ when not declared(getEnv) or defined(nimscript):
|
||||||
# waterproof. In case of equal names the first volume found will do.
|
# waterproof. In case of equal names the first volume found will do.
|
||||||
# Two colons "::" are the relative path to the parent. Three is to the
|
# Two colons "::" are the relative path to the parent. Three is to the
|
||||||
# grandparent etc.
|
# grandparent etc.
|
||||||
elif doslike:
|
elif doslikeFileSystem:
|
||||||
const
|
const
|
||||||
CurDir* = '.'
|
CurDir* = '.'
|
||||||
ParDir* = ".."
|
ParDir* = ".."
|
||||||
|
|
@ -331,14 +330,16 @@ when not declared(getEnv) or defined(nimscript):
|
||||||
if ext == "" or ext[0] == ExtSep: result = ext # no copy needed here
|
if ext == "" or ext[0] == ExtSep: result = ext # no copy needed here
|
||||||
else: result = ExtSep & ext
|
else: result = ExtSep & ext
|
||||||
|
|
||||||
proc searchExtPos(s: string): int =
|
proc searchExtPos*(path: string): int =
|
||||||
|
## Returns index of the '.' char in `path` if it signifies the beginning
|
||||||
|
## of extension. Returns -1 otherwise.
|
||||||
# BUGFIX: do not search until 0! .DS_Store is no file extension!
|
# BUGFIX: do not search until 0! .DS_Store is no file extension!
|
||||||
result = -1
|
result = -1
|
||||||
for i in countdown(len(s)-1, 1):
|
for i in countdown(len(path)-1, 1):
|
||||||
if s[i] == ExtSep:
|
if path[i] == ExtSep:
|
||||||
result = i
|
result = i
|
||||||
break
|
break
|
||||||
elif s[i] in {DirSep, AltSep}:
|
elif path[i] in {DirSep, AltSep}:
|
||||||
break # do not skip over path
|
break # do not skip over path
|
||||||
|
|
||||||
proc splitFile*(path: string): tuple[dir, name, ext: string] {.
|
proc splitFile*(path: string): tuple[dir, name, ext: string] {.
|
||||||
|
|
@ -432,7 +433,7 @@ when not declared(getEnv) or defined(nimscript):
|
||||||
## Checks whether a given `path` is absolute.
|
## Checks whether a given `path` is absolute.
|
||||||
##
|
##
|
||||||
## On Windows, network paths are considered absolute too.
|
## On Windows, network paths are considered absolute too.
|
||||||
when doslike:
|
when doslikeFileSystem:
|
||||||
var len = len(path)
|
var len = len(path)
|
||||||
result = (len > 0 and path[0] in {'/', '\\'}) or
|
result = (len > 0 and path[0] in {'/', '\\'}) or
|
||||||
(len > 1 and path[0] in {'a'..'z', 'A'..'Z'} and path[1] == ':')
|
(len > 1 and path[0] in {'a'..'z', 'A'..'Z'} and path[1] == ':')
|
||||||
|
|
@ -461,7 +462,7 @@ when not declared(getEnv) or defined(nimscript):
|
||||||
var start: int
|
var start: int
|
||||||
if path[0] == '/':
|
if path[0] == '/':
|
||||||
# an absolute path
|
# an absolute path
|
||||||
when doslike:
|
when doslikeFileSystem:
|
||||||
if drive != "":
|
if drive != "":
|
||||||
result = drive & ":" & DirSep
|
result = drive & ":" & DirSep
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue