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 caller provided the address of an error string (sError),
38 * this populates the error string (if possible) and returns FALSE
40 * If caller did not provide the address of an error string (sError==NULL)
41 * this rethrows the error
43 static BOOL NTAPI False(CException *e, CString *sError)
47 TCHAR szError[4096] = _T("");
48 e->GetErrorMessage(szError, sizeof(szError)/sizeof(szError[0]));
55 * @brief Open given path-file and read data from it to member variables.
57 * Errors are returned in sError, unless it is NULL, in which case they are thrown
59 BOOL ProjectFile::Read(LPCTSTR path, CString *sError)
63 CMarkdown::EntityMap entities;
65 CMarkdown::File xmlfile = path;
66 if (xmlfile.pImage == NULL)
68 CFileException::ThrowOsError(GetLastError(), path);
70 // If encoding is other than UTF-8, assume CP_ACP
71 CMarkdown::String encoding = CMarkdown(xmlfile).Move("?xml").GetAttribute("encoding");
72 UINT codepage = lstrcmpiA(encoding.A, "UTF-8") == 0 ? CP_UTF8 : CP_ACP;
74 CMarkdown project = CMarkdown(xmlfile).Move("project").Pop();
75 CMarkdown paths = CMarkdown(project).Move("paths").Pop();
76 m_leftFile = CMarkdown::String(CMarkdown(paths).Move("left").GetInnerText()->Unicode(codepage)->Resolve(entities)).W;
77 m_rightFile = CMarkdown::String(CMarkdown(paths).Move("right").GetInnerText()->Unicode(codepage)->Resolve(entities)).W;
78 m_filter = CMarkdown::String(CMarkdown(paths).Move("filter").GetInnerText()->Unicode(codepage)->Resolve(entities)).W;
79 sscanf(CMarkdown::String(CMarkdown(paths).Move("subfolders").GetInnerText()).A, "%d", &m_subfolders);
83 return False(e, sError);
89 * @brief Save data from member variables to path-file.
90 * @note paths are converted to UTF-8
92 BOOL ProjectFile::Save(LPCTSTR path, CString *sError)
96 static const char szFormat[]
98 "<?xml version='1.0' encoding='UTF-8'?>\n"
101 "\t\t<left>%s</left>\n"
102 "\t\t<right>%s</right>\n"
103 "\t\t<filter>%s</filter>\n"
104 "\t\t<subfolders>%d</subfolders>\n"
110 CStdioFile(path, CFile::modeCreate|CFile::modeWrite|CFile::typeText).m_pStream,
112 CMarkdown::String(CMarkdown::HSTR(GetLeft().AllocSysString())->Entities()->Octets(CP_UTF8)).A,
113 CMarkdown::String(CMarkdown::HSTR(GetRight().AllocSysString())->Entities()->Octets(CP_UTF8)).A,
114 CMarkdown::String(CMarkdown::HSTR(GetFilter().AllocSysString())->Entities()->Octets(CP_UTF8)).A,
115 GetSubfolders() ? 1 : 0
118 catch (CException *e)
120 return False(e, sError);
126 * @brief Returns if left path is defined.
128 BOOL ProjectFile::HasLeft() const
130 return !m_leftFile.IsEmpty();
134 * @brief Returns if right path is defined.
136 BOOL ProjectFile::HasRight() const
138 return !m_rightFile.IsEmpty();
142 * @brief Returns if filter is defined.
144 BOOL ProjectFile::HasFilter() const
146 return !m_filter.IsEmpty();
150 * @brief Returns if subfolder is included.
152 BOOL ProjectFile::HasSubfolders() const
154 return (m_subfolders != -1);
158 * @brief Returns left path.
160 CString ProjectFile::GetLeft() const
166 * @brief Set left path, returns old left path.
168 CString ProjectFile::SetLeft(const CString& sLeft)
170 CString sLeftOld = GetLeft();
177 * @brief Returns right path.
179 CString ProjectFile::GetRight() const
185 * @brief Set right path, returns old right path.
187 CString ProjectFile::SetRight(const CString& sRight)
189 CString sRightOld = GetRight();
190 m_rightFile = sRight;
196 * @brief Returns filter.
198 CString ProjectFile::GetFilter() const
204 * @brief Set filter, returns old filter.
206 CString ProjectFile::SetFilter(const CString& sFilter)
208 CString sFilterOld = GetFilter();
215 * @brief Returns subfolder included -setting.
217 int ProjectFile::GetSubfolders() const
223 * @brief set subfolder, returns old subfolder value.
225 int ProjectFile::SetSubfolders(const int iSubfolder)
227 int iSubfoldersOld = GetSubfolders();
228 m_subfolders = iSubfolder ? 1 : 0;
230 return iSubfoldersOld;
234 * @brief Reads one value from XML data.
236 BOOL ProjectFile::GetVal(TCHAR *pPaths, TCHAR *pVal, CString * sval,
237 TCHAR *ptag1, TCHAR *ptag2, TCHAR *pbuf)
239 if (pPaths && pVal && pVal > pPaths)
241 TCHAR tmpPath[MAX_PATH] = {0};
242 TCHAR *pTagEnd = _tcsstr(pbuf, ptag2);
243 if ((pTagEnd - pVal) < (MAX_PATH * sizeof(TCHAR)))
245 pVal += _tcslen(ptag1);
246 _tcsncpy(tmpPath, pVal, pTagEnd - pVal);
255 * @brief Returns left and right paths and recursive from project file
257 * @param [out] sLeft Left path
258 * @param [out] sRight Right path
259 * @param [out] bSubFolders If TRUE subfolders included (recursive compare)
261 void ProjectFile::GetPaths(CString & sLeft, CString & sRight,
262 BOOL & bSubfolders) const
269 bSubfolders = (GetSubfolders() == 1);