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"
29 ProjectFile::ProjectFile()
35 * @brief Get message from exception into sError, or else throw it.
37 * If this successfully extracts the error description into the string, it simply returns FALSE
38 * If it fails to extract the error description, it rethrows the exception
40 static BOOL NTAPI False(CException *e, CString *sError)
45 e->GetErrorMessage(szError, 1024);
52 * @brief Open given path-file and read data from it to member variables.
54 BOOL ProjectFile::Read(LPCTSTR path, CString *sError)
58 CMarkdown::EntityMap entities;
60 CMarkdown::File xmlfile = path;
61 if (xmlfile.pImage == NULL)
63 CFileException::ThrowOsError(GetLastError(), path);
65 // If encoding is other than UTF-8, assume CP_ACP
66 CMarkdown::String encoding = CMarkdown(xmlfile).Move("?xml").GetAttribute("encoding");
67 UINT codepage = lstrcmpiA(encoding.A, "UTF-8") == 0 ? CP_UTF8 : CP_ACP;
69 CMarkdown project = CMarkdown(xmlfile).Move("project").Pop();
70 CMarkdown paths = CMarkdown(project).Move("paths").Pop();
71 m_leftFile = CMarkdown::String(CMarkdown(paths).Move("left").GetInnerText()->Unicode(codepage)->Resolve(entities)).W;
72 m_rightFile = CMarkdown::String(CMarkdown(paths).Move("right").GetInnerText()->Unicode(codepage)->Resolve(entities)).W;
73 m_filter = CMarkdown::String(CMarkdown(paths).Move("filter").GetInnerText()->Unicode(codepage)->Resolve(entities)).W;
74 sscanf(CMarkdown::String(CMarkdown(paths).Move("subfolders").GetInnerText()).A, "%d", &m_subfolders);
78 return False(e, sError);
84 * @brief Save data from member variables to path-file.
85 * @note paths are converted to UTF-8
87 BOOL ProjectFile::Save(LPCTSTR path, CString *sError)
91 static const char szFormat[]
93 "<?xml version='1.0' encoding='UTF-8'?>\n"
96 "\t\t<left>%s</left>\n"
97 "\t\t<right>%s</right>\n"
98 "\t\t<filter>%s</filter>\n"
99 "\t\t<subfolders>%d</subfolders>\n"
105 CStdioFile(path, CFile::modeCreate|CFile::modeWrite|CFile::typeText).m_pStream,
107 CMarkdown::String(CMarkdown::HSTR(GetLeft().AllocSysString())->Entities()->Octets(CP_UTF8)).A,
108 CMarkdown::String(CMarkdown::HSTR(GetRight().AllocSysString())->Entities()->Octets(CP_UTF8)).A,
109 CMarkdown::String(CMarkdown::HSTR(GetFilter().AllocSysString())->Entities()->Octets(CP_UTF8)).A,
110 GetSubfolders() ? 1 : 0
113 catch (CException *e)
115 return False(e, sError);
121 * @brief Returns if left path is defined.
123 BOOL ProjectFile::HasLeft() const
125 return !m_leftFile.IsEmpty();
129 * @brief Returns if right path is defined.
131 BOOL ProjectFile::HasRight() const
133 return !m_rightFile.IsEmpty();
137 * @brief Returns if filter is defined.
139 BOOL ProjectFile::HasFilter() const
141 return !m_filter.IsEmpty();
145 * @brief Returns if subfolder is included.
147 BOOL ProjectFile::HasSubfolders() const
149 return (m_subfolders != -1);
153 * @brief Returns left path.
155 CString ProjectFile::GetLeft() const
161 * @brief Set left path, returns old left path.
163 CString ProjectFile::SetLeft(const CString& sLeft)
165 CString sLeftOld = GetLeft();
172 * @brief Returns right path.
174 CString ProjectFile::GetRight() const
180 * @brief Set right path, returns old right path.
182 CString ProjectFile::SetRight(const CString& sRight)
184 CString sRightOld = GetRight();
185 m_rightFile = sRight;
191 * @brief Returns filter.
193 CString ProjectFile::GetFilter() const
199 * @brief Set filter, returns old filter.
201 CString ProjectFile::SetFilter(const CString& sFilter)
203 CString sFilterOld = GetFilter();
210 * @brief Returns subfolder included -setting.
212 int ProjectFile::GetSubfolders() const
218 * @brief set subfolder, returns old subfolder value.
220 int ProjectFile::SetSubfolders(const int iSubfolder)
222 int iSubfoldersOld = GetSubfolders();
223 m_subfolders = iSubfolder ? 1 : 0;
225 return iSubfoldersOld;
229 * @brief Reads one value from XML data.
231 BOOL ProjectFile::GetVal(TCHAR *pPaths, TCHAR *pVal, CString * sval,
232 TCHAR *ptag1, TCHAR *ptag2, TCHAR *pbuf)
234 if (pPaths && pVal && pVal > pPaths)
236 TCHAR tmpPath[MAX_PATH] = {0};
237 TCHAR *pTagEnd = _tcsstr(pbuf, ptag2);
238 if ((pTagEnd - pVal) < (MAX_PATH * sizeof(TCHAR)))
240 pVal += _tcslen(ptag1);
241 _tcsncpy(tmpPath, pVal, pTagEnd - pVal);
250 * @brief Returns left and right paths and recursive from project file
252 * @param [out] sLeft Left path
253 * @param [out] sRight Right path
254 * @param [out] bSubFolders If TRUE subfolders included (recursive compare)
256 void ProjectFile::GetPaths(CString & sLeft, CString & sRight,
257 BOOL & bSubfolders) const
264 bSubfolders = (GetSubfolders() == 1);