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

  1. //----------------------------------------------------------------------------
  2. // Borland Class Library
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.8  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #if !defined(CLASSLIB_FILE_H)
  9. #define CLASSLIB_FILE_H
  10.  
  11. #if !defined(CLASSLIB_DEFS_H)
  12. # include <classlib/defs.h>
  13. #endif
  14. #if !defined(CLASSLIB_TIME_H)
  15. # include <classlib/time.h>
  16. #endif
  17. #if !defined(CLASSLIB_DATE_H)
  18. # include <classlib/date.h>
  19. #endif
  20. #if defined(BI_HAS_HREADWRITE) && !defined(SERVICES_WSYSINC_H)
  21. # include <services/wsysinc.h>
  22. #endif
  23. #if !defined (__TCHAR_H)
  24. # include <tchar.h>
  25. #endif
  26.  
  27. #if !defined(__STDLIB_H) && !defined(_INC_STDLIB)
  28. # include <stdlib.h>
  29. #endif
  30. #if !defined(__STDIO_H) && !defined(_INC_STDIO)
  31. # include <stdio.h>
  32. #endif
  33. #if !defined(__FCNTL_H) && !defined(_INC_FCNTL)
  34. # include <fcntl.h>
  35. #endif
  36. #if !defined(__STAT_H) && !defined(_INC_STAT)
  37. # include <sys/stat.h>
  38. #endif
  39. #if !defined(__SHARE_H)
  40. # include <share.h>
  41. #endif
  42. #if !defined(__IO_H)
  43. # include <io.h>
  44. #endif
  45. #if !defined(__DOS_H)
  46. # include <dos.h>
  47. #endif
  48.  
  49. #if defined(BI_COMP_MSC) && !defined(_INC_LOCKING)
  50. # include <sys/locking.h>
  51. #endif
  52.  
  53. #if defined(BI_CLASSLIB_NO_po)
  54. # pragma option -po-
  55. #endif
  56.  
  57. #if defined(BI_NAMESPACE)
  58. namespace ClassLib {
  59. #endif
  60.  
  61. class _BIDSCLASS TFileName;
  62.  
  63. //
  64. //
  65. //
  66. struct TFileStatus {
  67.     TTime createTime;
  68.     TTime modifyTime;
  69.     TTime accessTime;
  70.     long  size;
  71.     uint8 attribute;
  72.     _TCHAR  fullName[_MAX_PATH];
  73. };
  74.  
  75. #if defined(BI_NAMESPACE)
  76. }       // namespace ClassLib
  77. #endif
  78.  
  79. class _EXPCLASS ostream;
  80.  
  81. #if defined(BI_NAMESPACE)
  82. namespace ClassLib {
  83. #endif
  84.  
  85. ostream _BIDSFAR & _BIDSFUNC operator << (ostream _BIDSFAR&, const TFileStatus _BIDSFAR&);
  86.  
  87. //
  88. // class TFile
  89. // ~~~~~ ~~~~~
  90. class _BIDSCLASS TFile {
  91.   public:
  92.     enum { FileNull = -1 };
  93.  
  94.     // Open mode
  95.     //
  96.     enum {
  97.       ReadOnly    = O_RDONLY,
  98.       ReadWrite   = O_RDWR,
  99.       WriteOnly   = O_WRONLY,
  100.       Create      = O_CREAT | O_TRUNC,
  101.       CreateExcl  = O_CREAT | O_EXCL,
  102.       Append      = O_APPEND,
  103.  
  104.     // Share mode
  105.     //
  106. #if defined(BI_PLAT_WIN32)
  107.       Compat      = 0,        // SH_COMPAT
  108.       DenyNone    = SH_DENYNO,
  109. #else
  110.       Compat      = SH_COMPAT,
  111.       DenyNone    = SH_DENYNONE,
  112. #endif
  113.       DenyRead    = SH_DENYRD,
  114.       DenyWrite   = SH_DENYWR,
  115.       DenyRdWr    = SH_DENYRW,
  116.       NoInherit   = O_NOINHERIT
  117.     };
  118.  
  119.     // Permission flags for Create
  120.     //
  121.     enum {
  122.       PermRead    = S_IREAD,
  123.       PermWrite   = S_IWRITE,
  124.       PermRdWr    = S_IREAD | S_IWRITE
  125.     };
  126.  
  127.     // DOS file type flags
  128.     //
  129.     enum {
  130.       Normal      = _A_NORMAL,  // FA_NORMAL
  131.       RdOnly      = _A_RDONLY,  // FA_RDONLY
  132.       Hidden      = _A_HIDDEN,  // FA_HIDDEN
  133.       System      = _A_SYSTEM,  // FA_SYSTEM
  134.       Volume      = 0x08,       // FA_LABEL
  135.       Directory   = _A_SUBDIR,  // FA_DIREC
  136.       Archive     = _A_ARCH,    // FA_ARCH
  137.     };
  138.  
  139.     enum seek_dir {
  140.       beg = 0,
  141.       cur = 1,
  142.       end = 2
  143.     };
  144.  
  145.     TFile();
  146.     TFile(int handle);
  147.     TFile(const TFile _BIDSFAR& file);
  148.     TFile(const _TCHAR _BIDSFAR* name,
  149.           uint16 access = ReadOnly,
  150.           uint16 permission = PermRdWr);
  151.    ~TFile();
  152.  
  153.     int  Open(const _TCHAR _BIDSFAR* name, uint16 access, uint16 permission);
  154.     int  Close();
  155.  
  156.     int  GetHandle() const;
  157.  
  158.     long Position() const;
  159.     long Length() const;
  160.     void Length(long newLen);
  161.     long Seek(long offset, int origin = beg);
  162.     long SeekToBegin();
  163.     long SeekToEnd();
  164.     int  GetStatus(TFileStatus _BIDSFAR& status) const;
  165.     int  IsOpen() const;
  166.  
  167.     int  Read(void _BIDSFAR* buffer, int numBytes);
  168. #if defined(BI_HAS_HREADWRITE)
  169.     long Read(void __huge* buffer, long numBytes);
  170. #endif
  171.     int  Write(const void _BIDSFAR* buffer, int numBytes);
  172. #if defined(BI_HAS_HREADWRITE)
  173.     long Write(const void __huge* buffer, long numBytes);
  174. #endif
  175.     void Flush();
  176.  
  177.     void LockRange(long position, uint32 count);
  178.     void UnlockRange(long Position, uint32 count);
  179.  
  180.     static int GetStatus(const _TCHAR _BIDSFAR* name, TFileStatus _BIDSFAR& status);
  181.     static int SetStatus(const _TCHAR _BIDSFAR* name, const TFileStatus _BIDSFAR& status);
  182.     static void Remove(const _TCHAR _BIDSFAR* name);
  183.     static void Rename(const _TCHAR _BIDSFAR* oldName, const _TCHAR _BIDSFAR* newName);
  184.  
  185.   private:
  186.     int Handle;         // Low-level C file handle
  187.     int ShouldClose;    // Should C++ object close file on dtor
  188. };
  189.  
  190. //---------------------------------------------------------------------------
  191. // Inlines
  192. //
  193.  
  194. inline TFile::TFile() : Handle(FileNull), ShouldClose(0)
  195. {
  196. }
  197.  
  198. inline TFile::TFile(int h) : Handle(h), ShouldClose(0)
  199. {
  200. }
  201.  
  202. inline TFile::TFile(const TFile& file) : ShouldClose(1)
  203. {
  204.   Handle = ::dup(file.Handle);
  205. }
  206.  
  207. inline TFile::TFile(const _TCHAR* name, uint16 access, uint16 permission) :
  208.   Handle(FileNull),
  209.   ShouldClose(1)
  210. {
  211.   Open(name, access, permission);
  212. }
  213.  
  214. inline TFile::~TFile()
  215. {
  216.   if (IsOpen() && ShouldClose)
  217.     ::close(Handle);
  218. }
  219.  
  220. inline int TFile::GetHandle() const
  221. {
  222.   return Handle;
  223. }
  224.  
  225. inline long TFile::Position() const
  226. {
  227.   return ::tell(Handle);
  228. }
  229.  
  230. inline int TFile::IsOpen() const
  231. {
  232.   return Handle > FileNull;
  233. }
  234.  
  235. inline void TFile::Length(long newLen)
  236. {
  237.   ::chsize(Handle, newLen);
  238. }
  239.  
  240. inline long TFile::Seek(long offset, int origin)
  241. {
  242.   return ::lseek(Handle, offset, origin);
  243. }
  244.  
  245. inline long TFile::SeekToBegin()
  246. {
  247.   return ::lseek(Handle, 0, beg);
  248. }
  249.  
  250. inline long TFile::SeekToEnd()
  251. {
  252.   return ::lseek(Handle, 0, end);
  253. }
  254.  
  255. inline int TFile::Read(void* buffer, int numBytes)
  256. {
  257. #if defined(BI_COMP_BORLANDC)
  258.   return ::_rtl_read(Handle, buffer, numBytes);
  259. #else
  260.   return ::read(Handle, buffer, numBytes);
  261. #endif
  262. }
  263.  
  264. inline int TFile::Write(const void* buffer, int numBytes)
  265. {
  266. #if defined(BI_COMP_BORLANDC)
  267.   return ::_rtl_write(Handle, buffer, numBytes);
  268. #else
  269.   return ::write(Handle, buffer, numBytes);
  270. #endif
  271. }
  272.  
  273. inline void TFile::Flush()
  274. {
  275.   ::close(::dup(Handle));
  276. }
  277.  
  278. inline void TFile::LockRange(long position, uint32 count)
  279. {
  280. #if defined(BI_COMP_MSC)
  281.   ::_lseek(Handle, position, SEEK_SET);
  282.   ::_locking(Handle, _LK_LOCK, count);
  283. #else
  284.   ::lock(Handle, position, count);
  285. #endif
  286. }
  287.  
  288. inline void TFile::UnlockRange(long position, uint32 count)
  289. {
  290. #if defined(BI_COMP_MSC)
  291.   ::_lseek(Handle, position, SEEK_SET);
  292.   ::_locking(Handle, _LK_UNLCK, count);
  293. #else
  294.   ::unlock(Handle, position, count);
  295. #endif
  296. }
  297.  
  298. inline void TFile::Remove(const _TCHAR* name)
  299. {
  300.   ::_tremove(name);
  301. }
  302.  
  303. inline void TFile::Rename(const _TCHAR* oldName, const _TCHAR* newName)
  304. {
  305.   ::_trename(oldName, newName);
  306. }
  307.  
  308. #if defined(BI_HAS_HREADWRITE)
  309. inline long TFile::Read(void __huge* Buffer, long NumBytes)
  310. {
  311.   return _hread(Handle, Buffer, NumBytes);
  312. }
  313.  
  314. inline long TFile::Write(const void __huge* Buffer, long NumBytes)
  315. {
  316.   return _hwrite(Handle, Buffer, NumBytes);
  317. }
  318. #endif
  319.  
  320. #if defined(BI_NAMESPACE)
  321. }   // namespace ClassLib
  322. #endif
  323.  
  324. #if defined(BI_CLASSLIB_NO_po)
  325. # pragma option -po.
  326. #endif
  327.  
  328. #endif  // CLASSLIB_FILE_H
  329.