OSDN Git Service

PATCH: [ 1000020 ] Simple project files (no GUI)
[winmerge-jp/winmerge-jp.git] / Src / ProjectFile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 //    License (GPLv2+):
3 //    This program is free software; you can redistribute it and/or modify
4 //    it under the terms of the GNU General Public License as published by
5 //    the Free Software Foundation; either version 2 of the License, or (at
6 //    your option) any later version.
7 //    
8 //    This program is distributed in the hope that it will be useful, but
9 //    WITHOUT ANY WARRANTY; without even the implied warranty of
10 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //    GNU General Public License for more details.
12 //
13 //    You should have received a copy of the GNU General Public License
14 //    along with this program; if not, write to the Free Software
15 //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 /////////////////////////////////////////////////////////////////////////////
17 /** 
18  * @file  ProjectFile.cpp
19  *
20  * @brief Implementation file for ProjectFile class
21  */
22 // RCS ID line follows -- this is updated by CVS
23 // $Id$
24
25 #include "stdafx.h"
26 #include "ProjectFile.h"
27
28 ProjectFile::ProjectFile()
29 {
30         m_subfolders = -1;
31 }
32
33 /** 
34  * @brief Open given path-file and read data from it to member variables.
35  */
36 BOOL ProjectFile::Read(LPCTSTR path, CString *sError)
37 {
38         ASSERT(sError != NULL);
39         CFile file;
40         CFileException e;
41
42         if (!file.Open(path, CFile::modeRead, &e))
43         {
44                 TCHAR szError[1024];
45                 e.GetErrorMessage(szError, 1024);
46                 *sError = szError;
47                 return FALSE;
48         }
49
50         char buf[4096] = {0};
51         TCHAR buf2[4096] = {0};
52         TCHAR tmpPath[MAX_PATH] = {0};
53         UINT bytesRead = file.Read(buf, 4095);
54
55         USES_CONVERSION;
56         _tcsncpy(buf2, A2T(buf), 4096);
57
58         if (_tcsstr(buf2, _T("<?xml")) && _tcsstr(buf2, _T("?>")))
59         {
60                 TCHAR *pProject = _tcsstr(buf2, _T("<project>"));
61                 
62                 if (pProject)
63                 {
64                         TCHAR *pPaths = _tcsstr(buf2, _T("<paths>"));
65                         TCHAR *pLeft = _tcsstr(buf2, _T("<left>"));
66                         TCHAR *pRight = _tcsstr(buf2, _T("<right>"));
67                         TCHAR *pFilter = _tcsstr(buf2, _T("<filter>"));
68                         TCHAR *pSubs = _tcsstr(buf2, _T("<subfolders>"));
69
70                         CString subs;
71                         GetVal(pPaths, pLeft, &m_leftFile, _T("<left>"), _T("</left>"), buf2);
72                         GetVal(pPaths, pRight, &m_rightFile, _T("<right>"), _T("</right>"), buf2);
73                         GetVal(pPaths, pFilter, &m_filter, _T("<filter>"), _T("</filter>"), buf2);
74                         if (GetVal(pPaths, pSubs, &subs, _T("<subfolders>"), _T("</subfolders>"), buf2))
75                                 m_subfolders = _ttoi(subs);
76                 }
77         }
78
79         file.Close();
80
81         return TRUE;
82 }
83
84 /** 
85  * @brief Save data from member variables to path-file.
86  */
87 BOOL ProjectFile::Save(LPCTSTR path)
88 {
89         UINT flags = CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite;
90
91         return TRUE;
92 }
93
94 /** 
95  * @brief Returns if left path is defined.
96  */
97 BOOL ProjectFile::HasLeft() const
98 {
99         return !m_leftFile.IsEmpty();
100 }
101
102 /** 
103  * @brief Returns if right path is defined.
104  */
105 BOOL ProjectFile::HasRight() const
106 {
107         return !m_rightFile.IsEmpty();
108 }
109
110 /** 
111  * @brief Returns if filter is defined.
112  */
113 BOOL ProjectFile::HasFilter() const
114 {
115         return !m_filter.IsEmpty();
116 }
117
118 /** 
119  * @brief Returns if subfolder is included.
120  */
121 BOOL ProjectFile::HasSubfolders() const
122 {
123         return (m_subfolders != -1);
124 }
125
126 /** 
127  * @brief Returns left path.
128  */
129 CString ProjectFile::GetLeft() const
130 {
131         return m_leftFile;
132 }
133
134 /** 
135  * @brief Returns right path.
136  */
137 CString ProjectFile::GetRight() const
138 {
139         return m_rightFile;
140 }
141
142 /** 
143  * @brief Returns filter.
144  */
145 CString ProjectFile::GetFilter() const
146 {
147         return m_filter;
148 }
149
150 /** 
151  * @brief Returns subfolder included -setting.
152  */
153 int ProjectFile::GetSubfolders() const
154 {
155         return m_subfolders;
156 }
157
158 /** 
159  * @brief Reads one value from XML data.
160  */
161 BOOL ProjectFile::GetVal(TCHAR *pPaths, TCHAR *pVal, CString * sval,
162                 TCHAR *ptag1, TCHAR *ptag2, TCHAR *pbuf)
163 {
164         if (pPaths && pVal && pVal > pPaths)
165         {
166                 TCHAR tmpPath[MAX_PATH] = {0};
167                 TCHAR *pTagEnd = _tcsstr(pbuf, ptag2);
168                 if ((pTagEnd - pVal) < (MAX_PATH * sizeof(TCHAR)))
169                 {
170                         pVal += _tcslen(ptag1);
171                         _tcsncpy(tmpPath, pVal, pTagEnd - pVal);
172                         *sval = tmpPath;
173                         return TRUE;
174                 }
175         }
176         return FALSE;
177 }
178
179 /** 
180  * @brief Returns left and right paths and recursive from project file
181  * 
182  * @param [out] sLeft Left path
183  * @param [out] sRight Right path
184  * @param [out] bSubFolders If TRUE subfolders included (recursive compare)
185  */
186 void ProjectFile::GetPaths(CString & sLeft, CString & sRight,
187         BOOL & bSubfolders) const
188 {
189         if (HasLeft())
190                 sLeft = GetLeft();
191         if (HasRight())
192                 sRight = GetRight();
193         if (HasSubfolders())
194                 bSubfolders = (GetSubfolders() == 1);
195 }