]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
windows: Restructure if/else blocks with unnecessary indentation.
[libserialport.git] / serialport.c
index e624f717858ee5cf88755ea50cb1dae4d78760f9..8d71865bad14ce5dc55d36f44d83570fb08c8d8a 100644 (file)
@@ -758,18 +758,16 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
        }
 
        /* Start write. */
-       if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl) == 0) {
-               if (GetLastError() == ERROR_IO_PENDING) {
-                       DEBUG("Waiting for write to complete");
-                       GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
-                       DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
-                       RETURN_INT(bytes_written);
-               } else {
-                       RETURN_FAIL("WriteFile() failed");
-               }
-       } else {
+       if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl)) {
                DEBUG("Write completed immediately");
                RETURN_INT(count);
+       } else if (GetLastError() == ERROR_IO_PENDING) {
+               DEBUG("Waiting for write to complete");
+               GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
+               DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
+               RETURN_INT(bytes_written);
+       } else {
+               RETURN_FAIL("WriteFile() failed");
        }
 #else
        size_t bytes_written = 0;
@@ -917,6 +915,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)
+{
+       DWORD errors;
+       COMSTAT comstat;
+
+       if (bytes_read == 0)
+               RETURN_OK();
+
+       if (ClearCommError(port->hdl, &errors, &comstat) == 0)
+               RETURN_FAIL("ClearCommError() failed");
+
+       if (comstat.cbInQue == 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 +957,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 ||
@@ -952,29 +968,18 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
        }
 
        /* Start read. */
-       if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0) {
-               if (GetLastError() == ERROR_IO_PENDING) {
-                       DEBUG("Waiting for read to complete");
-                       GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
-                       DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count);
-               } else {
-                       RETURN_FAIL("ReadFile() failed");
-               }
-       } else {
+       if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl)) {
                DEBUG("Read completed immediately");
                bytes_read = count;
+       } else if (GetLastError() == ERROR_IO_PENDING) {
+               DEBUG("Waiting for read to complete");
+               GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
+               DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count);
+       } else {
+               RETURN_FAIL("ReadFile() 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);
 
@@ -1054,8 +1059,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 +1077,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