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*/
25 #define _B FOREGROUND_BLUE
26 #define _G FOREGROUND_GREEN
27 #define _R FOREGROUND_RED
28 #define _I FOREGROUND_INTENSITY
31 /*return values for chop_blank*/
33 #define CP_BLANK_NOT_FOUND 1
34 #define CP_END_OF_STRING 2
41 typedef struct _CLRTABLE
79 /* note that 1 is at the end of list
80 to avoid to confuse it with 10-15*/
85 /*magenta synonimous*/
95 move string pointer to next word (skip all spaces)
96 on erro retunr nonzero value
99 INT
chop_blank(LPTSTR
*arg_str
)
103 str
= _tcschr(*arg_str
,_T(' '));
106 str
= _tcschr (*arg_str
, _T('\0'));
109 return CP_BLANK_NOT_FOUND
;
114 while(_istspace(*str
))
117 if (*str
== _T('\0'))
120 return CP_END_OF_STRING
;
131 read a color value in hex (like win nt's cmd syntax)
132 if an error occurs return -1
135 WORD
hex_clr(LPTSTR str
)
148 if( ch
>= _T('A') && ch
<= _T('F') )
158 ret
|= (ch
-_T('0')) << 4;
163 if( ch
>= _T('A') && ch
<= _T('F') )
164 ret
|= (ch
-_T('A')+10) <<4;
174 read a color value from a string (like 4nt's syntax)
175 if an error occurs return -1
178 WORD
txt_clr(LPTSTR str
)
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
;
191 /*search for x on y*/
193 WORD
str_to_color(LPTSTR
* arg_str
)
198 WORD tmp_clr
,ret_clr
;
209 if(_tcsnicmp(str
,"bri",3) == 0 )
217 if( (tmp_clr
= txt_clr(str
)) == (WORD
)-1 )
222 /*skip spaces and "on"*/
223 if ( chop_blank(&str
) || chop_blank(&str
) )
226 ret_clr
= tmp_clr
| (bBri
<< 3);
230 if(_tcsnicmp(str
,"bri",3) == 0 )
239 if( (tmp_clr
= txt_clr(str
)) == (WORD
)-1 )
246 return SC_HEX
| ret_clr
| tmp_clr
<< 4 | bBri
<< 7;
251 /****main function****/
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
256 (only the char* is moved, no chars in the string are modfied)
259 it returns the color in the l.o. byte, plus two flags in the
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")
264 if succedes also move the LPTSTR to end of
265 string that specify color
269 BOOL
StringToColor(LPWORD lpColor
, LPTSTR
*str
)
273 wRet
= str_to_color (str
);
274 if (wRet
== (WORD
)-1)
278 if (wRet
== (WORD
)-1)