Solved

TCHAR implementation for UNIX?

Posted on 1998-08-29
6
1,867 Views
Last Modified: 2013-12-26
I need to get a Unix-compatible implementation of TCHAR.  Microsoft Visual C++ provides a full TCHAR generic character implementatation.  TCHAR datatypes are conditionally typedef'ed to wchar_t or char, depending on the setting of the -DUNICODE compiler directive.  It deals with the following:

TCHAR -> wchar_t or char
LPTSTR -> LPWSTR or LPSTR
_tcslen -> wcslen or strlen
_T"abc" -> Unicode string "abc" or ANSI string "abc"
tprintf -> wprintf or printf

Is there a UNIX implementation of TCHAR?  I thought it was part of the ANSI standard, and not just a Microsoft idea.

For example, I'd like to make this very simple program 100% portable between NT and UNIX, but retain it's ability to be compiled as implementing Unicode strings or non-Unicode strings based on the #defines at the top.  This needs to be generic across multiple UNIX implementations (let's say Solaris and HP-UX).  Is there a standard UNIX header that does this for me? Is there a 3rd party library that does this?

#define UNICODE
#define _UNICODE

#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>

void main()
{
      TCHAR test[20];
      LPTSTR ptr;
      int n;

      _tcscpy(test,&_T("ABCDEFG"));
      ptr = test;

      n=_tcslen(test);

      _ftprintf(stdout, _T("Text is: %s and length is  %d\n"),test,n);

      return;
}
0
Comment
Question by:tphipps
  • 3
  • 2
6 Comments
 

Author Comment

by:tphipps
ID: 1293049
Edited text of question
0
 
LVL 84

Expert Comment

by:ozo
ID: 1293050
Are you asking about
#include <wchar.h>
?
0
 

Author Comment

by:tphipps
ID: 1293051
Thanks ozo, but <wchar.h> isn't what I'm looking for. It's the wide character support header for Unix, but I'm looking for a generic tchar implementation.  This means that if I code something along the lines of this:

TCHAR abc[10]=_T"abcdefg";
int x;
x=_tcslen(abc);

and then #define UNICODE, it should come out as

wchar_t abc[10]=L"abcdefg";
int x
x=wcslen(abc);

but if I don't #define UNICODE then it should come out as

char abc[10]="abcdefg";
int x;
x=strlen(abc);

It's a pretty simple conversion, but a pain in the ass to write #define headers for given the sheer number of string functions, enclosed text etc.  This is exactly what happens with Visual C++ 2.0 or greater under Win32.  I know that the wchar_t/wcsxxx functions are standard, but how about tchar/tcsxxx?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:Answers2000
ID: 1293052
Look up "Using Generic-Text Mappings" in the MSDev Help, you'll find a lot these are Microsoft Extensions (I'll try and copy the topic but it probably won't format well).

This is not to say another compiler won't either (a) have it's own extensions, or (b) even have something the same as the MS extensions.  But you can't rely on all standard compilers having this funcitonality.

(begin quote)
To simplify code development for various international markets, the Microsoft run-time library provides Microsoft-specific "generic-text" mappings for many data types, routines, and other objects. These mappings are defined in TCHAR.H. You can use these name mappings to write generic code that can be compiled for any of the three kinds of character sets: ASCII (SBCS), MBCS, or Unicode, depending on a manifest constant you define using a #define statement. Generic-text mappings are Microsoft extensions that are not ANSI compatible.

Preprocessor Directives for Generic-Text Mappings
 

 
#define
 Compiled Version
 Example
 

 
_UNICODE
 Unicode (wide-character)
 _tcsrev maps to _wcsrev
 
_MBCS
 Multibyte-character
 _tcsrev maps to _mbsrev
 
None (the default: neither _UNICODE nor _MBCS defined)
 SBCS (ASCII)
 _tcsrev maps to strrev
 




For example, the generic-text function _tcsrev, defined in TCHAR.H, maps to mbsrev if MBCS has been defined in your program, or to _wcsrev if _UNICODE has been defined. Otherwise _tcsrev maps to strrev.

The generic-text data type _TCHAR, also defined in TCHAR.H, maps to type char if _MBCS is defined, to type wchar_t if _UNICODE is defined, and to type char if neither constant is defined. Other data type mappings are provided in TCHAR.H for programming convenience, but _TCHAR is the type that is most useful.

Generic-Text Data Type Mappings
 

 
Generic-Text Data Type Name
 SBCS (_UNICODE, _MBCS Not Defined)
 
_MBCS Defined
 
_UNICODE Defined
 

 
_TCHAR
 char
 char
 wchar_t
 
_TINT
 int
 int
 wint_t
 
_TSCHAR
 signed char
 signed char
 wchar_t
 
_TUCHAR
 unsigned char
 unsigned char
 wchar_t
 
_TXCHAR
 char
 unsigned char
 wchar_t
 
_T or _TEXT
 No effect (removed by preprocessor)
 No effect (removed by preprocessor)
 L (converts following character or string to its Unicode counterpart)
 




For a complete list of generic-text mappings of routines, variables, and other objects, see Appendix B, Generic-Text Mappings.

The following code fragments illustrate the use of _TCHAR and _tcsrev for mapping to the MBCS, Unicode, and SBCS models.




_TCHAR *RetVal, *szString;
RetVal = _tcsrev(szString);
If MBCS has been defined, the preprocessor maps the preceding fragment to the following code:




char *RetVal, *szString;
RetVal = _mbsrev(szString);
If _UNICODE has been defined, the preprocessor maps the same fragment to the following code:




wchar_t *RetVal, *szString;
RetVal = _wcsrev(szString);
If neither _MBCS nor _UNICODE has been defined, the preprocessor maps the fragment to single-byte ASCII code, as follows:




char *RetVal, *szString;
RetVal = strrev(szString);
Thus you can write, maintain, and compile a single source code file to run with routines that are specific to any of the three kinds of character sets.

See Also Generic-text mappings, Data type mappings, Constants and global variable mappings, Routine mappings, A sample generic-text propgram

END Microsoft Specific
(end quote)



0
 

Author Comment

by:tphipps
ID: 1293053
Answers2000, I think you have it.  The key phrase was "Microsoft-specific".  Looks like I'm going to need to write my own TCHAR.H equivalent for my Unix platforms.  Re-sub this as an answer and you have the points.  Thanks!
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1293054
Thanks tphipps

This is a dummy answer.
The real answer was submitted by me at "Date: Sunday, August 30 1998 - 05:02AM PDT".  I won't bother recopying the whole thing as the comment should store in the PAQ.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how do i create updater to My Activex application? 3 79
if loop in java 3 129
while loop over for loop 7 93
Change to event 1 101
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now