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 >
C/C++ Source or Header  |  1993-05-07  |  6KB  |  133 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* COPYRIGHT:                                                                 */
  4. /* ----------                                                                 */
  5. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  6. /*                                                                            */
  7. /* DISCLAIMER OF WARRANTIES:                                                  */
  8. /* -------------------------                                                  */
  9. /* The following [enclosed] code is sample code created by IBM                */
  10. /* Corporation.  This sample code is not part of any standard IBM product     */
  11. /* and is provided to you solely for the purpose of assisting you in the      */
  12. /* development of your applications.  The code is provided "AS IS",           */
  13. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  14. /* arising out of your use of the sample code, even if they have been         */
  15. /* advised of the possibility of such damages.                                */
  16. /*                                                                            */
  17. /******************************************************************************/
  18. /*-------------------------------------------------------------*\
  19. |  transtab.C  -  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 ebc_code and an asc_code.                                 |
  27. |  For EBCDIC-ASCII translation we want key access with         |
  28. |  ebc_code as key,                                             |
  29. |  for ASCII-EBCDIC translation we want key access with         |
  30. |  asc_code 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. /*-------------------------------------------------------------*\
  58. |  Now we define the two Map templates and two maps.            |
  59. |  We want both of them to be based on the Hashtable KeySet.    |
  60. \*-------------------------------------------------------------*/
  61. #include <imaphks.h>
  62.  
  63. typedef IGMapOnHashKeySet
  64.           < TranslationElement, char,
  65.             KeyOpsTransE2A <TranslationElement, char> >  TransE2AMap;
  66. typedef IGMapOnHashKeySet
  67.           < TranslationElement, char,
  68.             KeyOpsTransA2E <TranslationElement, char> >  TransA2EMap;
  69.  
  70. void display(char*, char*);
  71.  
  72. int main(int argc, char* argv[])  {
  73.  
  74.    TransA2EMap  A2EMap;
  75.    TransE2AMap  E2AMap;
  76.  
  77.        //    char translationtable[256] =  ....
  78. #include <xebc2asc.h>
  79.  
  80.        /*-----------------------------------------------------*\
  81.        |  Load the translation table into both maps.           |
  82.        |  The maps organize themselves according to the key    |
  83.        |  specification already given.                         |
  84.        \*-----------------------------------------------------*/
  85.    for (int i=0; i < 256; i++)
  86.    {
  87.                        /*      asc_code         ebc_code      */
  88.       TranslationElement te(translationtable[i],   i   );
  89.  
  90.       E2AMap.add(te);
  91.       A2EMap.add(te);
  92.    }
  93.        // What do we want to convert now?
  94.    char* to_convert;
  95.    if  (argc > 1)  to_convert = argv[1];
  96.    else            to_convert = "$7  (=Dollar seven)";
  97.  
  98.    size_t text_length = strlen(to_convert) +1;
  99.  
  100.    char* converted_to_asc = new char[text_length];
  101.    char* converted_to_ebc = new char[text_length];
  102.  
  103.        // Convert the strings in place, character by character
  104.    for (i=0; to_convert[i] != 0x00; i++)   {
  105.       converted_to_asc[i]
  106.         = E2AMap.elementWithKey(to_convert[i]).asc_code;
  107.       converted_to_ebc[i]
  108.         = A2EMap.elementWithKey(to_convert[i]).ebc_code;
  109.    }
  110.  
  111.    display("To convert", to_convert);
  112.    display("After EBCDIC-ASCII conversion", converted_to_asc);
  113.    display("After ASCII-EBCDIC conversion", converted_to_ebc);
  114.  
  115.    delete[] converted_to_asc;
  116.    delete[] converted_to_ebc;
  117.  
  118.    return 0;
  119. }
  120.  
  121. #include <iostream.h>
  122. #include <iomanip.h>
  123.  
  124. void display(char* title, char* text)  {
  125.   cout << "\n" << title <<":   \n";
  126.   cout << "  Text: '" << text << "'\n";
  127.   cout << "  Hex:   " << hex;
  128.   for (int i=0; text[i] != 0x00; i++)    {
  129.      cout << (int)text[i] << " ";
  130.   }
  131.   cout << dec << endl;
  132. }
  133.