Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wackoisgod/1fdcdd39e74619f9e3177e4d9ff6fc87 to your computer and use it in GitHub Desktop.
Save wackoisgod/1fdcdd39e74619f9e3177e4d9ff6fc87 to your computer and use it in GitHub Desktop.
// TestMakeSire.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <windows.h>
#include <ctype.h>
#include <shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
int LongPathToPrefixedWidePath(const char* path, wchar_t* buffer, int bufferSize)
{
const int longPathPrefixLength = 4;
const wchar_t longPathPrefix[longPathPrefixLength] = { L'\\', L'\\', L'?', L'\\' };
const int wideStringLength = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
bool isAbsolute = path[1] == ':' && (path[2] == '\\' || path[2] == '/');
if (bufferSize > longPathPrefixLength)
{
memcpy(buffer, longPathPrefix, sizeof(wchar_t) * longPathPrefixLength);
buffer += longPathPrefixLength;
}
if (!isAbsolute)
{
// We cannot use Win32 API methods for converting the path from relative to absolute because those methods do not handle long paths
const DWORD currentDirectorySize = GetCurrentDirectoryW(0, NULL);
int requiredBufferSize = longPathPrefixLength + currentDirectorySize + wideStringLength;
if (buffer == nullptr || bufferSize < requiredBufferSize)
return requiredBufferSize;
GetCurrentDirectoryW(currentDirectorySize, buffer);
buffer[currentDirectorySize - 1] = '\\';
buffer += currentDirectorySize;
}
else
{
if (bufferSize < longPathPrefixLength + wideStringLength)
return longPathPrefixLength + wideStringLength;
}
MultiByteToWideChar(CP_UTF8, 0, path, -1, buffer, wideStringLength);
return 0;
}
const int k_LongPathPrefixLength = 4;
const wchar_t g_LongPathPrefix[k_LongPathPrefixLength] = { L'\\', L'\\', L'?', L'\\' };
bool MakeDirectory(const char* path)
{
/* pretend we can always create device roots */
if (isalpha(path[0]) && 0 == memcmp(&path[1], ":\\\0", 3))
return true;
const int wideStringLength = LongPathToPrefixedWidePath(path, NULL, 0);
wchar_t* widePath = static_cast<wchar_t*>(alloca(sizeof(wchar_t) * wideStringLength));
LongPathToPrefixedWidePath(path, widePath, wideStringLength);
if (!CreateDirectoryW(widePath, NULL))
{
auto error = GetLastError();
switch (error)
{
case ERROR_ALREADY_EXISTS:
return PathIsDirectoryA(path);
default:
return false;
}
}
else
return true;
}
int main()
{
std::cout << "Hello World!\n";
MakeDirectory("..\\..\\Builds\\");
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment