Provide (experimental) clang-format file
[TortoiseGit.git] / src / TortoiseShell / PreserveChdir.cpp
blob562df089e29614d18deb83ce55d5d2691c74c0d7
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2016 - TortoiseGit
4 // Copyright (C) 2003-2007 - 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 "PreserveChdir.h"
23 PreserveChdir::PreserveChdir()
25 DWORD len = GetCurrentDirectory(0, nullptr);
26 if (!len)
27 return;
29 m_originalCurrentDirectory = std::make_unique<TCHAR[]>(len);
30 if (GetCurrentDirectory(len, m_originalCurrentDirectory.get()) == 0)
31 m_originalCurrentDirectory.reset();
34 PreserveChdir::~PreserveChdir()
36 if (!m_originalCurrentDirectory)
37 return;
39 DWORD len = GetCurrentDirectory(0, nullptr);
40 auto currentDirectory = std::make_unique<TCHAR[]>(len);
42 // _tchdir is an expensive function - don't call it unless we really have to
43 GetCurrentDirectory(len, currentDirectory.get());
44 if (wcscmp(currentDirectory.get(), m_originalCurrentDirectory.get()) != 0)
45 SetCurrentDirectory(m_originalCurrentDirectory.get());