home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedZip.Cab / F112572_Tools.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-17  |  4.4 KB  |  154 lines

  1. /*******************************************************************************
  2.  File:        Tools.cpp
  3.  Description: Zip Manager sample application using The Xceed Zip Compression
  4.               Library 4
  5.  Copyright (c) 1995-1999 Xceed Software Inc.
  6. *******************************************************************************/
  7.  
  8. #include <vcl\vcl.h>
  9. // For Directory Browser
  10. #include <shlobj.h>
  11. #include <objbase.h>
  12. // ---------------------
  13. #pragma hdrstop
  14.  
  15. #include "XceedZipLib_OCX.h"
  16. #include "Tools.h"
  17.  
  18. //
  19. // The ParseTab function extracts a field from a string containing tab-separated
  20. // fields. For example, this function can extract the third item in a string that
  21. // was assigned the value of an item in a multi-column listbox, since items in
  22. // each column of a listbox are separated by tabs. Pass the column number in the
  23. // FieldIndex parameter. The variable passed in the FieldIndex parameter will be
  24. // incremented. The return value of ParseTab is the extracted string.
  25. //
  26. void ParseTab(AnsiString Str, int &FieldIndex, AnsiString &ReturnValue)
  27. {
  28.   int i;
  29.  
  30.   ReturnValue = "";
  31.  
  32.   if ((FieldIndex > 0) && (FieldIndex <= Str.Length()))
  33.   {
  34.     for(i=FieldIndex; i <= Str.Length(); i++)
  35.       if ((i <= Str.Length()) && (Str[i] == '\t'))
  36.           break;
  37.  
  38.     if (i <= Str.Length())
  39.     {
  40.       ReturnValue = Str.SubString(FieldIndex, i - FieldIndex);
  41.       FieldIndex = i + 1;
  42.     }
  43.     else
  44.     {
  45.       ReturnValue = Str.SubString(FieldIndex, Str.Length()-FieldIndex);
  46.       FieldIndex = 0;
  47.     }
  48.   }
  49.   else
  50.     FieldIndex = 0;
  51. }
  52.  
  53. //
  54. // The AssignFromLb procedure takes all the entries in the first column of a
  55. // listbox and places them in a TStringList. In this demo, AssignFromLb is used
  56. // to take all the path and filenames and place them in a TStringList. When the
  57. // demo is running, it may look as if only filenames appear in the first column,
  58. // and the pathnames are in column 7, but that is only on screen. In reality,
  59. // the data in column 1 contains paths and filenames.
  60. //
  61. void AssignFromLb(TListBox *SourceLb, TXceedZip *pZip)
  62. {
  63.   int i, C;
  64.   AnsiString Value;
  65.  
  66.   pZip->FilesToProcess = "";
  67.  
  68.   if (SourceLb->SelCount > 0)
  69.   {
  70.       for (i = 0; i < SourceLb->Items->Count; i++)
  71.     {
  72.       if (SourceLb->Selected[i])
  73.       {
  74.         C = 1;
  75.         ParseTab(SourceLb->Items->Strings[i], C, Value);
  76.         pZip->AddFilesToProcess( WideString( Value ) );
  77.       }
  78.     }
  79.   }
  80. }
  81.  
  82. //
  83. // The HandleError function is a sample error handling routine for the purposes
  84. // of this demo, but can be used in your own programs as well. When an error
  85. // occurs, it displays a message box indicating the nature of the error. If a
  86. // warning occurs, an information message box will be shown.  The function also
  87. // returns a result of 0 if ErrorCode was 0, 1 if a warning occured, or 2 if an
  88. // error occured.
  89. //
  90. int HandleError(xcdError ErrorCode, AnsiString DoingWhat, TXceedZip* pZip)
  91. {
  92.   BSTR bstrMessage = NULL;
  93.   int ErrorType = 2;
  94.  
  95.   switch (ErrorCode)
  96.   {
  97.     case xerWarnings:
  98.     case xerFilesSkipped:
  99.     case xerProcessStarted:
  100.       ErrorType = 1;
  101.       break;
  102.  
  103.     case xerSuccess:
  104.       ErrorType = 0;
  105.       break;
  106.  
  107.     default:
  108.         bstrMessage = pZip->GetErrorDescription( xvtError, ErrorCode );
  109.         break;
  110.     }
  111.  
  112.   if( bstrMessage )
  113.   {
  114.     Application->MessageBox( AnsiString( bstrMessage ).c_str(), DoingWhat.c_str(), MB_OK + MB_ICONERROR );
  115.     SysFreeString( bstrMessage );
  116.   }
  117.  
  118.   return ErrorType;
  119. }
  120.  
  121. //
  122. // This function displays a dialog box prompting the user to select a
  123. // directory. If the user cancels, the directory path will be blank.
  124. // This directory browser is not available under Windows NT3.51 or previous
  125. //
  126. bool SelectDirectory(char * pszDir, HWND hParent)
  127. {
  128.   BROWSEINFO    bi;
  129.   LPITEMIDLIST  pidlBrowse;
  130.  
  131.   // Fill in the BROWSEINFO structure.
  132.   bi.hwndOwner      = hParent;
  133.   bi.pidlRoot       = NULL;
  134.   bi.pszDisplayName = pszDir;
  135.   bi.lpszTitle      = "Select the destination directory";
  136.   bi.ulFlags        = BIF_RETURNONLYFSDIRS;
  137.   bi.lpfn           = NULL;
  138.   bi.lParam         = 0;
  139.   bi.iImage         = 0;
  140.  
  141.   // Browse for a folder and return its PIDL.
  142.   pidlBrowse = SHBrowseForFolder(&bi);
  143.  
  144.   if (pidlBrowse == NULL)
  145.     return false;
  146.  
  147.   bool bResult = (SHGetPathFromIDList(pidlBrowse, pszDir) != 0);
  148.  
  149.   CoTaskMemFree(pidlBrowse);
  150.  
  151.   return bResult;
  152. }
  153.  
  154.