home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / data / convert / convert.cpp next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  3.7 KB  |  123 lines

  1. //************************************************************
  2. // Data Types - Using the IString Conversion Functions 
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //
  8. //
  9. // Usage:      convert <conversionfunction> <string>
  10. //
  11. //************************************************************
  12. #include <istring.hpp>
  13. #include <stream.h>
  14.  
  15. typedef IString String;
  16.  
  17. typedef String& (String::*pStringConversionMember)();
  18. typedef String (*pStringConversionFunction)(const String &);
  19.  
  20. // Array of IString conversion functions that operate on the
  21. // calling string.
  22. static pStringConversionMember
  23.   members[] = { String::b2c,
  24.                 String::b2d,
  25.                 String::b2x,
  26.                 String::c2b,
  27.                 String::c2d,
  28.                 String::c2x,
  29.                 String::d2b,
  30.                 String::d2c,
  31.                 String::d2x,
  32.                 String::x2b,
  33.                 String::x2c,
  34.                 String::x2d };
  35.  
  36. // Array of IString static conversion functions that return a new string.
  37. static pStringConversionFunction
  38.   functions[] = { IString::b2c,
  39.                   IString::b2d,
  40.                   IString::b2x,
  41.                   IString::c2b,
  42.                   IString::c2d,
  43.                   IString::c2x,
  44.                   IString::d2b,
  45.                   IString::d2c,
  46.                   IString::d2x,
  47.                   IString::x2b,
  48.                   IString::x2c,
  49.                   IString::x2d };
  50.  
  51. // Array of command-line options.
  52. static String
  53.   options( "b2c "
  54.            "b2d "
  55.            "b2x "
  56.            "c2b "
  57.            "c2d "
  58.            "c2x "
  59.            "d2b "
  60.            "d2c "
  61.            "d2x "
  62.            "x2b "
  63.            "x2c "
  64.            "x2d " );
  65.  
  66. int main( int argc, char *argv[] )
  67.   {
  68.   if ( argc == 3 )
  69.     {
  70.     // Get conversion function name.
  71.     String opt( argv[1] );
  72.  
  73.     // Get string to be converted.
  74.     String arg( argv[2] );
  75.  
  76.     // Get the index of the conversion function in the options array.
  77.     // Note that indexing begins at one and 0 is returned if no
  78.     // conversion function is matched.
  79.     unsigned int i = options.wordIndexOfPhrase( opt );
  80.  
  81.     // Perform the conversions and print the results.
  82.     if ( i )
  83.       {
  84.       String opt = options.word(i--); // Note the index must be
  85.                                       // decremented for
  86.                                       // the function call.
  87.  
  88.       String input  = arg;
  89.       String result = functions[i](arg); // Call the static function to
  90.                                          // return a new string.
  91.  
  92.       // Print the results.
  93.       cout << opt << "(\"" << input << "\")   \t=\t" << result << "\n";
  94.       cout << "(\"" << input << "\")." << opt << "()\t=\t";
  95.  
  96.       // Call the nonstatic function to operate on the string itself and
  97.       // print the results.
  98.       (input.*members[i])();
  99.       cout << input << "\n";
  100.  
  101.       // Reverse the given option (for example, from b2x to x2b).
  102.       opt.reverse();
  103.       i = options.wordIndexOfPhrase( opt ) - 1;
  104.       result = functions[i](input);
  105.       cout << opt << "(\"" << input << "\")   \t=\t" << result << "\n";
  106.       cout << "(\"" << input << "\")." << opt << "()\t=\t";
  107.       (input.*members[i])();
  108.       cout << input << "\n";
  109.       if ( result != arg || result != input )
  110.          cout << "Something is wrong with this!\a\n";
  111.       else
  112.          cout << "This seemed to work OK!\n";
  113.       }
  114.     else
  115.       cout << "Invalid conversion function\nChoose one of:\n\t"
  116.            << options << '\n';
  117.     }
  118.   else
  119.     cout << "Invalid input\nSyntax is:   convert opt input\n";
  120.  
  121.   return 0;
  122.   }
  123.