OSDN Git Service

Installer: Textual change
[tortoisegit/TortoiseGitJp.git] / src / Utils / StdioFileT.cpp
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2006 - Stefan Kueng\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "stdafx.h"\r
20 #include "StdioFileT.h"\r
21 \r
22 CStdioFileT::CStdioFileT() : CStdioFile()\r
23 {\r
24 }\r
25 \r
26 CStdioFileT::CStdioFileT(LPCTSTR lpszFileName, UINT nOpenFlags)\r
27 {\r
28         ASSERT(lpszFileName != NULL);\r
29         ASSERT(AfxIsValidString(lpszFileName));\r
30 \r
31         CFileException e;\r
32         if (!Open(lpszFileName, nOpenFlags, &e))\r
33                 AfxThrowFileException(e.m_cause, e.m_lOsError, e.m_strFileName);\r
34 }\r
35 \r
36 \r
37 void CStdioFileT::WriteString(LPCSTR lpsz)\r
38 {\r
39         ASSERT(lpsz != NULL);\r
40         ASSERT(m_pStream != NULL);\r
41 \r
42         if (lpsz == NULL)\r
43         {\r
44                 AfxThrowInvalidArgException();\r
45         }\r
46 \r
47         if (fputs(lpsz, m_pStream) == EOF)\r
48                 AfxThrowFileException(CFileException::diskFull, _doserrno, m_strFileName);\r
49 }\r
50 \r
51 \r
52 void CStdioFileT::WriteString(LPCWSTR lpsz)\r
53 {\r
54         ASSERT(lpsz != NULL);\r
55         ASSERT(m_pStream != NULL);\r
56 \r
57         if (lpsz == NULL)\r
58         {\r
59                 AfxThrowInvalidArgException();\r
60         }\r
61 \r
62         if (fputws(lpsz, m_pStream) == EOF)\r
63                 AfxThrowFileException(CFileException::diskFull, _doserrno, m_strFileName);\r
64 }\r
65 \r
66 BOOL CStdioFileT::ReadString(CStringA& rString)\r
67 {\r
68         ASSERT_VALID(this);\r
69 \r
70 #ifndef afxChNil\r
71         static TCHAR afxChNil = '\0';\r
72 #endif\r
73 \r
74         rString = &afxChNil;    // empty string without deallocating\r
75         const int nMaxSize = 128;\r
76         LPSTR lpsz = rString.GetBuffer(nMaxSize);\r
77         LPSTR lpszResult;\r
78         int nLen = 0;\r
79         for (;;)\r
80         {\r
81                 lpszResult = fgets(lpsz, nMaxSize+1, m_pStream);\r
82                 rString.ReleaseBuffer();\r
83 \r
84                 // handle error/eof case\r
85                 if (lpszResult == NULL && !feof(m_pStream))\r
86                 {\r
87                         clearerr(m_pStream);\r
88                         AfxThrowFileException(CFileException::genericException, _doserrno,\r
89                                 m_strFileName);\r
90                 }\r
91 \r
92                 // if string is read completely or EOF\r
93                 if (lpszResult == NULL ||\r
94                         (nLen = (int)strlen(lpsz)) < nMaxSize ||\r
95                         lpsz[nLen-1] == '\n')\r
96                         break;\r
97 \r
98                 nLen = rString.GetLength();\r
99                 lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;\r
100         }\r
101 \r
102         // remove '\n' from end of string if present\r
103         lpsz = rString.GetBuffer(0);\r
104         nLen = rString.GetLength();\r
105         if (nLen != 0 && lpsz[nLen-1] == '\n')\r
106                 rString.GetBufferSetLength(nLen-1);\r
107 \r
108         return lpszResult != NULL;\r
109 }\r