OSDN Git Service

PATCH: [ 1225880 ] Project file parsing based on CMarkdown class
[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 #include "markdown.h"
28
29 ProjectFile::ProjectFile()
30 {
31         m_subfolders = -1;
32 }
33
34 /** 
35  * @brief Get message from exception into sError, or else throw it.
36  */
37 static BOOL NTAPI False(CException *e, CString *sError)
38 {
39         if (sError == NULL)
40                 throw e;
41         TCHAR szError[1024];
42         e->GetErrorMessage(szError, 1024);
43         *sError = szError;
44         e->Delete();
45         return FALSE;
46 }
47
48 /** 
49  * @brief Open given path-file and read data from it to member variables.
50  */
51 BOOL ProjectFile::Read(LPCTSTR path, CString *sError)
52 {
53         try
54         {
55                 CMarkdown::EntityMap entities;
56                 entities.Load();
57                 CMarkdown::File xmlfile = path;
58                 if (xmlfile.pImage == NULL)
59                 {
60                         CFileException::ThrowOsError(GetLastError(), path);
61                 }
62                 // If encoding is other than UTF-8, assume CP_ACP
63                 CMarkdown::String encoding = CMarkdown(xmlfile).Move("?xml").GetAttribute("encoding");
64                 UINT codepage = lstrcmpiA(encoding.A, "UTF-8") == 0 ? CP_UTF8 : CP_ACP;
65
66                 CMarkdown project = CMarkdown(xmlfile).Move("project").Pop();
67                 CMarkdown paths = CMarkdown(project).Move("paths").Pop();
68                 m_leftFile = CMarkdown::String(CMarkdown(paths).Move("left").GetInnerText()->Unicode(codepage)->Resolve(entities)).W;
69                 m_rightFile = CMarkdown::String(CMarkdown(paths).Move("right").GetInnerText()->Unicode(codepage)->Resolve(entities)).W;
70                 m_filter = CMarkdown::String(CMarkdown(paths).Move("filter").GetInnerText()->Unicode(codepage)->Resolve(entities)).W;
71                 sscanf(CMarkdown::String(CMarkdown(paths).Move("subfolders").GetInnerText()).A, "%d", &m_subfolders);
72         }
73         catch (CException *e)
74         {
75                 return False(e, sError);
76         }
77         return TRUE;
78 }
79
80 /** 
81  * @brief Save data from member variables to path-file.
82  * @note paths are converted to UTF-8
83  */
84 BOOL ProjectFile::Save(LPCTSTR path, CString *sError)
85 {
86         try
87         {
88                 static const char szFormat[]
89                 (
90                         "<?xml version='1.0' encoding='UTF-8'?>\n"
91                         "<project>\n"
92                         "\t<paths>\n"
93                         "\t\t<left>%s</left>\n"
94                         "\t\t<right>%s</right>\n"
95                         "\t\t<filter>%s</filter>\n"
96                         "\t\t<subfolders>%d</subfolders>\n"
97                         "\t</paths>\n"
98                         "</project>\n"
99                 );
100                 fprintf
101                 (
102                         CStdioFile(path, CFile::modeCreate|CFile::modeWrite|CFile::typeText).m_pStream,
103                         szFormat,
104                         CMarkdown::String(CMarkdown::HSTR(GetLeft().AllocSysString())->Entities()->Octets(CP_UTF8)).A,
105                         CMarkdown::String(CMarkdown::HSTR(GetRight().AllocSysString())->Entities()->Octets(CP_UTF8)).A,
106                         CMarkdown::String(CMarkdown::HSTR(GetFilter().AllocSysString())->Entities()->Octets(CP_UTF8)).A,
107                         GetSubfolders() ? 1 : 0
108                 );
109         }
110         catch (CException *e)
111         {
112                 return False(e, sError);
113         }
114         return TRUE;
115 }
116
117 /** 
118  * @brief Returns if left path is defined.
119  */
120 BOOL ProjectFile::HasLeft() const
121 {
122         return !m_leftFile.IsEmpty();
123 }
124
125 /** 
126  * @brief Returns if right path is defined.
127  */
128 BOOL ProjectFile::HasRight() const
129 {
130         return !m_rightFile.IsEmpty();
131 }
132
133 /** 
134  * @brief Returns if filter is defined.
135  */
136 BOOL ProjectFile::HasFilter() const
137 {
138         return !m_filter.IsEmpty();
139 }
140
141 /** 
142  * @brief Returns if subfolder is included.
143  */
144 BOOL ProjectFile::HasSubfolders() const
145 {
146         return (m_subfolders != -1);
147 }
148
149 /** 
150  * @brief Returns left path.
151  */
152 CString ProjectFile::GetLeft() const
153 {
154         return m_leftFile;
155 }
156
157 /** 
158  * @brief Set left path, returns old left path.
159  */
160 CString ProjectFile::SetLeft(const CString& sLeft)
161 {
162         CString sLeftOld = GetLeft();
163         m_leftFile = sLeft;
164
165         return sLeftOld;
166 }
167
168 /** 
169  * @brief Returns right path.
170  */
171 CString ProjectFile::GetRight() const
172 {
173         return m_rightFile;
174 }
175
176 /** 
177  * @brief Set right path, returns old right path.
178  */
179 CString ProjectFile::SetRight(const CString& sRight)
180 {
181         CString sRightOld = GetRight();
182         m_rightFile = sRight;
183
184         return sRightOld;
185 }
186
187 /** 
188  * @brief Returns filter.
189  */
190 CString ProjectFile::GetFilter() const
191 {
192         return m_filter;
193 }
194
195 /** 
196  * @brief Set filter, returns old filter.
197  */
198 CString ProjectFile::SetFilter(const CString& sFilter)
199 {
200         CString sFilterOld = GetFilter();
201         m_filter = sFilter;
202
203         return sFilterOld;
204 }
205
206 /** 
207  * @brief Returns subfolder included -setting.
208  */
209 int ProjectFile::GetSubfolders() const
210 {
211         return m_subfolders;
212 }
213
214 /** 
215  * @brief set subfolder, returns old subfolder value.
216  */
217 int ProjectFile::SetSubfolders(const int iSubfolder)
218 {
219         int iSubfoldersOld = GetSubfolders(); 
220         m_subfolders = iSubfolder ? 1 : 0;
221
222         return iSubfoldersOld;
223 }
224
225 /** 
226  * @brief Reads one value from XML data.
227  */
228 BOOL ProjectFile::GetVal(TCHAR *pPaths, TCHAR *pVal, CString * sval,
229                 TCHAR *ptag1, TCHAR *ptag2, TCHAR *pbuf)
230 {
231         if (pPaths && pVal && pVal > pPaths)
232         {
233                 TCHAR tmpPath[MAX_PATH] = {0};
234                 TCHAR *pTagEnd = _tcsstr(pbuf, ptag2);
235                 if ((pTagEnd - pVal) < (MAX_PATH * sizeof(TCHAR)))
236                 {
237                         pVal += _tcslen(ptag1);
238                         _tcsncpy(tmpPath, pVal, pTagEnd - pVal);
239                         *sval = tmpPath;
240                         return TRUE;
241                 }
242         }
243         return FALSE;
244 }
245
246 /** 
247  * @brief Returns left and right paths and recursive from project file
248  * 
249  * @param [out] sLeft Left path
250  * @param [out] sRight Right path
251  * @param [out] bSubFolders If TRUE subfolders included (recursive compare)
252  */
253 void ProjectFile::GetPaths(CString & sLeft, CString & sRight,
254         BOOL & bSubfolders) const
255 {
256         if (HasLeft())
257                 sLeft = GetLeft();
258         if (HasRight())
259                 sRight = GetRight();
260         if (HasSubfolders())
261                 bSubfolders = (GetSubfolders() == 1);
262 }