|
| 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