home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / TESTDIR.PAK / TESTDIR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  2.7 KB  |  107 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  TESTDIR.CPP                                                           */
  4. /*                                                                        */
  5. /*  Copyright (c) 1991, 1995 Borland International                        */
  6. /*  All Rights Reserved.                                                  */
  7. /*                                                                        */
  8. /*  Sorted container example source file                                  */
  9. /*                                                                        */
  10. /*------------------------------------------------------------------------*/
  11. #include <classlib/arrays.h>
  12. #include <dir.h>
  13. #include <stdlib.h>
  14. #include <iostream.h>
  15. #include "filedata.h"
  16.  
  17. template <class T> class Directory
  18. {
  19.  
  20. public:
  21.  
  22.     Directory( const char *FileSpec );
  23.     void Show();
  24.  
  25. private:
  26.  
  27.     TISArrayAsVector<T> Array;
  28.  
  29. };
  30.  
  31. template <class T> Directory<T>::Directory( const char *FileSpec ) :
  32.     Array( 10, 0, 5 )
  33. {
  34.     struct ffblk FileBlock;
  35.     int Done = findfirst( FileSpec, &FileBlock, 0 );
  36.     while( !Done )
  37.         {
  38.         Array.Add( new T(FileBlock) );
  39.         Done = findnext( &FileBlock );
  40.         }
  41. }
  42.  
  43. static void ShowBlock( FilesByName &blk, void * )
  44. {
  45.     cout << blk << endl;
  46. }
  47.  
  48. static void ShowBlock( FilesByDate &blk, void * )
  49. {
  50.     cout << blk << endl;
  51. }
  52.  
  53. static void ShowBlock( FilesBySize &blk, void * )
  54. {
  55.     cout << blk << endl;
  56. }
  57.  
  58. template <class T> inline void Directory<T>::Show()
  59. {
  60.     Array.ForEach( ShowBlock, 0 );
  61. }
  62.  
  63. void SortByName( const char *FileSpec )
  64. {
  65.     Directory<FilesByName> dir( FileSpec );
  66.     dir.Show();
  67. }
  68.  
  69. void SortByDate( const char *FileSpec )
  70. {
  71.     Directory<FilesByDate> dir( FileSpec );
  72.     dir.Show();
  73. }
  74.  
  75. void SortBySize( const char *FileSpec )
  76. {
  77.     Directory<FilesBySize> dir( FileSpec );
  78.     dir.Show();
  79. }
  80.  
  81. int main( int argc, char *argv[] )
  82. {
  83.     if( argc < 2 || argc > 3 )
  84.         {
  85.         cerr << "Usage:  directory [options] filespec" << endl << endl;
  86.         cerr << "Options:" << endl;
  87.         cerr << "\t-sd\tsort by date" << endl;
  88.         cerr << "\t-sn\tsort by name" << endl;
  89.         cerr << "\t-ss\tsort by size" << endl;
  90.         exit(1);
  91.         }
  92.  
  93.     if( argc != 3 )
  94.         SortByName( argv[1] );
  95.     else
  96.         {
  97.         if( strcmp( argv[1], "-sn" ) == 0 )
  98.             SortByName( argv[2] );
  99.         else if( strcmp( argv[1], "-sd" ) == 0 )
  100.             SortByDate( argv[2] );
  101.         else if( strcmp( argv[1], "-ss" ) == 0 )
  102.             SortBySize( argv[2] );
  103.         }
  104.  
  105.     return 0;
  106. }
  107.