[swift-users] Pure Swift, cross platform way to open, read/write files?

Perry E. Metzger perry at piermont.com
Wed Dec 9 13:09:34 CST 2015


On Wed, 09 Dec 2015 10:25:11 -0800 Max Howell <max.howell at apple.com>
wrote:
> I feel I must point the community at:
> 
> https://github.com/apple/swift-package-manager/tree/master/Sources/POSIX
> 
> Which is mostly what I think you are calling the lower layer.

It looks like a start. Some of it (say the unlink routine) is what you
want, but some seems to be too high level, i.e. bindings against
library calls rather than system calls (i.e. things calling in to C
at the fopen/fputs layer rather than the open/write).

One would really like to re-implement the stdio stuff rather than
simply rebinding the C implementation. The implementation of much of
the stuff isn't very deep, and avoiding the potential safety issues
in the C library would be nice. (stdio only looks scary -- it is
mostly just a buffering layer above read(2)/write(2) plus some
formatting.)

Perry

> 
> >>> A lower layer is also needed -- simple Swifty wrappers for the
> >>> Unix system calls so that Swift implementations can get at them
> >>> without having to use unsafe constructs. open(2), read(2),
> >>> write(2), lseek(2) and close(2) would provide about 75% of what
> >>> one needs for the higher layer stuff (and everything one would
> >>> want for stdio) with quite minimal hacking.
> >> 
> >> I agree, but I foresee problems in the future when Swift is
> >> ported to Windows. :/
> > 
> > I'm a big believer in not prematurely generalizing an
> > implementation. APIs last a long time, implementations can be
> > redone later. :)
> 



-- 
Perry E. Metzger		perry at piermont.com


More information about the swift-users mailing list