home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / COMPOUND.ZIP / Dir.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-09  |  4.8 KB  |  181 lines

  1. //
  2. // Dir.cpp
  3. //+
  4. //    implementation file
  5. //-
  6. //    rev 11/09/98 gls continue
  7. //    rev 11/02/98 gls
  8. //
  9.  
  10. #include "stdafx.h"
  11. #include "gstg.h"
  12. #include "Dir.h"
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CDir
  22.  
  23. IMPLEMENT_DYNCREATE(CDir, CCmdTarget)
  24.  
  25. CDir::CDir()
  26. {
  27.     EnableAutomation();
  28.     
  29.     // To keep the application running as long as an OLE automation 
  30.     //    object is active, the constructor calls AfxOleLockApp.
  31.     
  32.     AfxOleLockApp();
  33. }
  34.  
  35. CDir::~CDir()
  36. {
  37.     // To terminate the application when all objects created with
  38.     //     with OLE automation, the destructor calls AfxOleUnlockApp.
  39.     
  40.     AfxOleUnlockApp();
  41. }
  42.  
  43.  
  44. void CDir::OnFinalRelease()
  45. {
  46.     // When the last reference for an automation object is released
  47.     // OnFinalRelease is called.  The base class will automatically
  48.     // deletes the object.  Add additional cleanup required for your
  49.     // object before calling the base class.
  50.  
  51.     CCmdTarget::OnFinalRelease();
  52. }
  53.  
  54.  
  55. BEGIN_MESSAGE_MAP(CDir, CCmdTarget)
  56.     //{{AFX_MSG_MAP(CDir)
  57.         // NOTE - the ClassWizard will add and remove mapping macros here.
  58.     //}}AFX_MSG_MAP
  59. END_MESSAGE_MAP()
  60.  
  61. BEGIN_DISPATCH_MAP(CDir, CCmdTarget)
  62.     //{{AFX_DISPATCH_MAP(CDir)
  63.     DISP_FUNCTION(CDir, "Scan", Scan, VT_I4, VTS_BSTR)
  64.     DISP_FUNCTION(CDir, "getFile", getFile, VT_BSTR, VTS_I4)
  65.     DISP_FUNCTION(CDir, "MkDir", MkDir, VT_I4, VTS_BSTR)
  66.     DISP_FUNCTION(CDir, "getFileCount", getFileCount, VT_I4, VTS_NONE)
  67.     DISP_FUNCTION(CDir, "getFileRoot", getFileRoot, VT_BSTR, VTS_BSTR)
  68.     DISP_FUNCTION(CDir, "getDir", getDir, VT_BSTR, VTS_I4)
  69.     DISP_FUNCTION(CDir, "getDirCount", getDirCount, VT_I4, VTS_NONE)
  70.     //}}AFX_DISPATCH_MAP
  71. END_DISPATCH_MAP()
  72.  
  73. // Note: we add support for IID_IDir to support typesafe binding
  74. //  from VBA.  This IID must match the GUID that is attached to the 
  75. //  dispinterface in the .ODL file.
  76.  
  77. // {9BD4DD41-724E-11D2-9F15-00A0246D0F63}
  78. static const IID IID_IDir =
  79. { 0x9bd4dd41, 0x724e, 0x11d2, { 0x9f, 0x15, 0x0, 0xa0, 0x24, 0x6d, 0xf, 0x63 } };
  80.  
  81. BEGIN_INTERFACE_MAP(CDir, CCmdTarget)
  82.     INTERFACE_PART(CDir, IID_IDir, Dispatch)
  83. END_INTERFACE_MAP()
  84.  
  85. // {9BD4DD42-724E-11D2-9F15-00A0246D0F63}
  86. IMPLEMENT_OLECREATE(CDir, "gstg.Dir", 0x9bd4dd42, 0x724e, 0x11d2, 0x9f, 0x15, 0x0, 0xa0, 0x24, 0x6d, 0xf, 0x63)
  87.  
  88. // ////////////////////////////////////////////////////////////
  89. // CDir message handlers
  90.  
  91. long CDir::Scan(LPCTSTR pszFileSpec) 
  92. {
  93.     BOOL    fSuccess = m_ScanDir.Scan( pszFileSpec );
  94.  
  95.     return( fSuccess ? S_OK : S_FALSE );
  96. }
  97. // ////////////////////////////////////////////////////////////
  98. //
  99. //    Get dir from index
  100. //
  101. BSTR CDir::getDir(long iDir) 
  102. {
  103.     CString sResult;
  104.  
  105.     m_ScanDir.GetDir( sResult, iDir );
  106.  
  107.     return( sResult.AllocSysString() );
  108. }
  109. // ////////////////////////////////////////////////////////////
  110. //
  111. //    Get count of dir entries from scan
  112. //
  113. long CDir::getDirCount() 
  114. {
  115.     return( m_ScanDir.getDirCount() );
  116. }
  117. // ////////////////////////////////////////////////////////////
  118. //
  119. //    Get path to specified file
  120. //
  121. BSTR CDir::getFile(long iFile) 
  122. {
  123.     CString sResult;
  124.  
  125.     m_ScanDir.GetFile( sResult, iFile );
  126.  
  127.     return( sResult.AllocSysString() );
  128. }
  129. // ////////////////////////////////////////////////////////////
  130. //
  131. //    Get count of files from a previous scan
  132. //
  133. long CDir::getFileCount() 
  134. {
  135.     return( m_ScanDir.getFileCount() );
  136. }
  137. // ////////////////////////////////////////////////////////////
  138. //
  139. //    Create the specified directory - recursively if needed
  140. //
  141. long CDir::MkDir(LPCTSTR pszDir) 
  142. {
  143.     ASSERT( pszDir != NULL );
  144.  
  145.     int iSlash = 0;             // index to the slash
  146.     CString sDir = pszDir;      // copy for manipulation
  147.     CString sMkDir;             // running directory to create
  148.  
  149.     while( (iSlash = sDir.Find( '\\' )) != -1 )
  150.         {
  151.         CString sSub = sDir.Left( iSlash+1 );   // get up to slash
  152.         sDir = sDir.Mid( iSlash+1 );            // skip to next
  153.  
  154.         sMkDir += sSub;                         // keep running total
  155.  
  156.         CreateDirectory( sMkDir, NULL );        // create the intermediate dir
  157.         }
  158.  
  159.     CreateDirectory( pszDir, NULL );            // last one to complete!
  160.  
  161.     return( S_OK );
  162. }
  163. // ////////////////////////////////////////////////////////////
  164. //
  165. //    Helper function to get filename from path
  166. //
  167. BSTR CDir::getFileRoot(LPCTSTR pszFilePath) 
  168. {
  169.     char    szFName[_MAX_PATH];
  170.     char    szExt[_MAX_PATH];
  171.  
  172.     _splitpath( pszFilePath, NULL, NULL, szFName, szExt );
  173.     CString sResult;
  174.     sResult.Format( "%s%s", szFName, szExt );;
  175.  
  176.     return( sResult.AllocSysString() );
  177. }
  178. // ////////////////////////////////////////////////////////////
  179. // ////////////////////////////////////////////////////////////
  180. // ////////////////////////////////////////////////////////////
  181.