home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / src / TDirCollection.cpp < prev    next >
C/C++ Source or Header  |  1999-05-21  |  2KB  |  113 lines

  1. /*
  2.  * TDirCollection.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_TDirCollection
  13. #define Uses_TDirEntry
  14. #define Uses_opstream
  15. #define Uses_ipstream
  16. #include <tvision/tv.h>
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/stat.h>
  21. #ifdef __OS2__
  22. #include <dir.h>
  23. #else
  24. #include <unistd.h>
  25. #endif
  26.  
  27. Boolean isDir( const char *str )
  28. {
  29.     /* SS: all changed */
  30.     struct stat s;
  31.  
  32.     if (stat(str, &s) == 0 && S_ISDIR(s.st_mode)) return True;
  33.     return False;
  34. }
  35.  
  36. Boolean pathValid( const char *path )
  37. {
  38.     /* SS: all changed */
  39.     char dir[PATH_MAX];
  40.     char name[PATH_MAX];
  41.  
  42.     expandPath(path, dir, name);
  43.     if (strcmp(dir, "/") == 0) strcat(dir, ".");
  44.     else {
  45.         if( dir[strlen(dir)-1] == '/' )    strcat(dir, ".");
  46.         else strcat(dir, "/.");
  47.         }
  48.     return isDir(dir);
  49. }
  50.  
  51. Boolean validFileName( const char *fileName )
  52. {
  53.     /* SS: all changed */
  54.     FILE *f;
  55.  
  56.     /*
  57.      * Patch from: Vasily Egoshin <wasa@nica.marstu.mari.su>
  58.      * Date: Thu, 9 Jan 1997 16:36:10 +0300 (MSK)
  59.      */
  60.     if ((f = fopen(fileName, "r")) != NULL)
  61.     {
  62.         /* the file exists and is readable ===> file name ok */
  63.  
  64.         fclose(f);
  65.         return True;
  66.     }
  67.     if ((f = fopen(fileName, "w")) != NULL)
  68.     {
  69.         /* file don't exists but it is writable ===> file name ok */
  70.  
  71.         fclose(f);
  72.         remove(fileName);
  73.         return True;
  74.     }
  75.     return False;    /* illegal file name */
  76. }
  77.  
  78. void getCurDir( char *dir )
  79. {
  80.     /* SS: all changed */
  81.     getcwd(dir, PATH_MAX);
  82.     backSlash2Slash( dir );
  83.     if (strcmp(dir, "/") != 0) strcat(dir, "/");
  84. }
  85.  
  86. Boolean isWild( const char *f )
  87. {
  88.     return Boolean( strpbrk( f, "?*" ) != 0 );
  89. }
  90.  
  91.  
  92. TStreamable *TDirCollection::build()
  93. {
  94.     return new TDirCollection( streamableInit );
  95. }
  96.  
  97. void TDirCollection::writeItem( void *obj, opstream& os )
  98. {
  99.     TDirEntry *item = (TDirEntry *)obj;
  100.     os.writeString( item->text() );
  101.     os.writeString( item->dir() );
  102. }
  103.  
  104. void *TDirCollection::readItem( ipstream& is )
  105. {
  106.     char *txt = is.readString();
  107.     char *dir = is.readString();
  108.     TDirEntry *entry = new TDirEntry( txt, dir );
  109.     delete txt;
  110.     delete dir;
  111.     return entry;
  112. }
  113.