473,466 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

TEXT macro for ascii and unicode initialization

I've been relying on gcc compiling this but now it doesn't so I'm
hoping to learn the right way.

See the header below.

TCHAR Message[] = TEXT("Press a Key to Change Setting...");

this basicly becomes

wchar_t Message[] = ((wchar_t *)L"Press a Key to Change Setting..."));

And now I get the gcc error: initializer fails to determine size of
`Message'

I have tried casting to wchar_t[] but that gives it's own error.

What's the legal way of doing this? Without templates please? And
typedef types rather than #define'd equivelents.

The whole point of my types library is so I don't assume built in
types. For instance a character could be 'unsigned char' or 'unsigned
wchar_t" if I wanted, maybe even 'unsigned int'.
I have after some pruning
***************************************

#ifdef DEFAULT_ASCII
#ifdef DEFAULT_UNICODE
#undef DEFAULT_UNICODE
#endif
#endif

#define ASCII_TEXT(x) ((XTYPE::ACHAR *)(x))
#define UNICODE_TEXT(x) ((XTYPE::UCHAR *)(L##x))

namespace XTYPE
{
typedef char ACHAR ;
typedef wchar_t UCHAR ;

typedef ACHAR * ASTRING ;
typedef CONST ACHAR * CASTRING ;

typedef UCHAR * USTRING ;
typedef CONST UCHAR * CUSTRING ;

#ifdef DEFAULT_ASCII
typedef ACHAR CHAR ;
#define TEXT(x) ASCII_TEXT(x)
typedef CHAR * STRING ;
typedef CONST STRING CSTRING ;
typedef CHAR TCHAR ;
typedef TCHAR * TSTRING ;
typedef CONST TSTRING CTSTRING ;
#endif

#ifdef DEFAULT_UNICODE
typedef UCHAR CHAR ;

#define TEXT(x) UNICODE_TEXT(x)

typedef CHAR * STRING ;
typedef CONST STRING CSTRING ;
typedef CHAR TCHAR ;
typedef TCHAR * TSTRING ;
typedef CONST TSTRING CTSTRING ;
#endif
} ;

Jul 22 '05 #1
7 6128
Blue wrote:
I've been relying on gcc compiling this but now it doesn't so I'm
hoping to learn the right way.

See the header below.

TCHAR Message[] = TEXT("Press a Key to Change Setting...");

this basicly becomes

wchar_t Message[] = ((wchar_t *)L"Press a Key to Change Setting..."));
Why are you casting?

int main( )
{
wchar_t Message[] = L"Press a Key to Change Setting...";
}
And now I get the gcc error: initializer fails to determine size of
`Message'

I have tried casting to wchar_t[] but that gives it's own error.

What's the legal way of doing this? Without templates please? And
typedef types rather than #define'd equivelents.


Jul 22 '05 #2
Blue wrote:
I've been relying on gcc compiling this but now it doesn't so I'm
hoping to learn the right way.

See the header below.

TCHAR Message[] = TEXT("Press a Key to Change Setting...");

this basicly becomes

wchar_t Message[] = ((wchar_t *)L"Press a Key to Change Setting..."));

