1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <sal/config.h>
19 #include "readwrite_helper.hxx"
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
);
35 auto nWritten
= write(fd
, dataToWrite
, cap_ssize_t(nToWrite
));
46 dataToWrite
+= nWritten
;
52 bool safeRead( int fd
, void* buffer
, std::size_t count
)
55 unsigned char* bufferForReading
= static_cast<unsigned char *>(buffer
);
58 auto nRead
= read(fd
, bufferForReading
, cap_ssize_t(nToRead
));
60 // We were interrupted before reading, retry.
67 // If we reach the EOF, we consider this a partial transfer and thus
73 bufferForReading
+= nRead
;
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */