Normalize whitespace in os.nim.

I can't edit it - trick with asking git to ignore whitespace doesn't
work if I need to change indention level.
This commit is contained in:
Michał Zieliński 2013-12-09 18:03:36 +01:00
commit 642a0d5563

View file

@ -21,7 +21,7 @@ import
when defined(windows): when defined(windows):
import winlean import winlean
elif defined(posix): elif defined(posix):
import posix import posix
else: else:
{.error: "OS module not ported to your operating system!".} {.error: "OS module not ported to your operating system!".}
@ -33,7 +33,7 @@ type
## from an environment variable ## from an environment variable
FWriteEnv* = object of FWriteIO ## effect that denotes a write FWriteEnv* = object of FWriteIO ## effect that denotes a write
## to an environment variable ## to an environment variable
FReadDir* = object of FReadIO ## effect that denotes a write operation to FReadDir* = object of FReadIO ## effect that denotes a write operation to
## the directory structure ## the directory structure
FWriteDir* = object of FWriteIO ## effect that denotes a write operation to FWriteDir* = object of FWriteIO ## effect that denotes a write operation to
@ -179,7 +179,7 @@ proc OSErrorMsg*(): string {.rtl, extern: "nos$1", deprecated.} =
## Returns "" if no error occured. ## Returns "" if no error occured.
## ##
## **Deprecated since version 0.9.4**: use the other ``OSErrorMsg`` proc. ## **Deprecated since version 0.9.4**: use the other ``OSErrorMsg`` proc.
result = "" result = ""
when defined(Windows): when defined(Windows):
var err = GetLastError() var err = GetLastError()
@ -276,7 +276,7 @@ proc OSLastError*(): TOSErrorCode =
## On Windows some OS calls can reset the error code to ``0`` causing this ## On Windows some OS calls can reset the error code to ``0`` causing this
## procedure to return ``0``. It is therefore advised to call this procedure ## procedure to return ``0``. It is therefore advised to call this procedure
## immediately after an OS call fails. On POSIX systems this is not a problem. ## immediately after an OS call fails. On POSIX systems this is not a problem.
when defined(windows): when defined(windows):
result = TOSErrorCode(GetLastError()) result = TOSErrorCode(GetLastError())
else: else:
@ -355,8 +355,8 @@ when defined(windows):
result = f.cFilename[0] == '.' result = f.cFilename[0] == '.'
template getFilename(f: expr): expr = $f.cFilename template getFilename(f: expr): expr = $f.cFilename
proc existsFile*(filename: string): bool {.rtl, extern: "nos$1", proc existsFile*(filename: string): bool {.rtl, extern: "nos$1",
tags: [FReadDir].} = tags: [FReadDir].} =
## Returns true if the file exists, false otherwise. ## Returns true if the file exists, false otherwise.
when defined(windows): when defined(windows):
@ -410,7 +410,7 @@ proc getLastAccessTime*(file: string): TTime {.rtl, extern: "nos$1".} =
result = winTimeToUnixTime(rdFileTime(f.ftLastAccessTime)) result = winTimeToUnixTime(rdFileTime(f.ftLastAccessTime))
findclose(h) findclose(h)
proc getCreationTime*(file: string): TTime {.rtl, extern: "nos$1".} = proc getCreationTime*(file: string): TTime {.rtl, extern: "nos$1".} =
## Returns the `file`'s creation time. ## Returns the `file`'s creation time.
when defined(posix): when defined(posix):
var res: TStat var res: TStat
@ -524,7 +524,7 @@ proc SplitPath*(path: string): tuple[head, tail: string] {.
## Splits a directory into (head, tail), so that ## Splits a directory into (head, tail), so that
## ``JoinPath(head, tail) == path``. ## ``JoinPath(head, tail) == path``.
## ##
## Examples: ## Examples:
## ##
## .. code-block:: nimrod ## .. code-block:: nimrod
## SplitPath("usr/local/bin") -> ("usr/local", "bin") ## SplitPath("usr/local/bin") -> ("usr/local", "bin")
@ -567,7 +567,7 @@ proc parentDir*(path: string): string {.
proc isRootDir*(path: string): bool {. proc isRootDir*(path: string): bool {.
noSideEffect, rtl, extern: "nos$1".} = noSideEffect, rtl, extern: "nos$1".} =
## Checks whether a given `path` is a root directory ## Checks whether a given `path` is a root directory
result = parentDirPos(path) < 0 result = parentDirPos(path) < 0
iterator parentDirs*(path: string, fromRoot=false, inclusive=true): string = iterator parentDirs*(path: string, fromRoot=false, inclusive=true): string =
@ -589,7 +589,7 @@ iterator parentDirs*(path: string, fromRoot=false, inclusive=true): string =
else: else:
for i in countup(0, path.len - 2): # ignore the last / for i in countup(0, path.len - 2): # ignore the last /
# deal with non-normalized paths such as /foo//bar//baz # deal with non-normalized paths such as /foo//bar//baz
if path[i] in {dirsep, altsep} and if path[i] in {dirsep, altsep} and
(i == 0 or path[i-1] notin {dirsep, altsep}): (i == 0 or path[i-1] notin {dirsep, altsep}):
yield path.substr(0, i) yield path.substr(0, i)
@ -642,7 +642,7 @@ proc splitFile*(path: string): tuple[dir, name, ext: string] {.
var dotPos = path.len var dotPos = path.len
for i in countdown(len(path)-1, 0): for i in countdown(len(path)-1, 0):
if path[i] == ExtSep: if path[i] == ExtSep:
if dotPos == path.len and i > 0 and if dotPos == path.len and i > 0 and
path[i-1] notin {dirsep, altsep}: dotPos = i path[i-1] notin {dirsep, altsep}: dotPos = i
elif path[i] in {dirsep, altsep}: elif path[i] in {dirsep, altsep}:
sepPos = i sepPos = i
@ -653,7 +653,7 @@ proc splitFile*(path: string): tuple[dir, name, ext: string] {.
proc extractFilename*(path: string): string {. proc extractFilename*(path: string): string {.
noSideEffect, rtl, extern: "nos$1".} = noSideEffect, rtl, extern: "nos$1".} =
## Extracts the filename of a given `path`. This is the same as ## Extracts the filename of a given `path`. This is the same as
## ``name & ext`` from ``splitFile(path)``. ## ``name & ext`` from ``splitFile(path)``.
if path.len == 0 or path[path.len-1] in {dirSep, altSep}: if path.len == 0 or path[path.len-1] in {dirSep, altSep}:
result = "" result = ""
@ -669,7 +669,7 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1",
var unused: widecstring var unused: widecstring
var res = newWideCString("", bufsize div 2) var res = newWideCString("", bufsize div 2)
var L = GetFullPathNameW(newWideCString(filename), bufsize, res, unused) var L = GetFullPathNameW(newWideCString(filename), bufsize, res, unused)
if L <= 0'i32 or L >= bufsize: if L <= 0'i32 or L >= bufsize:
OSError(OSLastError()) OSError(OSLastError())
result = res$L result = res$L
else: else:
@ -684,7 +684,7 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1",
var r = realpath(filename, result) var r = realpath(filename, result)
if r.isNil: OSError(OSLastError()) if r.isNil: OSError(OSLastError())
setlen(result, c_strlen(result)) setlen(result, c_strlen(result))
proc ChangeFileExt*(filename, ext: string): string {. proc ChangeFileExt*(filename, ext: string): string {.
noSideEffect, rtl, extern: "nos$1".} = noSideEffect, rtl, extern: "nos$1".} =
## Changes the file extension to `ext`. ## Changes the file extension to `ext`.
@ -740,12 +740,12 @@ proc isAbsolute*(path: string): bool {.rtl, noSideEffect, extern: "nos$1".} =
elif defined(posix): elif defined(posix):
result = path[0] == '/' result = path[0] == '/'
proc sameFile*(path1, path2: string): bool {.rtl, extern: "nos$1", proc sameFile*(path1, path2: string): bool {.rtl, extern: "nos$1",
tags: [FReadDir].} = tags: [FReadDir].} =
## Returns True if both pathname arguments refer to the same physical ## Returns True if both pathname arguments refer to the same physical
## file or directory. Raises an exception if any of the files does not ## file or directory. Raises an exception if any of the files does not
## exist or information about it can not be obtained. ## exist or information about it can not be obtained.
## ##
## This proc will return true if given two alternative hard-linked or ## This proc will return true if given two alternative hard-linked or
## sym-linked paths to the same file or directory. ## sym-linked paths to the same file or directory.
when defined(Windows): when defined(Windows):
@ -761,7 +761,7 @@ proc sameFile*(path1, path2: string): bool {.rtl, extern: "nos$1",
var f1 = OpenHandle(p1) var f1 = OpenHandle(p1)
var f2 = OpenHandle(p2) var f2 = OpenHandle(p2)
else: else:
template OpenHandle(path: expr): expr = template OpenHandle(path: expr): expr =
CreateFileA(path, 0'i32, FILE_SHARE_DELETE or FILE_SHARE_READ or CreateFileA(path, 0'i32, FILE_SHARE_DELETE or FILE_SHARE_READ or
@ -829,7 +829,7 @@ proc sameFileContent*(path1, path2: string): bool {.rtl, extern: "nos$1",
close(a) close(a)
close(b) close(b)
proc copyFile*(source, dest: string) {.rtl, extern: "nos$1", proc copyFile*(source, dest: string) {.rtl, extern: "nos$1",
tags: [FReadIO, FWriteIO].} = tags: [FReadIO, FWriteIO].} =
## Copies a file from `source` to `dest`. ## Copies a file from `source` to `dest`.
## ##
@ -870,7 +870,7 @@ proc copyFile*(source, dest: string) {.rtl, extern: "nos$1",
close(s) close(s)
close(d) close(d)
proc moveFile*(source, dest: string) {.rtl, extern: "nos$1", proc moveFile*(source, dest: string) {.rtl, extern: "nos$1",
tags: [FReadIO, FWriteIO].} = tags: [FReadIO, FWriteIO].} =
## Moves a file from `source` to `dest`. If this fails, `EOS` is raised. ## Moves a file from `source` to `dest`. If this fails, `EOS` is raised.
if crename(source, dest) != 0'i32: if crename(source, dest) != 0'i32:
@ -885,7 +885,7 @@ proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} =
if cremove(file) != 0'i32 and errno != ENOENT: if cremove(file) != 0'i32 and errno != ENOENT:
raise newException(EOS, $strerror(errno)) raise newException(EOS, $strerror(errno))
proc execShellCmd*(command: string): int {.rtl, extern: "nos$1", proc execShellCmd*(command: string): int {.rtl, extern: "nos$1",
tags: [FExecIO].} = tags: [FExecIO].} =
## Executes a `shell command`:idx:. ## Executes a `shell command`:idx:.
## ##
@ -897,7 +897,7 @@ proc execShellCmd*(command: string): int {.rtl, extern: "nos$1",
## module. ## module.
result = csystem(command) result = csystem(command)
# Environment handling cannot be put into RTL, because the ``envPairs`` # Environment handling cannot be put into RTL, because the ``envPairs``
# iterator depends on ``environment``. # iterator depends on ``environment``.
var var
@ -943,11 +943,11 @@ when defined(windows):
else: else:
const const
useNSGetEnviron = defined(macosx) and useNSGetEnviron = defined(macosx) and
(defined(createNimRtl) or defined(useNimRtl)) (defined(createNimRtl) or defined(useNimRtl))
when useNSGetEnviron: when useNSGetEnviron:
# From the manual: # From the manual:
# Shared libraries and bundles don't have direct access to environ, # Shared libraries and bundles don't have direct access to environ,
# which is only available to the loader ld(1) when a complete program # which is only available to the loader ld(1) when a complete program
# is being linked. # is being linked.
# The environment routines can still be used, but if direct access to # The environment routines can still be used, but if direct access to
@ -1025,13 +1025,13 @@ proc putEnv*(key, val: string) {.tags: [FWriteEnv].} =
if SetEnvironmentVariableA(key, val) == 0'i32: OSError(OSLastError()) if SetEnvironmentVariableA(key, val) == 0'i32: OSError(OSLastError())
iterator envPairs*(): tuple[key, value: TaintedString] {.tags: [FReadEnv].} = iterator envPairs*(): tuple[key, value: TaintedString] {.tags: [FReadEnv].} =
## Iterate over all `environments variables`:idx:. In the first component ## Iterate over all `environments variables`:idx:. In the first component
## of the tuple is the name of the current variable stored, in the second ## of the tuple is the name of the current variable stored, in the second
## its value. ## its value.
getEnvVarsC() getEnvVarsC()
for i in 0..high(environment): for i in 0..high(environment):
var p = find(environment[i], '=') var p = find(environment[i], '=')
yield (TaintedString(substr(environment[i], 0, p-1)), yield (TaintedString(substr(environment[i], 0, p-1)),
TaintedString(substr(environment[i], p+1))) TaintedString(substr(environment[i], p+1)))
iterator walkFiles*(pattern: string): string {.tags: [FReadDir].} = iterator walkFiles*(pattern: string): string {.tags: [FReadDir].} =
@ -1125,7 +1125,7 @@ iterator walkDir*(dir: string): tuple[kind: TPathComponent, path: string] {.
iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {. iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {.
tags: [FReadDir].} = tags: [FReadDir].} =
## walks over the directory `dir` and yields for each file in `dir`. The ## walks over the directory `dir` and yields for each file in `dir`. The
## full path for each file is returned. ## full path for each file is returned.
## Walking is recursive. `filter` controls the behaviour of the iterator: ## Walking is recursive. `filter` controls the behaviour of the iterator:
## ##
@ -1137,7 +1137,7 @@ iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {.
## ``pcDir`` follow real directories ## ``pcDir`` follow real directories
## ``pcLinkToDir`` follow symbolic links to directories ## ``pcLinkToDir`` follow symbolic links to directories
## --------------------- --------------------------------------------- ## --------------------- ---------------------------------------------
## ##
var stack = @[dir] var stack = @[dir]
while stack.len > 0: while stack.len > 0:
for k,p in walkDir(stack.pop()): for k,p in walkDir(stack.pop()):
@ -1146,14 +1146,14 @@ iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {.
of pcFile, pcLinkToFile: yield p of pcFile, pcLinkToFile: yield p
of pcDir, pcLinkToDir: stack.add(p) of pcDir, pcLinkToDir: stack.add(p)
proc rawRemoveDir(dir: string) = proc rawRemoveDir(dir: string) =
when defined(windows): when defined(windows):
when useWinUnicode: when useWinUnicode:
wrapUnary(res, RemoveDirectoryW, dir) wrapUnary(res, RemoveDirectoryW, dir)
else: else:
var res = RemoveDirectoryA(dir) var res = RemoveDirectoryA(dir)
let lastError = OSLastError() let lastError = OSLastError()
if res == 0'i32 and lastError.int32 != 3'i32 and if res == 0'i32 and lastError.int32 != 3'i32 and
lastError.int32 != 18'i32 and lastError.int32 != 2'i32: lastError.int32 != 18'i32 and lastError.int32 != 2'i32:
OSError(lastError) OSError(lastError)
else: else:
@ -1166,7 +1166,7 @@ proc removeDir*(dir: string) {.rtl, extern: "nos$1", tags: [
## ##
## If this fails, `EOS` is raised. This does not fail if the directory never ## If this fails, `EOS` is raised. This does not fail if the directory never
## existed in the first place. ## existed in the first place.
for kind, path in walkDir(dir): for kind, path in walkDir(dir):
case kind case kind
of pcFile, pcLinkToFile, pcLinkToDir: removeFile(path) of pcFile, pcLinkToFile, pcLinkToDir: removeFile(path)
of pcDir: removeDir(path) of pcDir: removeDir(path)
@ -1192,7 +1192,7 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} =
## ##
## The directory may contain several subdirectories that do not exist yet. ## The directory may contain several subdirectories that do not exist yet.
## The full path is created. If this fails, `EOS` is raised. It does **not** ## The full path is created. If this fails, `EOS` is raised. It does **not**
## fail if the path already exists because for most usages this does not ## fail if the path already exists because for most usages this does not
## indicate an error. ## indicate an error.
var omitNext = false var omitNext = false
when defined(doslike): when defined(doslike):
@ -1205,7 +1205,7 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} =
rawCreateDir(substr(dir, 0, i-1)) rawCreateDir(substr(dir, 0, i-1))
rawCreateDir(dir) rawCreateDir(dir)
proc copyDir*(source, dest: string) {.rtl, extern: "nos$1", proc copyDir*(source, dest: string) {.rtl, extern: "nos$1",
tags: [FWriteIO, FReadIO].} = tags: [FWriteIO, FReadIO].} =
## Copies a directory from `source` to `dest`. If this fails, `EOS` is raised. ## Copies a directory from `source` to `dest`. If this fails, `EOS` is raised.
createDir(dest) createDir(dest)
@ -1220,7 +1220,7 @@ proc copyDir*(source, dest: string) {.rtl, extern: "nos$1",
proc parseCmdLine*(c: string): seq[string] {. proc parseCmdLine*(c: string): seq[string] {.
noSideEffect, rtl, extern: "nos$1".} = noSideEffect, rtl, extern: "nos$1".} =
## Splits a command line into several components; ## Splits a command line into several components;
## This proc is only occassionally useful, better use the `parseopt` module. ## This proc is only occassionally useful, better use the `parseopt` module.
## ##
## On Windows, it uses the following parsing rules ## On Windows, it uses the following parsing rules
@ -1247,7 +1247,7 @@ proc parseCmdLine*(c: string): seq[string] {.
## causing a literal double quotation mark (") to be placed in argv. ## causing a literal double quotation mark (") to be placed in argv.
## ##
## On Posix systems, it uses the following parsing rules: ## On Posix systems, it uses the following parsing rules:
## Components are separated by whitespace unless the whitespace ## Components are separated by whitespace unless the whitespace
## occurs within ``"`` or ``'`` quotes. ## occurs within ``"`` or ``'`` quotes.
result = @[] result = @[]
var i = 0 var i = 0
@ -1260,31 +1260,31 @@ proc parseCmdLine*(c: string): seq[string] {.
if c[i] == '\0': break if c[i] == '\0': break
var inQuote = false var inQuote = false
while true: while true:
case c[i] case c[i]
of '\0': break of '\0': break
of '\\': of '\\':
var j = i var j = i
while c[j] == '\\': inc(j) while c[j] == '\\': inc(j)
if c[j] == '"': if c[j] == '"':
for k in 1..(j-i) div 2: a.add('\\') for k in 1..(j-i) div 2: a.add('\\')
if (j-i) mod 2 == 0: if (j-i) mod 2 == 0:
i = j i = j
else: else:
a.add('"') a.add('"')
i = j+1 i = j+1
else: else:
a.add(c[i]) a.add(c[i])
inc(i) inc(i)
of '"': of '"':
inc(i) inc(i)
if not inQuote: inQuote = true if not inQuote: inQuote = true
elif c[i] == '"': elif c[i] == '"':
a.add(c[i]) a.add(c[i])
inc(i) inc(i)
else: else:
inQuote = false inQuote = false
break break
of ' ', '\t': of ' ', '\t':
if not inQuote: break if not inQuote: break
a.add(c[i]) a.add(c[i])
inc(i) inc(i)
@ -1346,11 +1346,11 @@ proc getFilePermissions*(filename: string): set[TFilePermission] {.
var res = GetFileAttributesA(filename) var res = GetFileAttributesA(filename)
if res == -1'i32: OSError(OSLastError()) if res == -1'i32: OSError(OSLastError())
if (res and FILE_ATTRIBUTE_READONLY) != 0'i32: if (res and FILE_ATTRIBUTE_READONLY) != 0'i32:
result = {fpUserExec, fpUserRead, fpGroupExec, fpGroupRead, result = {fpUserExec, fpUserRead, fpGroupExec, fpGroupRead,
fpOthersExec, fpOthersRead} fpOthersExec, fpOthersRead}
else: else:
result = {fpUserExec..fpOthersRead} result = {fpUserExec..fpOthersRead}
proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {. proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {.
rtl, extern: "nos$1", tags: [FWriteDir].} = rtl, extern: "nos$1", tags: [FWriteDir].} =
## sets the file permissions for `filename`. `OSError` is raised in case of ## sets the file permissions for `filename`. `OSError` is raised in case of
@ -1361,15 +1361,15 @@ proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {.
if fpUserRead in permissions: p = p or S_IRUSR if fpUserRead in permissions: p = p or S_IRUSR
if fpUserWrite in permissions: p = p or S_IWUSR if fpUserWrite in permissions: p = p or S_IWUSR
if fpUserExec in permissions: p = p or S_IXUSR if fpUserExec in permissions: p = p or S_IXUSR
if fpGroupRead in permissions: p = p or S_IRGRP if fpGroupRead in permissions: p = p or S_IRGRP
if fpGroupWrite in permissions: p = p or S_IWGRP if fpGroupWrite in permissions: p = p or S_IWGRP
if fpGroupExec in permissions: p = p or S_IXGRP if fpGroupExec in permissions: p = p or S_IXGRP
if fpOthersRead in permissions: p = p or S_IROTH if fpOthersRead in permissions: p = p or S_IROTH
if fpOthersWrite in permissions: p = p or S_IWOTH if fpOthersWrite in permissions: p = p or S_IWOTH
if fpOthersExec in permissions: p = p or S_IXOTH if fpOthersExec in permissions: p = p or S_IXOTH
if chmod(filename, p) != 0: OSError(OSLastError()) if chmod(filename, p) != 0: OSError(OSLastError())
else: else:
when useWinUnicode: when useWinUnicode:
@ -1377,7 +1377,7 @@ proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {.
else: else:
var res = GetFileAttributesA(filename) var res = GetFileAttributesA(filename)
if res == -1'i32: OSError(OSLastError()) if res == -1'i32: OSError(OSLastError())
if fpUserWrite in permissions: if fpUserWrite in permissions:
res = res and not FILE_ATTRIBUTE_READONLY res = res and not FILE_ATTRIBUTE_READONLY
else: else:
res = res or FILE_ATTRIBUTE_READONLY res = res or FILE_ATTRIBUTE_READONLY
@ -1386,7 +1386,7 @@ proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {.
else: else:
var res2 = SetFileAttributesA(filename, res) var res2 = SetFileAttributesA(filename, res)
if res2 == - 1'i32: OSError(OSLastError()) if res2 == - 1'i32: OSError(OSLastError())
proc copyFileWithPermissions*(source, dest: string, proc copyFileWithPermissions*(source, dest: string,
ignorePermissionErrors = true) = ignorePermissionErrors = true) =
## Copies a file from `source` to `dest` preserving file permissions. ## Copies a file from `source` to `dest` preserving file permissions.
@ -1407,19 +1407,19 @@ proc copyFileWithPermissions*(source, dest: string,
if not ignorePermissionErrors: if not ignorePermissionErrors:
raise raise
proc inclFilePermissions*(filename: string, proc inclFilePermissions*(filename: string,
permissions: set[TFilePermission]) {. permissions: set[TFilePermission]) {.
rtl, extern: "nos$1", tags: [FReadDir, FWriteDir].} = rtl, extern: "nos$1", tags: [FReadDir, FWriteDir].} =
## a convenience procedure for: ## a convenience procedure for:
## ##
## .. code-block:: nimrod ## .. code-block:: nimrod
## setFilePermissions(filename, getFilePermissions(filename)+permissions) ## setFilePermissions(filename, getFilePermissions(filename)+permissions)
setFilePermissions(filename, getFilePermissions(filename)+permissions) setFilePermissions(filename, getFilePermissions(filename)+permissions)
proc exclFilePermissions*(filename: string, proc exclFilePermissions*(filename: string,
permissions: set[TFilePermission]) {. permissions: set[TFilePermission]) {.
rtl, extern: "nos$1", tags: [FReadDir, FWriteDir].} = rtl, extern: "nos$1", tags: [FReadDir, FWriteDir].} =
## a convenience procedure for: ## a convenience procedure for:
## ##
## .. code-block:: nimrod ## .. code-block:: nimrod
## setFilePermissions(filename, getFilePermissions(filename)-permissions) ## setFilePermissions(filename, getFilePermissions(filename)-permissions)
@ -1459,7 +1459,7 @@ when defined(windows):
if isNil(ownArgv): ownArgv = parseCmdLine($getCommandLine()) if isNil(ownArgv): ownArgv = parseCmdLine($getCommandLine())
result = ownArgv.len-1 result = ownArgv.len-1
proc paramStr*(i: int): TaintedString {.rtl, extern: "nos$1", proc paramStr*(i: int): TaintedString {.rtl, extern: "nos$1",
tags: [FReadIO].} = tags: [FReadIO].} =
## Returns the `i`-th `command line argument`:idx: given to the ## Returns the `i`-th `command line argument`:idx: given to the
## application. ## application.
@ -1494,7 +1494,7 @@ when defined(macosx):
type type
cuint32* {.importc: "unsigned int", nodecl.} = int cuint32* {.importc: "unsigned int", nodecl.} = int
## This is the same as the type ``uint32_t`` in *C*. ## This is the same as the type ``uint32_t`` in *C*.
# a really hacky solution: since we like to include 2 headers we have to # a really hacky solution: since we like to include 2 headers we have to
# define two procs which in reality are the same # define two procs which in reality are the same
proc getExecPath1(c: cstring, size: var cuint32) {. proc getExecPath1(c: cstring, size: var cuint32) {.
@ -1553,13 +1553,13 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [FReadIO].} =
proc getApplicationFilename*(): string {.rtl, extern: "nos$1", deprecated.} = proc getApplicationFilename*(): string {.rtl, extern: "nos$1", deprecated.} =
## Returns the filename of the application's executable. ## Returns the filename of the application's executable.
## **Deprecated since version 0.8.12**: use ``getAppFilename`` ## **Deprecated since version 0.8.12**: use ``getAppFilename``
## instead. ## instead.
result = getAppFilename() result = getAppFilename()
proc getApplicationDir*(): string {.rtl, extern: "nos$1", deprecated.} = proc getApplicationDir*(): string {.rtl, extern: "nos$1", deprecated.} =
## Returns the directory of the application's executable. ## Returns the directory of the application's executable.
## **Deprecated since version 0.8.12**: use ``getAppDir`` ## **Deprecated since version 0.8.12**: use ``getAppDir``
## instead. ## instead.
result = splitFile(getAppFilename()).dir result = splitFile(getAppFilename()).dir
@ -1580,7 +1580,7 @@ proc sleep*(milsecs: int) {.rtl, extern: "nos$1", tags: [FTime].} =
proc getFileSize*(file: string): biggestInt {.rtl, extern: "nos$1", proc getFileSize*(file: string): biggestInt {.rtl, extern: "nos$1",
tags: [FReadIO].} = tags: [FReadIO].} =
## returns the file size of `file`. Can raise ``EOS``. ## returns the file size of `file`. Can raise ``EOS``.
when defined(windows): when defined(windows):
var a: TWin32FindData var a: TWin32FindData
var resA = findfirstFile(file, a) var resA = findfirstFile(file, a)
@ -1589,20 +1589,20 @@ proc getFileSize*(file: string): biggestInt {.rtl, extern: "nos$1",
findclose(resA) findclose(resA)
else: else:
var f: TFile var f: TFile
if open(f, file): if open(f, file):
result = getFileSize(f) result = getFileSize(f)
close(f) close(f)
else: OSError(OSLastError()) else: OSError(OSLastError())
proc findExe*(exe: string): string {.tags: [FReadDir, FReadEnv].} = proc findExe*(exe: string): string {.tags: [FReadDir, FReadEnv].} =
## Searches for `exe` in the current working directory and then ## Searches for `exe` in the current working directory and then
## in directories listed in the ``PATH`` environment variable. ## in directories listed in the ``PATH`` environment variable.
## Returns "" if the `exe` cannot be found. On DOS-like platforms, `exe` ## Returns "" if the `exe` cannot be found. On DOS-like platforms, `exe`
## is added an ``.exe`` file extension if it has no extension. ## is added an ``.exe`` file extension if it has no extension.
result = addFileExt(exe, os.exeExt) result = addFileExt(exe, os.exeExt)
if ExistsFile(result): return if ExistsFile(result): return
var path = string(os.getEnv("PATH")) var path = string(os.getEnv("PATH"))
for candidate in split(path, pathSep): for candidate in split(path, pathSep):
var x = candidate / result var x = candidate / result
if ExistsFile(x): return x if ExistsFile(x): return x
result = "" result = ""
@ -1629,4 +1629,3 @@ proc expandTilde*(path: string): string =
result = path result = path
{.pop.} {.pop.}