Skip to content

Commit 41d856c

Browse files
author
iTeam-Projects
committed
Merge pull request #3 from Lqp1/master
Ajout des couleurs
2 parents 10508c6 + 30e177e commit 41d856c

7 files changed

Lines changed: 300 additions & 150 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
*.o
3+
libportab_test
4+
*.swp
5+
*.swo

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
UNAME := $(shell uname)
2+
ifeq ($(UNAME), Darwin)
3+
CC=clang
4+
else
5+
CC=gcc
6+
endif
7+
CFLAGS=-I.
8+
DEPS=portability.h
9+
OBJ=main.o portability.o
10+
11+
%.o: %.c $(DEPS)
12+
$(CC) -g -c -o $@ $< $(CFLAGS)
13+
14+
libportab_test: $(OBJ)
15+
$(CC) -o $@ $^ $(CFLAGS)
16+
17+
clean:
18+
@rm -vf *.o
19+
@rm -vf libportab_test

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
PortabilityCode
22
===============
33

4-
Bibliothèque de fonctions portables à destination des ING1 à l'ECE
4+
Bibliothèque de fonctions portables à destination des ING1 à l'ECE.
5+
6+
# TODO
7+
8+
- [ ] Se mettre au boulot !

TODO.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

main.c

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
31
#include "portability.h"
42

5-
int main()
3+
#define ERR(msg, ...) fprintf(stderr, "*** %s:%i " msg "\n",\
4+
__func__, __LINE__, ## __VA_ARGS__)
5+
6+
static void
7+
_system_call_test(const char *arg)
8+
{
9+
int val;
10+
11+
if (arg == NULL)
12+
{
13+
ERR("NULL argument");
14+
return;
15+
}
16+
val = portability_system_call(arg);
17+
printf("%s: %i\n", __func__, val);
18+
}
19+
20+
int main(int argc, char **argv)
621
{
7-
int i = 0;
8-
while (!kbhit())
9-
{
10-
printf("Temps: %d secondes\n", i);
11-
gotoligcol(0,0);
12-
i++;
13-
Sleep(1000);
14-
}
15-
16-
printf("\n\n\n=> %c", getchar());
17-
18-
return 0;
22+
_system_call_test(argc >= 2 ? argv[1] : NULL);
23+
24+
portability_background_color(COLOR_RED);
25+
printf("Red-------------\n");
26+
portability_background_color(COLOR_BLUE);
27+
printf("Blue-------------\n");
28+
portability_background_color(COLOR_GREEN);
29+
printf("Green-------------\n");
30+
portability_background_color(COLOR_BLACK);
31+
printf("BLACK-------------\n");
32+
portability_background_color(COLOR_GRAY);
33+
printf("Gray-------------\n");
34+
35+
portability_text_color(COLOR_RED);
36+
printf("Red-------------\n");
37+
portability_text_color(COLOR_BLUE);
38+
printf("Blue-------------\n");
39+
portability_background_color(COLOR_DEFAULT);
40+
portability_text_color(COLOR_GREEN);
41+
printf("Green-------------\n");
42+
portability_text_color(COLOR_GRAY);
43+
printf("Gray-------------\n");
44+
portability_text_color(COLOR_BLACK);
45+
printf("BLACK-------------\n");
46+
47+
48+
49+
50+
return 0;
1951
}
2052

