syncio
nimony/lib/std/syncio.nim
type FileMode = enum fmRead = (0, "fmRead") fmWrite = (1, "fmWrite") fmReadWrite = (2, "fmReadWrite") fmReadWriteExisting = (3, "fmReadWriteExisting") fmAppend = (4, "fmAppend")
func dollar`.FileMode(e: FileMode): stringtype FileSeekPos = enum fspSet = (0, "fspSet") fspCur = (1, "fspCur") fspEnd = (2, "fspEnd")
func dollar`.FileSeekPos(e: FileSeekPos): stringtype FilePermission = enum fpUserExec = (0, "fpUserExec") fpUserWrite = (1, "fpUserWrite") fpUserRead = (2, "fpUserRead") fpGroupExec = (3, "fpGroupExec") fpGroupWrite = (4, "fpGroupWrite") fpGroupRead = (5, "fpGroupRead") fpOthersExec = (6, "fpOthersExec") fpOthersWrite = (7, "fpOthersWrite") fpOthersRead = (8, "fpOthersRead")
func dollar`.FilePermission(e: FilePermission): stringfunc dollar`.FileFlag(e: FileFlag): stringtype File = ref FileObjThe type representing a file handle.
proc getFileHandle(f: ref FileObj): int32The underlying OS file descriptor (libc-free posix build). Used e.g. by
std/terminal'sisatty, which has nofileno/FILE* to go through.let stdin: ref FileObjStandard input file handle (fd 0).
let stdout: ref FileObjStandard output file handle (fd 1). Fully buffered; flushed on exit via the
system/exitshook (and onquit/abort).let stderr: ref FileObjStandard error file handle (fd 2). Unbuffered.
proc open(f: var ref FileObj; fd: int32; mode: FileMode): boolWraps an already-open OS file handle in a buffered
File— the native replacement for libcfdopen. Used byosprocto turn a pipe end into a stream. Returns false for an invalid handle.proc write(f: ref FileObj; s: string)proc write(f: ref FileObj; b: bool)proc write(f: ref FileObj; x: int64)proc write(f: ref FileObj; x: int32)proc write(f: ref FileObj; x: uint64)proc write(f: ref FileObj; x: uint32)proc write(f: ref FileObj; x: T)proc write(f: ref FileObj; c: char)proc write(f: ref FileObj; x: float64)Writes data to a file (overloaded for different types).
proc close(f: ref FileObj)Closes a file handle.
proc writeBuffer(f: ref FileObj; buffer: pointer; size: int64): int64Writes raw bytes to a file.
proc readBuffer(f: ref FileObj; buffer: pointer; size: int64): int64Reads raw bytes from a file.
proc failed(f: ref FileObj): boolChecks if a file operation has failed.
proc open(f: out ref FileObj; filename: string; mode: FileMode; bufSize: int64): boolOpens a file with specified mode and buffer size (
bufSize; use-1for default buffering). Returns whether the open succeeded.proc open(filename: string; mode: FileMode; bufSize: int64): ref FileObjOpens a file named
filenamewith givenmode.Default mode is readonly. Raises an
IOErrorif the file could not be opened.template echo(vanon: varargs)Prints arguments to stdout followed by a newline.
proc writeLine(f: ref FileObj; s: string)Writes a string followed by a newline to a file.
proc addReadLine(f: ref FileObj; s: var string): boolAppends a line from a file to a string.
proc readLine(f: ref FileObj; s: var string): boolReads a line from a file into a string.
iterator lines(filename: string): stringIterates over every line in
filename. Silently returns no lines if the file cannot be opened.iterator lines(f: ref FileObj): stringIterates over every remaining line of an already opened file.
proc quit(value: int64)Terminates the program with the given exit code, flushing the standard streams first (via
system/exits).const QuitSuccess: int64const QuitFailure: int64proc quit(msg: string)proc quit(msg: string; errorcode: int64)Exits the program with a status code or message.
Use
quit(int)to exit with a code only,quit(string)to print a message and exit with failure, or this overload to print a message and use a specific code.proc readAll(f: ref FileObj): stringproc readFile(filename: string): stringOpens a file named
filename, reads its entire contents and closes the file. RaisesIOErrorif the file cannot be opened.proc writeFile(filename: string; content: string)Opens
filenamefor writing, writescontent, and closes the file. RaisesIOErrorif the file cannot be opened.proc tryWriteFile(file: string; content: string): boolAttempts to write content to a file, returns whether the operation succeeded.
proc flushFile(f: ref FileObj)Flushes file buffers to the underlying device.
proc endOfFile(f: ref FileObj): boolReturns true if
fis at the end.proc getFilePos(f: ref FileObj): int64Retrieves 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.proc setFilePos(f: ref FileObj; pos: int64; relativeTo: FileSeekPos)Sets the position of the file pointer that is used for read/write operations. The file's first byte has the index zero.
proc slurp(path: string): stringReads a file into a string. For compatibility with Nim 2.