33 if (base_)
delete[] base_;
36 return traits_type::eof();
38 TFileBuf(
int fhdl,
int omode, streampos seekpos);
41 virtual int_type overflow(int_type c = tstreambuf::traits_type::eof());
42 virtual int_type underflow();
44 virtual tstreambuf* setbuf(char_type *s, streamsize n);
46 virtual pos_type seekoff(off_type,
47 ios_base::seekdir, ios_base::openmode which =
48 ios_base::in | ios_base::out);
50 virtual pos_type seekpos(pos_type sp,
51 ios_base::openmode which =
52 ios_base::in | ios_base::out);
57 return pptr() ?
static_cast<int>(pptr() - pbase()) : 0;
60 void setb(tchar* beg, tchar* end,
int = 0){
61 streamsize size_buf = end - beg;
62 buffered_ = beg && size_buf;
63 setbuf(beg, size_buf);
70 void unbuffered(
int buf){
73 setb(
nullptr,
nullptr, 0);
82 return static_cast<int> (ebuf_ - base_);
97 TFileStreamBase(
int fhdl,
int omode, streampos seekpos);
111class _OWLCLASS TFileInStream :
public TFileStreamBase,
public TInStream {
114 TFileInStream(TFileDocument& doc, HFILE fhdl,
int omode, streampos seekpos)
115 : TFileStreamBase{fhdl, omode, seekpos}, TInStream{doc, nullptr, omode} {}
125class _OWLCLASS TFileOutStream :
public TOutStream,
public TFileStreamBase {
127class _OWLCLASS TFileOutStream :
public TFileStreamBase,
public TOutStream {
130 TFileOutStream(TFileDocument& doc, HFILE fhdl,
int omode, streampos seekpos)
131 : TFileStreamBase{fhdl,omode,seekpos}, TOutStream{doc, nullptr, omode} {}
150 int share = omode &
shMask;
152 const auto fn =
_W2A(fileName.c_str());
157 how = (
static_cast<unsigned>(share-
shCompat)) >> 5;
169 if ((fhdl = ::_lopen(fn, OF_READ | OF_SHARE_COMPAT)) != HFILE_ERROR
170 || (fhdl = ::_lopen(fn, OF_READ | OF_SHARE_DENY_NONE)) != HFILE_ERROR) {
180 if ((fhdl = _lcreat(fn, 0)) == HFILE_ERROR)
191 if ((fhdl = ::_lopen(fn, how)) != HFILE_ERROR) {
193 ? SEEK_END : SEEK_SET)) == streampos(HFILE_ERROR)) {
198 const HANDLE f =
reinterpret_cast<HANDLE
>(
static_cast<INT_PTR
>(fhdl));
199 FileLength = GetFileSize(f,
nullptr);
200 InfoPresent = GetFileTime(f, &FileCreateTime, &FileAccessTime, &FileUpdateTime);
229 if (FHdl != HFILE_ERROR)
270 if (FHdl != HFILE_ERROR) {
305static LPCTSTR PropNames[] = {
313static int PropFlags[] = {
314 pfGetBinary|pfGetText,
315 pfGetBinary|pfGetText,
316 pfGetBinary|pfGetText,
317 pfGetBinary|pfGetText,
358 if (
_tcscmp(PropNames[i], name) == 0)
372 const auto formatFileTime = [](
const FILETIME& pft,
void* dest,
int textlen) ->
int
379 *
reinterpret_cast<FILETIME*
>(dest) = pft;
380 return sizeof(FILETIME);
383 FileTimeToSystemTime(&pft, &dt);
384 len = _sntprintf(buf,
COUNTOF(buf),
_T(
"%d/%d/%d %02d:%02d:%02d.%02d"),
385 dt.wMonth, dt.wDay, dt.wYear,
386 dt.wHour, dt.wMinute, dt.wSecond, dt.wMilliseconds / 10);
389 memcpy(dest, buf, textlen *
sizeof(
tchar));
390 *(
reinterpret_cast<LPTSTR
>(dest) + textlen) =
_T(
'\0');
398 *
reinterpret_cast<HFILE *
>(dest) = FHdl;
405 *
reinterpret_cast<unsigned long *
>(dest) = FileLength;
406 return sizeof(FileLength);
413 memcpy(dest, buf, textlen*
sizeof(
tchar));
414 *(
reinterpret_cast<LPTSTR
>(dest) + textlen) = 0;
418 return formatFileTime(FileCreateTime, dest, textlen);
420 return formatFileTime(FileUpdateTime, dest, textlen);
422 return formatFileTime(FileAccessTime, dest, textlen);
456 if ((fhdl = FHdl) == HFILE_ERROR) {
491 if ((fhdl = FHdl) == HFILE_ERROR) {
515TFileInStream::~TFileInStream()
517 (
static_cast<TFileDocument&
>(Doc)).CloseThisFile(buf.xfd, GetOpenMode());
523TFileOutStream::~TFileOutStream()
525 if (buf.out_waiting())
527 (
static_cast<TFileDocument&
>(Doc)).CloseThisFile(buf.xfd, GetOpenMode());
539TFileBuf::TFileBuf(
int fhdl,
int omode, streampos seekpos)
541 base_ = ebuf_ =
nullptr;
558TFileBuf::pos_type TFileBuf::seekoff(off_type off, ios_base::seekdir dir,
561 streamsize loff =
static_cast<streamsize
>(off);
566 else if (dir == ios::cur) {
567 streamsize count = in_avail();
573 if ((mode & ofBinary) == 0) {
574 tchar *tptr = gptr();
575 while (tptr != egptr())
576 if (*tptr++ ==
_T(
'\n'))
582 if (!IsRepresentable<LONG>(loff))
583 return streampos(eof());
584 LONG lOffset =
static_cast<LONG
>(loff);
585 int w = (dir==ios::beg) ? SEEK_SET : ((dir==ios::cur) ? SEEK_CUR:SEEK_END);
586 last_seek = ::_llseek(xfd, lOffset, w);
588 if (!unbuffered() && base()) {
589 size_t pb = (blen() > 8) ? 4 : 1;
591 setp(b + pb, b + pb);
592 setg(b, b + pb, b + pb);
594 return (last_seek == streampos(HFILE_ERROR)) ? streampos(eof()) : last_seek;
602 uint count = out_waiting();
606 tchar* srcp = pbase();
607 tchar* endp = srcp + count;
611 if ((mode & ofBinary) == 0) {
612 for (curp = srcp; curp < endp; curp++) {
613 if (*curp ==
_T(
'\n')){
615 count =
static_cast<int>(curp - srcp) + 1;
616 if (::_lwrite(xfd,
_W2A(srcp), count) != count)
618 *(srcp = curp) =
_T(
'\n');
621 count =
uint(curp - srcp);
623 if (::_lwrite(xfd,
_W2A(srcp), count) != count)
627 int pb = (blen() > 8) ? 4 : 1;
629 setp(b+pb, b+blen());
632 else if (in_avail()) {
633 last_seek = ::_llseek(xfd,
long(-in_avail()), SEEK_CUR);
634 setg(eback(), gptr(), gptr());
635 setp(gptr(), gptr());
636 if (last_seek == streampos(HFILE_ERROR))
648 if ((mode & (ofRead | ofWrite)) == ofWrite)
652 return static_cast<tchar>(*gptr());
657 if (!unbuffered() && base()) {
662 int pb = (blen() > 8) ? 4 : 1;
663 tchar* begp = base() + pb;
666 count = ::_lread(xfd, begp, blen()-pb);
667 if (count == HFILE_ERROR)
671 if ((mode & ofBinary) == 0) {
672 tchar* endp = begp + count;
677 for (curp = begp; curp < endp; curp++) {
678 if (*curp ==
_T(
'\r')) {
680 memcpy(dstp, srcp,
static_cast<int>(curp - srcp));
681 dstp +=
static_cast<int>(curp - srcp);
689 endp = dstp +
static_cast<int>(curp - srcp);
691 memcpy(dstp, srcp,
static_cast<int>(curp - srcp));
693 count =
static_cast<int>(endp - begp);
696 setg(base(), begp, begp + count);
700 c =
static_cast<utchar>(*gptr());
704 count = ::_lread(xfd, lahead, 1);
705 if (count == HFILE_ERROR) {
707 setg(
nullptr,
nullptr,
nullptr);
710 c =
static_cast<utchar>(lahead[0]);
711 if ((mode & ofBinary) == 0 && c ==
_T(
'\r'))
713 setg(lahead, lahead, lahead+1);
726TFileBuf::int_type TFileBuf::overflow(int_type c)
728 if ((mode & (ofRead | ofWrite)) == ofRead)
731 if (unbuffered() || !base()) {
736 if (c ==
_T(
'\n') && (mode & ofBinary) == 0) {
738 b[1] =
static_cast<tchar>(c);
742 b[0] =
static_cast<tchar>(c);
745 if (::_lwrite(xfd,
_W2A(b), count) != count)
756 int pb = (blen() > 8) ? 4 : 1;
758 setp(b+pb, b+blen());
762 sputc(
static_cast<tchar>(c));
769tstreambuf * TFileBuf::setbuf(char_type *s, streamsize n)
777TFileBuf::pos_type TFileBuf::seekpos(pos_type sp,
778 ios_base::openmode which){
779 return seekoff(streamoff(sp), ios::beg, which);
789TFileStreamBase::TFileStreamBase(
int fhdl,
int omode, streampos seekpos)
791 buf(fhdl, omode, seekpos)
799#if OWL_PERSISTENT_STREAMS
808 o->FHdl = HFILE_ERROR;
809 o->InfoPresent =
false;
818TFileDocument::Streamer::Write(opstream& os)
const
821 WriteBaseObject((TDocument*)GetObject(), os);
An abstract base class, TDocument is the base class for all document objects and serves as an interfa...
virtual int GetProperty(int index, void *dest, int textlen=0)
Retrieves the property identified by the given index.
virtual int FindProperty(LPCTSTR name)
return property index
bool NotifyViews(int eventId, TParam2=0, TView *exclude=nullptr)
Notifies the views of this document, and the views of any child documents, of a change.
virtual bool SetProperty(int index, const void *src)
native type
LPCTSTR GetDocPath() const
Returns the directory path for the document.
virtual LPCTSTR PropertyName(int index)
locale invariant name
virtual bool Close()
close document, does not delete or detach
virtual int PropertyFlags(int index)
pfXxxxx bit array
virtual bool SetDocPath(LPCTSTR path)
Sets the document path for Open and Save operations.
void SetOpenMode(int mode)
Sets the mode and protection flag values for the current document.
void SetDirty(bool dirty=true)
Updates the document's dirty flag using the specified parameter.
virtual bool IsOpen()
Checks to see if the document has any streams in its stream list.
virtual bool Commit(bool force=false)
save current data, force write
int GetOpenMode() const
Gets the mode and protection flag values for the current document.
virtual bool Revert(bool clear=false)
abort changes, no reload if true
Derived from TDocument, TFileDocument opens and closes views and provides stream support for views.
Derived from TStream and istream, TInStream is a base class used to define input streams for document...
Derived from TStream and ostream, TOutStream is a base class used to create output storage streams fo...
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition of class TFileDocument.
void ReadBaseObject(Base *base, ipstream &in)
TInStream * InStream(int mode, LPCTSTR strmId=0)
Overrides TDocument::InStream and provides generic input for the particular storage medium.
bool Revert(bool clear=false)
Calls TDocument::Revert to notify the views to refresh their data.
const uint vnDocClosed
document has just been closed
const uint vnDocOpened
document has just been opened
#define IS_PREV_OPEN(omode)
bool SetProperty(int index, const void *src)
Sets the property data, which must be in the native data type (either string or binary).
LPCTSTR PropertyName(int index)
Returns the text name of the property given the index value.
bool Close()
< String-aware overload
int PropertyFlags(int index)
< String-aware overload
bool Open(int mode, LPCTSTR path=0)
Overrides TDocument::Open and opens the file using the specified path.
int GetProperty(int index, void *dest, int textlen=0)
Overrides TDocument::GetProperty.
HFILE OpenThisFile(int omode, const tstring &fileName, std::streampos *pseekpos)
Opens the file document after checking the file sharing mode (omode).
void CloseThisFile(HFILE fhdl, int omode)
Closes the file handle if the associated file was opened by TFileDocument.
TOutStream * OutStream(int mode, LPCTSTR strmId=0)
< String-aware overload
friend class TFileOutStream
friend class TFileInStream
int FindProperty(LPCTSTR name)
return index
bool Commit(bool force=false)
< String-aware overload
@ FileHandle
The platform file handle (HFILE if Windows).
@ ModifyTime
The time the view or document was modified (FILETIME).
@ AccessTime
The time the view or document was last accessed (FILETIME).
@ CreateTime
The time the view or document was created (FILETIME).
@ StorageSize
An unsigned long containing the storage size (ulong).
@ ofRead
ios::in, open for reading
@ ofTruncate
ios::trunc, truncate file if already exists
@ shRead
DENY_WRITE functionality.
@ shDefault
use stream implementation default value
@ ofParent
use open mode of parent storage
@ ofNoReplace
ios::noreplace, open fails if file already exists
@ ofAppend
ios::app, append mode: all additions at eof
@ ofWrite
ios::out, open for writing
@ ofAtEnd
ios::ate, seek to eof upon original open
@ shReadWrite
DENY_NONE functionality.
@ ofNoCreate
ios::nocreate, open fails if file doesn't exist
@ shCompat
for non-compliant applications, avoid if possible
Object Windows Library (OWLNext Core).
std::streambuf tstreambuf
IMPLEMENT_STREAMABLE1(TApplication, TModule)
#define COUNTOF(s)
Array element count Important: Only use this with an argument of array type.