chore(iOS): Cleanup old iOS pasteboard code
[LibreOffice.git] / sal / osl / unx / readwrite_helper.cxx
blobf28fb16cd04fd4e5ee3d1c531d389034d0c62452
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <algorithm>
13 #include <cassert>
14 #include <cstddef>
15 #include <errno.h>
16 #include <limits>
17 #include <unistd.h>
19 #include "readwrite_helper.hxx"
21 namespace {
23 std::size_t cap_ssize_t(std::size_t value) {
24 return std::min(value, std::size_t(std::numeric_limits<ssize_t>::max()));
29 bool safeWrite(int fd, void* data, std::size_t dataSize)
31 auto nToWrite = dataSize;
32 unsigned char* dataToWrite = static_cast<unsigned char *>(data);
34 while ( nToWrite ) {
35 auto nWritten = write(fd, dataToWrite, cap_ssize_t(nToWrite));
36 if ( nWritten < 0 ) {
37 if ( errno == EINTR )
38 continue;
40 return false;
44 assert(nWritten > 0);
45 nToWrite -= nWritten;
46 dataToWrite += nWritten;
49 return true;
52 bool safeRead( int fd, void* buffer, std::size_t count )
54 auto nToRead = count;
55 unsigned char* bufferForReading = static_cast<unsigned char *>(buffer);
57 while ( nToRead ) {
58 auto nRead = read(fd, bufferForReading, cap_ssize_t(nToRead));
59 if ( nRead < 0 ) {
60 // We were interrupted before reading, retry.
61 if (errno == EINTR)
62 continue;
64 return false;
67 // If we reach the EOF, we consider this a partial transfer and thus
68 // an error.
69 if ( nRead == 0 )
70 return false;
72 nToRead -= nRead;
73 bufferForReading += nRead;
76 return true;
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */