1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006 - Stefan Kueng
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "StdioFileT.h"
22 CStdioFileT::CStdioFileT() : CStdioFile()
26 CStdioFileT::CStdioFileT(LPCTSTR lpszFileName
, UINT nOpenFlags
)
28 ASSERT(lpszFileName
!= NULL
);
29 ASSERT(AfxIsValidString(lpszFileName
));
32 if (!Open(lpszFileName
, nOpenFlags
, &e
))
33 AfxThrowFileException(e
.m_cause
, e
.m_lOsError
, e
.m_strFileName
);
37 void CStdioFileT::WriteString(LPCSTR lpsz
)
40 ASSERT(m_pStream
!= NULL
);
44 AfxThrowInvalidArgException();
47 if (fputs(lpsz
, m_pStream
) == EOF
)
48 AfxThrowFileException(CFileException::diskFull
, _doserrno
, m_strFileName
);
52 void CStdioFileT::WriteString(LPCWSTR lpsz
)
55 ASSERT(m_pStream
!= NULL
);
59 AfxThrowInvalidArgException();
62 if (fputws(lpsz
, m_pStream
) == EOF
)
63 AfxThrowFileException(CFileException::diskFull
, _doserrno
, m_strFileName
);
66 BOOL
CStdioFileT::ReadString(CStringA
& rString
)
71 static TCHAR afxChNil
= '\0';
74 rString
= &afxChNil
; // empty string without deallocating
75 const int nMaxSize
= 128;
76 LPSTR lpsz
= rString
.GetBuffer(nMaxSize
);
81 lpszResult
= fgets(lpsz
, nMaxSize
+1, m_pStream
);
82 rString
.ReleaseBuffer();
84 // handle error/eof case
85 if (lpszResult
== NULL
&& !feof(m_pStream
))
88 AfxThrowFileException(CFileException::genericException
, _doserrno
,
92 // if string is read completely or EOF
93 if (lpszResult
== NULL
||
94 (nLen
= (int)strlen(lpsz
)) < nMaxSize
||
98 nLen
= rString
.GetLength();
99 lpsz
= rString
.GetBuffer(nMaxSize
+ nLen
) + nLen
;
102 // remove '\n' from end of string if present
103 lpsz
= rString
.GetBuffer(0);
104 nLen
= rString
.GetLength();
105 if (nLen
!= 0 && lpsz
[nLen
-1] == '\n')
106 rString
.GetBufferSetLength(nLen
-1);
108 return lpszResult
!= NULL
;