home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / src / TFileList.cc < prev    next >
C/C++ Source or Header  |  1998-05-03  |  5KB  |  263 lines

  1. /*
  2.  * TFileList.cc
  3.  *
  4.  * Turbo Vision - Version 2.0
  5.  *
  6.  * Copyright (c) 1994 by Borland International
  7.  * All Rights Reserved.
  8.  *
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. #define Uses_TVMemMgr
  13. #define Uses_MsgBox
  14. #define Uses_TFileList
  15. #define Uses_TRect
  16. #define Uses_TSearchRec
  17. #define Uses_TEvent
  18. #define Uses_TGroup
  19. #define Uses_TKeys
  20. #include <tvision/tv.h>
  21.  
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <assert.h>
  25. #include <ctype.h>
  26. #include <dirent.h>
  27. #include <errno.h>
  28. #include <glob.h>
  29. #include <stddef.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <time.h>
  33.  
  34. void fexpand( char * );
  35.  
  36. TFileList::TFileList( const TRect& bounds,
  37.                       TScrollBar *aScrollBar) :
  38.     TSortedListBox( bounds, 2, aScrollBar )
  39. {
  40. }
  41.  
  42. TFileList::~TFileList()
  43. {
  44.    if ( list() )
  45.       destroy ( list() );
  46. }
  47.  
  48. void TFileList::focusItem( short item )
  49. {
  50.     TSortedListBox::focusItem( item );
  51.     message( owner, evBroadcast, cmFileFocused, list()->at(item) );
  52. }
  53.  
  54. void TFileList::selectItem( short item )
  55. {
  56.     message( owner, evBroadcast, cmFileDoubleClicked, list()->at(item) );
  57. }
  58.  
  59. void TFileList::getData( void * )
  60. {
  61. }
  62.  
  63. void TFileList::setData( void * )
  64. {
  65. }
  66.  
  67. ushort TFileList::dataSize()
  68. {
  69.     return 0;
  70. }
  71.  
  72. void* TFileList::getKey( const char *s )
  73. {
  74. static TSearchRec sR;
  75.  
  76.     if( (shiftState & kbShift) != 0 || *s == '.' )
  77.         sR.attr = FA_DIREC;
  78.     else
  79.         sR.attr = 0;
  80.     strcpy( sR.name, s );
  81.  
  82.     /* SS: changed */
  83.  
  84.     for (char *p = sR.name; *p != '\0'; p++) *p = toupper(*p);
  85.     return &sR;
  86. }
  87.  
  88. void TFileList::getText( char *dest, short item, short maxChars )
  89. {
  90.     TSearchRec *f = (TSearchRec *)(list()->at(item));
  91.  
  92.     strncpy( dest, f->name, maxChars );
  93.     dest[maxChars] = '\0';
  94.     if( f->attr & FA_DIREC )
  95.         strcat( dest, "/" );
  96. }
  97.  
  98.  
  99. void TFileList::readDirectory( const char *dir, const char *wildCard )
  100. {
  101.     char path[PATH_MAX];
  102.     strcpy( path, dir );
  103.     strcat( path, wildCard );
  104.     readDirectory( path );
  105. }
  106.  
  107. struct DirSearchRec : public TSearchRec
  108. {
  109.     /* SS: changed */
  110.  
  111.     void readFf_blk(char *filename, struct stat &s)
  112.     {
  113.         attr = FA_ARCH;
  114.         if (S_ISDIR(s.st_mode)) attr |= FA_DIREC;
  115.         strcpy(name, filename);
  116.         size = s.st_size;
  117.  
  118.         ftime t;
  119.         struct tm *broken = localtime(&s.st_mtime);
  120.         t.ft_tsec = broken->tm_sec / 2;
  121.         t.ft_min = broken->tm_min;
  122.         t.ft_hour = broken->tm_hour;
  123.         t.ft_day = broken->tm_mday;
  124.  
  125.         /*
  126.          * Month value should begin at 1.
  127.          * Date: Thu, 23 Jan 1997 11:34:50 +0100 (MET)
  128.          */
  129.         t.ft_month = broken->tm_mon + 1;
  130.         t.ft_year = broken->tm_year - 80;
  131.         time = *(long *) &t;
  132.     }
  133.  
  134.     void *operator new( size_t );
  135.  
  136. };
  137.  
  138. void *DirSearchRec::operator new( size_t sz )
  139. {
  140.     void *temp = ::operator new( sz );
  141.     if( TVMemMgr::safetyPoolExhausted() )
  142.         {
  143.         delete temp;
  144.         temp = 0;
  145.         }
  146.     return temp;
  147. }
  148.  
  149. void TFileList::readDirectory( const char *aWildCard )
  150. {
  151.     /* SS: changed */
  152.  
  153.     DIR *dp;
  154. //    DirSearchRec *p;    /* XXX */
  155.     DirSearchRec *p = NULL;    /* XXX */
  156.     char dir[PATH_MAX];
  157.     char file[PATH_MAX];
  158.     char path[PATH_MAX];
  159.     char *np;
  160.     dirent *de;
  161.     glob_t gl;
  162.     struct stat s;
  163.  
  164.     strcpy( path, aWildCard );
  165.     if (!isWild(path)) strcat(path, "*");
  166.     fexpand( path );
  167.     expandPath(path, dir, file);
  168.     TFileCollection *fileList = new TFileCollection( 5, 5 );
  169.  
  170.     /* find all filenames that match our wildcards */
  171.  
  172.     /*
  173.      * The use of 'glob' function was proposed by:
  174.      * Rainer Keuchel <r_keuchel@smaug.netwave.de>
  175.      * Date: 18 Jan 1997 22:52:12 +0000
  176.      */
  177. #ifdef GLOB_PERIOD
  178.     if (glob(path, GLOB_PERIOD, NULL, &gl) == 0)
  179. #else
  180.     if (glob(path, 0, NULL, &gl) == 0)
  181. #endif
  182.         for (int i = 0; i < gl.gl_pathc; i++)
  183.     {
  184.         /* is this a regular file ? */
  185.  
  186.         if (stat(gl.gl_pathv[i], &s) == 0 && S_ISREG(s.st_mode))
  187.         {
  188.             if ((p = new DirSearchRec) == NULL) break;
  189.  
  190.             /* strip directory part */
  191.  
  192.             if ((np = strrchr(gl.gl_pathv[i], '/')) != NULL) np++;
  193.             else np = gl.gl_pathv[i];
  194.             p->readFf_blk(np, s);
  195.             fileList->insert( p );
  196.         }
  197.     }
  198.     globfree(&gl);
  199.  
  200.     /* now read all directory names */
  201.  
  202.     sprintf(path, "%s.", dir);
  203.     if ((dp = opendir(path)) != NULL)
  204.     {
  205.         while ((de = readdir(dp)) != NULL)
  206.         {
  207.             /* we don't want these directories */
  208.  
  209.             if (strcmp(de->d_name, ".") == 0 ||
  210.                 strcmp(de->d_name, "..") == 0) continue;
  211.  
  212.             /* is it a directory ? */
  213.  
  214.             sprintf(path, "%s%s", dir, de->d_name);
  215.             if (stat(path, &s) == 0 && S_ISDIR(s.st_mode))
  216.             {
  217.                 if ((p = new DirSearchRec) == NULL) break;
  218.                 p->readFf_blk(de->d_name, s);
  219.                 fileList->insert( p );
  220.             }
  221.         }
  222.         closedir(dp);
  223.     }
  224.  
  225.     if( strlen( dir ) > 1 )
  226.         {
  227.         p = new DirSearchRec;
  228.         if( p != 0 )
  229.             {
  230.         sprintf(path, "%s..", dir);
  231.         if (stat(path, &s) == 0) p->readFf_blk("..", s);
  232.         else
  233.         {
  234.             strcpy( p->name, ".." );
  235.             p->size = 0;
  236.             p->time = 0x210000uL;
  237.             p->attr = FA_DIREC;
  238.         }
  239.         fileList->insert( p );
  240.             }
  241.         }
  242.  
  243.     if( p == 0 )
  244.         messageBox( tooManyFiles, mfOKButton | mfWarning );
  245.     newList(fileList);
  246.     if( list()->getCount() > 0 )
  247.         message( owner, evBroadcast, cmFileFocused, list()->at(0) );
  248.     else
  249.         {
  250.         static DirSearchRec noFile;
  251.         message( owner, evBroadcast, cmFileFocused, &noFile );
  252.         }
  253. }
  254.  
  255. #if !defined(NO_STREAMABLE)
  256.  
  257. TStreamable *TFileList::build()
  258. {
  259.     return new TFileList( streamableInit );
  260. }
  261.  
  262. #endif
  263.