move io out of system (#19442)

* move io out of system

* fix tests

* fix tests

* next step

* rename to syncio

* rename

* fix nimscript

* comma

* fix

* fix parts of errors

* good for now

* fix test
This commit is contained in:
flywind 2022-02-03 00:10:11 +08:00 • committed by GitHub
commit 891329cd4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 92 additions and 42 deletions

View file

@ -20,6 +20,8 @@
- `addr` is now available for all addressable locations, `unsafeAddr` is deprecated and - `addr` is now available for all addressable locations, `unsafeAddr` is deprecated and
becomes an alias for `addr`. becomes an alias for `addr`.
- io is about to move out of system; use `-d:nimPreviewSlimSystem` and import `std/syncio`.
## Standard library additions and changes ## Standard library additions and changes
- `macros.parseExpr` and `macros.parseStmt` now accept an optional - `macros.parseExpr` and `macros.parseStmt` now accept an optional

View file

@ -12,6 +12,9 @@
import os, pathnorm import os, pathnorm
when defined(nimSlimSystem):
import std/syncio
type type
AbsoluteFile* = distinct string AbsoluteFile* = distinct string
AbsoluteDir* = distinct string AbsoluteDir* = distinct string

View file

@ -47,7 +47,7 @@ template systemop(op) {.dirty.} =
registerCallback(c, "stdlib.system." & astToStr(op), `op Wrapper`) registerCallback(c, "stdlib.system." & astToStr(op), `op Wrapper`)
template ioop(op) {.dirty.} = template ioop(op) {.dirty.} =
registerCallback(c, "stdlib.io." & astToStr(op), `op Wrapper`) registerCallback(c, "stdlib.syncio." & astToStr(op), `op Wrapper`)
template macrosop(op) {.dirty.} = template macrosop(op) {.dirty.} =
registerCallback(c, "stdlib.macros." & astToStr(op), `op Wrapper`) registerCallback(c, "stdlib.macros." & astToStr(op), `op Wrapper`)

View file

@ -84,8 +84,8 @@ done with spaces only, tabulators are not allowed.
String literals are enclosed in double-quotes. The `var` statement declares String literals are enclosed in double-quotes. The `var` statement declares
a new variable named `name` of type `string` with the value that is a new variable named `name` of type `string` with the value that is
returned by the `readLine <io.html#readLine,File>`_ procedure. Since the returned by the `readLine <syncio.html#readLine,File>`_ procedure. Since the
compiler knows that `readLine <io.html#readLine,File>`_ returns a string, compiler knows that `readLine <syncio.html#readLine,File>`_ returns a string,
you can leave out the type in the declaration (this is called `local type you can leave out the type in the declaration (this is called `local type
inference`:idx:). So this will work too: inference`:idx:). So this will work too:
@ -97,7 +97,7 @@ Note that this is basically the only form of type inference that exists in
Nim: it is a good compromise between brevity and readability. Nim: it is a good compromise between brevity and readability.
The "hello world" program contains several identifiers that are already known The "hello world" program contains several identifiers that are already known
to the compiler: `echo`, `readLine <io.html#readLine,File>`_, etc. to the compiler: `echo`, `readLine <syncio.html#readLine,File>`_, etc.
These built-ins are declared in the system_ module which is implicitly These built-ins are declared in the system_ module which is implicitly
imported by any other module. imported by any other module.
@ -594,7 +594,7 @@ Procedures
========== ==========
To define new commands like `echo <system.html#echo,varargs[typed,]>`_ To define new commands like `echo <system.html#echo,varargs[typed,]>`_
and `readLine <io.html#readLine,File>`_ in the examples, the concept of a and `readLine <syncio.html#readLine,File>`_ in the examples, the concept of a
*procedure* is needed. You might be used to them being called *methods* or *procedure* is needed. You might be used to them being called *methods* or
*functions* in other languages, but Nim *functions* in other languages, but Nim
`differentiates these concepts <tut1.html#procedures-funcs-and-methods>`_. In `differentiates these concepts <tut1.html#procedures-funcs-and-methods>`_. In

View file

@ -37,6 +37,9 @@
when defined(nimHasStyleChecks): when defined(nimHasStyleChecks):
{.push styleChecks: off.} {.push styleChecks: off.}
when defined(nimSlimSystem):
import std/syncio
# TODO these constants don't seem to be fetched from a header file for unknown # TODO these constants don't seem to be fetched from a header file for unknown
# platforms - where do they come from and why are they here? # platforms - where do they come from and why are they here?
when false: when false:

View file

@ -51,6 +51,9 @@
import strutils, streams, parsexml, xmltree, unicode, strtabs import strutils, streams, parsexml, xmltree, unicode, strtabs
when defined(nimPreviewSlimSystem):
import std/syncio
type type
HtmlTag* = enum ## list of all supported HTML tags; order will always be HtmlTag* = enum ## list of all supported HTML tags; order will always be
## alphabetically ## alphabetically

View file

@ -164,6 +164,9 @@ import hashes, tables, strutils, lexbase, streams, macros, parsejson
import options # xxx remove this dependency using same approach as https://github.com/nim-lang/Nim/pull/14563 import options # xxx remove this dependency using same approach as https://github.com/nim-lang/Nim/pull/14563
import std/private/since import std/private/since
when defined(nimPreviewSlimSystem):
import std/syncio
export export
tables.`$` tables.`$`

View file

@ -48,7 +48,7 @@
## .. warning:: ## .. warning::
## For loggers that log to a console or to files, only error and fatal ## For loggers that log to a console or to files, only error and fatal
## messages will cause their output buffers to be flushed immediately. ## messages will cause their output buffers to be flushed immediately.
## Use the `flushFile proc <io.html#flushFile,File>`_ to flush the buffer ## Use the `flushFile proc <syncio.html#flushFile,File>`_ to flush the buffer
## manually if needed. ## manually if needed.
## ##
## Handlers ## Handlers
@ -146,6 +146,9 @@ import strutils, times
when not defined(js): when not defined(js):
import os import os
when defined(nimPreviewSlimSystem):
import std/syncio
type type
Level* = enum ## \ Level* = enum ## \
## Enumeration of logging levels. ## Enumeration of logging levels.
@ -346,7 +349,7 @@ method log*(logger: ConsoleLogger, level: Level, args: varargs[string, `$`]) =
## ##
## **Note:** Only error and fatal messages will cause the output buffer ## **Note:** Only error and fatal messages will cause the output buffer
## to be flushed immediately. Use the `flushFile proc ## to be flushed immediately. Use the `flushFile proc
## <io.html#flushFile,File>`_ to flush the buffer manually if needed. ## <syncio.html#flushFile,File>`_ to flush the buffer manually if needed.
## ##
## See also: ## See also:
## * `log method<#log.e,FileLogger,Level,varargs[string,]>`_ ## * `log method<#log.e,FileLogger,Level,varargs[string,]>`_
@ -422,7 +425,7 @@ when not defined(js):
## **Notes:** ## **Notes:**
## * Only error and fatal messages will cause the output buffer ## * Only error and fatal messages will cause the output buffer
## to be flushed immediately. Use the `flushFile proc ## to be flushed immediately. Use the `flushFile proc
## <io.html#flushFile,File>`_ to flush the buffer manually if needed. ## <syncio.html#flushFile,File>`_ to flush the buffer manually if needed.
## * This method is not available for the JavaScript backend. ## * This method is not available for the JavaScript backend.
## ##
## See also: ## See also:
@ -600,7 +603,7 @@ when not defined(js):
## **Notes:** ## **Notes:**
## * Only error and fatal messages will cause the output buffer ## * Only error and fatal messages will cause the output buffer
## to be flushed immediately. Use the `flushFile proc ## to be flushed immediately. Use the `flushFile proc
## <io.html#flushFile,File>`_ to flush the buffer manually if needed. ## <syncio.html#flushFile,File>`_ to flush the buffer manually if needed.
## * This method is not available for the JavaScript backend. ## * This method is not available for the JavaScript backend.
## ##
## See also: ## See also:

View file

@ -431,7 +431,7 @@ iterator memSlices*(mfile: MemFile, delim = '\l', eat = '\r'): MemSlice {.inline
iterator lines*(mfile: MemFile, buf: var string, delim = '\l', iterator lines*(mfile: MemFile, buf: var string, delim = '\l',
eat = '\r'): string {.inline.} = eat = '\r'): string {.inline.} =
## Replace contents of passed buffer with each new line, like ## Replace contents of passed buffer with each new line, like
## `readLine(File) <io.html#readLine,File,string>`_. ## `readLine(File) <syncio.html#readLine,File,string>`_.
## `delim`, `eat`, and delimiting logic is exactly as for `memSlices ## `delim`, `eat`, and delimiting logic is exactly as for `memSlices
## <#memSlices.i,MemFile,char,char>`_, but Nim strings are returned. ## <#memSlices.i,MemFile,char,char>`_, but Nim strings are returned.
## ##
@ -450,7 +450,7 @@ iterator lines*(mfile: MemFile, buf: var string, delim = '\l',
iterator lines*(mfile: MemFile, delim = '\l', eat = '\r'): string {.inline.} = iterator lines*(mfile: MemFile, delim = '\l', eat = '\r'): string {.inline.} =
## Return each line in a file as a Nim string, like ## Return each line in a file as a Nim string, like
## `lines(File) <io.html#lines.i,File>`_. ## `lines(File) <syncio.html#lines.i,File>`_.
## `delim`, `eat`, and delimiting logic is exactly as for `memSlices ## `delim`, `eat`, and delimiting logic is exactly as for `memSlices
## <#memSlices.i,MemFile,char,char>`_, but Nim strings are returned. ## <#memSlices.i,MemFile,char,char>`_, but Nim strings are returned.
## ##

View file

@ -34,6 +34,9 @@ import std/private/since
import strutils, pathnorm import strutils, pathnorm
when defined(nimPreviewSlimSystem):
import std/syncio
const weirdTarget = defined(nimscript) or defined(js) const weirdTarget = defined(nimscript) or defined(js)
since (1, 1): since (1, 1):

View file

@ -175,8 +175,12 @@ import strutils, lexbase, streams, tables
import std/private/decode_helpers import std/private/decode_helpers
import std/private/since import std/private/since
when defined(nimPreviewSlimSystem):
import std/syncio
include "system/inclrtl" include "system/inclrtl"
type type
CfgEventKind* = enum ## enumeration of all events that may occur when parsing CfgEventKind* = enum ## enumeration of all events that may occur when parsing
cfgEof, ## end of file reached cfgEof, ## end of file reached

View file

@ -67,6 +67,9 @@
import lexbase, streams import lexbase, streams
when defined(nimPreviewSlimSystem):
import std/syncio
type type
CsvRow* = seq[string] ## A row in a CSV file. CsvRow* = seq[string] ## A row in a CSV file.
CsvParser* = object of BaseLexer ## The parser object. CsvParser* = object of BaseLexer ## The parser object.

View file

@ -16,6 +16,8 @@
## ##
include "system/inclrtl" include "system/inclrtl"
when defined(nimPreviewSlimSystem):
import std/syncio
const const
useUnicode = true ## change this to deactivate proper UTF-8 support useUnicode = true ## change this to deactivate proper UTF-8 support

View file

@ -19,6 +19,9 @@
include system/inclrtl include system/inclrtl
import streams import streams
when defined(nimPreviewSlimSystem):
import std/syncio
{.push debugger: off.} # the user does not want to trace a part {.push debugger: off.} # the user does not want to trace a part
# of the standard library! # of the standard library!

View file

@ -92,10 +92,13 @@
## See also ## See also
## ======== ## ========
## * `asyncstreams module <asyncstreams.html>`_ ## * `asyncstreams module <asyncstreams.html>`_
## * `io module <io.html>`_ for `FileMode enum <io.html#FileMode>`_ ## * `io module <syncio.html>`_ for `FileMode enum <syncio.html#FileMode>`_
import std/private/since import std/private/since
when defined(nimPreviewSlimSystem):
import std/syncio
proc newEIO(msg: string): owned(ref IOError) = proc newEIO(msg: string): owned(ref IOError) =
new(result) new(result)
result.msg = msg result.msg = msg
@ -1331,7 +1334,7 @@ proc newFileStream*(f: File): owned FileStream =
## * `newStringStream proc <#newStringStream,string>`_ creates a new stream ## * `newStringStream proc <#newStringStream,string>`_ creates a new stream
## from string. ## from string.
## * `newFileStream proc <#newFileStream,string,FileMode,int>`_ is the same ## * `newFileStream proc <#newFileStream,string,FileMode,int>`_ is the same
## as using `open proc <io.html#open,File,string,FileMode,int>`_ ## as using `open proc <syncio.html#open,File,string,FileMode,int>`_
## on Examples. ## on Examples.
## * `openFileStream proc <#openFileStream,string,FileMode,int>`_ creates a ## * `openFileStream proc <#openFileStream,string,FileMode,int>`_ creates a
## file stream from the file name and the mode. ## file stream from the file name and the mode.
@ -1370,7 +1373,7 @@ proc newFileStream*(filename: string, mode: FileMode = fmRead,
## Creates a new stream from the file named `filename` with the mode `mode`. ## Creates a new stream from the file named `filename` with the mode `mode`.
## ##
## If the file cannot be opened, `nil` is returned. See the `io module ## If the file cannot be opened, `nil` is returned. See the `io module
## <io.html>`_ for a list of available `FileMode enums <io.html#FileMode>`_. ## <syncio.html>`_ for a list of available `FileMode enums <syncio.html#FileMode>`_.
## ##
## **Note:** ## **Note:**
## * **This function returns nil in case of failure.** ## * **This function returns nil in case of failure.**

View file

@ -66,6 +66,9 @@ import colors
when defined(windows): when defined(windows):
import winlean import winlean
when defined(nimPreviewSlimSystem):
import std/syncio
type type
PTerminal = ref object PTerminal = ref object
trueColorIsSupported: bool trueColorIsSupported: bool

View file

@ -11,6 +11,9 @@
import streams, parsexml, strtabs, xmltree import streams, parsexml, strtabs, xmltree
when defined(nimPreviewSlimSystem):
import std/syncio
type type
XmlError* = object of ValueError ## Exception that is raised XmlError* = object of ValueError ## Exception that is raised
## for invalid XML. ## for invalid XML.

View file

@ -1,18 +1,17 @@
# #
# #
# Nim's Runtime Library # Nim's Runtime Library
# (c) Copyright 2019 Nim contributors # (c) Copyright 2022 Nim contributors
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
# distribution, for details about the copyright. # distribution, for details about the copyright.
# #
## This is a part of `system.nim`, you should not manually import it. ## This module implements various synchronized I/O operations.
include system/inclrtl
include inclrtl
import std/private/since import std/private/since
import formatfloat import system/formatfloat
# ----------------- IO Part ------------------------------------------------ # ----------------- IO Part ------------------------------------------------
type type
@ -179,7 +178,7 @@ proc checkErr(f: File) =
{.push stackTrace: off, profiler: off.} {.push stackTrace: off, profiler: off.}
proc readBuffer*(f: File, buffer: pointer, len: Natural): int {. proc readBuffer*(f: File, buffer: pointer, len: Natural): int {.
tags: [ReadIOEffect], benign.} = tags: [ReadIOEffect], benign.} =
## reads `len` bytes into the buffer pointed to by `buffer`. Returns ## Reads `len` bytes into the buffer pointed to by `buffer`. Returns
## the actual number of bytes that have been read which may be less than ## the actual number of bytes that have been read which may be less than
## `len` (if not as many bytes are remaining), but not greater. ## `len` (if not as many bytes are remaining), but not greater.
result = cast[int](c_fread(buffer, 1, cast[csize_t](len), f)) result = cast[int](c_fread(buffer, 1, cast[csize_t](len), f))
@ -188,13 +187,13 @@ proc readBuffer*(f: File, buffer: pointer, len: Natural): int {.
proc readBytes*(f: File, a: var openArray[int8|uint8], start, proc readBytes*(f: File, a: var openArray[int8|uint8], start,
len: Natural): int {. len: Natural): int {.
tags: [ReadIOEffect], benign.} = tags: [ReadIOEffect], benign.} =
## reads `len` bytes into the buffer `a` starting at `a[start]`. Returns ## Reads `len` bytes into the buffer `a` starting at `a[start]`. Returns
## the actual number of bytes that have been read which may be less than ## the actual number of bytes that have been read which may be less than
## `len` (if not as many bytes are remaining), but not greater. ## `len` (if not as many bytes are remaining), but not greater.
result = readBuffer(f, addr(a[start]), len) result = readBuffer(f, addr(a[start]), len)
proc readChars*(f: File, a: var openArray[char]): int {.tags: [ReadIOEffect], benign.} = proc readChars*(f: File, a: var openArray[char]): int {.tags: [ReadIOEffect], benign.} =
## reads up to `a.len` bytes into the buffer `a`. Returns ## Reads up to `a.len` bytes into the buffer `a`. Returns
## the actual number of bytes that have been read which may be less than ## the actual number of bytes that have been read which may be less than
## `a.len` (if not as many bytes are remaining), but not greater. ## `a.len` (if not as many bytes are remaining), but not greater.
result = readBuffer(f, addr(a[0]), a.len) result = readBuffer(f, addr(a[0]), a.len)
@ -202,7 +201,7 @@ proc readChars*(f: File, a: var openArray[char]): int {.tags: [ReadIOEffect], be
proc readChars*(f: File, a: var openArray[char], start, len: Natural): int {. proc readChars*(f: File, a: var openArray[char], start, len: Natural): int {.
tags: [ReadIOEffect], benign, deprecated: tags: [ReadIOEffect], benign, deprecated:
"use other `readChars` overload, possibly via: readChars(toOpenArray(buf, start, len-1))".} = "use other `readChars` overload, possibly via: readChars(toOpenArray(buf, start, len-1))".} =
## reads `len` bytes into the buffer `a` starting at `a[start]`. Returns ## Reads `len` bytes into the buffer `a` starting at `a[start]`. Returns
## the actual number of bytes that have been read which may be less than ## the actual number of bytes that have been read which may be less than
## `len` (if not as many bytes are remaining), but not greater. ## `len` (if not as many bytes are remaining), but not greater.
if (start + len) > len(a): if (start + len) > len(a):
@ -216,7 +215,7 @@ proc write*(f: File, c: cstring) {.tags: [WriteIOEffect], benign.} =
proc writeBuffer*(f: File, buffer: pointer, len: Natural): int {. proc writeBuffer*(f: File, buffer: pointer, len: Natural): int {.
tags: [WriteIOEffect], benign.} = tags: [WriteIOEffect], benign.} =
## writes the bytes of buffer pointed to by the parameter `buffer` to the ## Writes the bytes of buffer pointed to by the parameter `buffer` to the
## file `f`. Returns the number of actual written bytes, which may be less ## file `f`. Returns the number of actual written bytes, which may be less
## than `len` in case of an error. ## than `len` in case of an error.
result = cast[int](c_fwrite(buffer, 1, cast[csize_t](len), f)) result = cast[int](c_fwrite(buffer, 1, cast[csize_t](len), f))
@ -224,7 +223,7 @@ proc writeBuffer*(f: File, buffer: pointer, len: Natural): int {.
proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: Natural): int {. proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: Natural): int {.
tags: [WriteIOEffect], benign.} = tags: [WriteIOEffect], benign.} =
## writes the bytes of `a[start..start+len-1]` to the file `f`. Returns ## Writes the bytes of `a[start..start+len-1]` to the file `f`. Returns
## the number of actual written bytes, which may be less than `len` in case ## the number of actual written bytes, which may be less than `len` in case
## of an error. ## of an error.
var x = cast[ptr UncheckedArray[int8]](a) var x = cast[ptr UncheckedArray[int8]](a)
@ -232,7 +231,7 @@ proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: Natural): int {.
proc writeChars*(f: File, a: openArray[char], start, len: Natural): int {. proc writeChars*(f: File, a: openArray[char], start, len: Natural): int {.
tags: [WriteIOEffect], benign.} = tags: [WriteIOEffect], benign.} =
## writes the bytes of `a[start..start+len-1]` to the file `f`. Returns ## Writes the bytes of `a[start..start+len-1]` to the file `f`. Returns
## the number of actual written bytes, which may be less than `len` in case ## the number of actual written bytes, which may be less than `len` in case
## of an error. ## of an error.
var x = cast[ptr UncheckedArray[int8]](a) var x = cast[ptr UncheckedArray[int8]](a)
@ -341,7 +340,7 @@ proc flushFile*(f: File) {.tags: [WriteIOEffect].} =
discard c_fflush(f) discard c_fflush(f)
proc getFileHandle*(f: File): FileHandle = proc getFileHandle*(f: File): FileHandle =
## returns the file handle of the file `f`. This is only useful for ## Returns the file handle of the file `f`. This is only useful for
## platform specific programming. ## platform specific programming.
## Note that on Windows this doesn't return the Windows-specific handle, ## Note that on Windows this doesn't return the Windows-specific handle,
## but the C library's notion of a handle, whatever that means. ## but the C library's notion of a handle, whatever that means.
@ -349,7 +348,7 @@ proc getFileHandle*(f: File): FileHandle =
c_fileno(f) c_fileno(f)
proc getOsFileHandle*(f: File): FileHandle = proc getOsFileHandle*(f: File): FileHandle =
## returns the OS file handle of the file `f`. This is only useful for ## Returns the OS file handle of the file `f`. This is only useful for
## platform specific programming. ## platform specific programming.
when defined(windows): when defined(windows):
result = FileHandle getOsfhandle(cint getFileHandle(f)) result = FileHandle getOsfhandle(cint getFileHandle(f))
@ -380,7 +379,7 @@ when defined(nimdoc) or (defined(posix) and not defined(nimscript)) or defined(w
proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect], proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect],
benign.} = benign.} =
## reads a line of text from the file `f` into `line`. May throw an IO ## Reads a line of text from the file `f` into `line`. May throw an IO
## exception. ## exception.
## A line of text may be delimited by `LF` or `CRLF`. The newline ## A line of text may be delimited by `LF` or `CRLF`. The newline
## character(s) are not part of the returned string. Returns `false` ## character(s) are not part of the returned string. Returns `false`
@ -500,7 +499,7 @@ proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect],
line.setLen(pos+sp) line.setLen(pos+sp)
proc readLine*(f: File): string {.tags: [ReadIOEffect], benign.} = proc readLine*(f: File): string {.tags: [ReadIOEffect], benign.} =
## reads a line of text from the file `f`. May throw an IO exception. ## Reads a line of text from the file `f`. May throw an IO exception.
## A line of text may be delimited by `LF` or `CRLF`. The newline ## A line of text may be delimited by `LF` or `CRLF`. The newline
## character(s) are not part of the returned string. ## character(s) are not part of the returned string.
result = newStringOfCap(80) result = newStringOfCap(80)
@ -602,7 +601,7 @@ proc readAll*(file: File): string {.tags: [ReadIOEffect], benign.} =
proc writeLine*[Ty](f: File, x: varargs[Ty, `$`]) {.inline, proc writeLine*[Ty](f: File, x: varargs[Ty, `$`]) {.inline,
tags: [WriteIOEffect], benign.} = tags: [WriteIOEffect], benign.} =
## writes the values `x` to `f` and then writes "\\n". ## Writes the values `x` to `f` and then writes "\\n".
## May throw an IO exception. ## May throw an IO exception.
for i in items(x): for i in items(x):
write(f, i) write(f, i)
@ -723,7 +722,7 @@ proc open*(f: var File, filename: string,
proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {. proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {.
tags: [], benign.} = tags: [], benign.} =
## reopens the file `f` with given `filename` and `mode`. This ## Reopens the file `f` with given `filename` and `mode`. This
## is often used to redirect the `stdin`, `stdout` or `stderr` ## is often used to redirect the `stdin`, `stdout` or `stderr`
## file variables. ## file variables.
## ##
@ -765,19 +764,19 @@ proc open*(filename: string,
sysFatal(IOError, "cannot open: " & filename) sysFatal(IOError, "cannot open: " & filename)
proc setFilePos*(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) {.benign.} = proc setFilePos*(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) {.benign.} =
## sets the position of the file pointer that is used for read/write ## Sets the position of the file pointer that is used for read/write
## operations. The file's first byte has the index zero. ## operations. The file's first byte has the index zero.
if c_fseek(f, pos, cint(relativeTo)) != 0: if c_fseek(f, pos, cint(relativeTo)) != 0:
raiseEIO("cannot set file position") raiseEIO("cannot set file position")
proc getFilePos*(f: File): int64 {.benign.} = proc getFilePos*(f: File): int64 {.benign.} =
## retrieves the current position of the file pointer that is used to ## Retrieves the current position of the file pointer that is used to
## read from the file `f`. The file's first byte has the index zero. ## read from the file `f`. The file's first byte has the index zero.
result = c_ftell(f) result = c_ftell(f)
if result < 0: raiseEIO("cannot retrieve file position") if result < 0: raiseEIO("cannot retrieve file position")
proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], benign.} = proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], benign.} =
## retrieves the file size (in bytes) of `f`. ## Retrieves the file size (in bytes) of `f`.
let oldPos = getFilePos(f) let oldPos = getFilePos(f)
discard c_fseek(f, 0, 2) # seek the end of the file discard c_fseek(f, 0, 2) # seek the end of the file
result = getFilePos(f) result = getFilePos(f)
@ -906,7 +905,7 @@ proc writeFile*(filename: string, content: openArray[byte]) {.since: (1, 1).} =
raise newException(IOError, "cannot open: " & filename) raise newException(IOError, "cannot open: " & filename)
proc readLines*(filename: string, n: Natural): seq[string] = proc readLines*(filename: string, n: Natural): seq[string] =
## read `n` lines from the file named `filename`. Raises an IO exception ## Reads `n` lines from the file named `filename`. Raises an IO exception
## in case of an error. Raises EOF if file does not contain at least `n` lines. ## in case of an error. Raises EOF if file does not contain at least `n` lines.
## Available at compile time. A line of text may be delimited by `LF` or `CRLF`. ## Available at compile time. A line of text may be delimited by `LF` or `CRLF`.
## The newline character(s) are not part of the returned strings. ## The newline character(s) are not part of the returned strings.
@ -948,7 +947,7 @@ iterator lines*(filename: string): string {.tags: [ReadIOEffect].} =
close(f) close(f)
iterator lines*(f: File): string {.tags: [ReadIOEffect].} = iterator lines*(f: File): string {.tags: [ReadIOEffect].} =
## Iterate over any line in the file `f`. ## Iterates over any line in the file `f`.
## ##
## The trailing newline character(s) are removed from the iterated lines. ## The trailing newline character(s) are removed from the iterated lines.
## ##

