home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / src / TFileList.cpp < prev    next >
C/C++ Source or Header  |  1999-05-26  |  6KB  |  299 lines

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