Provide (experimental) clang-format file
[TortoiseGit.git] / src / TortoiseMerge / WorkingFile.cpp
blob0e05719ae34b526750220d12a094a5d116000efd
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2017 - TortoiseGit
4 // Copyright (C) 2006-2007, 2011-2014 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "WorkingFile.h"
22 #include "AppUtils.h"
23 #include "PathUtils.h"
24 #include "resource.h"
25 #include "SmartHandle.h"
27 CWorkingFile::CWorkingFile(void)
29 ClearStoredAttributes();
32 CWorkingFile::~CWorkingFile(void)
36 void CWorkingFile::SetFileName(const CString& newFilename)
38 m_sFilename = newFilename;
39 m_sFilename.Replace('/', '\\');
40 m_sDescriptiveName.Empty();
41 ClearStoredAttributes();
44 void CWorkingFile::SetDescriptiveName(const CString& newDescName)
46 m_sDescriptiveName = newDescName;
49 CString CWorkingFile::GetDescriptiveName()
51 if (m_sDescriptiveName.IsEmpty())
53 CString sDescriptiveName = CPathUtils::GetFileNameFromPath(m_sFilename);
54 WCHAR pathbuf[MAX_PATH] = {0};
55 PathCompactPathEx(pathbuf, sDescriptiveName, 50, 0);
56 sDescriptiveName = pathbuf;
57 return sDescriptiveName;
59 return m_sDescriptiveName;
62 CString CWorkingFile::GetReflectedName()
64 return m_sReflectedName;
67 void CWorkingFile::SetReflectedName(const CString& newReflectedName)
69 m_sReflectedName = newReflectedName;
73 // Make an empty file with this name
74 void CWorkingFile::CreateEmptyFile()
76 CAutoFile hFile = CreateFile(m_sFilename, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
79 // Move the details of the specified file to the current one, and then mark the specified file
80 // as out of use
81 void CWorkingFile::TransferDetailsFrom(CWorkingFile& rightHandFile)
83 // We don't do this to files which are already in use
84 ASSERT(!InUse());
86 m_sFilename = rightHandFile.m_sFilename;
87 m_sDescriptiveName = rightHandFile.m_sDescriptiveName;
88 rightHandFile.SetOutOfUse();
89 m_attribs = rightHandFile.m_attribs;
92 CString CWorkingFile::GetWindowName(UINT type) const
94 CString sErrMsg;
95 // TortoiseMerge allows non-existing files to be used in a merge
96 // Inform the user (in a non-intrusive way) if a file is absent
97 if (! this->Exists())
99 sErrMsg = CString(MAKEINTRESOURCE(IDS_NOTFOUNDVIEWTITLEINDICATOR));
102 if(m_sDescriptiveName.IsEmpty())
104 // We don't have a proper name - use the filename part of the path
105 // return the filename part of the path.
106 CString ret;
107 if (type)
109 ret.LoadString(type);
110 ret += L" - ";
112 ret += CPathUtils::GetFileNameFromPath(m_sFilename) + L' ' + sErrMsg;
113 return ret;
115 else if (sErrMsg.IsEmpty())
117 return m_sDescriptiveName;
119 return m_sDescriptiveName + L' ' + sErrMsg;
122 bool CWorkingFile::Exists() const
124 return (!!PathFileExists(m_sFilename));
127 void CWorkingFile::SetOutOfUse()
129 m_sFilename.Empty();
130 m_sDescriptiveName.Empty();
131 ClearStoredAttributes();
135 bool CWorkingFile::HasSourceFileChanged() const
137 if (!InUse())
139 return false;
141 WIN32_FILE_ATTRIBUTE_DATA attribs = {0};
142 if (PathFileExists(m_sFilename))
144 if (GetFileAttributesEx(m_sFilename, GetFileExInfoStandard, &attribs))
146 if ( (m_attribs.nFileSizeHigh != attribs.nFileSizeHigh) ||
147 (m_attribs.nFileSizeLow != attribs.nFileSizeLow) )
148 return true;
149 return ( (CompareFileTime(&m_attribs.ftCreationTime, &attribs.ftCreationTime)!=0) ||
150 (CompareFileTime(&m_attribs.ftLastWriteTime, &attribs.ftLastWriteTime)!=0) );
154 return false;
157 void CWorkingFile::StoreFileAttributes()
159 ClearStoredAttributes();
161 WIN32_FILE_ATTRIBUTE_DATA attribs = {0};
162 if (GetFileAttributesEx(m_sFilename, GetFileExInfoStandard, &attribs))
164 m_attribs = attribs;
168 void CWorkingFile::ClearStoredAttributes()
170 static const WIN32_FILE_ATTRIBUTE_DATA attribsEmpty = {0};
171 m_attribs = attribsEmpty;
174 bool CWorkingFile::IsReadonly() const
176 return (m_attribs.dwFileAttributes != INVALID_FILE_ATTRIBUTES) && (m_attribs.dwFileAttributes & FILE_ATTRIBUTE_READONLY);