GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_seek.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3
4 Copyright (C) 2020-2024 Cyril Hrubis <metan@ucw.cz>
5
6 */
7
13#ifndef GP_SEEK_H
14#define GP_SEEK_H
15
16#include <sys/types.h>
17#include <stddef.h>
18
25 GP_SEEK_SET = 0,
26 GP_SEEK_CUR = 1,
27 GP_SEEK_END = 2,
28};
29
41static inline int gp_seek_off(ssize_t off, enum gp_seek_whence whence, size_t *cur_pos, size_t max_pos)
42{
43 switch (whence) {
44 case GP_SEEK_SET:
45 if (off < 0)
46 return -1;
47
48 if ((size_t)off > max_pos)
49 return 1;
50
51 *cur_pos = off;
52
53 return 0;
54 break;
55 case GP_SEEK_CUR:
56 if (off < 0) {
57 if (*cur_pos < (size_t)-off)
58 return -1;
59 } else {
60 if (*cur_pos + (size_t)off > max_pos)
61 return 1;
62 }
63
64 *cur_pos += off;
65
66 return 0;
67 break;
68 case GP_SEEK_END:
69 if (off > 0)
70 return 1;
71
72 if ((size_t)-off > max_pos)
73 return -1;
74
75 *cur_pos = max_pos + off;
76
77 return 0;
78 break;
79 }
80
81 return -1;
82}
83
84#endif /* GP_SEEK_H */
gp_seek_whence
Seek constants.
Definition gp_seek.h:24
static int gp_seek_off(ssize_t off, enum gp_seek_whence whence, size_t *cur_pos, size_t max_pos)
Computes position for a seek like parameters.
Definition gp_seek.h:41