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;
211 if(_tcsnicmp(str,"bri",3) == 0 )
219 if( (tmp_clr = txt_clr(str)) == (WORD)-1 )
224 /*skip spaces and "on"*/
225 if ( chop_blank(&str) || chop_blank(&str) )
228 ret_clr = tmp_clr | (bBri << 3);
233 if(_tcsnicmp(str,"bri",3) == 0 )
242 if( (tmp_clr = txt_clr(str)) == (WORD)-1 )
249 return SC_HEX | ret_clr | tmp_clr << 4 | bBri << 7;
254 /****main function****/
256 the only parameter is arg_str, a pointer to a string.
257 the string is modified so it will begin to first word after
259 (only the char* is moved, no chars in the string are modfied)
262 it returns the color in the l.o. byte, plus two flags in the
264 SC_HEX win nt's cmd syntax (for exampl a0)
265 SC_TXT 4nt's syntax ( "bri gre on bla" or "10 on 0")
267 if succedes also move the LPTSTR to end of
268 string that specify color
272 BOOL StringToColor(LPWORD lpColor, LPTSTR*str)
276 wRet = str_to_color (str);
277 if (wRet == (WORD)-1)
281 if (wRet == (WORD)-1)