-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.c
216 lines (175 loc) · 5.49 KB
/
utils.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* -*- c -*-
* Copyright (C) 1999 Gus Hartmann & Peter Keller
* Copyright (C) 2024 Ron Wills <ron@digitalcombine.ca>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "sweep.h"
#include <unistd.h>
/******************************************************************************
* Color conversion code borrowed and modified from the tmux project.
*
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
* Copyright (c) 2016 Avi Halachmi <avihpit@yahoo.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
static int color_dist(int R, int G, int B, int r, int g, int b) {
return ((R - r) * (R - r) + (G - g) * (G - g) + (B - b) * (B - b));
}
static int color_6_cube(int value) {
if (value < 48) return 0;
if (value < 114) return 1;
return ((value - 35) / 40);
}
static int color_rgb(int R, int G, int B) {
/* Convert an RGB triplet to the xterm(1) 256 colour palette.
*
* xterm provides a 6x6x6 colour cube (16 - 231) and 24 greys (232 - 255). We
* map our RGB colour to the closest in the cube, also work out the closest
* grey, and use the nearest of the two.
*
* Note that the xterm has much lower resolution for darker colours (they are
* not evenly spread out), so our 6 levels are not evenly spread: 0x0, 0x5f
* (95), 0x87 (135), 0xaf (175), 0xd7 (215) and 0xff (255). Greys are more
* evenly spread (8, 18, 28 ... 238).
*/
static const int q2c[6] = {0x00,0x5f,0x87,0xaf,0xd7,0xff};
int qr, qg, qb, cr, cg, cb, d, idx;
int grey_avg, grey_idx, grey;
/* Map RGB to 6x6x6 cube color space. */
qr = color_6_cube(R); cr = q2c[qr];
qb = color_6_cube(B); cb = q2c[qb];
qg = color_6_cube(G); cg = q2c[qg];
/* If we hit the color exactly return early. */
if (cr == R && cb == B && cg == G)
return ((16 + (36 * qr) + (16 * qg) + qb));
/* Work out the closest grey (average of RGB). */
grey_avg = (R + G + B) / 3;
if (grey_avg > 238)
grey_idx = 23;
else
grey_idx = (grey_avg - 3) / 10;
grey = 8 + (10 * grey_idx);
/* Is grey or 6x6x6 colour closest? */
d = color_dist(cr, cg, cb, R, G, B);
if (color_dist(grey, grey, grey, R, G, B) < d)
idx = 232 + grey_idx;
else
idx = 16 + (36 * qr) + (6 * qg) + qb;
return idx;
}
/*****************************************************************************/
/**********
* xcolor *
**********/
int xcolor(int r, int g, int b, int fallback) {
return (COLORS >= 256 ? color_rgb(r,g,b) : fallback);
}
/***********
* xwaddch *
***********/
int xwaddch(WINDOW* win, DrawChars *CharSet, ch_type ch) {
switch (CharSet->ch_flag) {
case CH_TYPE:
return waddch(win, ch.ch);
case UNI_TYPE:
return waddstr(win, ch.str);
}
return ERR;
}
/***********
* xmalloc *
***********/
void* xmalloc(size_t num) {
void *vec = NULL;
vec = (void*)malloc(sizeof(unsigned char) * num);
if (vec == NULL) {
log_error("Out of Memory.");
game_exit(EXIT_FAILURE);
}
return vec;
}
/**********
* strdup *
**********/
#ifndef HAVE_STRDUP
char *strdup(char *s) {
char *c = (char*)xmalloc(strlen(s) + 1);
strncpy(c, s, strlen(s) + 1);
return(c);
}
#endif
/***********
* xgetcwd *
***********/
char* xgetcwd(char *buf, size_t size) {
char *path = getcwd(buf, size);
if (path == NULL) {
log_error("Could not get current working directory.");
game_exit(EXIT_FAILURE);
}
return path;
}
/************
* xopendir *
************/
DIR* xopendir(const char *path) {
DIR *dirent = opendir(path);
if (dirent == NULL) {
return NULL;
}
return dirent;
}
/***********
* xexists *
***********/
int xexists(const char *path) {
return (access(path, F_OK) != -1);
}
/**********
* xtrunc *
**********/
const char *xtrunc(int length, const char *line) {
static char result[MAX_LINE];
memset(result, 0, MAX_LINE);
strncpy(result, line, xmin(length, MAX_LINE - 1));
return result;
}
/***************
* timer_start *
***************/
void timer_start() {
/* start the timer */
signal(SIGALRM, sighandler);
alarm(1);
}
/**************
* timer_stop *
**************/
void timer_stop() {
/* stop the timer */
signal(SIGALRM, SIG_IGN);
}