SYSTEM_TIME_ZONE_INFORMATION added.
[reactos.git] / rosapps / cmd / strtoclr.c
1 /*
2 * STRTOCLR.C - read color (for color command and other)
3 *
4 *
5 * History:
6 *
7 * 07-Oct-1999 (Paolo Pantaleo)
8 * Started.
9 *
10 *
11 */
12
13 /*only
14 BOOL StringToColor(LPWORD lpColor, LPTSTR*str)
15 is to be called
16 other are internal service functions*/
17
18
19 #include "cmd.h"
20
21 #include <ctype.h>
22 #include <tchar.h>
23
24
25 #define _B FOREGROUND_BLUE
26 #define _G FOREGROUND_GREEN
27 #define _R FOREGROUND_RED
28 #define _I FOREGROUND_INTENSITY
29
30
31 /*return values for chop_blank*/
32 #define CP_OK 0
33 #define CP_BLANK_NOT_FOUND 1
34 #define CP_END_OF_STRING 2
35
36 #define SC_HEX 0x0100
37 #define SC_TXT 0x0200
38
39
40
41 typedef struct _CLRTABLE
42 {
43 LPTSTR name;
44 WORD val;
45 } CLRTABLE;
46
47
48 CLRTABLE clrtable[] =
49 {
50 {"bla" ,0 },
51 {"blu" ,_B },
52 {"gre" ,_G },
53 {"cya" ,_B|_G },
54 {"red" ,_R },
55 {"mag" ,_B|_R },
56 {"yel" ,_R|_G },
57 {"whi" ,_R|_G|_B },
58 {"gra" ,_I },
59
60
61 {"0" ,0 },
62 {"2" ,_G },
63 {"3" ,_B|_G },
64 {"4" ,_R },
65 {"5" ,_B|_R },
66 {"6" ,_R|_G },
67 {"7" ,_R|_G|_B },
68
69 {"8" ,_I },
70 {"9" ,_I|_B },
71 {"10" ,_I|_G },
72 {"11" ,_I|_B|_G },
73 {"12" ,_I|_R },
74 {"13" ,_I|_B|_R },
75 {"14" ,_I|_R|_G },
76 {"15" ,_I|_R|_G|_B},
77
78
79 /* note that 1 is at the end of list
80 to avoid to confuse it with 10-15*/
81 {"1" ,_B },
82
83 /*cyan synonimous*/
84 {"aqu" ,_B|_G },
85 /*magenta synonimous*/
86 {"pur" ,_B|_R },
87
88
89 {"" ,0},
90 };
91
92
93
94 /*
95 move string pointer to next word (skip all spaces)
96 on erro retunr nonzero value
97 */
98 static
99 INT chop_blank(LPTSTR *arg_str)
100 {
101
102 LPTSTR str;
103 str = _tcschr(*arg_str,_T(' '));
104 if(!str)
105 {
106 str = _tcschr (*arg_str, _T('\0'));
107 if(str != NULL)
108 *arg_str=str;
109 return CP_BLANK_NOT_FOUND;
110 }
111
112
113
114 while(_istspace(*str))
115 str++;
116
117 if (*str == _T('\0'))
118 {
119 *arg_str=str;
120 return CP_END_OF_STRING;
121 }
122
123 *arg_str = str;
124
125 return CP_OK;
126 }
127
128
129
130 /*
131 read a color value in hex (like win nt's cmd syntax)
132 if an error occurs return -1
133 */
134 static
135 WORD hex_clr(LPTSTR str)
136 {
137 WORD ret= (WORD)-1;
138 TCHAR ch;
139
140 ch = str[1];
141
142 if(_istdigit(ch))
143 ret = ch-_T('0');
144 else
145 {
146 ch=_totupper(ch);
147
148 if( ch >= _T('A') && ch <= _T('F') )
149 ret = ch-_T('A')+10;
150 else
151 return (WORD)-1;
152 }
153
154
155 ch = str[0];
156
157 if(_istdigit(ch))
158 ret |= (ch-_T('0')) << 4;
159 else
160 {
161 ch=_totupper(ch);
162
163 if( ch >= _T('A') && ch <= _T('F') )
164 ret |= (ch-_T('A')+10) <<4;
165 else
166 return (WORD)-1;
167 }
168
169 return ret;
170 }
171
172
173 /*
174 read a color value from a string (like 4nt's syntax)
175 if an error occurs return -1
176 */
177 static
178 WORD txt_clr(LPTSTR str)
179 {
180 INT i;
181
182 for(i=0;*(clrtable[i].name);i++)
183 if( _tcsnicmp(str,clrtable[i].name,_tcslen(clrtable[i].name)) == 0)
184 return clrtable[i].val;
185
186 return (WORD)-1;
187 }
188
189
190
191 /*search for x on y*/
192 static
193 WORD str_to_color(LPTSTR* arg_str)
194 {
195 LPTSTR str;
196 BOOL bBri=FALSE;
197
198 WORD tmp_clr,ret_clr;
199
200 str = *arg_str;
201
202
203
204 if(!(*str))
205 return (WORD)-1;
206
207
208 /*foreground*/
209 if(_tcsnicmp(str,"bri",3) == 0 )
210 {
211 bBri = TRUE;
212
213 if(chop_blank(&str))
214 return (WORD)-1;
215 }
216
217 if( (tmp_clr = txt_clr(str)) == (WORD)-1 )
218 {
219 return (WORD)-1;
220 }
221
222 /*skip spaces and "on"*/
223 if ( chop_blank(&str) || chop_blank(&str) )
224 return (WORD)-1;
225
226 ret_clr = tmp_clr | (bBri << 3);
227
228 /*background*/
229
230 if(_tcsnicmp(str,"bri",3) == 0 )
231 {
232 bBri = TRUE;
233
234 if(chop_blank(&str))
235 return (WORD)-1;
236 }
237
238
239 if( (tmp_clr = txt_clr(str)) == (WORD)-1 )
240 return (WORD)-1;
241
242 chop_blank(&str);
243
244 *arg_str = str;
245
246 return SC_HEX | ret_clr | tmp_clr << 4 | bBri << 7;
247 }
248
249
250
251 /****main function****/
252 /*
253 the only parameter is arg_str, a pointer to a string.
254 the string is modified so it will begin to first word after
255 color specification
256 (only the char* is moved, no chars in the string are modfied)
257
258
259 it returns the color in the l.o. byte, plus two flags in the
260 h.o. byte, they are:
261 SC_HEX win nt's cmd syntax (for exampl a0)
262 SC_TXT 4nt's syntax ( "bri gre on bla" or "10 on 0")
263
264 if succedes also move the LPTSTR to end of
265 string that specify color
266 */
267
268
269 BOOL StringToColor(LPWORD lpColor, LPTSTR*str)
270 {
271 WORD wRet;
272
273 wRet = str_to_color (str);
274 if (wRet == (WORD)-1)
275 {
276 wRet=hex_clr (*str);
277 chop_blank (str);
278 if (wRet == (WORD)-1)
279 return FALSE;
280 }
281
282 *lpColor = wRet;
283
284 return TRUE;
285 }
286
287 /* EOF */