apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i...@apache.org
Subject svn commit: r1806301 - /apr/apr/trunk/file_io/win32/readwrite.c
Date Sat, 26 Aug 2017 13:57:26 GMT
Author: ivan
Date: Sat Aug 26 13:57:26 2017
New Revision: 1806301

URL: http://svn.apache.org/viewvc?rev=1806301&view=rev
Log:
Factor out a helper function that contains the implementation of writing
to buffered files.

* file_io/win32/readwrite.c
  (write_buffered): New helper function, factored out from ...
  (apr_file_write): ...here.

Patch by: Evgeny Kotkov <evgeny.kotkov {at} visualsvn.com>

Modified:
    apr/apr/trunk/file_io/win32/readwrite.c

Modified: apr/apr/trunk/file_io/win32/readwrite.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/file_io/win32/readwrite.c?rev=1806301&r1=1806300&r2=1806301&view=diff
==============================================================================
--- apr/apr/trunk/file_io/win32/readwrite.c (original)
+++ apr/apr/trunk/file_io/win32/readwrite.c Sat Aug 26 13:57:26 2017
@@ -289,6 +289,39 @@ static apr_status_t write_helper(HANDLE
     return APR_SUCCESS;
 }
 
+static apr_status_t write_buffered(apr_file_t *thefile, const char *buf,
+                                   apr_size_t len)
+{
+    apr_size_t blocksize;
+    apr_status_t rv;
+
+    if (thefile->direction == 0) {
+        /* Position file pointer for writing at the offset we are logically reading from
*/
+        apr_off_t offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
+        DWORD offlo = (DWORD)offset;
+        LONG offhi = (LONG)(offset >> 32);
+        if (offset != thefile->filePtr)
+            SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
+        thefile->bufpos = thefile->dataRead = 0;
+        thefile->direction = 1;
+    }
+
+    rv = 0;
+    while (rv == 0 && len > 0) {
+        if (thefile->bufpos == thefile->bufsize)   /* write buffer is full */
+            rv = apr_file_flush(thefile);
+
+        blocksize = len > thefile->bufsize - thefile->bufpos ?
+                    thefile->bufsize - thefile->bufpos : len;
+        memcpy(thefile->buffer + thefile->bufpos, buf, blocksize);
+        thefile->bufpos += blocksize;
+        buf += blocksize;
+        len -= blocksize;
+    }
+
+    return rv;
+}
+
 APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf, apr_size_t
*nbytes)
 {
     apr_status_t rv;
@@ -309,38 +342,10 @@ APR_DECLARE(apr_status_t) apr_file_write
     }
 
     if (thefile->buffered) {
-        char *pos = (char *)buf;
-        apr_size_t blocksize;
-        apr_size_t size = *nbytes;
-
         if (thefile->flags & APR_FOPEN_XTHREAD) {
             apr_thread_mutex_lock(thefile->mutex);
         }
-
-        if (thefile->direction == 0) {
-            /* Position file pointer for writing at the offset we are logically reading from
*/
-            apr_off_t offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
-            DWORD offlo = (DWORD)offset;
-            LONG  offhi = (LONG)(offset >> 32);
-            if (offset != thefile->filePtr)
-                SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
-            thefile->bufpos = thefile->dataRead = 0;
-            thefile->direction = 1;
-        }
-
-        rv = 0;
-        while (rv == 0 && size > 0) {
-            if (thefile->bufpos == thefile->bufsize)   /* write buffer is full */
-                rv = apr_file_flush(thefile);
-
-            blocksize = size > thefile->bufsize - thefile->bufpos ? 
-                                     thefile->bufsize - thefile->bufpos : size;
-            memcpy(thefile->buffer + thefile->bufpos, pos, blocksize);
-            thefile->bufpos += blocksize;
-            pos += blocksize;
-            size -= blocksize;
-        }
-
+        rv = write_buffered(thefile, buf, *nbytes, nbytes);
         if (thefile->flags & APR_FOPEN_XTHREAD) {
             apr_thread_mutex_unlock(thefile->mutex);
         }



Mime
View raw message