2006-02-16 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* STRTOCLR.C - read color (for color command and other)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* History:
|
|
|
|
*
|
|
|
|
* 07-Oct-1999 (Paolo Pantaleo)
|
|
|
|
* Started.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-05-20 17:43:37 +00:00
|
|
|
/*
|
|
|
|
* Only
|
|
|
|
* BOOL StringToColor(LPWORD lpColor, LPTSTR*str)
|
|
|
|
* has to be called.
|
|
|
|
* Other are internal service functions.
|
|
|
|
*/
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-01-24 23:00:42 +00:00
|
|
|
#include "precomp.h"
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
#define _B FOREGROUND_BLUE
|
|
|
|
#define _G FOREGROUND_GREEN
|
|
|
|
#define _R FOREGROUND_RED
|
|
|
|
#define _I FOREGROUND_INTENSITY
|
|
|
|
|
|
|
|
|
2013-05-20 17:43:37 +00:00
|
|
|
/* Return values for chop_blank */
|
|
|
|
#define CP_OK 0
|
|
|
|
#define CP_BLANK_NOT_FOUND 1
|
|
|
|
#define CP_END_OF_STRING 2
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-05-20 17:43:37 +00:00
|
|
|
/* NOTE: See the description for these flags in the StringToColor()'s description */
|
2006-02-16 23:23:37 +00:00
|
|
|
#define SC_HEX 0x0100
|
|
|
|
#define SC_TXT 0x0200
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct _CLRTABLE
|
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
LPTSTR name;
|
|
|
|
WORD val;
|
2006-02-16 23:23:37 +00:00
|
|
|
} CLRTABLE;
|
|
|
|
|
|
|
|
|
|
|
|
CLRTABLE clrtable[] =
|
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
{_T("bla"), 0 },
|
|
|
|
{_T("blu"), _B },
|
|
|
|
{_T("gre"), _G },
|
|
|
|
{_T("cya"), _B|_G },
|
|
|
|
{_T("red"), _R },
|
|
|
|
{_T("mag"), _B|_R },
|
|
|
|
{_T("yel"), _R|_G },
|
|
|
|
{_T("whi"), _R|_G|_B },
|
|
|
|
{_T("gra"), _I },
|
|
|
|
|
|
|
|
{_T("0") , 0 },
|
|
|
|
{_T("2") , _G },
|
|
|
|
{_T("3") , _B|_G },
|
|
|
|
{_T("4") , _R },
|
|
|
|
{_T("5") , _B|_R },
|
|
|
|
{_T("6") , _R|_G },
|
|
|
|
{_T("7") , _R|_G|_B },
|
|
|
|
|
|
|
|
{_T("8") , _I },
|
|
|
|
{_T("9") , _I|_B },
|
|
|
|
{_T("10") , _I|_G },
|
|
|
|
{_T("11") , _I|_B|_G },
|
|
|
|
{_T("12") , _I|_R },
|
|
|
|
{_T("13") , _I|_B|_R },
|
|
|
|
{_T("14") , _I|_R|_G },
|
|
|
|
{_T("15") , _I|_R|_G|_B },
|
|
|
|
|
|
|
|
/*
|
2013-05-20 17:43:37 +00:00
|
|
|
* Note that 1 is at the end of list
|
2013-06-29 23:50:13 +00:00
|
|
|
* to avoid to confuse it with 10-15
|
2013-05-20 17:43:37 +00:00
|
|
|
*/
|
2013-06-29 23:50:13 +00:00
|
|
|
{_T("1") , _B },
|
2013-05-20 17:43:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
/* Cyan synonym */
|
|
|
|
{_T("aqu"), _B|_G },
|
|
|
|
/* Magenta synonym */
|
|
|
|
{_T("pur"), _B|_R },
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
{_T("") ,0},
|
2006-02-16 23:23:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-05-20 17:43:37 +00:00
|
|
|
* Move string pointer to next word (skip all spaces).
|
|
|
|
* On error return nonzero value.
|
|
|
|
*/
|
2006-02-16 23:23:37 +00:00
|
|
|
static
|
|
|
|
INT chop_blank(LPTSTR *arg_str)
|
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
LPTSTR str;
|
|
|
|
str = _tcschr(*arg_str,_T(' '));
|
2013-06-30 16:10:54 +00:00
|
|
|
if (!str)
|
2013-06-29 23:50:13 +00:00
|
|
|
{
|
|
|
|
str = _tcschr (*arg_str, _T('\0'));
|
2013-06-30 16:10:54 +00:00
|
|
|
if (str != NULL)
|
2013-06-29 23:50:13 +00:00
|
|
|
*arg_str=str;
|
|
|
|
return CP_BLANK_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(_istspace(*str))
|
|
|
|
str++;
|
|
|
|
|
|
|
|
if (*str == _T('\0'))
|
|
|
|
{
|
|
|
|
*arg_str=str;
|
|
|
|
return CP_END_OF_STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
*arg_str = str;
|
|
|
|
|
|
|
|
return CP_OK;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-05-20 17:43:37 +00:00
|
|
|
* Read a color value in hex (like win nt's cmd syntax).
|
|
|
|
* If an error occurs return -1.
|
|
|
|
*/
|
2006-02-16 23:23:37 +00:00
|
|
|
static
|
|
|
|
WORD hex_clr(LPTSTR str)
|
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
WORD ret= (WORD)-1;
|
|
|
|
TCHAR ch;
|
|
|
|
|
|
|
|
ch = str[1];
|
|
|
|
|
2013-06-30 16:10:54 +00:00
|
|
|
if (_istdigit(ch))
|
2013-06-29 23:50:13 +00:00
|
|
|
ret = ch-_T('0');
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch=_totupper(ch);
|
|
|
|
|
2013-06-30 16:10:54 +00:00
|
|
|
if ( ch >= _T('A') && ch <= _T('F') )
|
2013-06-29 23:50:13 +00:00
|
|
|
ret = ch-_T('A')+10;
|
|
|
|
else
|
|
|
|
return (WORD)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch = str[0];
|
|
|
|
|
2013-06-30 16:10:54 +00:00
|
|
|
if (_istdigit(ch))
|
2013-06-29 23:50:13 +00:00
|
|
|
ret |= (ch-_T('0')) << 4;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch=_totupper(ch);
|
|
|
|
|
2013-06-30 16:10:54 +00:00
|
|
|
if ( ch >= _T('A') && ch <= _T('F') )
|
2013-06-29 23:50:13 +00:00
|
|
|
ret |= (ch-_T('A')+10) <<4;
|
|
|
|
else
|
|
|
|
return (WORD)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-05-20 17:43:37 +00:00
|
|
|
* Read a color value from a string (like 4nt's syntax).
|
|
|
|
* If an error occurs return -1.
|
|
|
|
*/
|
2006-02-16 23:23:37 +00:00
|
|
|
static
|
|
|
|
WORD txt_clr(LPTSTR str)
|
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
INT i;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
for(i = 0; *(clrtable[i].name); i++)
|
|
|
|
if (_tcsnicmp(str, clrtable[i].name, _tcslen(clrtable[i].name)) == 0)
|
|
|
|
return clrtable[i].val;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
return (WORD)-1;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-20 17:43:37 +00:00
|
|
|
/* Search for "x on y" */
|
2006-02-16 23:23:37 +00:00
|
|
|
static
|
|
|
|
WORD str_to_color(LPTSTR* arg_str)
|
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
LPTSTR str;
|
|
|
|
BOOL bBri;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
WORD tmp_clr,ret_clr;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
str = *arg_str;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
if (!(*str))
|
|
|
|
return (WORD)-1;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
/* foreground */
|
|
|
|
bBri = FALSE;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
if (_tcsnicmp(str,_T("bri"),3) == 0)
|
|
|
|
{
|
|
|
|
bBri = TRUE;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
if (chop_blank(&str))
|
|
|
|
return (WORD)-1;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
if ((tmp_clr = txt_clr(str)) == (WORD)-1)
|
|
|
|
{
|
|
|
|
return (WORD)-1;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
/* skip spaces and "on" keyword */
|
|
|
|
if (chop_blank(&str) || chop_blank(&str))
|
|
|
|
return (WORD)-1;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
ret_clr = tmp_clr | (bBri << 3);
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
/* background */
|
|
|
|
bBri = FALSE;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 16:10:54 +00:00
|
|
|
if (_tcsnicmp(str,_T("bri"),3) == 0 )
|
2013-06-29 23:50:13 +00:00
|
|
|
{
|
|
|
|
bBri = TRUE;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 16:10:54 +00:00
|
|
|
if (chop_blank(&str))
|
2013-06-29 23:50:13 +00:00
|
|
|
return (WORD)-1;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-30 16:10:54 +00:00
|
|
|
if ( (tmp_clr = txt_clr(str)) == (WORD)-1 )
|
2013-06-29 23:50:13 +00:00
|
|
|
return (WORD)-1;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
chop_blank(&str);
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
*arg_str = str;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
/* NOTE: See the note on SC_HEX in the StringToColor()'s description */
|
|
|
|
return /* SC_HEX | */ ret_clr | tmp_clr << 4 | bBri << 7;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-20 17:43:37 +00:00
|
|
|
/**** Main function ****/
|
2006-02-16 23:23:37 +00:00
|
|
|
/*
|
2013-05-20 17:43:37 +00:00
|
|
|
* The only parameter is arg_str, a pointer to a string.
|
|
|
|
* The string is modified so it will begin to first word after
|
|
|
|
* color specification
|
2016-11-05 14:55:55 +00:00
|
|
|
* (only the char* is moved, no chars in the string are modified).
|
2013-05-20 17:43:37 +00:00
|
|
|
*
|
|
|
|
* **** NOTE: The following functionality is deactivated ****
|
|
|
|
* it returns the color in the l.o. byte, plus two flags in the
|
|
|
|
* h.o. byte, they are:
|
2016-11-05 14:55:55 +00:00
|
|
|
* SC_HEX win nt's cmd syntax (for example a0)
|
2013-05-20 17:43:37 +00:00
|
|
|
* SC_TXT 4nt's syntax ( "bri gre on bla" or "10 on 0")
|
|
|
|
* **********************************************************
|
|
|
|
*
|
2016-11-05 14:55:55 +00:00
|
|
|
* If success also move the LPTSTR to end of
|
2013-05-20 17:43:37 +00:00
|
|
|
* string that specify color.
|
|
|
|
*/
|
2006-02-16 23:23:37 +00:00
|
|
|
BOOL StringToColor(LPWORD lpColor, LPTSTR*str)
|
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
WORD wRet;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
wRet = str_to_color (str);
|
|
|
|
if (wRet == (WORD)-1)
|
|
|
|
{
|
|
|
|
wRet=hex_clr (*str);
|
|
|
|
chop_blank (str);
|
|
|
|
if (wRet == (WORD)-1)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
*lpColor = wRet;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
return TRUE;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|