SC_Filesystem_win.cpp 5.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/*
 *  Copyright (c) 2005 Tim Walters. All rights reserved.
 *  Copyright (c) 2017 Brian Heim. All rights reserved.
 *  Created by Tim Walters on 2005-10-19.
 *
 *	Revision history:
 *	  Changed from SC_DirUtils to SC_Filesystem (Brian Heim, 2017-04-03)
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301 USA
 */

/*
 * SC_Filesystem implementation for Windows.
 */
#ifdef _WIN32

#include "SC_Filesystem.hpp"
#include "SC_Codecvt.hpp"

// boost
#include <boost/filesystem/operations.hpp> // is_directory

// system
#include <Shlobj.h> // SHGetKnownFolderPath

using Path = SC_Filesystem::Path;
using DirName = SC_Filesystem::DirName;
using DirMap = SC_Filesystem::DirMap;

//============ PATH UTILITIES =============//

Path SC_Filesystem::resolveIfAlias(const Path& p, bool& isAlias) { isAlias = false; return p; }

//============ GLOB UTILITIES =============//

// Keep a buffer of one filename to return on the next call to nextGlob.
// This is because FindFirstFile already loads one filename.
struct SC_Filesystem::Glob
{
	HANDLE mHandle; // find handle
	Path mFolder; // parent folder of the search path
	WIN32_FIND_DATAW mEntry;
	bool mAtEnd; // true if the NEXT call to nextGlob should fail
	Path mFilename; // filename to return on the next call to nextGlob
};

SC_Filesystem::Glob* SC_Filesystem::makeGlob(const char* pattern)
{
	Glob* glob = new Glob;

	// use make_preferred() to change / -> \ on Windows
	boost::filesystem::path path = SC_Codecvt::utf8_str_to_path(pattern).make_preferred();

	// remove a trailing backslash. Even if searching with 'foo/.', this will
	// change to 'foo' harmlessly. Use has_parent_path() because otherwise '.'
	// (referring to the CWD) won't be recognized as a valid query.
	if (path.filename_is_dot() && path.has_parent_path())
		path = path.parent_path();

	glob->mHandle = ::FindFirstFileW(path.wstring().c_str(), &glob->mEntry);
	if (glob->mHandle == INVALID_HANDLE_VALUE) {
		delete glob;
		return nullptr;
	}

	glob->mFolder = path.parent_path();
	glob->mAtEnd = false;
	return glob;
}

void SC_Filesystem::freeGlob(Glob* glob)
{
	::FindClose(glob->mHandle);
	delete glob;
}

Path SC_Filesystem::globNext(Glob* glob)
{
	bool isDirectory = false;

	// loop to ignore . and .. results
	do {
		if (glob->mAtEnd)
			return Path();
		glob->mFilename = glob->mFolder / glob->mEntry.cFileName;

		// record whether it's a directory here, since we overwrite mEntry on the next step
		isDirectory = (glob->mEntry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0;

		if (!::FindNextFileW(glob->mHandle, &glob->mEntry))
			glob->mAtEnd = true;
	} while (glob->mFilename.filename_is_dot() || glob->mFilename.filename_is_dot_dot());

	// add preferred separator (L'\\') for directories on Windows, to match
	// POSIX globbing. boost::filesystem::is_directory won't work for this because
	// in the case of input '.' and '..', the filename here is just a single folder,
	// not a relative path, and so can't be correctly identified. Plus, it's faster
	// to check the attributes than to make another system call.
	return isDirectory ? glob->mFilename += boost::filesystem::path::preferred_separator
		: glob->mFilename;
}

//============= PRIVATE METHODS ==============//

bool SC_Filesystem::isNonHostPlatformDirectoryName(const std::string& s)
{
	return s == "linux" || s == "osx" || s == "iphone";
}

Path SC_Filesystem::defaultSystemAppSupportDirectory()
{
	PWSTR wptr = nullptr;
126
	const HRESULT hr = SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &wptr);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	return FAILED(hr) ? Path() : Path(wptr) / SC_FOLDERNAME_APPLICATION_NAME;
}

Path SC_Filesystem::defaultUserHomeDirectory()
{
	PWSTR wptr = nullptr;
	const HRESULT hr = SHGetKnownFolderPath(FOLDERID_Profile, 0, nullptr, &wptr);
	return FAILED(hr) ? Path() : Path(wptr);
}

Path SC_Filesystem::defaultUserAppSupportDirectory()
{
	PWSTR wptr = nullptr;
	const HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &wptr);
	return FAILED(hr) ? Path() : Path(wptr) / SC_FOLDERNAME_APPLICATION_NAME;
}

Path SC_Filesystem::defaultUserConfigDirectory()
{
	return defaultUserAppSupportDirectory();
}

Path SC_Filesystem::defaultMyDocumentsDirectory()
{
	PWSTR wptr = nullptr;
	const HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, nullptr, &wptr);
	return FAILED(hr) ? Path() : Path(wptr) / SC_FOLDERNAME_APPLICATION_NAME;
}

Path SC_Filesystem::defaultResourceDirectory()
{
	WCHAR buf[MAX_PATH];
	GetModuleFileNameW(nullptr, buf, MAX_PATH);
	return Path(buf).parent_path();
}

#endif // _WIN32