home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CLASSINC.PAK / FILENAME.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  7.0 KB  |  255 lines

  1. //----------------------------------------------------------------------------
  2. // Borland Class Library
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.10  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #if !defined(CLASSLIB_FILENAME_H)
  9. #define CLASSLIB_FILENAME_H
  10.  
  11. #if !defined(CLASSLIB_DEFS_H)
  12. # include <classlib/defs.h>
  13. #endif
  14. #if !defined(SERVICES_CSTRING_H)
  15. # include <services/cstring.h>
  16. #endif
  17. #include <dir.h>  // struct ffblk
  18.  
  19. #if defined(BI_NAMESPACE)
  20. namespace ClassLib {
  21. #endif
  22.  
  23. struct TFileStatus;
  24.  
  25. //
  26. // class TFileName
  27. // ~~~~~ ~~~~~~~~~
  28. class _BIDSCLASS TFileName {
  29.   public:
  30.     // Construct an empty filename
  31.     //
  32.     TFileName();
  33.  
  34.     // Construct a filename from its parts. OK to pass 0 to skip part
  35.     //
  36.     TFileName(const _TCHAR far* serverName, const _TCHAR far* shareOrDeviceName,
  37.               const _TCHAR far* path, const _TCHAR far* file,
  38.               const _TCHAR far* ext);
  39.  
  40.     // Construct a filename from a freeform string
  41.     //
  42.     TFileName(const _TCHAR far* freeform);
  43.     TFileName(const string& freeform);
  44.  
  45.     // Construct a filename from another filename
  46.     //
  47.     TFileName(const TFileName& src);
  48.  
  49.     // Assign new filename or freeform to this name
  50.     //
  51.     TFileName& operator =(const TFileName& src);
  52.     TFileName& operator =(const string& src);
  53.     TFileName& operator =(const _TCHAR far* src);
  54.  
  55.     // Construct a filename representing a special file or directory
  56.     //
  57.     enum TSpecialType {
  58.       TempFile,     // A temporary filename
  59.       TempDir,      // Location of temporary files
  60.       CurrentDir,   // Current working directory if any
  61.       BootDir,      // Root dir of boot device (LDID_BOOT)
  62.       MachineDir,   // (LDID_MACHINE)
  63.       HomeDir,      // Home directory for OS (LDID_WIN)
  64.       SharedDir,    // Shared home directory for OS (LDID_SHARED)
  65.       SysDir,       // Location of system files (LDID_SYS)
  66.       ComputerName,
  67.     };
  68.     TFileName(TSpecialType type);
  69.  
  70.     // Normal fully qualified path string, & short filename version of it
  71.     // Convert a possibly logical drive based name to a UNC name if indicated
  72.     //
  73.     string Canonical(bool forceUNC = false) const;
  74.     string ShortName(bool forceUNC = false) const;
  75.  
  76.     // Nice, human readable rendering of the filename using system conventions
  77.     // (Returns null string for directory names or malformed names.  For
  78.     // directories, try Canonical() instead.)
  79.     //
  80.     string Title() const;
  81.  
  82.     // Human readable form of the filename squeezed to fit in 'maxLen' _TCHARs
  83.     //
  84.     string Squeezed(int maxLen) const;
  85.  
  86.     // Obtain any combination of various filename parts. Seperators inserted
  87.     // only as appropriate
  88.     //
  89.     enum TPart {
  90.       Server = 1,  // Server name
  91.       Device = 2,  // Logical device or sharename
  92.       Path   = 4,  // Directory path to the file
  93.       File   = 8,  // Filename part without the extension
  94.       Ext    =16,  // Extension
  95.     };
  96.     string GetParts(uint p) const;      // Return assembled string of parts
  97.     bool   HasParts(uint p) const;      // Does name have an given part
  98.     void   SetPart(uint p, const string& partStr);    // Modify sub part
  99.     void   MergeParts(uint p, const TFileName& source); // Modify sub parts
  100.     void   StripParts(uint p);         // Remove indicated parts
  101.  
  102.     // Information about the filename
  103.     //
  104.     bool IsValid() const;       // Is a valid name of any kind
  105.     bool IsUNC() const;         // Is UNC vs logical drive based name
  106.     bool Exists() const;        // Does device/dir/file exist?
  107.  
  108.     // Is another filename equivalent to this filename?
  109.     //
  110.     bool operator ==(const TFileName& other) const;
  111.  
  112.     // Remove the file or dir associated with this filename.
  113.     //
  114.     void Remove() const;
  115.  
  116.     // Move (rename) the file associated with this filename, and change this
  117.     // name to the new name
  118.     //
  119.     enum { ReplaceExisting=1, CopyAllowed=2, DelayUntilReboot=4 };
  120.     void Move(const TFileName& newName, uint32 how = CopyAllowed);
  121.  
  122.     // Copy the file associated with this filename to a new file
  123.     //
  124.     void Copy(const TFileName& newName, bool failIfExists) const;
  125.  
  126.     int ChangeDir() const;
  127.     int CreateDir() const;
  128.  
  129.     // Get and Set the file status struct for the item associated with this
  130.     // filename
  131.     //
  132.     int GetStatus(TFileStatus& status) const;
  133.     int SetStatus(const TFileStatus& status);
  134.  
  135.     // Other ideas...
  136.     //
  137.     static const _TCHAR* WildName() {return "*";}       // General wildstring
  138.     static const _TCHAR* WildPart(uint p) {return "*";} // Part specific?
  139.  
  140.   private:
  141.     void   Parse(const _TCHAR far* freeform);
  142.  
  143.     bool   Unc;
  144.     string ServerStr;
  145.     string DeviceStr;
  146.     string PathStr;
  147.     string FileStr;
  148.     string ExtStr;
  149. };
  150.  
  151. //
  152. // class TFileNameIterator
  153. // ~~~~~ ~~~~~~~~~~~~~~~~~
  154. class _BIDSCLASS TFileNameIterator {
  155.   public:
  156.     TFileNameIterator(const string& wildName);
  157.    ~TFileNameIterator();
  158.  
  159. //    operator bool() const;
  160.  
  161. #if defined(BI_PLAT_WIN32)
  162.     operator const TCHAR*() const;
  163.  
  164.     const WIN32_FIND_DATA& operator ++();
  165.     const WIN32_FIND_DATA& Current() const;
  166.  
  167. #elif defined(BI_PLAT_WIN16)
  168.     operator const _TCHAR*() const;
  169.  
  170.     const ffblk& operator ++();
  171.     const ffblk& Current() const;
  172. #endif
  173.  
  174.   private:
  175. #if defined(BI_PLAT_WIN32)
  176.     WIN32_FIND_DATA Data;
  177.     HANDLE          Handle;
  178. #elif defined(BI_PLAT_WIN16)
  179.     ffblk           Data;
  180. #endif
  181.     bool            Done;
  182. };
  183.  
  184. #if defined(BI_NAMESPACE)
  185. }   // namespace ClassLib
  186. #endif
  187.  
  188. //----------------------------------------------------------------------------
  189. // Inlines
  190. //
  191. #if !defined(CLASSLIB_FILE_H)
  192. # include <classlib/file.h>
  193. #endif
  194.  
  195. inline TFileName& TFileName::operator =(const string& src)
  196. {
  197.   Parse(src.c_str());
  198.   return *this;
  199. }
  200.  
  201. inline TFileName& TFileName::operator =(const _TCHAR far* src)
  202. {
  203.   Parse(src);
  204.   return *this;
  205. }
  206.  
  207. inline bool TFileName::operator ==(const TFileName& other) const
  208. {
  209.   string::set_case_sensitive(0);         // maybe true for some OSs
  210.   return Canonical() == other.Canonical();
  211. }
  212.  
  213. inline bool TFileName::IsUNC() const
  214. {
  215.   return Unc;
  216. }
  217.  
  218. inline int TFileName::GetStatus(TFileStatus& status) const
  219. {
  220.   return TFile::GetStatus(Canonical().c_str(), status);
  221. }
  222.  
  223. inline int TFileName::SetStatus(const TFileStatus& status)
  224. {
  225.   return TFile::SetStatus(Canonical().c_str(), status);
  226. }
  227.  
  228. #if defined(BI_PLAT_WIN32)
  229. inline const WIN32_FIND_DATA& TFileNameIterator::Current() const
  230. #elif defined(BI_PLAT_WIN16)
  231. inline const ffblk& TFileNameIterator::Current() const
  232. #endif
  233. {
  234.   return Data;
  235. }
  236.  
  237. //inline TFileNameIterator::operator bool() const
  238. //{
  239. //  return !Done;
  240. //}
  241.  
  242. #if defined(BI_PLAT_WIN32)
  243. inline TFileNameIterator::operator const TCHAR*() const
  244. {
  245.   return Done ? 0 : Data.cFileName;
  246. }
  247. #elif defined(BI_PLAT_WIN16)
  248. inline TFileNameIterator::operator const _TCHAR*() const
  249. {
  250.   return Done ? 0 : Data.ff_name;
  251. }
  252. #endif
  253.  
  254. #endif  // CLASSLIB_FILENAME_H
  255.