]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/colors.h
b43ccbd621105fe47b4372a9512bd21e0bcb98da
[user/henk/code/inspircd.git] / win / colors.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #ifndef COLORS_H
22 #define COLORS_H
23
24 #define TRED FOREGROUND_RED | FOREGROUND_INTENSITY
25 #define TGREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
26 #define TYELLOW FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
27 #define TNORMAL FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE
28 #define TWHITE TNORMAL | FOREGROUND_INTENSITY
29 #define TBLUE FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
30
31 /* Handles colors in printf */
32 int printf_c(const char * format, ...)
33 {
34         // Better hope we're not multithreaded, otherwise we'll have chickens crossing the road other side to get the to :P
35         static char message[500];
36         static char temp[500];
37         int color1, color2;
38
39         /* parse arguments */
40         va_list ap;
41         va_start(ap, format);
42         vsnprintf(message, 500, format, ap);
43         va_end(ap);
44
45         /* search for unix-style escape sequences */
46         int t;
47         int c = 0;
48         const char * p = message;
49         while (*p != 0)
50         {
51                 if (*p == '\033')
52                 {
53                         // Escape sequence -> copy into the temp buffer, and parse the color.
54                         p++;
55                         t = 0;
56                         while ((*p) && (*p != 'm'))
57                         {
58                                 temp[t++] = *p;
59                                 ++p;
60                         }
61
62                         temp[t] = 0;
63                         p++;
64
65                         if (*temp == '[')
66                         {
67                                 if (sscanf(temp, "[%u;%u", &color1, &color2) == 2)
68                                 {
69                                         switch(color2)
70                                         {
71                                         case 32:                // Green
72                                                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);              // Yellow
73                                                 break;
74
75                                         default:                // Unknown
76                                                 // White
77                                                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
78                                                 break;
79                                         }
80                                 }
81                                 else
82                                 {
83                                         switch (*(temp+1))
84                                         {
85                                                 case '0':
86                                                         // Returning to normal colour.
87                                                         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
88                                                         break;
89
90                                                 case '1':
91                                                         // White
92                                                         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), TWHITE);
93                                                         break;
94
95                                                 default:
96                                                         char message[50];
97                                                         sprintf(message, "Unknown color code: %s", temp);
98                                                         MessageBoxA(0, message, message, MB_OK);
99                                                         break;
100                                         }
101                                 }
102                         }
103                 }
104
105                 putchar(*p);
106                 ++c;
107                 ++p;
108         }
109
110         return c;
111 }
112
113 #endif
114