home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 10.ddi / TVSRC.ZIP / TFILLIST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  7.9 KB  |  323 lines

  1. /*------------------------------------------------------------*/
  2. /* filename -       tfillist.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  TFileList member functions                */
  6. /*------------------------------------------------------------*/
  7.  
  8. /*------------------------------------------------------------*/
  9. /*                                                            */
  10. /*    Turbo Vision -  Version 1.0                             */
  11. /*                                                            */
  12. /*                                                            */
  13. /*    Copyright (c) 1991 by Borland International             */
  14. /*    All Rights Reserved.                                    */
  15. /*                                                            */
  16. /*------------------------------------------------------------*/
  17.  
  18. #define Uses_TVMemMgr
  19. #define Uses_MsgBox
  20. #define Uses_TFileList
  21. #define Uses_TRect
  22. #define Uses_TSearchRec
  23. #define Uses_TEvent
  24. #define Uses_TGroup
  25. #include <tv.h>
  26.  
  27. #if !defined( __DIR_H )
  28. #include <Dir.h>
  29. #endif  // __DIR_H
  30.  
  31. #if !defined( __ERRNO_H )
  32. #include <Errno.h>
  33. #endif  // __ERRNO_H
  34.  
  35. #if !defined( __IO_H )
  36. #include <IO.h>
  37. #endif  // __IO_H
  38.  
  39. #if !defined( __STDIO_H )
  40. #include <Stdio.h>
  41. #endif  // __STDIO_H
  42.  
  43. #if !defined( __CTYPE_H )
  44. #include <ctype.h>
  45. #endif  // __CTYPE_H
  46.  
  47. #if !defined( __ASSERT_H )
  48. #include <Assert.h>
  49. #endif  // __ASSERT_H
  50.  
  51. #if !defined( __DOS_H )
  52. #include <Dos.h>
  53. #endif  // __DOS_H
  54.  
  55. #if !defined( __STRING_H )
  56. #include <String.h>
  57. #endif  // __STRING_H
  58.  
  59. void fexpand( char * );
  60.  
  61. TFileList::TFileList( const TRect& bounds,
  62.                       TScrollBar *aScrollBar) :
  63.     TSortedListBox( bounds, 2, aScrollBar )
  64. {
  65. }
  66.  
  67. TFileList::~TFileList()
  68. {
  69.    if ( list() )
  70.       destroy ( list() );
  71. }
  72.  
  73. void TFileList::focusItem( short item )
  74. {
  75.     TSortedListBox::focusItem( item );
  76.     message( owner, evBroadcast, cmFileFocused, list()->at(item) );
  77. }
  78.  
  79. void TFileList::getData( void * )
  80. {
  81. }
  82.  
  83. void TFileList::setData( void * )
  84. {
  85. }
  86.  
  87. ushort TFileList::dataSize()
  88. {
  89.     return 0;
  90. }
  91.  
  92. void* TFileList::getKey( const char *s )
  93. {
  94. static TSearchRec sR;
  95.  
  96.     if( (shiftState & 0x03) != 0 || *s == '.' )
  97.         sR.attr = FA_DIREC;
  98.     else
  99.         sR.attr = 0;
  100.     strcpy( sR.name, s );
  101.     strupr( sR.name );
  102.     return &sR;
  103. }
  104.  
  105. void TFileList::getText( char *dest, short item, short maxChars )
  106. {
  107.     TSearchRec *f = (TSearchRec *)(list()->at(item));
  108.  
  109.     strncpy( dest, f->name, maxChars );
  110.     dest[maxChars] = '\0';
  111.     if( f->attr & FA_DIREC )
  112.         strcat( dest, "\\" );
  113. }
  114.  
  115. void TFileList::handleEvent( TEvent & event )
  116. {
  117.     if( event.what == evMouseDown && event.mouse.doubleClick )
  118.         {
  119.         event.what = evCommand;
  120.         event.message.command = cmOK;
  121.         putEvent( event );
  122.         clearEvent( event );
  123.         }
  124.     else
  125.         TSortedListBox::handleEvent( event );
  126. }
  127.  
  128. void TFileList::readDirectory( const char *dir, const char *wildCard )
  129. {
  130.     char path[MAXPATH];
  131.     strcpy( path, dir );
  132.     strcat( path, wildCard );
  133.     readDirectory( path );
  134. }
  135.  
  136. struct DirSearchRec : public TSearchRec
  137. {
  138.  
  139.     void *operator new( size_t );
  140.  
  141. };
  142.  
  143. void *DirSearchRec::operator new( size_t sz )
  144. {
  145.     void *temp = ::operator new( sz );
  146.     if( TVMemMgr::safetyPoolExhausted() )
  147.         {
  148.         delete temp;
  149.         temp = 0;
  150.         }
  151.     return temp;
  152. }
  153.  
  154. void TFileList::readDirectory( const char *aWildCard )
  155. {
  156. ffblk s;
  157.  
  158. char path[MAXPATH];
  159. char drive[MAXDRIVE];
  160. char dir[MAXDIR];
  161. char file[MAXFILE];
  162. char ext[MAXEXT];
  163.  
  164. const unsigned findAttr = FA_RDONLY | FA_ARCH;
  165.  
  166.     strcpy( path, aWildCard );
  167.     fexpand( path );
  168.     fnsplit( path, drive, dir, file, ext );
  169.  
  170.     TFileCollection *fileList = new TFileCollection( 5, 5 );
  171.  
  172.     int res = findfirst( aWildCard, &s, findAttr );
  173.     DirSearchRec *p = (DirSearchRec *)&p;
  174.     while( p != 0 && res == 0 )
  175.         {
  176.         if( (s.ff_attrib & FA_DIREC) == 0 )
  177.             {
  178.             p = new DirSearchRec;
  179.             if( p != 0 )
  180.                 {
  181.                 memcpy( p, &s.ff_attrib, sizeof( DirSearchRec ) );
  182.                 fileList->insert( p );
  183.                 }
  184.             }
  185.         res = findnext( &s );
  186.         }
  187.  
  188.     fnmerge( path, drive, dir, "*", ".*" );
  189.  
  190.     res = findfirst( path, &s, FA_DIREC );
  191.     while( p != 0 && res == 0 )
  192.         {
  193.         if( (s.ff_attrib & FA_DIREC) != 0 && s.ff_name[0] != '.' )
  194.             {
  195.             p = new DirSearchRec;
  196.             if( p != 0 )
  197.                 {
  198.                 memcpy( p, &s.ff_attrib, sizeof( DirSearchRec ) );
  199.                 fileList->insert( p );
  200.                 }
  201.             }
  202.         res = findnext( &s );
  203.         }
  204.  
  205.     if( strlen( dir ) > 4 )
  206.         {
  207.         p = new DirSearchRec;
  208.         if( p != 0 )
  209.             {
  210.             if( findfirst( path, &s, FA_DIREC ) == 0 &&
  211.                 findnext( &s ) == 0 &&
  212.                 strcmp( s.ff_name, ".." ) == 0
  213.             )
  214.                 memcpy( p, &s.ff_attrib, sizeof( DirSearchRec ) );
  215.             else
  216.                 {
  217.                 strcpy( p->name, ".." );
  218.                 p->size = 0;
  219.                 p->time = 0x210000uL;
  220.                 p->attr = FA_DIREC;
  221.                 }
  222.             fileList->insert( p );
  223.             }
  224.         }
  225.  
  226.     if( p == 0 )
  227.         messageBox( tooManyFiles, mfOKButton | mfWarning );
  228.     newList(fileList);
  229.     if( list()->getCount() > 0 )
  230.         message( owner, evBroadcast, cmFileFocused, list()->at(0) );
  231.     else
  232.         {
  233.         static DirSearchRec noFile;
  234.         message( owner, evBroadcast, cmFileFocused, &noFile );
  235.         }
  236. }
  237.  
  238. /*
  239.     fexpand:    reimplementation of pascal's FExpand routine.  Takes a
  240.                 relative DOS path and makes an absolute path of the form
  241.  
  242.                     drive:\[subdir\ ...]filename.ext
  243.  
  244.                 works with '/' or '\' as the subdir separator on input;
  245.                 changes all to '\' on output.
  246.  
  247. */
  248.  
  249. void squeeze( char *path )
  250. {
  251.     char *dest = path;
  252.     char *src = path;
  253.     while( *src != 0 )
  254.         {
  255.         if( *src != '.' )
  256.             *dest++ = *src++;   // just copy it...
  257.         else
  258.             {
  259.             src++;
  260.             if( *src == '.' )
  261.                 {               // have a '..'
  262.                 src += 2;       // skip the following '\'
  263.                 dest--;         // back up to the previous '\'
  264.                 while( *--dest != '\\' ) // back up to the previous '\'
  265.                     ;
  266.                 dest++;         // move to the next position
  267.                 }
  268.             else
  269.                 {
  270.                 src++;          // skip the following '\'
  271.                 dest += 2;
  272.                 }
  273.             }
  274.         }
  275.     *dest = EOS;                // zero terminator
  276. }
  277.  
  278. void fexpand( char *rpath )
  279. {
  280. char path[MAXPATH];
  281. char drive[MAXDRIVE];
  282. char dir[MAXDIR];
  283. char file[MAXFILE];
  284. char ext[MAXEXT];
  285.  
  286.     int flags = fnsplit( rpath, drive, dir, file, ext );
  287.     if( (flags & DRIVE) == 0 )
  288.         {
  289.         drive[0] = getdisk() + 'A';
  290.         drive[1] = ':';
  291.         drive[2] = '\0';
  292.         }
  293.     drive[0] = toupper(drive[0]);
  294.     if( (flags & DIRECTORY) == 0 || (dir[0] != '\\' && dir[0] != '/') )
  295.         {
  296.         char curdir[MAXDIR];
  297.         getcurdir( drive[0] - 'A' + 1, curdir );
  298.         strcat( curdir, dir );
  299.         if( *curdir != '\\' && *curdir != '/' )
  300.             {
  301.             *dir = '\\';
  302.             strcpy( dir+1, curdir );
  303.             }
  304.         else
  305.             strcpy( dir, curdir );
  306.         }
  307.  
  308.     squeeze( dir );
  309.     char *p = dir;
  310.     while( (p = strchr( p, '/' )) != 0 )
  311.         *p = '\\';
  312.     fnmerge( path, drive, dir, file, ext );
  313.     strupr( path );
  314.     strcpy( rpath, path );
  315. }
  316.  
  317. TStreamable *TFileList::build()
  318. {
  319.     return new TFileList( streamableInit );
  320. }
  321.  
  322.  
  323.