forked from iTeam-org/PortabilityCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportability.c
More file actions
262 lines (225 loc) · 6.42 KB
/
portability.c
File metadata and controls
262 lines (225 loc) · 6.42 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* ***************************************************************************
* Cette bibliothèque de fonctions est distribuée librement par l'iTeam !
*
* Son but est de fournir un moyen simple pour compiler les codes enseignés
* à l'ECE (qui ne marchent correctement que sous Windows).
* Nous vous invitons à l'utiliser et l'améliorer. La dernière version
* est disponible sur https://github.com/iTeam-Projects/PortabilityCode
*
*
* iTeam - association de Promotion du Logiciel Libre
* ***************************************************************************/
#include "portability.h"
/* Since this is the implementation of wrappers, we must disable the 'aliases'
* absolutely. Else, we will call them recursively */
#undef gotoligcol
#undef kbhit
#undef Sleep
#undef system
#undef fflush
static unsigned int _portability_color_bg = COLOR_DEFAULT;
static unsigned int _portability_color_fg = COLOR_DEFAULT;
static unsigned char _init = 0;
#ifdef ERR
# undef ERR
#endif
#define ERR(msg, ...) do { \
fprintf(stderr, "*** Error in function %s: " msg "\n", \
__func__, ## __VA_ARGS__); \
fprintf(stderr, \
"*** NAUGHTY PROGRAMMER!!!\n" \
"*** SPANK SPANK SPANK!!!\n" \
"*** Now go fix your code. Tut tut tut!\n\n"); } while (0)
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*
* Private API *
*=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~*/
static void
_portability_change_terminal_mode(int dir)
{
#ifndef _WIN32
static struct termios old_term, new_term;
if (dir == 1)
{
tcgetattr( STDIN_FILENO, &old_term);
new_term = old_term;
new_term.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &new_term );
}
else
{
tcsetattr( STDIN_FILENO, TCSANOW, &old_term);
}
#else
(void) dir; // To avoid warning with -Wunused-parameter
#endif
}
static void
_portability_color_apply(void)
{
#ifdef _WIN32
HANDLE console;
unsigned int color;
color = 0;
console = GetStdHandle(STD_OUTPUT_HANDLE);
if(_portability_color_fg == COLOR_RED)
color |= FOREGROUND_RED;
if(_portability_color_fg == COLOR_BLUE)
color |= FOREGROUND_BLUE;
if(_portability_color_fg == COLOR_GREEN)
color |= FOREGROUND_GREEN;
if(_portability_color_fg == COLOR_GRAY)
color |= FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE;
if(_portability_color_bg == COLOR_RED)
color |= BACKGROUND_RED;
if(_portability_color_bg == COLOR_BLUE)
color |= BACKGROUND_BLUE;
if(_portability_color_bg == COLOR_GREEN)
color |= BACKGROUND_GREEN;
if(_portability_color_bg == COLOR_GRAY)
color |= BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE;
//if(_portability_color_bg == COLOR_BLACK)
// color |= BACKGROUND_BLACK;
SetConsoleTextAttribute(console, color);
#else
if(_portability_color_bg != COLOR_DEFAULT &&
_portability_color_fg != COLOR_DEFAULT)
printf("\033[0;%i;%im", _portability_color_fg,
_portability_color_bg + 10);
else if(_portability_color_bg != COLOR_DEFAULT)
printf("\033[0;%im", _portability_color_bg + 10);
else if(_portability_color_fg != COLOR_DEFAULT)
printf("\033[0;%im", _portability_color_fg);
else
printf("\033[0;m");
#endif
}
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*
* Public API *
*=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~*/
void
portability_init(void)
{
int chk;
/* Disable line buffering - setbuf is deprecated and its usage must be
* avoided as much as possible (cf. man 3 - BSD) */
chk = setvbuf(stdout, NULL, _IONBF, 0);
chk *= setvbuf(stderr, NULL, _IONBF, 0);
/* Lib has been properly initiated (or not) */
if (chk == 0)
_init = 1;
else
fprintf(stderr, "*** Error in %s: %s\n", __func__, strerror(errno));
}
void
portability_shutdown(void)
{
/* Nothing to implement here for now */
}
int
portability_system_call(const char *cmd)
{
int ret = -1;
if (!cmd) return -1;
if (strcasecmp(cmd, "pause") == 0)
{
printf("Press any key to continue...");
ret = getchar();
}
else if ((strcasecmp(cmd, "CLS") == 0) ||
(strcmp(cmd, "clear") == 0))
{
#ifdef _WIN32
ret = system("CLS");
#else
ret = system("clear");
#endif
}
else
{
ret = system(cmd);
}
return ret;
}
void
portability_clear_buffer(FILE *f)
{
char c;
// Si on veut vider le buffer d'entrée
if (f == stdin)
{
// Boucle vidant le buffer caractère par caractère
while( (c=getchar()) != '\n' && c != EOF) ;
}
else // Si on veut vraiment flush un flux de sortie
{
fflush(f);
}
}
int
portability_kbhit(void)
{
int val;
#ifdef _WIN32
val = kbhit();
#else
_portability_change_terminal_mode(1);
struct timeval tv = {0, 0};
fd_set rdfs;
FD_ZERO(&rdfs);
FD_SET( STDIN_FILENO, &rdfs);
val = select(STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv) == 1;
//return FD_ISSET(STDIN_FILENO, &rdfs);
#endif
return val;
}
void
portability_sleep(unsigned int time)
{
#ifdef _WIN32
Sleep(time);
#else
(void) usleep(time * 1000.0);
#endif
}
void
portability_gotoligcol(int poslig, int poscol)
{
if (!_init)
{
ERR("You didn't initialize portability with portability_init()\n"
"*** You MUST use portability_init() before using this function!");
return;
}
#ifdef _WIN32
COORD mycoord;
mycoord.X = poscol;
mycoord.Y = poslig;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), mycoord );
#else
printf("%c[%d;%df", 0x1B, poslig, poscol);
#endif
}
void
portability_background_color_set(Color color)
{
if (!_init)
{
ERR("You didn't initialize portability with portability_init()\n"
"*** You MUST use portability_init() before using this function!");
return;
}
_portability_color_bg = color;
_portability_color_apply();
}
void
portability_text_color_set(Color color)
{
if (!_init)
{
ERR("You didn't initialize portability with portability_init()\n"
"*** You MUST use portability_init() before using this function!");
return;
}
_portability_color_fg = color;
_portability_color_apply();
}