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

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * codecvt.cpp - Example program of codecvt facet. 
  6.  *               See Class Reference Section
  7.  *
  8.  * $Id: codecvt.cpp,v 1.1 1996/09/26 06:33:35 smithey Exp $
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  13.  * ALL RIGHTS RESERVED *
  14.  * The software and information contained herein are proprietary to, and
  15.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  16.  * intends to preserve as trade secrets such software and information.
  17.  * This software is furnished pursuant to a written license agreement and
  18.  * may be used, copied, transmitted, and stored only in accordance with
  19.  * the terms of such license and with the inclusion of the above copyright
  20.  * notice.  This software and information or any other copies thereof may
  21.  * not be provided or otherwise made available to any other person.
  22.  *
  23.  * Notwithstanding any other lease or license that may pertain to, or
  24.  * accompany the delivery of, this computer software and information, the
  25.  * rights of the Government regarding its use, reproduction and disclosure
  26.  * are as set forth in Section 52.227-19 of the FARS Computer
  27.  * Software-Restricted Rights clause.
  28.  * 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  31.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  32.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  33.  * P.O. Box 2328, Corvallis, Oregon 97339.
  34.  *
  35.  * This computer software and information is distributed with "restricted
  36.  * rights."  Use, duplication or disclosure is subject to restrictions as
  37.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  38.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  39.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  40.  * then the "Alternate III" clause applies.
  41.  *
  42.  **************************************************************************/
  43.  
  44. #include <sstream>
  45. #include "codecvte.h"
  46.  
  47. int main ()
  48. {
  49. #ifndef _RWSTD_NO_NAMESPACE
  50.   using namespace std;
  51. #endif
  52.  
  53.   mbstate_t state;
  54.  
  55.   // three strings to use as buffers
  56.   string ins("\xfc \xcc \xcd \x61 \xe1 \xd9 \xc6 \xe6 \xf5");
  57.   string ins2(ins.size(),'.');
  58.   string outs(ins.size()/ex_codecvt().encoding(),'.');
  59.  
  60.   // Print initial contents of buffers
  61.   cout << "Before:\n" << ins << endl;
  62.   cout << ins2 << endl;
  63.   cout << outs << endl << endl;
  64.  
  65.   // Initialize buffers
  66.   string::iterator in_it = ins.begin();
  67.   string::iterator out_it = outs.begin();
  68.   string::const_iterator const_in_it = ins.begin();
  69.   string::const_iterator const_out_it = outs.begin();
  70.  
  71.   // Create a user defined codecvt fact
  72.   // This facet converst from ISO Latin 
  73.   // Alphabet No. 1 (ISO 8859-1) to 
  74.   // U.S. ASCII code page 437
  75.   // This facet replaces the default for
  76.   // codecvt<char,char,mbstate_t>
  77.   locale loc(locale(),new ex_codecvt);
  78.  
  79.   // Now get the facet from the locale 
  80.   const codecvt<char,char,mbstate_t>& cdcvt = 
  81. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  82.     use_facet<codecvt<char,char,mbstate_t> >(loc);
  83. #else
  84.     use_facet(loc,(codecvt<char,char,mbstate_t>*)0);
  85. #endif
  86.  
  87.   // convert the buffer
  88.   cdcvt.in(state,ins.begin(),ins.end(),const_in_it,
  89.            outs.begin(),outs.end(),out_it);
  90.     
  91.   cout << "After in:\n" << ins << endl;
  92.   cout << ins2 << endl;
  93.   cout << outs << endl << endl;
  94.  
  95.   // Lastly, convert back to the original codeset
  96.   in_it = ins.begin();
  97.   out_it = outs.begin();
  98.   cdcvt.out(state, outs.begin(),outs.end(),const_out_it,
  99.             ins2.begin(),ins2.end(),in_it);
  100.     
  101.   cout << "After out:\n" << ins << endl;
  102.   cout << ins2 << endl;
  103.   cout << outs << endl;
  104.  
  105.   return 0;
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.