Code Monkey home page Code Monkey logo

fileio.swift's Introduction

FileIO

FileIO is a small framework designed that abstracts POSIX file IO into Swift-like idioms and classes.

Dependencies

FileIO makes use of Using.swift to provide a pattern for appropriately opening and closing files. It should be downloaded automatically when you attempt to compile any of the projects in the workspace. You may need to build a second time if the first time fails.

Usage

Basic file information can be ascertained by opening creating a File object by providing a path.

let file = File(path: "/path/to/file")
if file.exists {
	print(file.size)
}

Operations that read or modify a file currently follow the NSFileHandle pattern of providing different objects for reading, writing, or updating a file. They should be wrapped in a using statement to ensure that they are closed appropriately.

using (file.openForReading()) { reader in
	let oneByte: UInt8 = reader.read()
}

You can read and write data structures simply:

struct Foo { let bar: Int; let baz: Int }
let writableFoo = Foo(bar: 1, baz: 2)
using (file.openForWriting()) { writer in
	writer.write(writableFoo)
}

using (file.openForReading()) { reader in
	let readFoo: Foo = reader.read()
	print(readFoo)
}

Note that many data structures, such as strings, arrays, and many other types do not store their data inline. Instead, they have a pointer to the data. This is often a concern. Fortunately, FileIO abstracts reading and writing arrays.

let lotsOfBytes = Array<UInt8>(count: 256, repeatedValue: 0x00)
using (file.openForWriting()) { $0.write(lotsOfBytes) }
using (file.openForReading()) { let twoHundredAndFiftySixBytes: [UInt8] = $0.read(256) }

Future

There are three primary issues that I'd like to address as this project matures.

  1. Error handling -- Right now, there is no error handling. I'd like to update this project to throw exceptions that vaguely mirrored data provided by errno.
  2. Asynchronicity -- All file I/O is handled synchronously. Many modern I/O idioms provide synchronous and asynchronous file access. For files, these will likely be tossed into a backgrounded serial queue.
  3. Generic stream writing/reading -- In Unix, files are just one form of data streams. I'd like to extend this to read and write from stdin/stdout/stderr as well as define a generic protocol for handling networked stream interfaces as well.

Contact

For complaints, compliments, or discussion, I can be reached at:

Grayson Hansard
@Grayson
[email protected]
Github/Grayson

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.