Re: Optimization of code
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Fri, 04 Sep 2009 21:41:01 -0400
Over the last 20 years, a significant fraction of my revenue has come from controlling
external devices that take 8-bit character string commands. Only at the lowest level do I
convert Unicode to ANSI. If I get the notification that there was an unconvertible
character, I return from the call with an INVALID_PARAMETER type error, e.g.,
BOOL CMyDocument::SendToDevice(const CString & data)
{
CStringA command;
#ifdef _UNICODE
BOOL error;
int size = ::WideCharToMultiByte(CP_ACP, ...flags..., (LPCWSTR)data, -1, NULL, 0,
"?", &error);
if(!error)
{
LPSTR p = command.GetBuffer(size);
::WideCharToMultiByte(CP_ACP, ...flags..., (LPCWSTR)data, -1, p, size, "?",
&error);
}
if(error)
{
::SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
#else
command = data;
#endif
...send data to device...(asks asynch thread to do it)
return TRUE;
}
Note that I explicitly call WCTMB because I want the ability to see if there was an error,
and although T2A would be shorter code, it would be less robust. Since the particular
product is international (and they have customers in China, Japan and Korea), I've been
told this test actually caught some errors, particularly for the new line of controllers
that would accept character data, but only 8-bit data (which they displayed). They tell
me a future release will accept UTF-8 and they will enhance the LCD display to allow
localization, but that won't be for a couple more years.
joe
****
On Fri, 04 Sep 2009 09:09:31 -0400, r norman <r_s_norman@xxxxxxxxxxx> wrote:
There exist many external devices that programs must communicate withJoseph M. Newcomer [MVP]
that require ASCII text strings as part of their protocol. Often
these have to be parsed for processing and character-by-character
manipulation alone would be a terrible burden. I can easily accept
switching to Unicode for all processes contained purely within the
local machine but it would be a disaster to lose ANSI support
altogether.
On Thu, 3 Sep 2009 23:41:30 -0700, "Tom Serface" <tom@xxxxxxxxxxxxx>
wrote:
I've started just going to Unicode only and forgetting about the stupid _T()
stuff (just using L"" instead). I wish, like C#, I didn't even have to do
that and it could just assume Unicode for all strings, but ... I don't see
any good reason to use ANSI any longer and on the off chance I really need a
char based array I can always just use char. I think it's time to skinny
down MFC and just lose ANSI support altogether, but, like you, I'm not
holding my breath.
Tom
"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:07c1a5l8nb9ilamcevstaotfo1inaqsllj@xxxxxxxxxx
Sorry. TCHAR and the entire tchar.h existed and I used them extensively
in VS6. I've
been writing Unicode-aware code with TCHAR and tchar.h since VS4.2; they
may have existed
before that point, but that's when I started becoming Unicode-aware. What
was missing was
CStringA/CStringW distinctions. which was a real pain.
I keep forgetting how primitive the MFC library and C++ compiler were in
VS6. So the
choices currently are a great IDE and crappy language/library, or a crappy
IDE and a great
language/library. It shouldn't have been so hard to get great in both!
If we can just get the cuteness factor and the designers' egos out of the
equation, we
might actually start to see usable software come out from Microsoft again.
But I'm not
holding my breath for that.
joe
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Optimization of code
- From: Eddards
- Re: Optimization of code
- From: Joseph M . Newcomer
- Re: Optimization of code
- From: Eddards
- Re: Optimization of code
- From: Giovanni Dicanio
- Re: Optimization of code
- From: Scot T Brennecke
- Re: Optimization of code
- From: Joseph M . Newcomer
- Re: Optimization of code
- From: Tom Serface
- Re: Optimization of code
- From: r norman
- Optimization of code
- Prev by Date: Re: MFC and Worker Thread issue
- Next by Date: Re: Optimization of code
- Previous by thread: Re: Optimization of code
- Next by thread: Re: Optimization of code
- Index(es):
Relevant Pages
|