1 /////////////////////////////////////////////////////////////////////////////
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.
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.
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 /////////////////////////////////////////////////////////////////////////////
18 * @file ProjectFile.cpp
20 * @brief Implementation file for ProjectFile class
22 // RCS ID line follows -- this is updated by CVS
26 #include "ProjectFile.h"
31 * @brief Standard constructor.
33 ProjectFile::ProjectFile()
35 , m_bLeftReadOnly(FALSE)
36 , m_bRightReadOnly(FALSE)
41 * @brief Get message from exception into sError, or else throw it.
43 * If this successfully extracts the error description into the string, it simply returns FALSE
44 * If it fails to extract the error description, it rethrows the exception
46 static BOOL NTAPI False(CException *e, CString *sError)
51 e->GetErrorMessage(szError, 1024);
58 * @brief Open given path-file and read data from it to member variables.
59 * @param [in] path Path to project file.
60 * @param [out] sError Error string if error happened.
61 * @return TRUE if reading succeeded, FALSE if error happened.
63 BOOL ProjectFile::Read(LPCTSTR path, CString *sError)
65 return Serialize(false, path, sError);
69 * @brief Save data from member variables to path-file.
70 * @param [in] path Path to project file.
71 * @param [out] sError Error string if error happened.
72 * @return TRUE if saving succeeded, FALSE if error happened.
73 * @note paths are converted to UTF-8
75 BOOL ProjectFile::Save(LPCTSTR path, CString *sError)
77 return Serialize(true, path, sError);
82 * @brief Read or write project file
83 * @param [in] writing TRUE if project file is saved, FALSE if it is loaded.
84 * @param [in] path Path to project file.
85 * @param [out] sError Error string if error happened.
86 * @return TRUE if operation succeeded, FALSE if error happened.
88 BOOL ProjectFile::Serialize(bool writing, LPCTSTR path, CString *sError)
90 int leftReadOnly = m_bLeftReadOnly ? 1 : 0;
91 int rightReadOnly = m_bRightReadOnly ? 1 : 0;
95 XmlDoc::XML_LOADSAVE loadSave = (writing ? XmlDoc::XML_SAVE : XmlDoc::XML_LOAD);
97 XmlDoc doc(path, loadSave, _T("UTF-8"));
100 XmlElement project(doc, _T("project"));
102 XmlElement paths(doc, _T("paths"));
104 XmlElement(doc, _T("left"), m_leftFile);
106 XmlElement(doc, _T("left-readonly"), leftReadOnly);
108 XmlElement(doc, _T("right"), m_rightFile);
110 XmlElement(doc, _T("right-readonly"), rightReadOnly);
112 XmlElement(doc, _T("filter"), m_filter);
114 XmlElement(doc, _T("subfolders"), m_subfolders);
121 catch (CException *e)
123 return False(e, sError);
128 m_bLeftReadOnly = (leftReadOnly == 1);
129 m_bRightReadOnly = (rightReadOnly == 1);
135 * @brief Returns if left path is defined.
137 BOOL ProjectFile::HasLeft() const
139 return !m_leftFile.IsEmpty();
143 * @brief Returns if right path is defined.
145 BOOL ProjectFile::HasRight() const
147 return !m_rightFile.IsEmpty();
151 * @brief Returns if filter is defined.
153 BOOL ProjectFile::HasFilter() const
155 return !m_filter.IsEmpty();
159 * @brief Returns if subfolder is included.
161 BOOL ProjectFile::HasSubfolders() const
163 return (m_subfolders != -1);
167 * @brief Returns left path.
168 * @param [out] pReadOnly TRUE if readonly was specified for path.
170 CString ProjectFile::GetLeft(BOOL * pReadOnly /*=NULL*/) const
173 *pReadOnly = m_bLeftReadOnly;
178 * @brief Returns if left path is specified read-only.
180 BOOL ProjectFile::GetLeftReadOnly() const
182 return m_bLeftReadOnly;
186 * @brief Set left path, returns old left path.
187 * @param [in] sLeft Left path.
188 * @param [in] bReadOnly Will path be recorded read-only?
190 CString ProjectFile::SetLeft(const CString& sLeft, const BOOL * pReadOnly /*=NULL*/)
192 CString sLeftOld = GetLeft();
195 m_bLeftReadOnly = *pReadOnly;
201 * @brief Returns right path.
202 * @param [out] pReadOnly TRUE if readonly was specified for path.
204 CString ProjectFile::GetRight(BOOL * pReadOnly /*=NULL*/) const
207 *pReadOnly = m_bRightReadOnly;
212 * @brief Returns if right path is specified read-only.
214 BOOL ProjectFile::GetRightReadOnly() const
216 return m_bRightReadOnly;
220 * @brief Set right path, returns old right path.
221 * @param [in] sRight Right path.
222 * @param [in] bReadOnly Will path be recorded read-only?
224 CString ProjectFile::SetRight(const CString& sRight, const BOOL * pReadOnly /*=NULL*/)
226 CString sRightOld = GetRight();
227 m_rightFile = sRight;
229 m_bRightReadOnly = *pReadOnly;
235 * @brief Returns filter.
237 CString ProjectFile::GetFilter() const
243 * @brief Set filter, returns old filter.
245 CString ProjectFile::SetFilter(const CString& sFilter)
247 CString sFilterOld = GetFilter();
254 * @brief Returns subfolder included -setting.
256 int ProjectFile::GetSubfolders() const
262 * @brief set subfolder, returns old subfolder value.
264 int ProjectFile::SetSubfolders(const int iSubfolder)
266 int iSubfoldersOld = GetSubfolders();
267 m_subfolders = iSubfolder ? 1 : 0;
269 return iSubfoldersOld;
273 * @brief Reads one value from XML data.
275 BOOL ProjectFile::GetVal(TCHAR *pPaths, TCHAR *pVal, CString * sval,
276 TCHAR *ptag1, TCHAR *ptag2, TCHAR *pbuf)
278 if (pPaths && pVal && pVal > pPaths)
280 TCHAR tmpPath[MAX_PATH] = {0};
281 TCHAR *pTagEnd = _tcsstr(pbuf, ptag2);
282 if ((pTagEnd - pVal) < (MAX_PATH * sizeof(TCHAR)))
284 pVal += _tcslen(ptag1);
285 _tcsncpy(tmpPath, pVal, pTagEnd - pVal);
294 * @brief Returns left and right paths and recursive from project file
296 * @param [out] sLeft Left path
297 * @param [out] sRight Right path
298 * @param [out] bSubFolders If TRUE subfolders included (recursive compare)
300 void ProjectFile::GetPaths(CString & sLeft, CString & sRight,
301 BOOL & bSubfolders) const
308 bSubfolders = (GetSubfolders() == 1);