Cleanup code and docs; add missing header.
- The source and header files should have standard comment headers. - Windows support has been removed, as I've decomissioned my Windows desktop in favour of a Linux desktop. - Commander.h wasn't being added to the install directory.
This commit is contained in:
parent
0fff669b40
commit
0071b84ab5
|
@ -0,0 +1,27 @@
|
|||
scsl : The Shimmering Clarity Standard C++ Library
|
||||
==================================================
|
||||
|
||||
scsl is a collection of software I found myself needing to use repeatedly.
|
||||
|
||||
Full `Doxygen documentation`_ is available.
|
||||
|
||||
.. _Doxygen documentation: https://docs.shimmering-clarity.net/scsl/
|
||||
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
Copyright 2023 K. Isom <kyle@imap.cc>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
142
WinHelpers.cc
142
WinHelpers.cc
|
@ -1,142 +0,0 @@
|
|||
//
|
||||
// Created by kyle on 2023-10-10.
|
||||
//
|
||||
|
||||
#if defined(__WIN64__) || defined(__WIN32__) || defined(WIN32)
|
||||
|
||||
#include "WinHelpers.h"
|
||||
|
||||
|
||||
namespace scsl {
|
||||
namespace Windows {
|
||||
|
||||
|
||||
int
|
||||
DisplayWinError(LPTSTR lpszFunction, HANDLE handle)
|
||||
{
|
||||
// Retrieve the system error message for the last-error code
|
||||
DWORD dw = GetLastError();
|
||||
#ifndef NDEBUG
|
||||
LPVOID lpMsgBuf;
|
||||
LPVOID lpDisplayBuf;
|
||||
|
||||
FormatMessage(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL,
|
||||
dw,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPTSTR) &lpMsgBuf,
|
||||
0, NULL);
|
||||
|
||||
// Display the error message and exit the process
|
||||
lpDisplayBuf = (LPVOID) LocalAlloc(LMEM_ZEROINIT,
|
||||
(lstrlen((LPCTSTR) lpMsgBuf) +
|
||||
lstrlen((LPCTSTR) lpszFunction) +
|
||||
40) * sizeof(TCHAR));
|
||||
StringCchPrintf((LPTSTR) lpDisplayBuf,
|
||||
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
|
||||
TEXT("%s failed with error %d: %s"),
|
||||
lpszFunction, dw, lpMsgBuf);
|
||||
MessageBox(NULL, (LPCTSTR) lpDisplayBuf, TEXT("Error"), MB_OK);
|
||||
|
||||
LocalFree(lpMsgBuf);
|
||||
LocalFree(lpDisplayBuf);
|
||||
#endif
|
||||
if ((handle != NULL) && (handle != INVALID_HANDLE_VALUE)) {
|
||||
CloseHandle(handle);
|
||||
}
|
||||
return dw;
|
||||
}
|
||||
|
||||
|
||||
BOOL SetPrivilege(
|
||||
HANDLE hToken, // access token handle
|
||||
LPCTSTR lpszPrivilege, // name of privilege to enable/disable
|
||||
BOOL bEnablePrivilege // to enable or disable privilege
|
||||
)
|
||||
{
|
||||
TOKEN_PRIVILEGES tp;
|
||||
LUID luid;
|
||||
|
||||
if (!LookupPrivilegeValue(
|
||||
NULL, // lookup privilege on local system
|
||||
lpszPrivilege, // privilege to lookup
|
||||
&luid)) // receives LUID of privilege
|
||||
{
|
||||
printf("LookupPrivilegeValue error: %u\n", GetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
tp.PrivilegeCount = 1;
|
||||
tp.Privileges[0].Luid = luid;
|
||||
if (bEnablePrivilege)
|
||||
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
||||
else
|
||||
tp.Privileges[0].Attributes = 0;
|
||||
|
||||
// Enable the privilege or disable all privileges.
|
||||
|
||||
if (!AdjustTokenPrivileges(
|
||||
hToken,
|
||||
FALSE,
|
||||
&tp,
|
||||
sizeof(TOKEN_PRIVILEGES),
|
||||
(PTOKEN_PRIVILEGES) NULL,
|
||||
(PDWORD) NULL)) {
|
||||
printf("AdjustTokenPrivileges error: %u\n", GetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
|
||||
printf("The token does not have the specified privilege. \n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
HANDLE
|
||||
CreateFileWindows(const char *path)
|
||||
{
|
||||
HANDLE fHandle;
|
||||
|
||||
return CreateFileA(
|
||||
(LPSTR) path,
|
||||
(GENERIC_READ | GENERIC_WRITE),
|
||||
(FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE),
|
||||
NULL,
|
||||
CREATE_ALWAYS,
|
||||
FILE_ATTRIBUTE_NORMAL,
|
||||
NULL);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
CreateFixedSizeFile(const char *path, size_t size)
|
||||
{
|
||||
_LARGE_INTEGER fileSize;
|
||||
|
||||
fileSize.QuadPart = size;
|
||||
|
||||
HANDLE fHandle = CreateFileWindows(path);
|
||||
if (SetFilePointerEx(fHandle, fileSize, nullptr, FILE_BEGIN) != TRUE) {
|
||||
return DisplayWinError("SetFilePointerEx", fHandle);
|
||||
}
|
||||
|
||||
if (SetEndOfFile(fHandle) != TRUE) {
|
||||
return DisplayWinError("SetEndOfFile", fHandle);
|
||||
}
|
||||
|
||||
CloseHandle(fHandle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Windows
|
||||
} // namespace scsl
|
||||
|
||||
|
||||
#endif
|
39
WinHelpers.h
39
WinHelpers.h
|
@ -1,39 +0,0 @@
|
|||
//
|
||||
// Created by kyle on 2023-10-10.
|
||||
//
|
||||
|
||||
#ifndef SCSL_WINHELPERS_H
|
||||
#define SCSL_WINHELPERS_H
|
||||
|
||||
#if defined(__WIN64__) || defined(__WIN32__) || defined(WIN32)
|
||||
|
||||
#include <Windows.h>
|
||||
#include <winbase.h>
|
||||
#include <fileapi.h>
|
||||
#include <strsafe.h>
|
||||
|
||||
namespace scsl {
|
||||
namespace Windows {
|
||||
|
||||
|
||||
int DisplayWinError(LPTSTR lpszFunction, HANDLE handle);
|
||||
|
||||
BOOL SetPrivilege(
|
||||
HANDLE hToken, // access token handle
|
||||
LPCTSTR lpszPrivilege, // name of privilege to enable/disable
|
||||
BOOL bEnablePrivilege // to enable or disable privilege
|
||||
);
|
||||
|
||||
HANDLE CreateFileWindows(const char *path);
|
||||
|
||||
int CreateFixedSizeFile(const char *path, size_t size);
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Windows
|
||||
} // namespace scsl
|
||||
|
||||
#endif // Windows-only guards.
|
||||
|
||||
#endif //SCSL_WINHELPERS_H
|
Loading…
Reference in New Issue