home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / stl.exe / codecvte.h < prev    next >
C/C++ Source or Header  |  1998-02-09  |  7KB  |  230 lines

  1. /**************************************************************************
  2.  *
  3.  * codecvte.h - Example of user defined codecvt facet. 
  4.  *               See Class Reference Section
  5.  *
  6.  * $Id: codecvte.h,v 1.2 1996/09/28 00:06:02 smithey Exp $
  7.  *
  8.  ***************************************************************************
  9.  *
  10.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  11.  * ALL RIGHTS RESERVED *
  12.  * The software and information contained herein are proprietary to, and
  13.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  14.  * intends to preserve as trade secrets such software and information.
  15.  * This software is furnished pursuant to a written license agreement and
  16.  * may be used, copied, transmitted, and stored only in accordance with
  17.  * the terms of such license and with the inclusion of the above copyright
  18.  * notice.  This software and information or any other copies thereof may
  19.  * not be provided or otherwise made available to any other person.
  20.  *
  21.  * Notwithstanding any other lease or license that may pertain to, or
  22.  * accompany the delivery of, this computer software and information, the
  23.  * rights of the Government regarding its use, reproduction and disclosure
  24.  * are as set forth in Section 52.227-19 of the FARS Computer
  25.  * Software-Restricted Rights clause.
  26.  * 
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  29.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  30.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  31.  * P.O. Box 2328, Corvallis, Oregon 97339.
  32.  *
  33.  * This computer software and information is distributed with "restricted
  34.  * rights."  Use, duplication or disclosure is subject to restrictions as
  35.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  36.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  37.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  38.  * then the "Alternate III" clause applies.
  39.  *
  40.  **************************************************************************/
  41.  
  42. #include <locale>
  43. #include <strstream>
  44. #include <functional>
  45. #include <algorithm>
  46.  
  47. #ifndef _RWSTD_NO_NAMESPACE
  48. using namespace std;
  49. #endif
  50.  
  51. #define RWSTD_TABLE_SIZE 59
  52. // 
  53. // This facet performs a conversion from  Latin Alphabet No. 1 
  54. // (ISO 8859-1) to U.S.  ASCII code page 437.  Some conversions are one
  55. // way (from ISO to ASCII, but not back again) because this ASCII 
  56. // code page has no equivilent to the ISO character.
  57. //
  58. class ex_codecvt : public codecvt<char,char,mbstate_t>
  59. {
  60. private:
  61.     static char table_[RWSTD_TABLE_SIZE][3]; 
  62.                            
  63. protected:
  64.   virtual result do_in(mbstate_t&,
  65.       const char* from, const char* from_end, const char*& from_next,
  66.       char* to, char* to_limit, char*& to_next) const
  67.   {  
  68.     bool match;
  69.     int i = min(to_limit-to,from_end-from);
  70.     from_next = from;
  71.     to_next = to;
  72.     for (int j =0; j < i; j++)
  73.     {
  74.       match = false;
  75.       for (int k = 0; k < RWSTD_TABLE_SIZE; k++)
  76.       {  
  77.         if (*from_next >= table_[k][0] && 
  78.             *from_next <= table_[k][1])
  79.         {
  80.           *to_next = table_[k][2];
  81.           match = true;
  82.           break;
  83.         }
  84.       }
  85.       if (!match)
  86.         *to_next = *from_next;
  87.       from_next++;
  88.       to_next++;
  89.     }
  90.     return ok;
  91.   }
  92.  
  93.   virtual result do_out(mbstate_t&,
  94.     const char* from, const char* from_end, const char*& from_next,
  95.     char* to, char* to_limit, char*& to_next) const
  96.   {
  97.     bool match;
  98.     int i = min(to_limit-to,from_end-from);
  99.     from_next = from;
  100.     to_next = to;
  101.     for (int j =0; j < i; j++)
  102.     {
  103.       match = false;
  104.       for (int k = 0; k < RWSTD_TABLE_SIZE; k++)
  105.       {  
  106.         if (*from_next == table_[k][2] && 
  107.             table_[k][0] == table_[k][1])
  108.         {
  109.           *to_next = table_[k][1];
  110.           match = true;
  111.           break;
  112.         }
  113.       }
  114.       if (!match)
  115.         *to_next = *from_next;
  116.       from_next++;
  117.       to_next++;
  118.     }
  119.     return ok;
  120.   }
  121.  
  122.   virtual bool do_always_noconv() const _RWSTD_THROW_SPEC_NULL
  123.   { return false; }
  124.  
  125.   virtual int do_encoding() const _RWSTD_THROW_SPEC_NULL
  126.   { return 1; }
  127.  
  128.   virtual int do_length (const mbstate_t&, const char* from,
  129.                          const char* end, size_t max) const
  130.   { return min((size_t)(end - from),max); }
  131.  
  132.   virtual int do_max_length() const _RWSTD_THROW_SPEC_NULL
  133.   { return INT_MAX; }
  134.  
  135. };
  136.  
  137. char ex_codecvt::table_[RWSTD_TABLE_SIZE][3] =
  138.                              { 0xa2, 0xa2, 0x9b,
  139.                                0xa3, 0xa3, 0x9c,
  140.                                0xa5, 0xa5, 0x9d,
  141.                                0xa7, 0xa7, 0x15,
  142.                                0xa8, 0xa8, 0x22,
  143.                                0xaa, 0xaa, 0xa6,
  144.                                0xab, 0xab, 0xae,
  145.                                0xb5, 0xb5, 0xe6,
  146.                                0xb6, 0xb6, 0x14,
  147.                                0xb7, 0xb7, 0xfa,
  148.                                0xba, 0xba, 0xa7,
  149.                                0xbb, 0xbb, 0xaf,
  150.                                0xbc, 0xbc, 0xac,
  151.                                0xbd, 0xbd, 0xab,
  152.                                0xbf, 0xbf, 0xa8,
  153.                                0xc0, 0xc3, 0x41,
  154.                                0xc4, 0xc4, 0x8e,
  155.                                0xc5, 0xc5, 0x41,
  156.                                0xc6, 0xc6, 0x92,
  157.                                0xc7, 0xc7, 0x80,
  158.                                0xc8, 0xc8, 0x45,
  159.                                0xc9, 0xc9, 0x90,
  160.                                0xca, 0xcb, 0x45,
  161.                                0xcc, 0xcf, 0x49,
  162.                                0xd1, 0xd1, 0xa5,
  163.                                0xd2, 0xd5, 0x4f,
  164.                                0xd6, 0xd6, 0x99,
  165.                                0xd8, 0xd8, 0xed,
  166.                                0xd9, 0xdb, 0x55,
  167.                                0xdc, 0xdc, 0x9a,
  168.                                0xdd, 0xdd, 0x59,
  169.                                0xdf, 0xdf, 0xe1,
  170.                                0xe0, 0xe0, 0x85,
  171.                                0xe1, 0xe1, 0xa0,
  172.                                0xe2, 0xe2, 0x83,
  173.                                0xe3, 0xe3, 0x61,
  174.                                0xe4, 0xe4, 0x84,
  175.                                0xe5, 0xe5, 0x86,
  176.                                0xe6, 0xe6, 0x91,
  177.                                0xe7, 0xe7, 0x87,
  178.                                0xe8, 0xe8, 0x8a,
  179.                                0xe9, 0xe9, 0x82,
  180.                                0xea, 0xea, 0x88,
  181.                                0xeb, 0xeb, 0x89,
  182.                                0xec, 0xec, 0x8d,
  183.                                0xed, 0xed, 0xa1,
  184.                                0xee, 0xee, 0x8c,
  185.                                0xef, 0xef, 0x8b,
  186.                                0xf1, 0xf1, 0xa4,
  187.                                0xf2, 0xf2, 0x95,
  188.                                0xf3, 0xf3, 0xa2,
  189.                                0xf4, 0xf4, 0x93,
  190.                                0xf5, 0xf5, 0x6f,
  191.                                0xf6, 0xf6, 0x94,
  192.                                0xf9, 0xf9, 0x97,
  193.                                0xfa, 0xfa, 0xa3,
  194.                                0xfb, 0xfb, 0x96,
  195.                                0xfc, 0xfc, 0x81,
  196.                                0xff, 0xff, 0x98 };
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.