diff options
author | bptato <nincsnevem662@gmail.com> | 2022-12-31 14:05:41 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-12-31 14:05:41 +0100 |
commit | 7b6a2c6d4d1d055953d1e72ca6096490f368f922 (patch) | |
tree | b62a763e349df0e77b2ed508bcc50b4c08036518 /src/io | |
parent | e9cc4edbb3695651b0abd7e33e40d942ad98ab2a (diff) | |
download | chawan-7b6a2c6d4d1d055953d1e72ca6096490f368f922.tar.gz |
posixstream/socketstream: fix cross-platform compilation
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/posixstream.nim | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/io/posixstream.nim b/src/io/posixstream.nim index b686be9b..8378c194 100644 --- a/src/io/posixstream.nim +++ b/src/io/posixstream.nim @@ -14,6 +14,22 @@ type ErrorInterrupted* = object of IOError ErrorInvalid* = object of IOError +proc raisePosixIOError*() = + # In the nim stdlib, these are only constants on linux amd64, so we + # can't use a switch. + if errno == EAGAIN: + raise newException(ErrorAgain, "eagain") + elif errno == EWOULDBLOCK: + raise newException(ErrorWouldBlock, "would block") + elif errno == EBADF: + raise newException(ErrorBadFD, "bad fd") + elif errno == EFAULT: + raise newException(ErrorFault, "fault") + elif errno == EINVAL: + raise newException(ErrorInvalid, "invalid") + else: + raise newException(IOError, $strerror(errno)) + proc psReadData(s: Stream, buffer: pointer, len: int): int = assert len != 0 let s = cast[PosixStream](s) @@ -30,14 +46,7 @@ proc psReadData(s: Stream, buffer: pointer, len: int): int = if result == 0: raise newException(EOFError, "eof") if result == -1: - if errno == EAGAIN: - raise newException(ErrorAgain, "eagain") - case errno - of EWOULDBLOCK: raise newException(ErrorWouldBlock, "would block") - of EBADF: raise newException(ErrorBadFD, "bad fd") - of EFAULT: raise newException(ErrorFault, "fault") - of EINVAL: raise newException(ErrorInvalid, "invalid") - else: raise newException(IOError, $strerror(errno) & " (" & $errno & ")") + raisePosixIOError() proc psWriteData(s: Stream, buffer: pointer, len: int) = let s = cast[PosixStream](s) |