home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / testdir.pak / TESTDIR.CPP < prev    next >
C/C++ Source or Header  |  1997-07-23  |  3KB  |  122 lines

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