And now I get the gcc error: initializer fails to determine size of
`Message'
That's right. You're defining an array without specifying the size. This
is only possible if the compiler can determine the size from the
initializer. But the intializer is a pointer. Why do you cast the
literal into a pointer?
I have tried casting to wchar_t[] but that gives it's own error.
You cannot cast into an array, especially not into one without a size.
What's the legal way of doing this?
Leave out the cast.
Without templates please?
Why?
And typedef types rather than #define'd equivelents.

The whole point of my types library is so I don't assume built in
types. For instance a character could be 'unsigned char' or 'unsigned
wchar_t" if I wanted, maybe even 'unsigned int'.
Why unsigned?
I have after some pruning
***************************************

#ifdef DEFAULT_ASCII
#ifdef DEFAULT_UNICODE
#undef DEFAULT_UNICODE
#endif
#endif

#define ASCII_TEXT(x) ((XTYPE::ACHAR *)(x))
#define UNICODE_TEXT(x) ((XTYPE::UCHAR *)(L##x))

namespace XTYPE
{
typedef char ACHAR ;
typedef wchar_t UCHAR ;

typedef ACHAR * ASTRING ;
typedef CONST ACHAR * CASTRING ;

typedef UCHAR * USTRING ;
typedef CONST UCHAR * CUSTRING ;

#ifdef DEFAULT_ASCII
typedef ACHAR CHAR ;
#define TEXT(x) ASCII_TEXT(x)
typedef CHAR * STRING ;
typedef CONST STRING CSTRING ;
typedef CHAR TCHAR ;
typedef TCHAR * TSTRING ;
typedef CONST TSTRING CTSTRING ;
#endif

#ifdef DEFAULT_UNICODE
typedef UCHAR CHAR ;

#define TEXT(x) UNICODE_TEXT(x)

typedef CHAR * STRING ;
typedef CONST STRING CSTRING ;
typedef CHAR TCHAR ;
typedef TCHAR * TSTRING ;
typedef CONST TSTRING CTSTRING ;
#endif
} ;

Why do all your types SHOUT?

Jul 22 '05 #3
Rolf Magnus wrote:
#ifdef DEFAULT_UNICODE
typedef UCHAR CHAR ;

#define TEXT(x) UNICODE_TEXT(x)

typedef CHAR * STRING ;
typedef CONST STRING CSTRING ;
typedef CHAR TCHAR ;
typedef TCHAR * TSTRING ;
typedef CONST TSTRING CTSTRING ;
#endif
} ;


Why do all your types SHOUT?


Maybe because everyone else was doing it.

Some esthetic style guides put all typedefs into a SHOUTING
pseudo-namespace.

Some postfix a _t.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces

Jul 22 '05 #4
On Thu, 12 Feb 2004 08:46:17 -0500, Jeff Schwab <je******@comcast.net>
wrote:
Why are you casting?


Take a look at the code. I'm abstracting strings.

I do it so I can construct strings of any datatype.

I have TCHAR String = TEXT("Hello World\n") so the code is written and
I can choose at compile time if I want the application to use ASCII by
default or unicode. I can also decide if I want string literals to be
const by default. Simple abstraction. I use to use defines but then
I decovered how wonderful typedef can be. If this just isn't possible
anymore I'll have to go back to full macro data types.
Jul 22 '05 #5
Blue wrote:
Jeff Schwab wrote:
Why are you casting?


Take a look at the code. I'm abstracting strings.

I do it so I can construct strings of any datatype.

I have TCHAR String = TEXT("Hello World\n") so the code is written and
I can choose at compile time if I want the application to use ASCII by
default or unicode. I can also decide if I want string literals to be
const by default. Simple abstraction. I use to use defines but then
I decovered how wonderful typedef can be. If this just isn't possible
anymore I'll have to go back to full macro data types.


But why are you casting?

Always try to remove typecasts. The better your design, the less likely they
are needed. Only use them in a pinch.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #6
On Thu, 12 Feb 2004 15:12:11 +0100, Rolf Magnus <ra******@t-online.de>
wrote:
Blue wrote:
I've been relying on gcc compiling this but now it doesn't so I'm
hoping to learn the right way.

See the header below.

TCHAR Message[] = TEXT("Press a Key to Change Setting...");

this basicly becomes

wchar_t Message[] = ((wchar_t *)L"Press a Key to Change Setting..."));

And now I get the gcc error: initializer fails to determine size of
`Message'
That's right. You're defining an array without specifying the size. This
is only possible if the compiler can determine the size from the
initializer. But the intializer is a pointer. Why do you cast the
literal into a pointer?
I have tried casting to wchar_t[] but that gives it's own error.


You cannot cast into an array, especially not into one without a size.


You can if the compiler allows it. You have a literal of a size that
can be determined. Cast it to something else and the size can be
determined from simple math. String literals themselves are hacks
where they should be declared as: { 'h', 'e', 'l', 'l', 'o', '\n'}.;
Without templates please?


Why?


