2 * STRTOCLR.C - read color (for color command and other)
7 * 07-Oct-1999 (Paolo Pantaleo)
14 BOOL StringToColor(LPWORD lpColor, LPTSTR*str)
16 other are internal service functions*/
21 #define _B FOREGROUND_BLUE
22 #define _G FOREGROUND_GREEN
23 #define _R FOREGROUND_RED
24 #define _I FOREGROUND_INTENSITY
27 /*return values for chop_blank*/
29 #define CP_BLANK_NOT_FOUND 1
30 #define CP_END_OF_STRING 2
37 typedef struct _CLRTABLE
53 {_T("whi") ,_R
|_G
|_B
},
68 {_T("11") ,_I
|_B
|_G
},
70 {_T("13") ,_I
|_B
|_R
},
71 {_T("14") ,_I
|_R
|_G
},
72 {_T("15") ,_I
|_R
|_G
|_B
},
75 /* note that 1 is at the end of list
76 to avoid to confuse it with 10-15*/
81 /*magenta synonimous*/
91 move string pointer to next word (skip all spaces)
92 on erro retunr nonzero value
95 INT
chop_blank(LPTSTR
*arg_str
)
99 str
= _tcschr(*arg_str
,_T(' '));
102 str
= _tcschr (*arg_str
, _T('\0'));
105 return CP_BLANK_NOT_FOUND
;
110 while(_istspace(*str
))
113 if (*str
== _T('\0'))
116 return CP_END_OF_STRING
;
127 read a color value in hex (like win nt's cmd syntax)
128 if an error occurs return -1
131 WORD
hex_clr(LPTSTR str
)
144 if( ch
>= _T('A') && ch
<= _T('F') )
154 ret
|= (ch
-_T('0')) << 4;
159 if( ch
>= _T('A') && ch
<= _T('F') )
160 ret
|= (ch
-_T('A')+10) <<4;
170 read a color value from a string (like 4nt's syntax)
171 if an error occurs return -1
174 WORD
txt_clr(LPTSTR str
)
178 for(i
= 0; *(clrtable
[i
].name
); i
++)
179 if (_tcsnicmp(str
, clrtable
[i
].name
, _tcslen(clrtable
[i
].name
)) == 0)
180 return clrtable
[i
].val
;
187 /*search for x on y*/
189 WORD
str_to_color(LPTSTR
* arg_str
)
194 WORD tmp_clr
,ret_clr
;
205 if (_tcsnicmp(str
,_T("bri"),3) == 0)
209 if (chop_blank(&str
))
213 if ((tmp_clr
= txt_clr(str
)) == (WORD
)-1)
218 /*skip spaces and "on"*/
219 if (chop_blank(&str
) || chop_blank(&str
))
222 ret_clr
= tmp_clr
| (bBri
<< 3);
227 if(_tcsnicmp(str
,_T("bri"),3) == 0 )
236 if( (tmp_clr
= txt_clr(str
)) == (WORD
)-1 )
243 return SC_HEX
| ret_clr
| tmp_clr
<< 4 | bBri
<< 7;
248 /****main function****/
250 the only parameter is arg_str, a pointer to a string.
251 the string is modified so it will begin to first word after
253 (only the char* is moved, no chars in the string are modfied)
256 it returns the color in the l.o. byte, plus two flags in the
258 SC_HEX win nt's cmd syntax (for exampl a0)
259 SC_TXT 4nt's syntax ( "bri gre on bla" or "10 on 0")
261 if succedes also move the LPTSTR to end of
262 string that specify color
266 BOOL
StringToColor(LPWORD lpColor
, LPTSTR
*str
)
270 wRet
= str_to_color (str
);
271 if (wRet
== (WORD
)-1)
275 if (wRet
== (WORD
)-1)