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

  1. /*------------------------------------------------------------*/
  2. /* filename -       tdircoll.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  TDirCollection 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_TDirCollection
  19. #define Uses_TDirEntry
  20. #define Uses_opstream
  21. #define Uses_ipstream
  22. #include <tv.h>
  23.  
  24. #if !defined( __DIR_H )
  25. #include <Dir.h>
  26. #endif  // __DIR_H
  27.  
  28. #if !defined( __STRING_H )
  29. #include <String.h>
  30. #endif  // __STRING_H
  31.  
  32. #if !defined( __DOS_H )
  33. #include <Dos.h>
  34. #endif  // __DOS_H
  35.  
  36. #pragma warn -asc
  37.  
  38. Boolean driveValid( char drive )
  39. {
  40.     asm {
  41.         MOV DL, drive
  42.         MOV AH, 36h
  43.         SUB DL, 'A'-1
  44.         INT 21h
  45.         INC AX
  46.         JE  __1
  47.         MOV AL, 1
  48.         }
  49. __1:
  50.     return Boolean(_AX); 
  51. }
  52.  
  53. #pragma warn .asc
  54.  
  55. Boolean isDir( const char *str )
  56. {
  57.     ffblk ff;
  58.     return Boolean( findfirst( str, &ff, FA_DIREC ) == 0 &&
  59.                     (ff.ff_attrib & FA_DIREC) != 0 );
  60. }
  61.  
  62. Boolean pathValid( const char *path )
  63. {
  64.     char expPath[MAXPATH];
  65.     strcpy( expPath, path );
  66.     fexpand( expPath );
  67.     int len = strlen(expPath);
  68.     if( len <= 3 )
  69.         return driveValid(expPath[0]);
  70.  
  71.     if( expPath[len-1] == '\\' )
  72.         expPath[len-1] = EOS;
  73.  
  74.     return isDir( expPath );
  75. }
  76.  
  77. Boolean validFileName( const char *fileName )
  78. {
  79.     static const char * const illegalChars = ";,=+<>|\"[] \\";
  80.  
  81.     char path[MAXPATH];
  82.     char dir[MAXDIR];
  83.     char name[MAXFILE];
  84.     char ext[MAXEXT];
  85.  
  86.     fnsplit( fileName, path, dir, name, ext );
  87.     strcat( path, dir );
  88.     if( *dir != EOS && !pathValid( path ) )
  89.         return False;
  90.     if( strpbrk( name, illegalChars ) != 0 ||
  91.         strpbrk( ext+1, illegalChars) != 0 ||
  92.         strchr( ext+1, '.' ) != 0
  93.       )
  94.         return False;
  95.     return True;
  96. }
  97.  
  98. void getCurDir( char *dir )
  99. {
  100.     dir[0] = getdisk() + 'A';
  101.     dir[1] = ':';
  102.     dir[2] = '\\';
  103.     getcurdir( 0, dir+3 );
  104.     if( strlen( dir ) > 3 )
  105.         strcat( dir, "\\" );
  106. }
  107.  
  108. Boolean isWild( const char *f )
  109. {
  110.     return Boolean( strpbrk( f, "?*" ) != 0 );
  111. }
  112.  
  113. TStreamable *TDirCollection::build()
  114. {
  115.     return new TDirCollection( streamableInit );
  116. }
  117.  
  118. void TDirCollection::writeItem( void *obj, opstream& os )
  119. {
  120.     TDirEntry *item = (TDirEntry *)obj;
  121.     os.writeString( item->text() );
  122.     os.writeString( item->dir() );
  123. }
  124.  
  125. void *TDirCollection::readItem( ipstream& is )
  126. {
  127.     const char *txt = is.readString();
  128.     const char *dir = is.readString();
  129.     return new TDirEntry( txt, dir );
  130. }
  131.