aboutsummaryrefslogtreecommitdiff
path: root/video_player/include/Utils.hpp
blob: a5219f48d04727239a35e22d38bcb1d391523bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once

#include <unistd.h>
#include <errno.h>

static ssize_t read_eintr(int fd, void *buffer, size_t size) {
    while(true) {
        ssize_t bytes_read = read(fd, buffer, size);
        if(bytes_read == -1) {
            if(errno != EINTR)
                return -1;
        } else {
            return bytes_read;
        }
    }
}

static ssize_t write_all(int fd, const void *buffer, size_t size) {
    ssize_t bytes_written = 0;
    while((size_t)bytes_written < size) {
        ssize_t written = write(fd, (char*)buffer + bytes_written, size - bytes_written);
        if(written == -1) {
            if(errno != EINTR)
                return -1;
        } else {
            bytes_written += written;
        }
    }
    return bytes_written;
}