No need to explicitly initialize CString and use Empty() for clearing CString
[TortoiseGit.git] / src / TortoiseMerge / WorkingFile.cpp
blobc89a7fada51b8e99c67553ba051fb2f40e722c97
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2006-2007, 2011-2014 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "WorkingFile.h"
21 #include "AppUtils.h"
22 #include "PathUtils.h"
23 #include "resource.h"
24 #include "SmartHandle.h"
26 CWorkingFile::CWorkingFile(void)
28 ClearStoredAttributes();
31 CWorkingFile::~CWorkingFile(void)
35 void CWorkingFile::SetFileName(const CString& newFilename)
37 m_sFilename = newFilename;
38 m_sFilename.Replace('/', '\\');
39 m_sDescriptiveName.Empty();
40 ClearStoredAttributes();
43 void CWorkingFile::SetDescriptiveName(const CString& newDescName)
45 m_sDescriptiveName = newDescName;
48 CString CWorkingFile::GetDescriptiveName()
50 if (m_sDescriptiveName.IsEmpty())
52 CString sDescriptiveName = CPathUtils::GetFileNameFromPath(m_sFilename);
53 WCHAR pathbuf[MAX_PATH] = {0};
54 PathCompactPathEx(pathbuf, sDescriptiveName, 50, 0);
55 sDescriptiveName = pathbuf;
56 return sDescriptiveName;
58 return m_sDescriptiveName;
61 CString CWorkingFile::GetReflectedName()
63 return m_sReflectedName;
66 void CWorkingFile::SetReflectedName(const CString& newReflectedName)
68 m_sReflectedName = newReflectedName;
72 // Make an empty file with this name
73 void CWorkingFile::CreateEmptyFile()
75 CAutoFile hFile = CreateFile(m_sFilename, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
78 // Move the details of the specified file to the current one, and then mark the specified file
79 // as out of use
80 void CWorkingFile::TransferDetailsFrom(CWorkingFile& rightHandFile)
82 // We don't do this to files which are already in use
83 ASSERT(!InUse());
85 m_sFilename = rightHandFile.m_sFilename;
86 m_sDescriptiveName = rightHandFile.m_sDescriptiveName;
87 rightHandFile.SetOutOfUse();
88 m_attribs = rightHandFile.m_attribs;
91 CString CWorkingFile::GetWindowName() const
93 CString sErrMsg;
94 // TortoiseMerge allows non-existing files to be used in a merge
95 // Inform the user (in a non-intrusive way) if a file is absent
96 if (! this->Exists())
98 sErrMsg = CString(MAKEINTRESOURCE(IDS_NOTFOUNDVIEWTITLEINDICATOR));
101 if(m_sDescriptiveName.IsEmpty())
103 // We don't have a proper name - use the filename part of the path
104 // return the filename part of the path.
105 return CPathUtils::GetFileNameFromPath(m_sFilename) + _T(" ") + sErrMsg;
107 else if (sErrMsg.IsEmpty())
109 return m_sDescriptiveName;
111 return m_sDescriptiveName + _T(" ") + sErrMsg;
114 bool CWorkingFile::Exists() const
116 return (!!PathFileExists(m_sFilename));
119 void CWorkingFile::SetOutOfUse()
121 m_sFilename.Empty();
122 m_sDescriptiveName.Empty();
123 ClearStoredAttributes();
127 bool CWorkingFile::HasSourceFileChanged() const
129 if (!InUse())
131 return false;
133 WIN32_FILE_ATTRIBUTE_DATA attribs = {0};
134 if (PathFileExists(m_sFilename))
136 if (GetFileAttributesEx(m_sFilename, GetFileExInfoStandard, &attribs))
138 if ( (m_attribs.nFileSizeHigh != attribs.nFileSizeHigh) ||
139 (m_attribs.nFileSizeLow != attribs.nFileSizeLow) )
140 return true;
141 return ( (CompareFileTime(&m_attribs.ftCreationTime, &attribs.ftCreationTime)!=0) ||
142 (CompareFileTime(&m_attribs.ftLastWriteTime, &attribs.ftLastWriteTime)!=0) );
146 return false;
149 void CWorkingFile::StoreFileAttributes()
151 ClearStoredAttributes();
153 WIN32_FILE_ATTRIBUTE_DATA attribs = {0};
154 if (GetFileAttributesEx(m_sFilename, GetFileExInfoStandard, &attribs))
156 m_attribs = attribs;
160 void CWorkingFile::ClearStoredAttributes()
162 static const WIN32_FILE_ATTRIBUTE_DATA attribsEmpty = {0};
163 m_attribs = attribsEmpty;
166 bool CWorkingFile::IsReadonly() const
168 return (m_attribs.dwFileAttributes != INVALID_FILE_ATTRIBUTES) && (m_attribs.dwFileAttributes & FILE_ATTRIBUTE_READONLY);