home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / transtab / transtab.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  5.7 KB  |  132 lines

  1. /**********************************************************************
  2. *                                                                     *
  3. *  IBM(R) VisualAge(TM) for C++ for Windows(R), Version 3.5           *
  4. *                                                                     *
  5. *  PID: 5622-880                                                      *
  6. *  - Licensed Material - Program-Property of IBM                      *
  7. *  (C) Copyright IBM Corp. 1991, 1995 - All Right Reserved.           *
  8. *                                                                     *
  9. *  US Government Users Restricted Rights - Use, duplication or        *
  10. *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.  *
  11. *                                                                     *
  12. *  VisualAge, and IBM are trademarks or registered trademarks of      *
  13. *  International Business Machines Corporation.                       *
  14. *  Windows is a registered trademark of Microsoft Corporation.        *
  15. *                                                                     *
  16. **********************************************************************/
  17.  
  18. /*---------------------------------------------------------------*\
  19. |  transtab.CPP  -  Translation table to demonstrate using a Map  |
  20. |                                                            """  |
  21. |  This example demontrates the use of a Map through a            |
  22. |  bidirectional mapping between the 256 EBCDIC characters and    |
  23. |  the 256 ASCII code points.                                     |
  24. |                                                                 |
  25. |  We build a map with 256 elements, each of which has            |
  26. |  an ebcCode and an ascCode.                                     |
  27. |  For EBCDIC-ASCII translation we want key access with           |
  28. |  ebcCode as key,                                                |
  29. |  for ASCII-EBCDIC translation we want key access with           |
  30. |  ascCode as key.                                                |
  31. |  Therefore this example demonstrates the principle of using     |
  32. |  different keys on the same element type when stored in         |
  33. |  different collections.                                         |
  34. |                                                                 |
  35. |  What you can learn from this example:                          |
  36. |  -------------------------------------                          |
  37. |  - What to do to use the map abstraction.                       |
  38. |  - How to specify the required element functions in two ways:   |
  39. |     1. defining operators for the element to store in the map   |
  40. |     2. defining an operation class that contains                |
  41. |        element and key functions                                |
  42. |  - How to use the same element with different keys in           |
  43. |    different maps.                                              |
  44. |                                                                 |
  45. |  This example does not show the most efficient way of           |
  46. |  implementing an ASCII-EBCDIC translation.                      |
  47. |                                                                 |
  48. \*---------------------------------------------------------------*/
  49.  
  50. #include "transelm.h"
  51.  
  52.        // Get the standard operation classes:
  53. #include <istdops.h>
  54.  
  55. #include "trmapops.h"
  56.  
  57.        //    char const translationTable[256] =  ....
  58. #include "xebc2asc.h"
  59.  
  60. /*-------------------------------------------------------------*\
  61. |  Now we define the two Map templates and two maps.            |
  62. |  We want both of them to be based on the Hashtable KeySet.    |
  63. \*-------------------------------------------------------------*/
  64. #include <imaphsh.h>
  65.  
  66. typedef IGMapAsHshTable
  67.           < TranslationElement, char, TranslationOpsE2A >  TransE2AMap;
  68.  
  69. typedef IGMapAsHshTable
  70.           < TranslationElement, char, TranslationOpsA2E >  TransA2EMap;
  71.  
  72. void display(char*, char*);
  73.  
  74. int main(int argc, char* argv[])  {
  75.  
  76.    TransA2EMap  A2EMap;
  77.    TransE2AMap  E2AMap;
  78.  
  79.        /*-----------------------------------------------------*\
  80.        |  Load the translation table into both maps.           |
  81.        |  The maps organize themselves according to the key    |
  82.        |  specification already given.                         |
  83.        \*-----------------------------------------------------*/
  84.    for (int i=0; i < 256; i++)
  85.    {
  86.                        /*      ascCode          ebcCode      */
  87.       TranslationElement te(translationTable[i],   i   );
  88.  
  89.       E2AMap.add(te);
  90.       A2EMap.add(te);
  91.    }
  92.        // What do we want to convert now?
  93.    char* toConvert;
  94.    if  (argc > 1)  toConvert = argv[1];
  95.    else            toConvert = "$7  (=Dollar seven)";
  96.  
  97.    size_t textLength = strlen(toConvert) +1;
  98.  
  99.    char* convertedToAsc = new char[textLength];
  100.    char* convertedToEbc = new char[textLength];
  101.  
  102.        // Convert the strings in place, character by character
  103.    for (i=0; toConvert[i] != 0x00; i++)   {
  104.       convertedToAsc[i]
  105.         = E2AMap.elementWithKey(toConvert[i]).ascCode ();
  106.       convertedToEbc[i]
  107.         = A2EMap.elementWithKey(toConvert[i]).ebcCode ();
  108.    }
  109.  
  110.    display("To convert", toConvert);
  111.    display("After EBCDIC-ASCII conversion", convertedToAsc);
  112.    display("After ASCII-EBCDIC conversion", convertedToEbc);
  113.  
  114.    delete[] convertedToAsc;
  115.    delete[] convertedToEbc;
  116.  
  117.    return 0;
  118. }
  119.  
  120. #include <iostream.h>
  121. #include <iomanip.h>
  122.  
  123. void display (char* title, char* text)  {
  124.   cout << endl << title << ':' << endl;
  125.   cout << "  Text: '" << text << "'" << endl;
  126.   cout << "  Hex:   " << hex;
  127.   for (int i=0; text[i] != 0x00; i++)    {
  128.      cout << (int)(unsigned char) text[i] << " ";
  129.   }
  130.   cout << dec << endl;
  131. }
  132.