tetsuok / swift-filestream

File stream APIs for reading from or writing to files on Linux and macOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

swift-filestream Build Status

Provides efficient file stream API for reading from or writing to files on Linux and macOS, which is currently missing in Swift's standard library. The goals of this project can be summarized as

  • Enable write command line tools or scrips easily to process text data.
  • The output stream API conform to the TextOutputStream protocol.
  • Runtime performance comparable to file I/O in Python.
  • Minimal dependency. No dependency on Foundation.
  • Easy to integrate into Xcode projects.

Non-goals

  • Building more generalized abstraction layer
  • Memory-mapped file I/O support.
  • Windows support.

Sample Usage

Reading data from files

import FileStream

let filename = "/path/to/file"
guard var input = InputFileStream(filename) else {
  print("Cannot open", filename)
  return
}
defer { input.close() }
// Read blocks of data (not line)
while let data = input.read() {
  // process data
}

You can read data until EOF:

guard var input = InputFileStream(filename) else {
  print("Cannot open", filename)
  return
}
defer { input.close() }
let data = input.readAll()
// process data

Writing textual data into files

import FileStream

let filename = "/path/to/file"
guard var output = OutputFileStream(filename) else {
  print("Cannot open", filename)
  return
}
defer { output.close() }

// Print to the opened output stream
print("hello, world!", to: &output)

About

File stream APIs for reading from or writing to files on Linux and macOS

License:Apache License 2.0


Languages

Language:Swift 75.0%Language:C 18.2%Language:Makefile 6.9%