Usage example

The design point for ASCII/EBCDIC Mixed Mode support for Enhanced ASCII was to make it possible for C++ class library and DLL developers to develop a common mixed mode version of their library instead of producing separate ASCII and EBCDIC versions. The class library or DLL developer can accomplish this by calling explicit ASCII and EBCDIC versions of C-RTL functions based on the results of a call to __isASCII(). __isASCII() is used to determine the character mode of the user application. It is assumed that the end user application is not bimodal. A simple ASCII/EBCDIC bimodal "Hello World!" program shows how a bimodal class library or DLL can be produced. For this example, it is assumed that the user application (the code containing main()) is compiled ASCII while the bimodal code, which is contained in a separate compile unit, is compiled EBCDIC.

#include <stdio.h>
void printItOut(const char *, const char *);

void main(void) {
   printItOut("%s\n", "Hello World!");

Assuming the preceding code was compiled using the ASCII compile option, the C/C++ Compiler will generate values for the characters in the format string and the "Hello World!\n" string in the ISO8859-1 code page. A separate compile unit contains the bimodal printItOut() function, as shown below:

#define _AE_BIMODAL 1
     #include <stdio.h>
     #include <_Nascii.h>

     void printItOut(const char *format, const char *string {
        if (__isASCII())
           __printf_a(format, string);
           __printf_e(format, string);

In the example, the format and string arguments passed on the call to printItOut will be in the ISO8859-1 code page. __isASCII() returns the character mode of the current thread. In this example, the character mode of the initial processing thread is ASCII. This was set during C-RTL initialization since the compile unit containing main() was compiled ASCII.

Since __isASCII() returns the value one, __printf_a() is called, passing along the format and string arguments. The format and string arguments are encoded using ISO8859-1. Since the code in our ASCII/EBCDIC Bimodal part was compiled XPLINK and _AE_BIMODAL is defined, the __printf_a() function call is pragma mapped by stdio.h to be \174\174A00118, which is the Enhanced ASCII version of the printf() function. Hello World! in ISO8859-1 will be sent to stdout. By default, stdout is assumed EBCDIC and the Hello World! string will show up on stdout as unreadable characters. The Hello World! string will show up legibly on stdout if the application is being run with auto conversion on or the output of the "Hello World!" program is piped into iconv as follows:
hellow 2 >&1 | iconv -f ISO8859-1 -t IBM-1047