home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
SAMPLES
/
ICLCC
/
TRANSTAB.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-07
|
6KB
|
133 lines
/******************************************************************************/
/* */
/* COPYRIGHT: */
/* ---------- */
/* Copyright (C) International Business Machines Corp., 1991,1992. */
/* */
/* DISCLAIMER OF WARRANTIES: */
/* ------------------------- */
/* The following [enclosed] code is sample code created by IBM */
/* Corporation. This sample code is not part of any standard IBM product */
/* and is provided to you solely for the purpose of assisting you in the */
/* development of your applications. The code is provided "AS IS", */
/* without warranty of any kind. IBM shall not be liable for any damages */
/* arising out of your use of the sample code, even if they have been */
/* advised of the possibility of such damages. */
/* */
/******************************************************************************/
/*-------------------------------------------------------------*\
| transtab.C - Translation table to demonstrate using a Map |
| """ |
| This example demontrates the use of a Map through a |
| bidirectional mapping between the 256 EBCDIC characters and |
| the 256 ASCII code points. |
| |
| We build a map with 256 elements, each of which has |
| an ebc_code and an asc_code. |
| For EBCDIC-ASCII translation we want key access with |
| ebc_code as key, |
| for ASCII-EBCDIC translation we want key access with |
| asc_code as key. |
| Therefore this example demonstrates the principle of using |
| different keys on the same element type when stored in |
| different collections. |
| |
| What you can learn from this example: |
| ------------------------------------- |
| - What to do to use the map abstraction. |
| - How to specify the required element functions in two ways: |
| 1. defining operators for the element to store in the map |
| 2. defining an operation class that contains |
| element and key functions |
| - How to use the same element with different keys in |
| different maps. |
| |
| This example does not show the most efficient way of |
| implementing an ASCII-EBCDIC translation. |
| |
\*-------------------------------------------------------------*/
#include "transelm.h"
// Get the standard operation classes:
#include <istdops.h>
#include "trmapops.h"
/*-------------------------------------------------------------*\
| Now we define the two Map templates and two maps. |
| We want both of them to be based on the Hashtable KeySet. |
\*-------------------------------------------------------------*/
#include <imaphks.h>
typedef IGMapOnHashKeySet
< TranslationElement, char,
KeyOpsTransE2A <TranslationElement, char> > TransE2AMap;
typedef IGMapOnHashKeySet
< TranslationElement, char,
KeyOpsTransA2E <TranslationElement, char> > TransA2EMap;
void display(char*, char*);
int main(int argc, char* argv[]) {
TransA2EMap A2EMap;
TransE2AMap E2AMap;
// char translationtable[256] = ....
#include <xebc2asc.h>
/*-----------------------------------------------------*\
| Load the translation table into both maps. |
| The maps organize themselves according to the key |
| specification already given. |
\*-----------------------------------------------------*/
for (int i=0; i < 256; i++)
{
/* asc_code ebc_code */
TranslationElement te(translationtable[i], i );
E2AMap.add(te);
A2EMap.add(te);
}
// What do we want to convert now?
char* to_convert;
if (argc > 1) to_convert = argv[1];
else to_convert = "$7 (=Dollar seven)";
size_t text_length = strlen(to_convert) +1;
char* converted_to_asc = new char[text_length];
char* converted_to_ebc = new char[text_length];
// Convert the strings in place, character by character
for (i=0; to_convert[i] != 0x00; i++) {
converted_to_asc[i]
= E2AMap.elementWithKey(to_convert[i]).asc_code;
converted_to_ebc[i]
= A2EMap.elementWithKey(to_convert[i]).ebc_code;
}
display("To convert", to_convert);
display("After EBCDIC-ASCII conversion", converted_to_asc);
display("After ASCII-EBCDIC conversion", converted_to_ebc);
delete[] converted_to_asc;
delete[] converted_to_ebc;
return 0;
}
#include <iostream.h>
#include <iomanip.h>
void display(char* title, char* text) {
cout << "\n" << title <<": \n";
cout << " Text: '" << text << "'\n";
cout << " Hex: " << hex;
for (int i=0; text[i] != 0x00; i++) {
cout << (int)text[i] << " ";
}
cout << dec << endl;
}