home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / CFX_Wizard / examples / directorylist / request.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-12  |  5.5 KB  |  181 lines

  1. /////////////////////////////////////////////////////////////////////
  2. //
  3. // CFX_DIRECTORYLIST - Cold Fusion custom tag which enumerates
  4. //                           the contents of a specified directory.
  5. //
  6. // Copyright 96. All Rights Reserved.
  7. //
  8. // This tag enumerates the contents of the directory specified 
  9. // in the DIRECTORY attribute and returns the information as 
  10. // a query with the name specified in the NAME attribute. The
  11. // columns in the query include: Type, Name, Date, and Size.
  12. //
  13. // Example Use:
  14. //
  15. //    <CFX_DIRECTORYLIST NAME="RootDir" DIRECTORY="C:\"> 
  16. //
  17. //  <CFOUTPUT QUERY="RootDir">     
  18. //        (#Type#) #Name# - #Date# #Size# <BR>
  19. //    </CFOUTPUT>
  20. //
  21. //
  22. // NOTE: If you wish to experiment with this tag from within
  23. //         Cold Fusion you need to add it to the registry of
  24. //         custom tags using the Cold Fusion Administrator.
  25. //         If you want to run the tag from within the debugger
  26. //       you should set the path of the tag to the \Debug
  27. //         directory of the tag's project directory.
  28. //
  29.  
  30. #include "stdafx.h"        // Standard MFC libraries
  31. #include "cfx.h"        // CFX Custom Tag API
  32.  
  33.  
  34. // Constants
  35. #define TAG_ERROR_HEADER    "Error occurred in CFX_DIRECTORYLIST tag"
  36.  
  37. // Forward declarations for helper functions
  38. HANDLE GetFirstFile( CCFXRequest* pRequest, CString strDir, WIN32_FIND_DATA& findData ) ;
  39. BOOL GetNextFile( CCFXRequest* pRequest, HANDLE hFind, WIN32_FIND_DATA& findData ) ;
  40. LPCSTR GetRequiredAttribute( CCFXRequest* pRequest, LPCSTR lpszAttribName ) ;
  41.  
  42.  
  43. void ProcessTagRequest( CCFXRequest* pRequest ) 
  44. {
  45.     try
  46.     {
  47.         // Get the DIRECTORY attribute (determines which directory to query)
  48.         CString strDirectory = GetRequiredAttribute( pRequest, "DIRECTORY" ) ;
  49.         if ( strDirectory.Right(1) != "\\" )
  50.             strDirectory += ( "\\" ) ;
  51.  
  52.         // Get the NAME attribute (determines what to name the query we return)
  53.         CString strName = GetRequiredAttribute( pRequest, "NAME" ) ;
  54.         
  55.         // Create a query object to return to the client
  56.         CCFXStringSet* pColumns = pRequest->CreateStringSet() ;
  57.         int iType = pColumns->AddString("TYPE") ;
  58.         int iName = pColumns->AddString("NAME") ;
  59.         int iDate = pColumns->AddString("DATE") ;
  60.         int iSize = pColumns->AddString("SIZE") ;
  61.         CCFXQuery* pQuery = pRequest->AddQuery( strName, pColumns ) ;
  62.  
  63.         // Iterate over all of the files in the directory and create
  64.         // a row in the query for each one
  65.         WIN32_FIND_DATA findData ;
  66.         HANDLE hFind = GetFirstFile( pRequest, strDirectory, findData ) ;
  67.         if ( hFind == NULL )
  68.             return ;
  69.  
  70.         while (TRUE)
  71.         {
  72.              // Add a row to the query for this file
  73.             int iRow = pQuery->AddRow() ;
  74.  
  75.             // Determine the type
  76.             if ( (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
  77.                 pQuery->SetData( iRow, iType, "Directory" ) ;
  78.             else
  79.                 pQuery->SetData( iRow, iType, "File" ) ;
  80.  
  81.             // Set the file name
  82.             pQuery->SetData( iRow, iName, findData.cFileName ) ;
  83.  
  84.             // Set the file date
  85.             CTime fileDate( findData.ftLastWriteTime ) ;
  86.             CString strDate = fileDate.Format( "%m/%d/%y %I:%M %p" ) ;
  87.             pQuery->SetData( iRow, iDate, strDate ) ;
  88.  
  89.             // Set the file size
  90.             CString strSize ;
  91.             strSize.Format( "%ld", findData.nFileSizeLow ) ;
  92.             pQuery->SetData( iRow, iSize, strSize ) ;
  93.  
  94.             // Get the next file
  95.             if ( !GetNextFile( pRequest, hFind, findData ) )
  96.                 break ;            
  97.         }
  98.         
  99.         ::FindClose( hFind ) ;
  100.     }
  101.  
  102.     // Catch Cold Fusion exceptions & re-raise them
  103.     catch( CCFXException* e )
  104.     {
  105.         pRequest->ReThrowException( e ) ;
  106.     }
  107.     
  108.     // Catch ALL other exceptions and throw them as 
  109.     // Cold Fusion exceptions (DO NOT REMOVE! -- 
  110.     // this prevents the server from crashing in 
  111.     // case of an unexpected exception)
  112.     catch( ... )
  113.     {
  114.         pRequest->ThrowException( 
  115.             TAG_ERROR_HEADER,
  116.             "Unexpected error occurred while processing tag." ) ;
  117.     }
  118. }
  119.  
  120.  
  121. // Get the first available file in the directory
  122. HANDLE GetFirstFile( CCFXRequest* pRequest, CString strDir, WIN32_FIND_DATA& findData )
  123. {
  124.     HANDLE hFind = ::FindFirstFile( strDir + "*.*", &findData ) ;
  125.     if ( hFind == INVALID_HANDLE_VALUE )
  126.     {
  127.         DWORD dwError = ::GetLastError() ;
  128.         if ( dwError == ERROR_NO_MORE_FILES )
  129.             return NULL ;
  130.         else
  131.         {
  132.             CString strErr ;
  133.             strErr.Format( 
  134.                 "Win32 error number %ld occurred while attempting "
  135.                 "to find first file in directory '" + strDir + "'.", dwError ) ;
  136.             pRequest->ThrowException( TAG_ERROR_HEADER, strErr ) ;
  137.         }
  138.     }
  139.     return hFind ;
  140. }
  141.  
  142.  
  143. // Get the next available file in the directory
  144. BOOL GetNextFile( CCFXRequest* pRequest, HANDLE hFind, WIN32_FIND_DATA& findData )
  145. {
  146.     if ( !::FindNextFile( hFind, &findData ) )
  147.     {
  148.         DWORD dwError = ::GetLastError() ;
  149.         if ( dwError == ERROR_NO_MORE_FILES )
  150.             return FALSE ;
  151.         else
  152.         {
  153.             CString strErr ;
  154.             strErr.Format( 
  155.                 "Win32 error number %ld occurred while attempting "
  156.                 "to enumerate the files in the directory.", dwError ) ;
  157.             ::FindClose( hFind ) ;
  158.             pRequest->ThrowException( TAG_ERROR_HEADER, strErr ) ;
  159.         }        
  160.     }
  161.     return TRUE ;
  162. }
  163.  
  164.  
  165. // Get the value for the passed attribute (throw an exception
  166. // if the attribute was not passed to the tag)
  167. LPCSTR GetRequiredAttribute( CCFXRequest* pRequest, LPCSTR lpszAttribName ) 
  168. {
  169.     // Verify that the attribute exists (throw an exception
  170.     // if it does not)
  171.     if ( !pRequest->AttributeExists(lpszAttribName) )
  172.     {
  173.         CString strErr = 
  174.             "The required attribute " + CString(lpszAttribName) +
  175.             " was not passed to the tag. " ;
  176.         pRequest->ThrowException( TAG_ERROR_HEADER, strErr ) ;    
  177.     }
  178.  
  179.     // Return the attribute
  180.     return pRequest->GetAttribute( lpszAttribName ) ;    
  181. }