portability.c

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#include "portability.h"
2+
3+
/* Since this is the implementation of wrappers, we must disable the 'aliases'
4+
* absolutely. Else, we will call them recursively */
5+
#undef gotoligcol
6+
#undef kbhit
7+
#undef sleep
8+
#undef system
9+
#undef fflush
10+
#ifdef _WIN32
11+
# undef strcasecmp
12+
#endif
13+
14+
15+
static unsigned int _portability_color_bg = COLOR_DEFAULT;
16+
static unsigned int _portability_color_fg = COLOR_DEFAULT;
17+
18+
int
19+
portability_system_call(const char *cmd)
20+
{
21+
int ret = -1;
22+
23+
if (!cmd) return -1;
24+
25+
if (strcasecmp(cmd, "pause") == 0) // Si on veut faire une pause
26+
{
27+
printf("Press any key to continue...");
28+
ret = getchar(); // Attend la saisie d'un caractère
29+
}
30+
else if ((strcasecmp(cmd, "cls") == 0) ||
31+
(strcmp(cmd, "clear") == 0))
32+
{
33+
#ifdef _WIN32 // Si on est sous Windows
34+
ret = system("cls");
35+
#else // Si on est sous Linux/Mac
36+
ret = system("clear");
37+
#endif
38+
}
39+
else
40+
{
41+
// Si ce n'est pas une commande gérée, on la transmet tel quel
42+
ret = system(cmd);
43+
}
44+
return ret;
45+
}
46+
47+
void
48+
portability_clear_buffer(FILE* f)
49+
{
50+
char c;
51+
52+
if (!f) return;
53+
54+
// Si on veux vider le buffer d'entrée
55+
if (f == stdin)
56+
{
57+
// Boucle vidant le buffer caractère par caractère
58+
while( (c=getchar()) != '\n' && c != EOF) ;
59+
}
60+
else // Si on veux vraiment flush un flux de sortie
61+
{
62+
fflush(f);
63+
}
64+
}
65+
66+
#ifndef _WIN32
67+
68+
void portability_change_terminal_mode(int dir)
69+
{
70+
static struct termios old_term, new_term;
71+
72+
if (dir == 1)
73+
{
74+
tcgetattr( STDIN_FILENO, &old_term);
75+
new_term = old_term;
76+
new_term.c_lflag &= ~( ICANON | ECHO );
77+
tcsetattr( STDIN_FILENO, TCSANOW, &new_term );
78+
}
79+
else
80+
{
81+
tcsetattr( STDIN_FILENO, TCSANOW, &old_term);
82+
}
83+
}
84+
85+
#endif
86+
87+
int portability_kbhit()
88+
{
89+
int val;
90+
#ifdef _WIN32
91+
val = kbhit();
92+
#else
93+
portability_change_terminal_mode(1);
94+
struct timeval tv = {0, 0};
95+
fd_set rdfs;
96+
97+
FD_ZERO(&rdfs);
98+
FD_SET( STDIN_FILENO, &rdfs);
99+
100+
val = select(STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv) == 1;
101+
//return FD_ISSET(STDIN_FILENO, &rdfs);
102+
#endif
103+
104+
return val;
105+
}
106+
107+
unsigned int portability_sleep(unsigned int time)
108+
{
109+
#ifdef _WIN32
110+
Sleep(time);
111+
return 0;
112+
#else
113+
return usleep(time * 1000);
114+
#endif
115+
}
116+
117+
void portability_gotoligcol(int poslig, int poscol)
118+
{
119+
#ifdef _WIN32
120+
COORD mycoord;
121+
mycoord.X = poscol;
122+
mycoord.Y = poslig;
123+
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), mycoord );
124+
#else
125+
printf("%c[%d;%df", 0x1B, poslig, poscol); // How about line buferring here?!
126+
#endif
127+
}
128+
129+
130+
static void
131+
_portability_color_apply()
132+
{
133+
134+
#ifdef _WIN32
135+
HANDLE console;
136+
unsigned int color;
137+
138+
color = 0;
139+
console = GetStdHandle(STD_OUTPUT_HANDLE);
140+
141+
if(_portability_color_fg == COLOR_RED)
142+
color |= FOREGROUND_RED;
143+
if(_portability_color_fg == COLOR_BLUE)
144+
color |= FOREGROUND_BLUE;
145+
if(_portability_color_fg == COLOR_GREEN)
146+
color |= FOREGROUND_GREEN;
147+
if(_portability_color_fg == COLOR_GRAY)
148+
color |= FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE;
149+
150+
if(_portability_color_bg == COLOR_RED)
151+
color |= BACKGROUND_RED;
152+
if(_portability_color_bg == COLOR_BLUE)
153+
color |= BACKGROUND_BLUE;
154+
if(_portability_color_bg == COLOR_GREEN)
155+
color |= BACKGROUND_GREEN;
156+
if(_portability_color_bg == COLOR_GRAY)
157+
color |= BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE;
158+
//if(_portability_color_bg == COLOR_BLACK)
159+
// color |= BACKGROUND_BLACK;
160+
161+
SetConsoleTextAttribute(console, color);
162+
#else
163+
164+
printf("\033[0m");
165+
166+
if(_portability_color_bg != COLOR_DEFAULT &&
167+
_portability_color_fg != COLOR_DEFAULT)
168+
printf("\033[0;%i;%im", _portability_color_fg,
169+
_portability_color_bg + 10);
170+
else if(_portability_color_bg != COLOR_DEFAULT)
171+
printf("\033[7;%im", _portability_color_bg);
172+
else if(_portability_color_fg != COLOR_DEFAULT)
173+
printf("\033[0;%im", _portability_color_fg);
174+
175+
#endif
176+
}
177+
178+
void
179+
portability_background_color(unsigned int color)
180+
{
181+
_portability_color_bg = color ;
182+
_portability_color_apply();
183+
}
184+
185+
void
186+
portability_text_color(unsigned int color)
187+
{
188+
_portability_color_fg = color ;
189+
_portability_color_apply();
190+
}
191+

0 commit comments

Comments
 (0)