View file

@ -2680,7 +2680,7 @@ proc slurp*(filename: string): string {.magic: "Slurp".}
## This is an alias for `staticRead <#staticRead,string>`_. ## This is an alias for `staticRead <#staticRead,string>`_.
proc staticRead*(filename: string): string {.magic: "Slurp".} proc staticRead*(filename: string): string {.magic: "Slurp".}
## Compile-time `readFile <io.html#readFile,string>`_ proc for easy ## Compile-time `readFile <syncio.html#readFile,string>`_ proc for easy
## `resource`:idx: embedding: ## `resource`:idx: embedding:
## ##
## The maximum file size limit that `staticRead` and `slurp` can read is ## The maximum file size limit that `staticRead` and `slurp` can read is
@ -3150,8 +3150,11 @@ when defined(genode):
import system/widestrs import system/widestrs
export widestrs export widestrs
import system/io when not defined(nimPreviewSlimSystem):
export io {.deprecated: """io is about to move out of system; use `-d:nimPreviewSlimSystem` and
import `std/syncio`.""".}
import std/syncio
export syncio
when not defined(createNimHcr) and not defined(nimscript): when not defined(createNimHcr) and not defined(nimscript):
include nimhcr include nimhcr

View file

@ -7,7 +7,6 @@ is in separate files:
* `iterators <iterators.html>`_ * `iterators <iterators.html>`_
* `assertions <assertions.html>`_ * `assertions <assertions.html>`_
* `dollars <dollars.html>`_ * `dollars <dollars.html>`_
* `io <io.html>`_
* `widestrs <widestrs.html>`_ * `widestrs <widestrs.html>`_

