~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 5350f89393d84636b1caad8382669556448ca0cd CTU-FEE-B0B35APO-Semestral-project/image-viewer/src/nonblocking_io.c -rw-r--r-- 1.6 KiB
5350f893 — František Boháček feat: optimize upscale to not use floats 3 years ago
                                                                                
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "nonblocking_io.h"
#include <stdint.h>
#include <asm-generic/errno-base.h>
#include <asm-generic/errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>

int file_set_blocking(int file, struct termios *old) {
  int oldfl;
  oldfl = fcntl(file, F_GETFL);
  if (oldfl == -1) {
    return oldfl;
  }

  if(tcsetattr(file, TCSANOW, old) == -1) {
    return -1;
  }

  return fcntl(file, F_SETFL, oldfl & ~O_NONBLOCK);
}

int file_set_nonblocking(int file, struct termios *old)
{
  fcntl(file, F_SETFL, O_NONBLOCK);

  struct termios attrs;
  if (tcgetattr(file, &attrs) < 0) {
    return -1;
  }

  if (old != NULL) {
    tcgetattr(file, old);
  }

  cfmakeraw(&attrs);

  tcsetattr(file, TCSANOW, &attrs);
  return 1;
}

int file_read_nonblocking(int file, size_t max_size, uint8_t *data)
{
  int read_bytes = read(file, data, max_size);

  int error = errno;
  if (read_bytes == -1 && error == EAGAIN) {
    read_bytes = 0; // Do not treat EAGAIN as an error.
  } else {
    errno = error;
  }

  return read_bytes;
}

/*bool file_write_nonblocking(int file, size_t size, uint8_t *data, int max_delay) {
  int written = 0;
  bool correct = true;

  TimeMeasure measure = tmeasure_start();

  while (written < size && !tmeasure_exceededmilli(&measure, max_delay)) {
    int status = write(file, data + written, size - written);

    if (status == -1) {
      int error = errno;

      if (error != EAGAIN) {
        errno = error;
        correct = false;
        break;
      }
    } else {
      written += status;
    }
  }

  if (correct && written < size) {
    errno = ETIMEDOUT;
  }

  return correct;
  }*/
Do not follow this link