From 0071b84ab54aca82cb42bca553a9ff00728c9d7f Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 16 Oct 2023 02:18:09 -0700 Subject: [PATCH] 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. --- README.rst | 27 ++++++++++ WinHelpers.cc | 142 -------------------------------------------------- WinHelpers.h | 39 -------------- 3 files changed, 27 insertions(+), 181 deletions(-) create mode 100644 README.rst delete mode 100644 WinHelpers.cc delete mode 100644 WinHelpers.h diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..552001d --- /dev/null +++ b/README.rst @@ -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 + +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. + diff --git a/WinHelpers.cc b/WinHelpers.cc deleted file mode 100644 index 7420e12..0000000 --- a/WinHelpers.cc +++ /dev/null @@ -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 \ No newline at end of file diff --git a/WinHelpers.h b/WinHelpers.h deleted file mode 100644 index e00b98d..0000000 --- a/WinHelpers.h +++ /dev/null @@ -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 -#include -#include -#include - -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