View file

@ -17,6 +17,9 @@ when defined(nimHasStyleChecks):
{.passc: "-DWIN32_LEAN_AND_MEAN".} {.passc: "-DWIN32_LEAN_AND_MEAN".}
when defined(nimPreviewSlimSystem):
from std/syncio import FileHandle
const const
useWinUnicode* = not defined(useWinAnsi) useWinUnicode* = not defined(useWinAnsi)

View file

@ -5,6 +5,9 @@ discard """
import sequtils, strutils import sequtils, strutils
when defined(nimPreviewSlimSystem):
import std/syncio
let rules = toSeq(lines("input")) let rules = toSeq(lines("input"))
.mapIt(it.split(" => ").mapIt(it.replace("/", ""))) .mapIt(it.split(" => ").mapIt(it.replace("/", "")))

View file

@ -5,6 +5,9 @@
from stdtest/specialpaths import buildDir from stdtest/specialpaths import buildDir
when defined(nimSlimSystem):
import std/syncio
import std/[ import std/[
# Core: # Core:
bitops, typetraits, lenientops, macros, volatile, bitops, typetraits, lenientops, macros, volatile,

View file

@ -187,7 +187,6 @@ proc getDocList(): seq[string] =
# don't ignore these even though in lib/system (not include files) # don't ignore these even though in lib/system (not include files)
const goodSystem = """ const goodSystem = """
lib/system/io.nim
lib/system/nimscript.nim lib/system/nimscript.nim
lib/system/assertions.nim lib/system/assertions.nim
lib/system/iterators.nim lib/system/iterators.nim