Because templaces are an ugly mistake and I'd rather go back to C or
take up C# to avoid them but only if I'm forced to though it would be
easier to hack gcc to suppot it again with a command line option.

I'd just like to know if there is a way before I do anything that
drastic.
And typedef types rather than #define'd equivelents.

The whole point of my types library is so I don't assume built in
types. For instance a character could be 'unsigned char' or 'unsigned
wchar_t" if I wanted, maybe even 'unsigned int'.


Why unsigned?


Who knows. Why not? You never heard of programs that assume char's
are unsigned? Unsigned char are more logical. If I decide I want
unsigned chars why should a programming languaged work against me?
Why do all your types SHOUT?


Because they started life as defines. Also because it makes them
easier to see compared to variable names. . That's only a small
portion of the code. I have redefined all types this way. I have use
INT and I can choose the it's size. It's also confined them to a
namespace so the global namespace isn't polluted and god forbid you
have to use some of my code you could comparmentalize it. But
ultimetly I just like it take way.

I suppose next your going to critisize my indentation style and go
into how c++ in the future will evolve to force everyone to use your
style...

Jul 22 '05 #7
> >Why are you casting?

Take a look at the code. I'm abstracting strings.

I do it so I can construct strings of any datatype.
I think you mean "arrays of any char type". In C++, the word "string"
has the connotation of a container class of a char type,
instead of a pointer in C.
I have TCHAR String = TEXT("Hello World\n") so the code is written and
XTYPE::TCHAR presumably
If you have TCHAR in the global namespace (eg. if you import XTYPE
to global ns) you will run into a problem (more info below).
I can choose at compile time if I want the application to use ASCII by
default or unicode.
Obviously. But this does not require casting.
You wrote:
#define ASCII_TEXT(x) ((XTYPE::ACHAR *)(x))
#define UNICODE_TEXT(x) ((XTYPE::UCHAR *)(L##x))
Instead you should write:

#define ASCII_TEXT(x) x
#define WIDE_TEXT(x) L##x

Note - wchar_t is NOT unicode, if you do not understand
the difference you should go and do some googling.

Also, you have used many names that are confusingly similar to
different conventions; if you ever intend someone else to look
at your code then you might want to rethink them:
typedef char ACHAR ;
typedef wchar_t UCHAR ;
UCHAR is sometimes used as a typedef for "unsigned char"
typedef CHAR * STRING ;
Calling a pointer a string is confusing (especially in C++)
I suggest PCHAR instead
typedef CONST STRING CSTRING ;
Reminiscent of "CString" (a string class in Microsoft compilers)
and more obfuscated than "const STRING"
(You haven't defined CONST anywhere, so I can only assume that
you mean "const")
typedef CHAR TCHAR ;


TCHAR is defined in MS Windows (to be "char" if in ascii mode,
or "wchar_t" if in unicode mode). If anyone ever tries to port
your code to MS Windows they will run into this problem.

Note that what you are trying to do has already been done, in a
way that works (if with a few problems) and is understood by
the unwashed masses of MS Windows coders; see if you can
get hold of "tchar.h" and study it or appropriate certain parts of it.
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Svennglenn | last post by:
I'm working on a program that is supposed to save different information to text files. Because the program is in swedish i have to use unicode text for ÅÄÖ letters. When I run the following...
3
by: JSM | last post by:
Hi, I am just trying to port an existing simple encryption routine to C#. this routine simply adds/substracts 10 ascii characters to each character in a text file (except quotes). The routine...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
13
by: Martin Herbert Dietze | last post by:
Hi, I need to calculate the physical length of text in a text input. The term "physical" means in this context, that I consider 7bit-Ascii as one-byte-per character. Other characters may be...
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
29
by: list | last post by:
Hi folks, I am new to Googlegroups. I asked my questions at other forums, since now. I have an important question: I have to check files if they are binary(.bmp, .avi, .jpg) or text(.txt,...
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
4
by: tinkerbarbet | last post by:
Hi I've read around quite a bit about Unicode and python's support for it, and I'm still unclear about how it all fits together in certain scenarios. Can anyone help clarify? * When I say "#...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.