home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedEncoding.Cab / F112927_MemoryEncodeDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-11  |  23.0 KB  |  747 lines

  1. // Xceed Binary Encoding Library - Memory Encode sample
  2. // Copyright (c) 2001 Xceed Software Inc.
  3. //
  4. // [MemoryEncodeDlg.cpp]
  5. //
  6. // This form module contains the main form's code. It demonstrates how to
  7. // encode a chunk of memory data using different kinds of encoding methods,
  8. // and decode an encoded memory data. It specifically uses:
  9. //  - The Encode and Decode method.
  10. //  - The ContinueOnInvalidData, IncludeHeaderFooter, EndOfLineType, MaxLineLength,
  11. //    HeaderDataForkLength, HeaderResourceForkLength and EncodingFormat propeties.
  12. //
  13. // This file is part of the Xceed Binary Encoding Library sample applications.
  14. // The source code in this file is only intended as a supplement to Xceed
  15. // Binary Encoding Library's documentation, and is provided "as is", without
  16. // warranty of any kind, either expressed or implied.
  17.  
  18.  
  19. #include "stdafx.h"
  20. #include "MemoryEncode.h"
  21. #include "MemoryEncodeDlg.h"
  22.  
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CAboutDlg dialog used for App About
  31.  
  32. class CAboutDlg : public CDialog
  33. {
  34. public:
  35.     CAboutDlg();
  36.  
  37. // Dialog Data
  38.     //{{AFX_DATA(CAboutDlg)
  39.     enum { IDD = IDD_ABOUTBOX };
  40.     //}}AFX_DATA
  41.  
  42.     // ClassWizard generated virtual function overrides
  43.     //{{AFX_VIRTUAL(CAboutDlg)
  44.     protected:
  45.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  46.     //}}AFX_VIRTUAL
  47.  
  48. // Implementation
  49. protected:
  50.     //{{AFX_MSG(CAboutDlg)
  51.     //}}AFX_MSG
  52.     DECLARE_MESSAGE_MAP()
  53. };
  54.  
  55. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  56. {
  57.     //{{AFX_DATA_INIT(CAboutDlg)
  58.     //}}AFX_DATA_INIT
  59. }
  60.  
  61. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63.     CDialog::DoDataExchange(pDX);
  64.     //{{AFX_DATA_MAP(CAboutDlg)
  65.     //}}AFX_DATA_MAP
  66. }
  67.  
  68. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  69.     //{{AFX_MSG_MAP(CAboutDlg)
  70.         // No message handlers
  71.     //}}AFX_MSG_MAP
  72. END_MESSAGE_MAP()
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CMemoryEncodeDlg dialog
  76.  
  77. CMemoryEncodeDlg::CMemoryEncodeDlg(CWnd* pParent /*=NULL*/)
  78.     : CDialog(CMemoryEncodeDlg::IDD, pParent)
  79. {
  80.     //{{AFX_DATA_INIT(CMemoryEncodeDlg)
  81.     m_lEncodingMethod = -1;
  82.     m_lEOLType = -1;
  83.     m_lMaxLineLength = 0;
  84.     m_sDecodedText = _T("");
  85.     m_sEncodedText = _T("");
  86.     m_sDecodedSize = _T("");
  87.     m_sEncodedSize = _T("");
  88.     //}}AFX_DATA_INIT
  89.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  90.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  91. }
  92.  
  93. void CMemoryEncodeDlg::DoDataExchange(CDataExchange* pDX)
  94. {
  95.     CDialog::DoDataExchange(pDX);
  96.     //{{AFX_DATA_MAP(CMemoryEncodeDlg)
  97.     DDX_CBIndex(pDX, IDC_CBO_ENCODINGMETHOD, m_lEncodingMethod);
  98.     DDX_CBIndex(pDX, IDC_CBO_EOLTYPE, m_lEOLType);
  99.     DDX_Control(pDX, IDC_CBO_EOLTYPE, m_cboEOLType);
  100.     DDX_Control(pDX, IDC_CBO_ENCODINGMETHOD, m_cboEncodingMethod);
  101.     DDX_Text(pDX, IDC_TXT_MAXLINELENGTH, m_lMaxLineLength);
  102.     DDX_Text(pDX, IDC_TXT_DECODEDTEXT, m_sDecodedText);
  103.     DDX_Text(pDX, IDC_TXT_ENCODEDTEXT, m_sEncodedText);
  104.     DDX_Text(pDX, IDC_LBL_DECODEDSIZE, m_sDecodedSize);
  105.     DDX_Text(pDX, IDC_LBL_ENCODEDSIZE, m_sEncodedSize);
  106.     //}}AFX_DATA_MAP
  107. }
  108.  
  109. BEGIN_MESSAGE_MAP(CMemoryEncodeDlg, CDialog)
  110.     //{{AFX_MSG_MAP(CMemoryEncodeDlg)
  111.     ON_WM_SYSCOMMAND()
  112.     ON_WM_PAINT()
  113.     ON_WM_QUERYDRAGICON()
  114.     ON_BN_CLICKED(IDC_CMD_ENCODE, OnCmdEncode)
  115.     ON_BN_CLICKED(IDQUIT, OnQuit)
  116.     ON_EN_CHANGE(IDC_TXT_DECODEDTEXT, OnChangeTxtDecodedtext)
  117.     ON_EN_CHANGE(IDC_TXT_ENCODEDTEXT, OnChangeTxtEncodedtext)
  118.     ON_EN_SETFOCUS(IDC_TXT_DECODEDTEXT, OnSetfocusTxtDecodedtext)
  119.     ON_EN_SETFOCUS(IDC_TXT_ENCODEDTEXT, OnSetfocusTxtEncodedtext)
  120.     ON_BN_CLICKED(IDC_CMD_DECODE, OnCmdDecode)
  121.     //}}AFX_MSG_MAP
  122. END_MESSAGE_MAP()
  123.  
  124. /////////////////////////////////////////////////////////////////////////////
  125. // CMemoryEncodeDlg message handlers
  126.  
  127. BOOL CMemoryEncodeDlg::OnInitDialog()
  128. {
  129.   //
  130.   // Set default values for the text to encode and the MaxLineLength text box
  131.   //
  132.   m_sDecodedText = "This is a little test to show you how the memory encoding works. \r\n"
  133.                    "And it is very easy to use.";
  134.   m_lMaxLineLength = 78;
  135.  
  136.     CDialog::OnInitDialog();
  137.  
  138.     // Add "About..." menu item to system menu.
  139.  
  140.     // IDM_ABOUTBOX must be in the system command range.
  141.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  142.     ASSERT(IDM_ABOUTBOX < 0xF000);
  143.  
  144.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  145.     if (pSysMenu != NULL)
  146.     {
  147.         CString strAboutMenu;
  148.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  149.         if (!strAboutMenu.IsEmpty())
  150.         {
  151.             pSysMenu->AppendMenu(MF_SEPARATOR);
  152.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  153.         }
  154.     }
  155.  
  156.     // Set the icon for this dialog.  The framework does this automatically
  157.     //  when the application's main window is not a dialog
  158.     SetIcon(m_hIcon, TRUE);            // Set big icon
  159.     SetIcon(m_hIcon, FALSE);        // Set small icon
  160.     
  161.   //----------------------------------------------------------------------------------
  162.   // Initialize the size labels and fill the 2 combo boxes
  163.   //----------------------------------------------------------------------------------
  164.  
  165.   // Fill the combo boxes
  166.     m_cboEncodingMethod.AddString( "UU encoding" );
  167.     m_cboEncodingMethod.AddString( "XX encoding" );
  168.     m_cboEncodingMethod.AddString( "Hexadecimal" );
  169.     m_cboEncodingMethod.AddString( "Base 64" );
  170.     m_cboEncodingMethod.AddString( "Quoted printable" );
  171.     m_cboEncodingMethod.AddString( "BinHex" );
  172.  
  173.   m_cboEncodingMethod.SetItemData( 0, emUUEncode );
  174.   m_cboEncodingMethod.SetItemData( 1, emXXEncode );
  175.   m_cboEncodingMethod.SetItemData( 2, emHexadecimal );
  176.   m_cboEncodingMethod.SetItemData( 3, emBase64 );
  177.   m_cboEncodingMethod.SetItemData( 4, emQuoted );
  178.   m_cboEncodingMethod.SetItemData( 5, emBinHex );
  179.  
  180.   m_cboEncodingMethod.SetCurSel( emUUEncode );
  181.  
  182.   m_cboEOLType.AddString( "None" );
  183.   m_cboEOLType.AddString( "Carriage return/line feed" );
  184.   m_cboEOLType.AddString( "Line feed" );
  185.   m_cboEOLType.AddString( "Carriage return" );
  186.  
  187.   m_cboEOLType.SetItemData( 0, ltNone );
  188.   m_cboEOLType.SetItemData( 1, ltCrLf );
  189.   m_cboEOLType.SetItemData( 2, ltLf );
  190.   m_cboEOLType.SetItemData( 3, ltCr );
  191.  
  192.   m_cboEOLType.SetCurSel( ltNone );
  193.  
  194.   OnChangeTxtDecodedtext();
  195.   OnChangeTxtEncodedtext();
  196.  
  197.     return TRUE;  // return TRUE  unless you set the focus to a control
  198. }
  199.  
  200. void CMemoryEncodeDlg::OnSysCommand(UINT nID, LPARAM lParam)
  201. {
  202.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  203.     {
  204.         CAboutDlg dlgAbout;
  205.         dlgAbout.DoModal();
  206.     }
  207.     else
  208.     {
  209.         CDialog::OnSysCommand(nID, lParam);
  210.     }
  211. }
  212.  
  213. // If you add a minimize button to your dialog, you will need the code below
  214. //  to draw the icon.  For MFC applications using the document/view model,
  215. //  this is automatically done for you by the framework.
  216.  
  217. void CMemoryEncodeDlg::OnPaint() 
  218. {
  219.     if (IsIconic())
  220.     {
  221.         CPaintDC dc(this); // device context for painting
  222.  
  223.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  224.  
  225.         // Center icon in client rectangle
  226.         int cxIcon = GetSystemMetrics(SM_CXICON);
  227.         int cyIcon = GetSystemMetrics(SM_CYICON);
  228.         CRect rect;
  229.         GetClientRect(&rect);
  230.         int x = (rect.Width() - cxIcon + 1) / 2;
  231.         int y = (rect.Height() - cyIcon + 1) / 2;
  232.  
  233.         // Draw the icon
  234.         dc.DrawIcon(x, y, m_hIcon);
  235.     }
  236.     else
  237.     {
  238.         CDialog::OnPaint();
  239.     }
  240. }
  241.  
  242. // The system calls this to obtain the cursor to display while the user drags
  243. //  the minimized window.
  244. HCURSOR CMemoryEncodeDlg::OnQueryDragIcon()
  245. {
  246.     return (HCURSOR) m_hIcon;
  247. }
  248.  
  249. //====================================================================================
  250. //
  251. // EVENTS - triggered by the form and its controls
  252. //
  253. //====================================================================================
  254.  
  255. //------------------------------------------------------------------------------------
  256. // Update the decoded size label when the user modify the decoded text box content
  257. //------------------------------------------------------------------------------------
  258. void CMemoryEncodeDlg::OnChangeTxtDecodedtext() 
  259. {
  260.   if( UpdateData( TRUE ) )
  261.   {
  262.     m_sDecodedSize.Format( "%d", m_sDecodedText.GetLength() );
  263.     UpdateData( FALSE );
  264.   }
  265. }
  266.  
  267. //------------------------------------------------------------------------------------
  268. // Update the encoded size label when the user modify the encoded text box content
  269. //------------------------------------------------------------------------------------
  270. void CMemoryEncodeDlg::OnChangeTxtEncodedtext() 
  271. {
  272.     if( UpdateData( TRUE ) )
  273.   {
  274.     m_sEncodedSize.Format( "%d", m_sEncodedText.GetLength() );
  275.     UpdateData( FALSE );
  276.   }
  277. }
  278.  
  279. void CMemoryEncodeDlg::OnSetfocusTxtDecodedtext() 
  280. {
  281.   SetDefID( IDC_CMD_ENCODE );
  282. }
  283.  
  284. void CMemoryEncodeDlg::OnSetfocusTxtEncodedtext() 
  285. {
  286.   SetDefID( IDC_CMD_DECODE );
  287. }
  288.  
  289. //------------------------------------------------------------------------------------
  290. // Quit the sample application
  291. //------------------------------------------------------------------------------------
  292. void CMemoryEncodeDlg::OnQuit() 
  293. {
  294.   EndDialog( IDOK );
  295. }
  296.  
  297. //------------------------------------------------------------------------------------
  298. // Do the encoding of the decoded text
  299. //------------------------------------------------------------------------------------
  300. void CMemoryEncodeDlg::OnCmdEncode() 
  301. {
  302.   if( UpdateData( TRUE ) )
  303.   {
  304.     CWaitCursor xWaitCursor;
  305.  
  306.     COleException* pExcept = new COleException;
  307.     DXceedBinaryEncoding xEncoder;
  308.     COleVariant vaEncoded;
  309.     COleVariant vaToEncode;
  310.  
  311.     try
  312.     {
  313.       // Create an instance of the XceedBinaryEncoding class
  314.       if( !xEncoder.CreateDispatch( "Xceed.BinaryEncoding", pExcept ) )
  315.       {
  316.         throw pExcept;
  317.       }
  318.  
  319.       // Create and prepare the encoding format (XX, UU, BinHex, ...)
  320.       // This function can throw an exception that will be caught here.
  321.       PrepareEncodingFormat( xEncoder );
  322.  
  323.       // Store in the variant to encode the content of the text box 
  324.       // as an ANSI String. We could have used a BSTR representation
  325.       // of the string but it would have unnecessarily double the string size.
  326.       // The Variant will report containing a VT_BSTR, but the actual data and 
  327.       // size will reflect the Ansi string format.
  328.       vaToEncode.SetString( m_sDecodedText, VT_BSTRT );
  329.  
  330.       // Encode the string in a single call (bEndOfData is TRUE).
  331.       vaEncoded = xEncoder.Encode( vaToEncode, TRUE );
  332.  
  333.       // We assign an empty string as a default encoded value.
  334.       m_sEncodedText = "";
  335.  
  336.       // We check that the encoded variant is really a byte array. For instance,
  337.       // it won't be if there was no characters to encode (empty string).
  338.       if( vaEncoded.vt == ( VT_ARRAY | VT_UI1 ) )
  339.       {
  340.         LONG lHigh;
  341.         LONG lLow;
  342.  
  343.         // We extracts the upper bound and the lower bound of the
  344.         // byte array.
  345.         SafeArrayGetUBound( vaEncoded.parray, 1, &lHigh );
  346.         SafeArrayGetLBound( vaEncoded.parray, 1, &lLow );
  347.  
  348.         // This pointer will contain the address of the byte array
  349.         char* pszEncoded = NULL;
  350.  
  351.         // We get a pointer on the actual data in the safe array.
  352.         SafeArrayAccessData( vaEncoded.parray, (void**)&pszEncoded );
  353.  
  354.         // We set the encoded string to the variable associated to the
  355.         // encoded text box, specifying the length of the encoded string
  356.         // using the upper and lower bound.
  357.         m_sEncodedText = CString( pszEncoded, lHigh - lLow + 1 );
  358.  
  359.         // We release the lock on the safe array
  360.         SafeArrayUnaccessData( vaEncoded.parray );
  361.       }
  362.  
  363.       // Update mainly the encoded text box
  364.       UpdateData( FALSE );
  365.     }
  366.     catch( COleDispatchException* pEx )
  367.     {
  368.       pEx->ReportError();
  369.       pEx->Delete();
  370.     }
  371.     catch( COleException* pEx )
  372.     {
  373.       pEx->ReportError();
  374.       pEx->Delete();
  375.     }
  376.     catch( CException* pEx )
  377.     {
  378.       pEx->ReportError();
  379.       pEx->Delete();
  380.     }
  381.  
  382.     pExcept->Delete();
  383.  
  384.     //Update the encoded text size label
  385.     OnChangeTxtEncodedtext();
  386.   }
  387. }
  388.  
  389. //------------------------------------------------------------------------------------
  390. // Do the decoding of the encoded text
  391. //------------------------------------------------------------------------------------
  392. void CMemoryEncodeDlg::OnCmdDecode() 
  393. {
  394.   if( UpdateData( TRUE ) )
  395.   {
  396.     CWaitCursor xWaitCursor;
  397.  
  398.     COleException* pEx = new COleException;
  399.     DXceedBinaryEncoding xEncoder;
  400.     COleVariant vaEncoded;
  401.     COleVariant vaDecoded;
  402.  
  403.     try
  404.     {
  405.       // Create an instance of the XceedBinaryEncoding class
  406.       if( !xEncoder.CreateDispatch( "Xceed.BinaryEncoding", pEx ) )
  407.       {
  408.         throw pEx;
  409.       }
  410.  
  411.       // Create and prepare the encoding format (XX, UU, BinHex, ...)
  412.       // This function can throw an exception that will be caught here.
  413.       PrepareDecodingFormat( xEncoder );
  414.  
  415.       // Store in the variant to decode the content of the text box 
  416.       // as an ANSI String. We could have used a BSTR representation
  417.       // of the string but it would have unnecessarily double the string size.
  418.       // The Variant will report containing a VT_BSTR, but the actual data and 
  419.       // size will reflect the Ansi string format.
  420.       vaEncoded.SetString( m_sEncodedText, VT_BSTRT );
  421.  
  422.       // Decode the string in a single call (bEndOfData is TRUE).
  423.       vaDecoded = xEncoder.Decode( vaEncoded, TRUE );
  424.  
  425.       // We assign an empty string as a default decoded value.
  426.       m_sDecodedText = "";
  427.  
  428.       // We check that the decoded variant is really a byte array. For instance,
  429.       // it won't be if there was no characters to decode (empty string).
  430.       if( vaDecoded.vt == ( VT_ARRAY | VT_UI1 ) )
  431.       {
  432.         LONG lHigh;
  433.         LONG lLow;
  434.  
  435.         // We extracts the upper bound and the lower bound of the
  436.         // byte array.
  437.         SafeArrayGetUBound( vaDecoded.parray, 1, &lHigh );
  438.         SafeArrayGetLBound( vaDecoded.parray, 1, &lLow );
  439.  
  440.         // This pointer will contain the address of the byte array
  441.         char* pszDecoded = NULL;
  442.  
  443.         // We get a pointer on the actual data in the safe array.
  444.         SafeArrayAccessData( vaDecoded.parray, (void**)&pszDecoded );
  445.  
  446.         // We display the decoded string by assigning this Ansi string
  447.         // to the CString member, using the constructor accepting a char* and a size.
  448.         m_sDecodedText = CString( pszDecoded, lHigh - lLow + 1 );
  449.  
  450.         // We release the lock on the safe array
  451.         SafeArrayUnaccessData( vaDecoded.parray );
  452.       }
  453.  
  454.       // Update mainly the decoded text box
  455.       UpdateData( FALSE );
  456.     }
  457.     catch( COleDispatchException* pExcept )
  458.     {
  459.       pExcept->ReportError();
  460.       pExcept->Delete();
  461.     }
  462.     catch( COleException* pExcept )
  463.     {
  464.       pExcept->ReportError();
  465.       pExcept->Delete();
  466.     }
  467.     catch( CException* pExcept )
  468.     {
  469.       pExcept->ReportError();
  470.       pExcept->Delete();
  471.     }
  472.  
  473.     pEx->Delete();
  474.  
  475.     // Update the decoded text size label
  476.     OnChangeTxtDecodedtext();
  477.   }
  478. }
  479.  
  480. //====================================================================================
  481. //
  482. // FUNCTIONS
  483. //
  484. //====================================================================================
  485.  
  486. //------------------------------------------------------------------------------------
  487. // Prepare the encoding format according to the user selection
  488. //------------------------------------------------------------------------------------
  489. void CMemoryEncodeDlg::PrepareEncodingFormat( DXceedBinaryEncoding& xEncoder )
  490. {
  491.   if( UpdateData( TRUE ) )
  492.   {
  493.     COleException* pEx = new COleException;
  494.  
  495.     switch( m_lEncodingMethod )
  496.     {
  497.       case emUUEncode :
  498.       {
  499.         DXceedUUEncodingFormat xUUEncode;
  500.  
  501.         if( !xUUEncode.CreateDispatch( "Xceed.UUEncoding", pEx ) )
  502.         {
  503.           throw pEx;
  504.         }
  505.  
  506.         // We don't want any header/footer for this is only a memory encoding
  507.         // process. The header/footer are mainly useful with files.
  508.         xUUEncode.SetIncludeHeaderFooter( FALSE );
  509.         xUUEncode.SetEndOfLineType( m_lEOLType );
  510.  
  511.         // Set the maximum line length specified by the user. This is a mandatory
  512.         // value as the EndOfLineType can not be None for UUEncoding.
  513.         xUUEncode.SetMaxLineLength( m_lMaxLineLength );
  514.  
  515.         // Set the previously initialized Encoding format of the Encoder object
  516.         // received as a parameter of this function.
  517.         xEncoder.SetRefEncodingFormat( xUUEncode.m_lpDispatch );
  518.  
  519.         // Free the temporary encoding format. The previous assignation adding
  520.         // a reference to the encoding format object, this object will effectively
  521.         // be freed by the xEncoder object when the latter will be released.
  522.         break;
  523.       }
  524.  
  525.       case emXXEncode :
  526.       {
  527.         DXceedXXEncodingFormat xXXEncode;
  528.  
  529.         if( !xXXEncode.CreateDispatch( "Xceed.XXEncoding", pEx ) )
  530.         {
  531.           throw pEx;
  532.         }
  533.  
  534.         // We don't want any header/footer for this is only a memory encoding
  535.         // process. The header/footer are mainly useful with files.
  536.         xXXEncode.SetIncludeHeaderFooter( FALSE );
  537.         xXXEncode.SetEndOfLineType( m_lEOLType );
  538.  
  539.         // Set the maximum line length specified by the user. This is a mandatory
  540.         // value as the EndOfLineType can not be None for XXEncoding.
  541.         xXXEncode.SetMaxLineLength( m_lMaxLineLength );
  542.  
  543.         xEncoder.SetRefEncodingFormat( xXXEncode.m_lpDispatch );
  544.         break;
  545.       }
  546.  
  547.       case emBase64 :
  548.       {
  549.         DXceedBase64EncodingFormat xBase64;
  550.         
  551.         if( !xBase64.CreateDispatch( "Xceed.Base64Encoding", pEx ) )
  552.         {
  553.           throw pEx;
  554.         }
  555.  
  556.         xBase64.SetEndOfLineType( m_lEOLType );
  557.  
  558.         // Set the maximum line length specified by the user. This value will
  559.         // be ignored by the Xceed Binary Encoding Library if the End of line
  560.         // type is set to None
  561.         xBase64.SetMaxLineLength( m_lMaxLineLength );
  562.  
  563.         xEncoder.SetRefEncodingFormat( xBase64.m_lpDispatch );
  564.         break;
  565.       }
  566.  
  567.       case emBinHex :
  568.       {
  569.         DXceedBinHexEncodingFormat xBinHex;
  570.  
  571.         if( !xBinHex.CreateDispatch( "Xceed.BinHexEncoding", pEx ) )
  572.         {
  573.           throw pEx;
  574.         }
  575.  
  576.         xBinHex.SetEndOfLineType( m_lEOLType );
  577.  
  578.         // For the BinHex format, we must specify the data fork length and the
  579.         // resource fork length.
  580.         // The DataForkLength is mandatory and must be set to the size
  581.         // of the data that will be encoded.
  582.         xBinHex.SetHeaderDataForkLength( m_sDecodedText.GetLength() );
  583.  
  584.         // The ResourceForkLength, used by MAC system, is not relevant
  585.         // under a PC system. We set it to 0.
  586.         xBinHex.SetHeaderResourceForkLength( 0 );
  587.         xBinHex.SetIncludeHeaderFooter( FALSE );
  588.  
  589.         xEncoder.SetRefEncodingFormat( xBinHex.m_lpDispatch );
  590.         break;
  591.       }
  592.  
  593.       case emHexadecimal :
  594.       {
  595.         DXceedHexaEncodingFormat xHexa;
  596.  
  597.         if( !xHexa.CreateDispatch( "Xceed.HexaEncoding", pEx ) )
  598.         {
  599.           throw pEx;
  600.         }
  601.  
  602.         xHexa.SetEndOfLineType( m_lEOLType );
  603.  
  604.         // Set the maximum line length specified by the user. This value will
  605.         // be ignored by the Xceed Binary Encoding Library if the End of line
  606.         // type is set to None
  607.         xHexa.SetMaxLineLength( m_lMaxLineLength );
  608.  
  609.         xEncoder.SetRefEncodingFormat( xHexa.m_lpDispatch );
  610.         break;
  611.       }
  612.  
  613.       case emQuoted :
  614.       {
  615.         DXceedQuotedPrintableEncodingFormat xQuoted;
  616.  
  617.         if( !xQuoted.CreateDispatch( "Xceed.QuotedPrintableEncoding", pEx ) )
  618.         {
  619.           throw pEx;
  620.         }
  621.  
  622.         xQuoted.SetEndOfLineType( m_lEOLType );
  623.  
  624.         // Set the maximum line length specified by the user. This value will
  625.         // be ignored by the Xceed Binary Encoding Library if the End of line
  626.         // type is set to None
  627.         xQuoted.SetMaxLineLength( m_lMaxLineLength );
  628.  
  629.         xEncoder.SetRefEncodingFormat( xQuoted.m_lpDispatch );
  630.         break;
  631.       }
  632.     }
  633.  
  634.     pEx->Delete();
  635.   }
  636. }
  637.  
  638. //------------------------------------------------------------------------------------
  639. // Prepare the decoding format according to the user selection
  640. //------------------------------------------------------------------------------------
  641. void CMemoryEncodeDlg::PrepareDecodingFormat( DXceedBinaryEncoding& xDecoder )
  642. {
  643.   if( UpdateData( TRUE ) )
  644.   {
  645.     COleException* pEx = new COleException;
  646.  
  647.     switch( m_lEncodingMethod )
  648.     {
  649.       case emUUEncode :
  650.       {
  651.         DXceedUUEncodingFormat xUUEncode;
  652.  
  653.         if( !xUUEncode.CreateDispatch( "Xceed.UUEncoding", pEx ) )
  654.         {
  655.           throw pEx;
  656.         }
  657.  
  658.         // The encoded string does not contain any header/footer
  659.         // The header/footer are usually present only in files.
  660.         xUUEncode.SetIncludeHeaderFooter( FALSE );
  661.         // We want to ignore any invalid encoded characters
  662.         xUUEncode.SetContinueOnInvalidData( TRUE );
  663.         xDecoder.SetRefEncodingFormat( xUUEncode.m_lpDispatch );
  664.         break;
  665.       }
  666.  
  667.       case emXXEncode :
  668.       {
  669.         DXceedXXEncodingFormat xXXEncode;
  670.  
  671.         if( !xXXEncode.CreateDispatch( "Xceed.XXEncoding", pEx ) )
  672.         {
  673.           throw pEx;
  674.         }
  675.  
  676.         xXXEncode.SetContinueOnInvalidData( TRUE );
  677.         // The encoded string does not contain any header/footer
  678.         // The header/footer are usually present only in files.
  679.         xXXEncode.SetIncludeHeaderFooter( FALSE );
  680.         xDecoder.SetRefEncodingFormat( xXXEncode.m_lpDispatch );
  681.         break;
  682.       }
  683.  
  684.       case emBase64 :
  685.       {
  686.         DXceedBase64EncodingFormat xBase64;
  687.         
  688.         if( !xBase64.CreateDispatch( "Xceed.Base64Encoding", pEx ) )
  689.         {
  690.           throw pEx;
  691.         }
  692.  
  693.         xBase64.SetContinueOnInvalidData( TRUE );
  694.         xDecoder.SetRefEncodingFormat( xBase64.m_lpDispatch );
  695.         break;
  696.       }
  697.  
  698.       case emHexadecimal :
  699.       {
  700.         DXceedHexaEncodingFormat xHexa;
  701.  
  702.         if( !xHexa.CreateDispatch( "Xceed.HexaEncoding", pEx ) )
  703.         {
  704.           throw pEx;
  705.         }
  706.  
  707.         xHexa.SetContinueOnInvalidData( TRUE );
  708.         xDecoder.SetRefEncodingFormat( xHexa.m_lpDispatch );
  709.         break;
  710.       }
  711.  
  712.       case emQuoted :
  713.       {
  714.         DXceedQuotedPrintableEncodingFormat xQuoted;
  715.  
  716.         if( !xQuoted.CreateDispatch( "Xceed.QuotedPrintableEncoding", pEx ) )
  717.         {
  718.           throw pEx;
  719.         }
  720.  
  721.         xQuoted.SetContinueOnInvalidData( TRUE );
  722.         xDecoder.SetRefEncodingFormat( xQuoted.m_lpDispatch );
  723.         break;
  724.       }
  725.  
  726.       case emBinHex :
  727.       {
  728.         DXceedBinHexEncodingFormat xBinHex;
  729.  
  730.         if( !xBinHex.CreateDispatch( "Xceed.BinHexEncoding", pEx ) )
  731.         {
  732.           throw pEx;
  733.         }
  734.  
  735.         xBinHex.SetContinueOnInvalidData( TRUE );
  736.         // The encoded string does not contain any formating
  737.         // The formating are usually present only in files.
  738.         xBinHex.SetIncludeHeaderFooter( FALSE );
  739.         xDecoder.SetRefEncodingFormat( xBinHex.m_lpDispatch );
  740.         break;
  741.       }
  742.     }
  743.  
  744.     pEx->Delete();
  745.   }
  746. }
  747.