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
32 /* NOTE: See the description for these flags in the StringToColor()'s description */
37 typedef struct _CLRTABLE
53 {_T("whi"), _R
|_G
|_B
},
62 {_T("7") , _R
|_G
|_B
},
67 {_T("11") , _I
|_B
|_G
},
69 {_T("13") , _I
|_B
|_R
},
70 {_T("14") , _I
|_R
|_G
},
71 {_T("15") , _I
|_R
|_G
|_B
},
74 * Note that 1 is at the end of list
75 * to avoid to confuse it with 10-15
89 * Move string pointer to next word (skip all spaces).
90 * On error return nonzero value.
93 INT
chop_blank(LPTSTR
*arg_str
)
96 str
= _tcschr(*arg_str
,_T(' '));
99 str
= _tcschr (*arg_str
, _T('\0'));
102 return CP_BLANK_NOT_FOUND
;
105 while(_istspace(*str
))
108 if (*str
== _T('\0'))
111 return CP_END_OF_STRING
;
121 * Read a color value in hex (like win nt's cmd syntax).
122 * If an error occurs return -1.
125 WORD
hex_clr(LPTSTR str
)
138 if ( ch
>= _T('A') && ch
<= _T('F') )
147 ret
|= (ch
-_T('0')) << 4;
152 if ( ch
>= _T('A') && ch
<= _T('F') )
153 ret
|= (ch
-_T('A')+10) <<4;
163 * Read a color value from a string (like 4nt's syntax).
164 * If an error occurs return -1.
167 WORD
txt_clr(LPTSTR str
)
171 for(i
= 0; *(clrtable
[i
].name
); i
++)
172 if (_tcsnicmp(str
, clrtable
[i
].name
, _tcslen(clrtable
[i
].name
)) == 0)
173 return clrtable
[i
].val
;
179 /* Search for "x on y" */
181 WORD
str_to_color(LPTSTR
* arg_str
)
186 WORD tmp_clr
,ret_clr
;
196 if (_tcsnicmp(str
,_T("bri"),3) == 0)
200 if (chop_blank(&str
))
204 if ((tmp_clr
= txt_clr(str
)) == (WORD
)-1)
209 /* skip spaces and "on" keyword */
210 if (chop_blank(&str
) || chop_blank(&str
))
213 ret_clr
= tmp_clr
| (bBri
<< 3);
218 if (_tcsnicmp(str
,_T("bri"),3) == 0 )
222 if (chop_blank(&str
))
226 if ( (tmp_clr
= txt_clr(str
)) == (WORD
)-1 )
233 /* NOTE: See the note on SC_HEX in the StringToColor()'s description */
234 return /* SC_HEX | */ ret_clr
| tmp_clr
<< 4 | bBri
<< 7;
238 /**** Main function ****/
240 * The only parameter is arg_str, a pointer to a string.
241 * The string is modified so it will begin to first word after
242 * color specification
243 * (only the char* is moved, no chars in the string are modfied).
245 * **** NOTE: The following functionality is deactivated ****
246 * it returns the color in the l.o. byte, plus two flags in the
247 * h.o. byte, they are:
248 * SC_HEX win nt's cmd syntax (for exampl a0)
249 * SC_TXT 4nt's syntax ( "bri gre on bla" or "10 on 0")
250 * **********************************************************
252 * If succedes also move the LPTSTR to end of
253 * string that specify color.
255 BOOL
StringToColor(LPWORD lpColor
, LPTSTR
*str
)
259 wRet
= str_to_color (str
);
260 if (wRet
== (WORD
)-1)
264 if (wRet
== (WORD
)-1)