From: Martin Ling Date: Wed, 6 May 2015 19:11:17 +0000 (+0100) Subject: windows: Refactor duplicated code into a static function. X-Git-Tag: libserialport-0.1.1~36 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=94a314a806f69343ca7280982be230bcf28d1c2c;p=libserialport.git windows: Refactor duplicated code into a static function. --- diff --git a/serialport.c b/serialport.c index e624f71..58da1e9 100644 --- a/serialport.c +++ b/serialport.c @@ -917,6 +917,26 @@ SP_API enum sp_return sp_nonblocking_write(struct sp_port *port, #endif } +#ifdef _WIN32 +/* Restart wait operation if buffer was emptied. */ +static enum sp_return restart_wait_if_needed(struct sp_port *port, unsigned int bytes_read) +{ + int ret, bytes_remaining; + + ret = sp_input_waiting(port); + + if (ret < 0) + RETURN_CODEVAL(ret); + + bytes_remaining = ret; + + if (bytes_read > 0 && bytes_remaining == 0) + TRY(restart_wait(port)); + + RETURN_OK(); +} +#endif + SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms) { @@ -939,8 +959,6 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, #ifdef _WIN32 DWORD bytes_read = 0; - DWORD bytes_remaining; - int ret; /* Set timeout. */ if (port->timeouts.ReadIntervalTimeout != 0 || @@ -965,16 +983,7 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, bytes_read = count; } - ret = sp_input_waiting(port); - - if (ret < 0) - RETURN_CODEVAL(ret); - - bytes_remaining = ret; - - /* Restart wait operation if buffer was emptied. */ - if (bytes_read > 0 && bytes_remaining == 0) - TRY(restart_wait(port)); + TRY(restart_wait_if_needed(port, bytes_read)); RETURN_INT(bytes_read); @@ -1054,8 +1063,6 @@ SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, #ifdef _WIN32 DWORD bytes_read; - DWORD bytes_remaining; - int ret; /* Set timeout. */ if (port->timeouts.ReadIntervalTimeout != MAXDWORD || @@ -1074,16 +1081,7 @@ SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0) RETURN_FAIL("GetOverlappedResult() failed"); - ret = sp_input_waiting(port); - - if (ret < 0) - RETURN_CODEVAL(ret); - - bytes_remaining = ret; - - /* Restart wait operation if buffer was emptied. */ - if (bytes_read > 0 && bytes_remaining == 0) - TRY(restart_wait(port)); + TRY(restart_wait_if_needed(port, bytes_read)); RETURN_INT(bytes_read); #else