OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindwos
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4// Copyright (c) 1998 by Yura Bidus, All Rights Reserved
5//
6// Fully rewritten by Yura Bidus
7//----------------------------------------------------------------------------
8
9#if !defined(OWL_FILE_H)
10#define OWL_FILE_H
11
12#include <owl/private/defs.h>
13#if defined(BI_HAS_PRAGMA_ONCE)
14# pragma once
15#endif
16
17#include <owl/time.h>
18#include <owl/except.h>
19#include <mmiscapi.h> // FOURCC_RIFF
20#include <fstream>
21
22namespace owl {
23
24#include <owl/preclass.h>
25
26/// \addtogroup utility
27/// @{
28
29
30//------------------------------------------------------------------------------
31//
32/// \struct TFileStatus
33// ~~~~~~ ~~~~~~~~~~~
34//
43
45
46//
47/// The byte order type.
49 boLittle_Endian, ///< LSB at lowest address: Intel //
50 boBig_Endian, ///< MSB at lowest address: Motorola //
51};
52
53//
54//
55//
57{
58 uint16 w=0x0001;
59 uint8 *b=reinterpret_cast<uint8*>(&w);
60 return (b[0] ? boLittle_Endian : boBig_Endian);
61}
62
63//------------------------------------------------------------------------------
64/// These identify host program variable types and let the binary I/O package read
65/// and write routines know how to store or retrieve bytes from host buffer arrays
66/// and when to sign-extend.
67//
89#define BINNTYPE (varLastMember+1)
90
91
92//----------------------------------------------------------------------------
93// TBinField - Binary I/O Structure Field Description
94//
95/// The TBinField struct describes a group of like-typed fields in a structure to be
96/// read or written using the Binary I/O Struct calls.
97//
98struct TBinField {
99 TVarType Type; ///< Declared type of struct field
100 int Bytes; ///< # of bytes occupied in file
101 int Count; ///< # of repeatitions of this item
102};
103
104
105#define TFILE_ERROR static_cast<uint32>(-1)
106
107/// Represents a NULL file handle.
108#define FileNull nullptr
109
110#define TFILE64_ERROR uint64(0xFFFFFFFF)
111
112class _OWLCLASS TFileHandle;
113
114//------------------------------------------------------------------------------
115//
116// class TFile
117// ~~~~~ ~~~~~
118/// The TFile class encapsulates standard file characteristics and operations.
119//
121 public:
122 enum TSeekDir {
123 beg = 0, ///< Seek from the beginning of the file.
124 cur = 1, ///< Seek from the current position in the file.
125 end = 2 ///< Seek from the end of the file.
126 };
127
128 /// Open mode -> remapped into OS specific value internally
130 ReadOnly = 0x0001,
131 WriteOnly = 0x0002,
132 ReadWrite = WriteOnly|ReadOnly,
133
134 /// Subsequent open operations on the object will succeed only if write access is requested.
135 PermWrite = 0x0010,
136
137 /// Subsequent open operations on the object will succeed only if read access is requested.
138 PermRead = 0x0020,
139
140 PermReadWrite = PermWrite|PermRead, //
141 PermExclusive = 0x0040, //
142 PermNone = 0x0080, //
143
144 /// Creates a new file. The function fails if the specified file already exists.
145 CreateNew = 0x0100,
146
147 /// Creates a new file. The function overwrites the file if it exists.
148 CreateAlways = 0x0200,
149
150 /// Opens the file. The function fails if the file does not exist.
151 OpenExisting = 0x0400,
152
153 /// Opens the file. Once opened, the file is truncated so that its size is zero
154 /// bytes. The calling process must open the file with at least WriteOnly
155 /// access. The function fails if the file does not exist.
156 TruncateExist = 0x0800,
157
158 Text = 0x1000, ///< type Text are used in derived classes only
159 ///< default type is Binary
160 };
161
162 /// file attributes -> internally remapped into OS values
164 Normal = 0x00,
165 RdOnly = 0x01,
166 Hidden = 0x02,
167 System = 0x04,
168 Volume = 0x08,
169 Directory = 0x10,
170 Archive = 0x20,
171 Temporary = 0x40,
172 };
173
174 /// Binary data type enumerations.
185
186 TFile();
187 TFile(const tstring& fileName, const uint32 mode = ReadOnly|PermRead|OpenExisting);
189 // if TFileHandle not support Clone() will throw TXNotSupportedCall()
190 TFile(const TFile & file);
191 virtual ~TFile();
192
193 virtual TFileHandle* GetHandle() const;
194 const tstring GetName() const;
195
196 virtual bool Open(const tstring& fileName, const uint32 mode = ReadOnly|PermRead|OpenExisting);
197 virtual bool Close();
198
199 virtual bool Length(uint64 newLen);
200 virtual uint64 Position64() const;
201 virtual uint64 Length64() const;
202 virtual bool Length(uint32 newLen);
203 virtual uint32 Position() const;
204 virtual uint32 Length() const;
205
206 virtual uint64 Seek(int64 offset, TSeekDir origin = beg);
207 virtual uint32 Seek(long offset, TSeekDir origin = beg);
208
209 uint64 SeekToBegin64();
210 uint64 SeekToEnd64();
211 uint32 SeekToBegin();
212 uint32 SeekToEnd();
213
214 bool IsOpen() const;
215 uint32 GetOpenMode() const;
216 uint32 LastError();
217
218 virtual uint32 Read(void * buffer, uint32 numBytes);
219 virtual bool Write(const void * buffer, uint32 numBytes);
220 virtual bool Flush();
221
222 uint ReadStruct(void * buffer, TBinType btype, TByteOrderType type);
223 uint ReadStruct(void * buffer, TBinField * fields,
225 static uint ReadStruct(uint8 * readBuf, void * buffer,
227 uint WriteStruct(void * buffer, TBinType btype, TByteOrderType type);
228 uint WriteStruct(void * buffer, TBinField * fields,
230 static uint WriteStruct(uint8 * writeBuf, void * buffer,
232
233 static uint StructSize(TBinField * fields);
234
235 virtual bool LockRange(uint32 position, uint32 count);
236 virtual bool UnlockRange(uint32 position, uint32 count);
237 virtual bool LockRange(uint64 position, uint64 count);
238 virtual bool UnlockRange(uint64 position, uint64 count);
239
240#if defined(OWL5_COMPAT) // must be moved to TFileName
241 // don't use, this functions call right to TFileName
242 static bool GetStatus(LPCTSTR name, TFileStatus & status);
243 // don't use, this functions call right to TFileName
244 static bool SetStatus(LPCTSTR name, const TFileStatus & status);
245#endif
246 bool GetStatus(TFileStatus & status) const;
247
248 // streaming support (internal functions)
249 virtual uint8 readUint8();
250 virtual uint16 readUint16();
251 virtual uint32 readUint32();
252 virtual uint64 readUint64();
253 virtual float readFloat();
254 virtual double readDouble();
255 virtual LPTSTR readString( tchar *);
256
257 virtual void writeUint8( const uint8 );
258 virtual void writeUint16( const uint16 );
259 virtual void writeUint32( const uint32 );
260 virtual void writeUint64( const uint64 );
261 virtual void writeFloat( const float );
262 virtual void writeDouble( const double );
263 virtual void writeString( const tchar *);
264
265 protected:
266 TFileHandle* Handle; ///< Low-level C file handle
267 bool ShouldClose; ///< Should C++ object close file on dtor
268
269 // read/write structure helpers
270
271/// Buffer used with structure read/write.
273
274/// Size of Buffer used with structure read/write.
276};
277
278//------------------------------------------------------------------------------
279// Abstract class
280//
281// class TFileHandle
282// ~~~~~ ~~~~~~~~~~~
283//
285 friend class _OWLCLASS TFile;
286 protected:
288 virtual ~TFileHandle(){}
289 public:
290 virtual uint GetOpenMode() = 0;
291 virtual const tstring GetName() = 0;
292 virtual uint32 LastError() = 0;
293
294 virtual bool IsOpen() = 0;
295 virtual TFileHandle* Clone() const = 0;
296 virtual bool Close() = 0;
297 virtual uint32 Read(void * buffer, uint32 numBytes) = 0;
298 virtual bool Write(const void * buffer, uint32 numBytes) = 0;
299 virtual bool Length(uint64 newLen) = 0;
300 virtual uint64 Position64() const = 0;
301 virtual uint64 Length64() const = 0;
302 virtual bool Length(uint32 newLen) = 0;
303 virtual uint32 Position() const = 0;
304 virtual uint32 Length() const = 0;
305 virtual uint64 Seek(int64 offset, TFile::TSeekDir origin = TFile::beg) = 0;
306 virtual uint32 Seek(long offset, TFile::TSeekDir origin = TFile::beg) = 0;
307 virtual bool Flush() = 0;
308 virtual bool LockRange(uint32 position, uint32 count) = 0;
309 virtual bool UnlockRange(uint32 position, uint32 count) = 0;
310 virtual bool LockRange(uint64 position, uint64 count) = 0;
311 virtual bool UnlockRange(uint64 position, uint64 count) = 0;
312 virtual bool GetStatus(TFileStatus & status) const = 0;
313
314 private:
316};
317///////////////////////////////////
318//
319// class TDiskFileHandle
320// ~~~~~ ~~~~~~~~~~~~~~~
321//
323 friend class _OWLCLASS TFileName;
324 public:
326
327 TDiskFileHandle* Clone() const;
328
329 virtual uint GetOpenMode() { return OpenMode; }
330 virtual const tstring GetName() { return FileName; }
331 virtual uint32 LastError();
332 virtual bool IsOpen() { return Handle != INVALID_HANDLE_VALUE; }
333
334 virtual bool Close();
335 virtual uint32 Read(void * buffer, uint32 numBytes);
336 virtual bool Write(const void * buffer, uint32 numBytes);
337 virtual bool Length(uint64 newLen);
338 virtual uint64 Position64() const;
339 virtual uint64 Length64() const;
340 virtual bool Length(uint32 newLen);
341 virtual uint32 Position() const;
342 virtual uint32 Length() const;
343 virtual uint64 Seek(int64 offset, TFile::TSeekDir origin = TFile::beg);
344 virtual uint32 Seek(long offset, TFile::TSeekDir origin = TFile::beg);
345 virtual bool Flush();
346 virtual bool LockRange(uint32 position, uint32 count);
347 virtual bool UnlockRange(uint32 position, uint32 count);
348 virtual bool LockRange(uint64 position, uint64 count);
349 virtual bool UnlockRange(uint64 position, uint64 count);
350 virtual bool GetStatus(TFileStatus & status) const;
351
352 protected:
357
358 private:
360};
361
362//------------------------------------------------------------------------------
363//
364// class TBufferedFile
365// ~~~~~ ~~~~~~~~~~~~~
366/// The TBufferedFile class is derived from TFile encapsulates standard file
367/// characteristics and operations using buffered I/O.
368//
370 public:
371/// The initial size of the buffer.
372 enum { DefaultBufferSize = 2048};
373
375 TBufferedFile(const tstring& fileName, const uint32 mode = ReadOnly|PermRead|OpenExisting);
376 // if TFileHandle isn't support Clone() will be throw
379
380 virtual ~TBufferedFile();
381
382 virtual bool Close();
383 virtual bool Length(uint64 newLen);
384 virtual uint64 Position64() const;
385 virtual uint64 Length64() const;
386 virtual uint32 Position() const;
387 virtual uint32 Length() const;
388 virtual bool Length(uint32 newLen);
389
390 virtual uint64 Seek(int64 offset, TSeekDir origin = beg);
391 virtual uint32 Seek(long offset, TSeekDir origin = beg);
392
393 virtual uint32 Read(void * buffer, uint32 numBytes);
394 virtual bool Write(const void* buffer, uint32 numBytes);
395 virtual bool Flush();
396
397 // if buffer == 0, allocate memory
398 virtual void SetBuffer(uint8* buffer, uint size, bool shouldDelete = true);
399
400 protected:
401
402 virtual void InitBuffer(uint size = DefaultBufferSize);
403 virtual bool FlushBuffer();
404
405 protected:
406/// Buffer used to store data in.
408
409/// True if the buffer should be deleted.
411
412/// Size of FileBuffer in bytes.
414
415/// Pointer to current position in the buffer.
417
418/// Offset in file to current position.
420
421/// Offset in file to byte 0 of the buffer.
423
424/// Offset in file to the last data byte in the buffer.
426
427/// True if the buffer is empty; false otherwise.
429};
430
431//------------------------------------------------------------------------------
432//
433/// The TTextFile class is derived from TBufferedFile and encapsulates standard file
434/// characteristics and operations using text based buffered I/O.
435//
437 public:
438 TTextFile();
439 TTextFile(const TTextFile & file);
441 TTextFile(const tstring& fileName, const uint32 mode = ReadOnly|PermRead|OpenExisting|Text);
442
443 virtual LPTSTR GetString(LPTSTR buffer, uint32 size);
444 tstring GetString(int size);
445 virtual bool WriteString(LPCTSTR str);
446 bool WriteString(const tstring& str) {return WriteString(str.c_str());}
447
448 // streaming support (internal functions)
449 virtual uint8 readUint8();
450 virtual uint16 readUint16();
451 virtual uint32 readUint32();
452 virtual uint64 readUint64();
453 virtual float readFloat();
454 virtual double readDouble();
455 virtual LPTSTR readString( tchar *);
456
457 virtual void writeUint8( const uint8 );
458 virtual void writeUint16( const uint16 );
459 virtual void writeUint32( const uint32 );
460 virtual void writeUint64( const uint64 );
461 virtual void writeFloat( const float );
462 virtual void writeDouble( const double );
463 virtual void writeString( const tchar *);
464};
465
466//------------------------------------------------------------------------------
467//
468// TFileLineIterator
469//
470/// The TFileLineIterator class is used to iterate through a TTextFile file.
471//
473 public:
475 virtual ~TFileLineIterator();
476
477 const tchar* operator *() const;
478 operator const tchar*() const;
479 const tchar* operator ++();
480 const tchar* Current() const;
481 uint Line() const;
482
483 protected:
485 // all work do here -> must fill LineBuffer
486 virtual bool NextLine();
487
488 protected:
489/// Pointer to the file being iterated through.
491
492/// Buffer lines of text are loaded into.
494
495/// Size of the buffer allocated for loading a line of text.
497
498/// Current line number in buffer. Line numbering starts at 1.
500
501 bool Done;
502/// Set true when another line cannot be loaded.
503};
504
505//------------------------------------------------------------------------------
506//
507/// \class TXBadFormat
508// ~~~~~ ~~~~~~~~~~~~~
509/// The TXBadFormat class is used for throwing exceptions when a bad file format is
510/// encountered.
511//
513 public:
514 TXBadFormat();
515
516 TXBadFormat* Clone();
517 void Throw();
518
519 static void Raise();
520};
521
522// structures for manipulating RIFF headers
523#if !defined(owlFCC)
524# if 0
525# define owlFCC(ch4) (((static_cast<DWORD>(ch4) & 0xFF) << 24) | \
526 ((static_cast<DWORD>(ch4) & 0xFF00) << 8) | \
527 ((static_cast<DWORD>(ch4) & 0xFF0000) >> 8) | \
528 ((static_cast<DWORD>(ch4) & 0xFF000000) >> 24))
529# else
530//# define extFCC(ch4) ((DWORD)(ch4))
531# define owlFCC(ch0, ch1, ch2, ch3) \
532 (static_cast<uint32>(static_cast<uint8>(ch0)) | (static_cast<uint32>(static_cast<uint8>(ch1)) << 8) | \
533 (static_cast<uint32>(static_cast<uint8>(ch2)) << 16) | (static_cast<uint32>(static_cast<uint8>(ch3)) << 24 ))
534# endif
535#endif
536
537//------------------------------------------------------------------------------
538//
539/// RIFF chunk information data structure
540//
541struct TCkInfo {
542 enum ckFlags {
543 ckDirty = 0x0001,
544 ckList = 0x0002,
545 ckRiff = 0x0004,
546 };
547
548 TCkInfo();
549
550 uint32 CkId; ///< chunk ID
551 uint32 Size; ///< chunk size
552 uint32 Type; ///< form type or list type
553 uint32 Offset; ///< offset of data portion of chunk
554 uint32 Flags; ///< flags used by MMIO functions
555};
556
557//------------------------------------------------------------------------------
558//
559// TRiffFile read/write RIFF files
560// ~~~~~~~~~
561/// The TRiffFile class is used for reading and writing RIFF files.
562//
564 public:
566 ffReset = 0x0001,
567 ffFindAny = 0x0002,
568 ffFindChunk = 0x0004,
569 ffFindList = 0x0008,
570 ffFindRiff = 0x0010,
571 };
577
578 public:
579 TRiffFile();
580 TRiffFile(const TRiffFile & file);
582 TRiffFile(const tstring& fileName, const uint32 acess = ReadOnly|PermRead|OpenExisting);
583
584 bool CreateChunk(TCkInfo& info, const TCreateFlags = cfCreateChunk);
585 bool Ascent(TCkInfo& info);
586 bool Descent(TCkInfo& info, TCkInfo* parent = 0, const TFindFlags = ffFindAny);
587};
588
589//------------------------------------------------------------------------------
590//
591/// QuickTime atom information data structure
592//
593struct TQtInfo {
594 enum qtFlags {qtDirty = 0x0001,}; ///< only dirty flag currently (for internal using)
595
596 TQtInfo();
597
599 uint32 Type; ///< chunk identifier
602};
603
604//------------------------------------------------------------------------------
605//
606// TQtFile
607// ~~~~~~~
608//
609/// The TQtFile class is used for reading and writing QuickTime files.
610//
612 public:
614 ffFindAny = 0x0000,
615 ffReset = 0x0001,
616 ffFindChunk = 0x0002,
617 };
618 public:
619 TQtFile();
620 TQtFile(const TQtFile & file);
622 TQtFile(const tstring& fileName, const uint32 mode = ReadOnly|PermRead|OpenExisting);
623
624 bool Ascent(TQtInfo & info);
625 bool Descent(TQtInfo & info, TQtInfo* parent = 0,
626 const TFindFlags = TFindFlags(ffFindChunk|ffReset));
627 bool CreateChunk(TQtInfo & info);
628};
629
630
631//------------------------------------------------------------------------------
632// Not finished
633// class TStreamFile
634// ~~~~~ ~~~~~
635# if defined(UNICODE)
636 //using std::wfstream;
637 typedef std::wfstream _tfstream;
638# else
639 //using std::fstream;
640 typedef std::fstream _tfstream;
641# endif
642
643// class TFileHandle
644// ~~~~~ ~~~~~~~~~~~
645//
646class TStreamFile;
648 friend class TStreamFile;
649 protected:
650 TStreamHandle(TStreamFile* parent, const tstring& filename, uint32 mode);
651 virtual ~TStreamHandle(){}
652
653 public:
654 virtual uint GetOpenMode() { return OpenMode; }
655 virtual const tstring GetName() { return FileName; }
656 virtual uint32 LastError();
657
658 virtual bool IsOpen();
659
660 TStreamHandle* Clone() const;
661
662 virtual bool Close();
663 virtual uint32 Read(void * buffer, uint32 numBytes);
664 virtual bool Write(const void * buffer, uint32 numBytes);
665 virtual bool Length(uint64 newLen);
666 virtual uint64 Position64() const;
667 virtual uint64 Length64() const;
668 virtual bool Length(uint32 newLen);
669 virtual uint32 Position() const;
670 virtual uint32 Length() const;
671 virtual uint64 Seek(int64 offset, TFile::TSeekDir origin = TFile::beg);
672 virtual uint32 Seek(long offset, TFile::TSeekDir origin = TFile::beg);
673 virtual bool Flush();
674 virtual bool LockRange(uint32 position, uint32 count);
675 virtual bool UnlockRange(uint32 position, uint32 count);
676 virtual bool LockRange(uint64 position, uint64 count);
677 virtual bool UnlockRange(uint64 position, uint64 count);
678 virtual bool GetStatus(TFileStatus & status) const;
679
680 protected:
684};
685
686///////////////////////////////////////////////////////////////
687// class TStreamFile
688// ~~~~~ ~~~~~~~~~~~
689//
690// class _OWLCLASS TStreamFile
691// fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'f:\vs70builds\3077\vc\Compiler\Utc\src\P2\p2symtab.c', line 4533)
692class _OWLCLASS TStreamFile : public TFile, public _tfstream {
693 public:
694 TStreamFile();
697 TStreamFile(const tstring& filename, const uint32 mode = ReadOnly|PermRead|OpenExisting);
698 virtual ~TStreamFile(){}
699
700 virtual bool Open(const tstring& fileName, const uint32 mode = ReadOnly|PermRead|OpenExisting);
701};
702//------------------------------------------------------------------------------
703
704/// @}
705
706#include <owl/posclass.h>
707
708//---------------------------------------------------------------------------
709// Inlines
710
711
712//
713/// Creates a TFile object with a file handle of FileNull.
714//
716:
717 Handle(FileNull),ShouldClose(false),Buffer(nullptr),BufSize(0)
718{
719}
720
721//
722/// Creates a TFile object and opens file name with the given attributes.
723//
724inline TFile::TFile(const tstring& fileName, const uint32 mode)
725:
726 Handle(FileNull), ShouldClose(true), Buffer(nullptr), BufSize(0)
727{
728 Open(fileName, mode);
729}
730
731//
732/// Creates a TFile object with a file handle of handle. Set shouldClose true if the
733/// file should be closed on deletion
734/// \note this function can only be used within file.cpp.
735//
737:
738 Handle(handle), ShouldClose(shouldClose), Buffer(nullptr), BufSize(0)
739{
740}
741//
742/// Returns Handle.
744 return Handle;
745}
746//
747/// Returns Name.
748inline const tstring TFile::GetName() const {
750 return Handle->GetName();
751}
752//
753/// Returns true if the file is open, false otherwise.
754//
755inline bool TFile::IsOpen() const {
756 return Handle != FileNull;
757}
758//
759/// Returns OpenMode.
762 return Handle->GetOpenMode();
763}
764//
765/// Repositions the file pointer to the beginning of the file. Returns the position
766/// moved to or TFILE_ERROR on error. To get extended error information, call
767/// LastError.
768//
770 return Seek(0l, beg);
771}
772//
773/// Repositions the file pointer to the end of the file. Returns the position moved
774/// to or TFILE_ERROR on error. To get extended error information, call LastError.
776 return Seek(0l, end);
777}
778//
779/// Repositions the file pointer to the beginning of the file. Returns
780/// the position moved to or TFILE64_ERROR on error. To get extended error
781/// information, call LastError.
783 return Seek(static_cast<int64>(0), beg);
784}
785//
786/// Repositions the file pointer to the end of the file. Returns the
787/// position moved to or TFILE64_ERROR on error. To get extended error information,
788/// call LastError.
790 return Seek(static_cast<int64>(0), end);
791}
792
793//
794/// Returns the last error.
795///
796/// Under Windows 9x/NT this is a call to GetLastError().
797//
800 return Handle->LastError();
801}
802
803//
804/// Reads numBytes from the file into buffer. The number of bytes read is returned.
809
810//
811/// Writes numbytes of buffer to the file. Returns true if the operation is
812/// successful; false otherwise.
813inline bool TFile::Write(const void* buffer, uint32 numBytes){
815 return Handle->Write(buffer, numBytes);
816}
817
818//
819/// Returns the current position of the file pointer. Returns TFILE_ERROR to
820/// indicate an error. To get extended error information, call LastError.
821inline uint32 TFile::Position() const{
823 return Handle->Position();
824}
825
826//
827/// Returns the file length.
828inline uint32 TFile::Length() const{
830 return Handle->Length();
831}
832
833//
834/// Resizes file to newLen. Returns true if successful, false otherwise. The file
835/// must first be opened for success.
838 return Handle->Length(newLen);
839}
840
841//
842/// Repositions the file pointer to offset bytes from the specified origin. Returns
843/// the position moved to or TFILE_ERROR on error. To get extended error
844/// information, call LastError.
849
850//
851/// Resizes file to newLen. Returns true if successful, false
852/// otherwise. The file must first be opened for success.
855 return Handle->Length(newLen);
856}
857//
858/// 32 Bit Only: Returns the current position of the file pointer. Returns
859/// TFILE64_ERROR to indicate an error. To get extended error information, call
860/// LastError.
863 return Handle->Position64();
864}
865//
866/// Returns the file length.
867inline uint64 TFile::Length64() const {
869 return Handle->Length64();
870}
871//
872/// Repositions the file pointer to offset bytes from the specified
873/// origin. Returns the position moved to or TFILE64_ERROR on error. To get extended
874/// error information, call LastError.
880//
881/// Locks count bytes, beginning at position of the file. Returns true
882/// if successful; false otherwise.
884{
886 return Handle->LockRange(position, count);
887}
888//
889/// 32 Bit Only: Unlocks the range at the given Position. Returns true if
890/// successful; false otherwise.
892{
894 return Handle->UnlockRange(position, count);
895}
896//
897/// Fills status with the current file status. Returns true if successful, false
898/// otherwise.
899inline bool TFile::GetStatus(TFileStatus & status) const
900{
902 return Handle->GetStatus(status);
903}
904//
905// streaming support
907 // this version just read 256 characters ??? for now
908 if(Read(str, 256) == TFILE_ERROR)
909 return nullptr;
910 return str;
911}
912//
913inline void TFile::writeString( const tchar * str){
914 Write(str, static_cast<int>(::_tcslen(str)));
915}
916// inline stream operators
918 c = static_cast<int8>(file.readUint8());
919 return file;
920}
921//
923 c = static_cast<uint8>(file.readUint8());
924 return file;
925}
926//
928 i = static_cast<int16>(file.readUint16());
929 return file;
930}
931//
933 i = file.readUint16();
934 return file;
935}
936//
937inline TFile& operator >> ( TFile& file, signed int& i){
938 i = static_cast<signed int>(file.readUint32());
939 return file;
940}
941//
942inline TFile& operator >> ( TFile& file, unsigned int& i){
943 i = static_cast<uint>(file.readUint32());
944 return file;
945}
946//
947inline TFile& operator >> ( TFile& file, bool& b){
948 b = static_cast<bool>(file.readUint32());
949 return file;
950}
951//
953 i = static_cast<int32>(file.readUint32());
954 return file;
955}
957 i = file.readUint32();
958 return file;
959}
961 i = static_cast<int64>(file.readUint64());
962 return file;
963}
965 i = file.readUint64();
966 return file;
967}
968inline TFile& operator >> ( TFile& file, float& f){
969 f = file.readFloat();
970 return file;
971}
972inline TFile& operator >> ( TFile& file, double& d){
973 d = file.readDouble();
974 return file;
975}
977 file.readString(str);
978 return file;
979}
980// writing support
982 file.writeUint8(static_cast<uint8>(c));
983 return file;
984}
986 file.writeUint8(c);
987 return file;
988}
990 file.writeUint16(static_cast<int16>(i));
991 return file;
992}
994 file.writeUint16(static_cast<uint16>(i));
995 return file;
996}
997inline TFile& operator << ( TFile& file, signed int i){
998 file.writeUint32(static_cast<signed int>(i));
999 return file;
1000}
1001inline TFile& operator << ( TFile& file, unsigned int i){
1002 file.writeUint32(static_cast<uint>(i));
1003 return file;
1004}
1005inline TFile& operator << ( TFile& file, bool b){
1006 file.writeUint32(static_cast<uint32>(b));
1007 return file;
1008}
1010 file.writeUint32(static_cast<int32>(i));
1011 return file;
1012}
1014 file.writeUint32(static_cast<uint32>(i));
1015 return file;
1016}
1018 file.writeUint64(*reinterpret_cast<uint64*>(&i));
1019 return file;
1020}
1022 file.writeUint64(static_cast<uint64>(i));
1023 return file;
1024}
1025inline TFile& operator << ( TFile& file, float f){
1026 file.writeFloat(f);
1027 return file;
1028}
1029inline TFile& operator << ( TFile& file, double d){
1030 file.writeDouble(d);
1031 return file;
1032}
1033inline TFile& operator << ( TFile& file, const tchar * s){
1034 file.writeString(s);
1035 return file;
1036}
1037inline TFile& operator << ( TFile& file, const tstring& s){
1038 size_t size = s.length();
1039 file.writeUint32(static_cast<int>(size));
1040 file.Write(s.c_str(), static_cast<int>(size));
1041 return file;
1042}
1043
1044//
1045// TBufferedFile
1046
1047//
1048/// Creates a TBufferedFile object with a file handle of FileNull and allocated a
1049/// buffer of DefaultBufferSize bytes.
1050//
1055: TFile(file)
1056{
1057 InitBuffer();
1058}
1059//
1060/// Creates a TBufferedFile object with a file handle of handle. Set shouldClose
1061/// true if the file should be closed on deletion
1062/// \note this function can only be used within file.cpp.
1063//
1069//
1070/// Creates a TBufferedFile object and opens file name with the given attributes.
1071//
1073: TFile(fileName, mode)
1074{
1075 InitBuffer();
1076}
1077//
1078/// Flushes the buffer and closes the file. Returns true if successful, false
1079/// otherwise.
1080//
1081inline bool
1083{
1084 if(FlushBuffer())
1085 return TFile::Close();
1086 return false;
1087}
1088/// Flushes the buffer and then resizes file to newLen. Returns true if
1089/// successful, false otherwise. The file must first be opened for success.
1091 if(FlushBuffer())
1092 return TFile::Length(newLen);
1093 return false;
1094}
1095/// Returns the file length plus the length of unwritten data in the buffer.
1099/// Returns the current position of the file pointer. Returns TFILE64_ERROR
1100/// to indicate an error. To get extended error information, call LastError.
1102 return uint64(CurPos);
1103}
1104/// Returns the current position of the file pointer. Returns TFILE_ERROR to
1105/// indicate an error. To get extended error information, call LastError.
1107 return CurPos;
1108}
1109/// Returns the file length plus the length of unwritten data in the buffer.
1111 return TFile::Length()+(CurPos-StartPos);
1112}
1113/// Flushes the buffer and then resizes file to newLen. Returns true if successful,
1114/// false otherwise. The file must first be opened for success.
1116 if(FlushBuffer())
1117 return TFile::Length(newLen);
1118 return false;
1119}
1120//
1121/// Writes any data in the buffer to file and then resets the buffer. Returns true
1122/// if successful; false otherwise.
1123//
1125 if(FlushBuffer())
1126 return TFile::Flush();
1127 return false;
1128}
1129
1130//
1131// TTextFile
1132
1133//
1134/// Creates a TTextFile object with a file handle of FileNull and allocated a buffer
1135/// of DefaultBufferSize bytes.
1136//
1138}
1139//
1140/// Creates a TTextFile object with a file handle of handle. Set shouldClose true if
1141/// the file should be closed on deletion
1142/// \note this function can only be used within file.cpp.
1143//
1148//
1149/// Creates a TTextFile object and opens file name with the given attributes.
1150//
1152: TBufferedFile(fileName, mode)
1153{
1154}
1157{
1158}
1160 return GetString(str, MAX_PATH);
1161}
1162inline void TTextFile::writeString( const tchar * str){
1163 Write(str,static_cast<int>(::_tcslen(str)));
1164}
1165
1166//
1167// TFileLineIterator
1168
1169//
1170/// Protected default constructor. Intializes everything to 0.
1171//
1173: File(nullptr),LineBuffer(nullptr),LineNumber(0),Done(false)
1174{
1175}
1176//
1177/// Returns a pointer to the start of the buffer if the file has not been completely
1178/// iterated through; otherwise returns 0.
1179//
1181 return Done ? nullptr : LineBuffer;
1182}
1183//
1184/// Returns a pointer to the start of the buffer if the file has not been completely
1185/// iterated through; otherwise returns 0.
1186//
1187inline TFileLineIterator::operator const tchar*() const{
1188 return Done ? nullptr : LineBuffer;
1189}
1190//
1191/// Returns a pointer to the start of the buffer if the file has not been completely
1192/// iterated through; otherwise returns 0.
1193//
1194inline const tchar* TFileLineIterator::Current() const{
1195 return Done ? nullptr : LineBuffer;
1196}
1197//
1198/// Loads the next line in the file and then returns a pointer to the start of the
1199/// buffer if the file has not been completely iterated through; otherwise returns 0.
1200//
1202{
1203 Done = !NextLine();
1204 if(!Done){
1205 LineNumber++;
1206 return LineBuffer;
1207 }
1208 return nullptr;
1209}
1210//
1211/// Returns the current line number.
1212//
1214 return LineNumber;
1215}
1216
1217//
1218// TCkInfo
1219//
1221: CkId(0),Size(0),Type(0),Offset(0),Flags(0)
1222{
1223}
1224//
1225// TRiffFile
1226//
1227
1228/// Default constructor. Creates a TRiffFile object with a file handle of FileNull
1229/// and allocated a buffer of DefaultBufferSize bytes.
1231: TBufferedFile()
1232{
1233}
1234/// Creates a TRiffFile object with a filename name and opens the file with mode
1235/// mode.
1237: TBufferedFile(fileName, mode)
1238{
1239}
1242{
1243}
1248//
1249// TQtInfo
1250//
1252: Size(0),Type(0),Offset(0),Flags(0)
1253{
1254}
1255
1256//
1257// TQtFile
1258//
1259/// Default constructor. Creates a TQtFile object with a file handle of FileNull and
1260/// allocated a buffer of DefaultBufferSize bytes.
1262: TBufferedFile()
1263{
1264}
1265/// Creates a TQtFile object with a filename name and opens the file with mode mode.
1272{
1273}
1278//------------------------------------------------------------------------------
1280{
1281}
1287{
1288 Open(filename, mode);
1289}
1290//------------------------------------------------------------------------------
1291
1292} // OWL namespace
1293
1294
1295#endif // OWL_FILE_H
#define PRECONDITION(condition)
Definition checks.h:227
The TBufferedFile class is derived from TFile encapsulates standard file characteristics and operatio...
Definition file.h:369
TBufferedFile()
Creates a TBufferedFile object with a file handle of FileNull and allocated a buffer of DefaultBuffer...
Definition file.h:1051
virtual uint32 Length() const
Returns the file length plus the length of unwritten data in the buffer.
Definition file.h:1110
virtual bool Write(const void *buffer, uint32 numBytes)
Writes numBytes of buffer to the file.
Definition file.cpp:1281
uint32 EndPos
Offset in file to the last data byte in the buffer.
Definition file.h:425
virtual bool FlushBuffer()
Flushes the buffer by writing any unwritten data to the file.
Definition file.cpp:1073
uint8 * CurByte
Pointer to current position in the buffer.
Definition file.h:416
virtual uint64 Length64() const
Returns the file length plus the length of unwritten data in the buffer.
Definition file.h:1096
virtual void InitBuffer(uint size=DefaultBufferSize)
Allocates a buffer of size bytes.
Definition file.cpp:1062
uint FileBufSize
Size of FileBuffer in bytes.
Definition file.h:413
uint8 * FileBuffer
Buffer used to store data in.
Definition file.h:407
bool BufferEmpty
True if the buffer is empty; false otherwise.
Definition file.h:428
virtual uint64 Position64() const
Returns the current position of the file pointer.
Definition file.h:1101
virtual bool Close()
Flushes the buffer and closes the file.
Definition file.h:1082
uint32 StartPos
Offset in file to byte 0 of the buffer.
Definition file.h:422
virtual bool Flush()
Writes any data in the buffer to file and then resets the buffer.
Definition file.h:1124
virtual uint32 Position() const
Returns the current position of the file pointer.
Definition file.h:1106
uint32 CurPos
Offset in file to current position.
Definition file.h:419
bool ShouldDelete
True if the buffer should be deleted.
Definition file.h:410
virtual const tstring GetName()
Definition file.h:330
virtual uint GetOpenMode()
Definition file.h:329
tstring FileName
Definition file.h:356
virtual bool IsOpen()
Definition file.h:332
virtual uint32 Length() const =0
virtual bool LockRange(uint32 position, uint32 count)=0
virtual bool IsOpen()=0
virtual bool Length(uint64 newLen)=0
virtual TFileHandle * Clone() const =0
virtual bool GetStatus(TFileStatus &status) const =0
virtual ~TFileHandle()
Definition file.h:288
virtual bool UnlockRange(uint32 position, uint32 count)=0
virtual bool Write(const void *buffer, uint32 numBytes)=0
virtual uint32 LastError()=0
virtual uint64 Position64() const =0
virtual uint32 Seek(long offset, TFile::TSeekDir origin=TFile::beg)=0
virtual bool Close()=0
virtual uint GetOpenMode()=0
virtual uint32 Read(void *buffer, uint32 numBytes)=0
virtual uint64 Seek(int64 offset, TFile::TSeekDir origin=TFile::beg)=0
virtual bool LockRange(uint64 position, uint64 count)=0
virtual bool UnlockRange(uint64 position, uint64 count)=0
virtual uint32 Position() const =0
virtual bool Flush()=0
virtual const tstring GetName()=0
virtual uint64 Length64() const =0
virtual bool Length(uint32 newLen)=0
The TFile class encapsulates standard file characteristics and operations.
Definition file.h:120
virtual bool Open(const tstring &fileName, const uint32 mode=ReadOnly|PermRead|OpenExisting)
Opens file name with the given mode.
Definition file.cpp:429
bool IsOpen() const
Returns true if the file is open, false otherwise.
Definition file.h:755
virtual uint32 Length() const
Returns the file length.
Definition file.h:828
uint BufSize
Size of Buffer used with structure read/write.
Definition file.h:275
virtual uint64 Seek(int64 offset, TSeekDir origin=beg)
Repositions the file pointer to offset bytes from the specified origin.
Definition file.h:875
bool ShouldClose
Should C++ object close file on dtor.
Definition file.h:267
TOpenMode
Open mode -> remapped into OS specific value internally.
Definition file.h:129
uint8 * Buffer
Buffer used with structure read/write.
Definition file.h:272
virtual TCHAR * readString(tchar *)
Definition file.h:906
virtual bool Flush()
Performs any pending I/O functions. Returns true if successful; false otherwise.
Definition file.cpp:460
virtual void writeString(const tchar *)
Definition file.h:913
uint64 SeekToEnd64()
Repositions the file pointer to the end of the file.
Definition file.h:789
virtual bool LockRange(uint32 position, uint32 count)
Locks count bytes, beginning at position of the file.
Definition file.cpp:469
virtual uint32 Read(void *buffer, uint32 numBytes)
Reads numBytes from the file into buffer. The number of bytes read is returned.
Definition file.h:805
virtual uint64 Length64() const
Returns the file length.
Definition file.h:867
uint64 SeekToBegin64()
Repositions the file pointer to the beginning of the file.
Definition file.h:782
TFileHandle * Handle
Low-level C file handle.
Definition file.h:266
virtual bool Write(const void *buffer, uint32 numBytes)
Writes numbytes of buffer to the file.
Definition file.h:813
virtual bool UnlockRange(uint32 position, uint32 count)
Unlocks the range at the given Position.
Definition file.cpp:477
uint32 GetOpenMode() const
Returns OpenMode.
Definition file.h:760
TAttribute
file attributes -> internally remapped into OS values
Definition file.h:163
uint32 SeekToEnd()
Repositions the file pointer to the end of the file.
Definition file.h:775
uint32 SeekToBegin()
Repositions the file pointer to the beginning of the file.
Definition file.h:769
const tstring GetName() const
Returns Name.
Definition file.h:748
TFile()
Creates a TFile object with a file handle of FileNull.
Definition file.h:715
TBinType
Binary data type enumerations.
Definition file.h:175
@ TypeLong
Definition file.h:178
@ TypeLong64
Definition file.h:181
@ TypeFloat
Definition file.h:179
@ TypeChar
Definition file.h:176
@ TypeRect
Definition file.h:183
@ TypeShort
Definition file.h:177
@ TypeDouble
Definition file.h:180
@ TypePoint
Definition file.h:182
virtual TFileHandle * GetHandle() const
Returns Handle.
Definition file.h:743
uint32 LastError()
Returns the last error.
Definition file.h:798
@ end
Seek from the end of the file.
Definition file.h:125
@ beg
Seek from the beginning of the file.
Definition file.h:123
virtual uint32 Position() const
Returns the current position of the file pointer.
Definition file.h:821
virtual bool Close()
Closes the file. Returns true if successful, false otherwise.
Definition file.cpp:445
bool GetStatus(TFileStatus &status) const
Fills status with the current file status.
Definition file.h:899
virtual uint64 Position64() const
32 Bit Only: Returns the current position of the file pointer.
Definition file.h:861
The TFileLineIterator class is used to iterate through a TTextFile file.
Definition file.h:472
virtual bool NextLine()
Loads the next line in the file. Returns true if successful; false otherwise.
Definition file.cpp:1570
TFileLineIterator()
Protected default constructor. Intializes everything to 0.
Definition file.h:1172
TCHAR * LineBuffer
Buffer lines of text are loaded into.
Definition file.h:493
const tchar * operator*() const
Returns a pointer to the start of the buffer if the file has not been completely iterated through; ot...
Definition file.h:1180
uint Line() const
Returns the current line number.
Definition file.h:1213
uint BuffSize
Size of the buffer allocated for loading a line of text.
Definition file.h:496
uint LineNumber
Current line number in buffer. Line numbering starts at 1.
Definition file.h:499
const tchar * Current() const
Returns a pointer to the start of the buffer if the file has not been completely iterated through; ot...
Definition file.h:1194
TTextFile * File
Pointer to the file being iterated through.
Definition file.h:490
const tchar * operator++()
Loads the next line in the file and then returns a pointer to the start of the buffer if the file has...
Definition file.h:1201
The TFileName class constructs filenames.
Definition filename.h:37
The TQtFile class is used for reading and writing QuickTime files.
Definition file.h:611
TQtFile()
Default constructor.
Definition file.h:1261
The TRiffFile class is used for reading and writing RIFF files.
Definition file.h:563
@ cfCreateChunk
Definition file.h:573
TRiffFile()
Default constructor.
Definition file.h:1230
virtual bool Open(const tstring &fileName, const uint32 mode=ReadOnly|PermRead|OpenExisting)
Definition file.cpp:2118
virtual ~TStreamFile()
Definition file.h:698
TStreamFile(TStreamFile &file)
tstring FileName
Definition file.h:683
virtual const tstring GetName()
Definition file.h:655
virtual uint GetOpenMode()
Definition file.h:654
virtual ~TStreamHandle()
Definition file.h:651
TStreamFile * Parent
Definition file.h:681
The TTextFile class is derived from TBufferedFile and encapsulates standard file characteristics and ...
Definition file.h:436
bool WriteString(const tstring &str)
Definition file.h:446
virtual void writeString(const tchar *)
Definition file.h:1162
virtual TCHAR * readString(tchar *)
Definition file.h:1159
TTextFile()
Creates a TTextFile object with a file handle of FileNull and allocated a buffer of DefaultBufferSize...
Definition file.h:1137
virtual TCHAR * GetString(TCHAR *buffer, uint32 size)
Reads up to size characters and places them in buffer.
Definition file.cpp:1338
The TTime class encapsulates time functions and characteristics.
Definition time.h:38
The TXBadFormat class is used for throwing exceptions when a bad file format is encountered.
Definition file.h:512
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
#define _MAX_PATH
Definition cygwin.h:97
#define MAX_PATH
Definition cygwin.h:98
#define _tcslen
Definition cygwin.h:74
TByteOrderType
The byte order type.
Definition file.h:48
std::fstream _tfstream
Definition file.h:640
TVarType
These identify host program variable types and let the binary I/O package read and write routines kno...
Definition file.h:68
#define TFILE_ERROR
Definition file.h:105
#define FileNull
Represents a NULL file handle.
Definition file.h:108
TByteOrderType EndianType()
Definition file.h:56
@ boLittle_Endian
LSB at lowest address: Intel //.
Definition file.h:49
@ boBig_Endian
MSB at lowest address: Motorola //.
Definition file.h:50
@ varCHAR
Definition file.h:71
@ varUINT32
Definition file.h:82
@ varINT32
Definition file.h:77
@ varUCHAR
Definition file.h:72
@ varINT16
Definition file.h:76
@ varLastMember
Definition file.h:87
@ varINT8
Definition file.h:75
@ varINT64
Definition file.h:85
@ varLONG
Definition file.h:74
@ varFLOAT
Definition file.h:83
@ varPOINTER
Definition file.h:70
@ varEnd
Definition file.h:69
@ varSHORT
Definition file.h:73
@ varUSHORT
Definition file.h:78
@ varUINT64
Definition file.h:86
@ varUINT16
Definition file.h:81
@ varUINT8
Definition file.h:80
@ varULONG
Definition file.h:79
@ varDOUBLE
Definition file.h:84
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
__int64 int64
Definition number.h:36
signed long int32
Definition number.h:30
owl::opstream & operator<<(owl::opstream &os, const TColor &c)
Insert the color value into a persistent output stream.
Definition color.h:498
signed char int8
Definition number.h:28
unsigned char uint8
Definition number.h:32
unsigned long uint32
Definition number.h:34
char tchar
Definition defs.h:77
signed short int16
Definition number.h:29
unsigned __int64 uint64
Definition number.h:43
unsigned short uint16
Definition number.h:33
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
std::ostream tostream
Definition strmdefs.h:40
owl::ipstream & operator>>(owl::ipstream &is, TColor &c)
Extract the color value from a persistent input stream.
Definition color.h:489
ObjectWindows exception class & function definitions.
#define _OWLCFUNC(p)
Definition defs.h:342
#define _OWLCLASS
Definition defs.h:338
The TBinField struct describes a group of like-typed fields in a structure to be read or written usin...
Definition file.h:98
TVarType Type
Declared type of struct field.
Definition file.h:99
RIFF chunk information data structure.
Definition file.h:541
uint32 Flags
flags used by MMIO functions
Definition file.h:554
uint32 Offset
offset of data portion of chunk
Definition file.h:553
uint32 Size
chunk size
Definition file.h:551
uint32 Type
form type or list type
Definition file.h:552
uint32 CkId
chunk ID
Definition file.h:550
uint32 size
Definition file.h:39
tchar fullName[_MAX_PATH]
Definition file.h:41
uint attribute
Definition file.h:40
TTime modifyTime
Definition file.h:37
TTime createTime
Definition file.h:36
TTime accessTime
Definition file.h:38
QuickTime atom information data structure.
Definition file.h:593
uint32 Flags
Definition file.h:601
uint32 Offset
Definition file.h:600
uint32 Type
chunk identifier
Definition file.h:599
uint32 Size
Definition file.h:598