diff --git a/HarmonyLinkLib/CMakeLists.txt b/HarmonyLinkLib/CMakeLists.txt index 176f6ea..b794608 100644 --- a/HarmonyLinkLib/CMakeLists.txt +++ b/HarmonyLinkLib/CMakeLists.txt @@ -52,26 +52,30 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Resources/Version.rc.in ${CMAKE_CURRE # Explicitly list source files set(COMMON_SOURCES - "src/dllmain.c" - "src/HarmonyLinkLib.c" - "src/Version.c" + src/dllmain.c + src/HarmonyLinkLib.c + src/Version.c + src/Utilities.c - "src/Structs/Battery.c" - "src/Structs/CPUInfo.c" + src/Structs/Battery.c + src/Structs/CPUInfo.c + src/Structs/StringArray.c ) # Explicitly list include files set(COMMON_INCLUDES - "include/Core.h" - "include/HarmonyLinkLib.h" - "include/Version.h" + include/Core.h + include/HarmonyLinkLib.h + include/Version.h + include/Utilities.h - "include/Enums/Device.h" - "include/Enums/Platform.h" - "include/Enums/SteamDeck.h" + include/Enums/Device.h + include/Enums/Platform.h + include/Enums/SteamDeck.h - "include/Structs/Battery.h" - "include/Structs/CPUInfo.h" + include/Structs/Battery.h + include/Structs/CPUInfo.h + include/Structs/StringArray.h ) set(WINDOWS_SOURCES diff --git a/HarmonyLinkLib/include/Structs/Battery.h b/HarmonyLinkLib/include/Structs/Battery.h index 0ee37b3..af566e7 100644 --- a/HarmonyLinkLib/include/Structs/Battery.h +++ b/HarmonyLinkLib/include/Structs/Battery.h @@ -25,5 +25,7 @@ typedef struct unsigned char battery_percent; } FBattery; +HARMONYLINKLIB_API FBattery* FBattery_Init(const FBattery* self); + HARMONYLINKLIB_API void FBattery_print(const FBattery* self); diff --git a/HarmonyLinkLib/include/Structs/CPUInfo.h b/HarmonyLinkLib/include/Structs/CPUInfo.h index 3620f77..f9daddb 100644 --- a/HarmonyLinkLib/include/Structs/CPUInfo.h +++ b/HarmonyLinkLib/include/Structs/CPUInfo.h @@ -20,43 +20,27 @@ #include #include "Core.h" - -typedef struct { - char** data; // Array of strings (flags) - size_t FlagsCount; // Number of flags -} FlagsInfo; +#include "Structs/StringArray.h" typedef struct { char* VendorID; char* Model_Name; unsigned int Physical_Cores; unsigned int Logical_Cores; - FlagsInfo flagsInfo; + StringArray flagsInfo; } FCPUInfo; -// Initialize FlagsInfo -void FlagsInfo_Init(FlagsInfo* flagsInfo, size_t count); - // Initialize FCPUInfo -void FCPUInfo_Init(FCPUInfo* cpuInfo, const char* vendorID, const char* modelName, unsigned int physicalCores, unsigned int logicalCores, size_t flagsCount); +FCPUInfo* FCPUInfo_Init(const char* vendorID, const char* modelName, unsigned int physicalCores, unsigned int logicalCores, size_t flagsCount); // Print FlagsInfo -HARMONYLINKLIB_API void HL_FlagsInfo_Print(const FlagsInfo* flagsInfo); +HARMONYLINKLIB_API void HL_FlagsInfo_Print(const FCPUInfo* cpuInfo); // Print FCPUInfo HARMONYLINKLIB_API void HL_FCPUInfo_Print(const FCPUInfo* cpuInfo); -// Free FlagsInfo -HARMONYLINKLIB_API void HL_FlagsInfo_Free(FlagsInfo* flagsInfo); - // Free FCPUInfo -HARMONYLINKLIB_API void FCPUInfo_Free(FCPUInfo* cpuInfo); +HARMONYLINKLIB_API void HL_FCPUInfo_Free(FCPUInfo* cpuInfo); // Check if a flag exists in FlagsInfo -HARMONYLINKLIB_API bool HL_FlagsInfo_Contains(const FlagsInfo* flagsInfo, const char* flag); - -// Add a flag to FlagsInfo -void FlagsInfo_AddFlag(FlagsInfo* flagsInfo, size_t index, const char* flag); - -// Remove a flag from FlagsInfo by value -void FlagsInfo_Remove(FlagsInfo* flagsInfo, const char* flag); +HARMONYLINKLIB_API bool HL_FlagsInfo_Contains(const FCPUInfo* cpuInfo, const char* flag); diff --git a/HarmonyLinkLib/include/Structs/StringArray.h b/HarmonyLinkLib/include/Structs/StringArray.h new file mode 100644 index 0000000..ff7b433 --- /dev/null +++ b/HarmonyLinkLib/include/Structs/StringArray.h @@ -0,0 +1,51 @@ +// Copyright (c) 2024 Jordon Brooks +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// include/Structs/StringArray.h + +#pragma once + +#include +#include + +#include "Core.h" + +#define DEFAULT_INITIAL_FLAGS_SIZE 4 + +typedef struct { + char** data; // Array of strings (flags) + size_t FlagsCount; // Number of flags + size_t AllocatedSize; // Number of allocated slots +} StringArray; + +// Initialize FlagsInfo +void StringArray_Init(StringArray* flagsInfo, size_t overrideInitialSize); + +// Print FlagsInfo +HARMONYLINKLIB_API void HL_StringArray_Print(const StringArray* flagsInfo); + +// Free FlagsInfo +HARMONYLINKLIB_API void HL_StringArray_Free(StringArray* flagsInfo); + +// Check if a flag exists in FlagsInfo +HARMONYLINKLIB_API bool HL_StringArray_Contains(const StringArray* flagsInfo, const char* flag); + +// Add a flag to FlagsInfo +void StringArray_AddFlag(StringArray* flagsInfo, const char* flag); + +// Remove a flag from FlagsInfo by value +void StringArray_Remove(StringArray* flagsInfo, const char* flag); + +// Resize FlagsInfo array +void StringArray_Resize(StringArray* flagsInfo, size_t newSize); diff --git a/HarmonyLinkLib/include/Utilities.h b/HarmonyLinkLib/include/Utilities.h new file mode 100644 index 0000000..f50ad6a --- /dev/null +++ b/HarmonyLinkLib/include/Utilities.h @@ -0,0 +1,18 @@ +// Copyright (c) 2024 Jordon Brooks +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +// Utility function to convert char* to wchar_t* +wchar_t* convertToWideChar(const char* str); \ No newline at end of file diff --git a/HarmonyLinkLib/src/Structs/CPUInfo.c b/HarmonyLinkLib/src/Structs/CPUInfo.c index 182abe0..1c169e9 100644 --- a/HarmonyLinkLib/src/Structs/CPUInfo.c +++ b/HarmonyLinkLib/src/Structs/CPUInfo.c @@ -13,92 +13,66 @@ // limitations under the License. #include "Structs/CPUInfo.h" +#include "Utilities.h" #include #include -void FlagsInfo_Init(FlagsInfo *flagsInfo, size_t count) { - flagsInfo->FlagsCount = count; - flagsInfo->data = (char**)malloc(count * sizeof(char*)); - for (size_t i = 0; i < count; ++i) { - flagsInfo->data[i] = NULL; - } -} - -void FCPUInfo_Init(FCPUInfo *cpuInfo, const char *vendorID, const char *modelName, unsigned int physicalCores, +FCPUInfo* FCPUInfo_Init(const char *vendorID, const char *modelName, unsigned int physicalCores, unsigned int logicalCores, size_t flagsCount) { - cpuInfo->VendorID = strdup(vendorID); - cpuInfo->Model_Name = strdup(modelName); + FCPUInfo *cpuInfo = (FCPUInfo*)malloc(sizeof(FCPUInfo)); + + if (cpuInfo == NULL) { + fprintf(stderr, "Memory allocation failed for FCPUInfo.\n"); + exit(EXIT_FAILURE); + } + + cpuInfo->VendorID = _strdup(vendorID); + cpuInfo->Model_Name = _strdup(modelName); cpuInfo->Physical_Cores = physicalCores; cpuInfo->Logical_Cores = logicalCores; - FlagsInfo_Init(&cpuInfo->flagsInfo, flagsCount); + StringArray_Init(&cpuInfo->flagsInfo, flagsCount); + + return cpuInfo; } -void FlagsInfo_AddFlag(FlagsInfo *flagsInfo, size_t index, const char *flag) { - if (index < flagsInfo->FlagsCount) { - flagsInfo->data[index] = strdup(flag); +void HL_FlagsInfo_Print(const FCPUInfo* cpuInfo) { + if (!cpuInfo) + { + fprintf(stderr, "cpuInfo is nullptr!\n"); + return; } -} -void HL_FlagsInfo_Print(const FlagsInfo *flagsInfo) { - for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { - if (flagsInfo->data[i] != NULL) { - wprintf(L" %s\n", flagsInfo->data[i]); - } - } + HL_StringArray_Print(&cpuInfo->flagsInfo); } void HL_FCPUInfo_Print(const FCPUInfo *cpuInfo) { - wprintf(L"VendorID: %s\n", cpuInfo->VendorID); - wprintf(L"Model Name: %s\n", cpuInfo->Model_Name); + wchar_t* wVendorID = convertToWideChar(cpuInfo->VendorID); + wchar_t* wModelName = convertToWideChar(cpuInfo->Model_Name); + + wprintf(L"VendorID: %ls\n", wVendorID); + wprintf(L"Model Name: %ls\n", wModelName); wprintf(L"Physical Cores: %u\n", cpuInfo->Physical_Cores); wprintf(L"Logical Cores: %u\n", cpuInfo->Logical_Cores); wprintf(L"Flags:\n"); - HL_FlagsInfo_Print(&cpuInfo->flagsInfo); + HL_StringArray_Print(&cpuInfo->flagsInfo); + + free(wVendorID); + free(wModelName); } -void HL_FlagsInfo_Free(FlagsInfo *flagsInfo) { - for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { - free(flagsInfo->data[i]); +bool HL_FlagsInfo_Contains(const FCPUInfo* cpuInfo, const char* flag) { + if (!cpuInfo) + { + fprintf(stderr, "cpuInfo is nullptr!\n"); + return false; } - free(flagsInfo->data); -} -void FlagsInfo_Remove(FlagsInfo *flagsInfo, const char *flag) { - for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { - if (flagsInfo->data[i] && strcmp(flagsInfo->data[i], flag) == 0) { - free(flagsInfo->data[i]); - for (size_t j = i; j < flagsInfo->FlagsCount - 1; ++j) { - flagsInfo->data[j] = flagsInfo->data[j + 1]; - } - flagsInfo->data[flagsInfo->FlagsCount - 1] = NULL; - flagsInfo->FlagsCount--; - - // Use a temporary pointer to ensure realloc success - char** temp = realloc(flagsInfo->data, flagsInfo->FlagsCount * sizeof(char*)); - if (temp != NULL) { - flagsInfo->data = temp; - } else if (flagsInfo->FlagsCount > 0) { - // realloc failed and we're not deallocating to zero, handle error - fprintf(stderr, "Realloc failed. Memory not reallocated.\n"); - exit(EXIT_FAILURE); - } - break; - } - } -} - -bool HL_FlagsInfo_Contains(const FlagsInfo *flagsInfo, const char *flag) { - for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { - if (flagsInfo->data[i] && strcmp(flagsInfo->data[i], flag) == 0) { - return true; - } - } - return false; + return HL_StringArray_Contains(&cpuInfo->flagsInfo, flag); } void HL_FCPUInfo_Free(FCPUInfo *cpuInfo) { free(cpuInfo->VendorID); free(cpuInfo->Model_Name); - HL_FlagsInfo_Free(&cpuInfo->flagsInfo); + HL_StringArray_Free(&cpuInfo->flagsInfo); } diff --git a/HarmonyLinkLib/src/Structs/StringArray.c b/HarmonyLinkLib/src/Structs/StringArray.c new file mode 100644 index 0000000..aa2187f --- /dev/null +++ b/HarmonyLinkLib/src/Structs/StringArray.c @@ -0,0 +1,117 @@ +// Copyright (c) 2024 Jordon Brooks +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "Structs/StringArray.h" +#include "Utilities.h" + +#include +#include + +// Initialize FlagsInfo with optional initial size +void StringArray_Init(StringArray *flagsInfo, size_t overrideInitialSize) { + size_t initialSize = DEFAULT_INITIAL_FLAGS_SIZE; + + if (overrideInitialSize > 0) { + initialSize = overrideInitialSize; + } + + flagsInfo->FlagsCount = 0; + flagsInfo->AllocatedSize = initialSize; + flagsInfo->data = (char**)malloc(initialSize * sizeof(char*)); + for (size_t i = 0; i < initialSize; ++i) { + flagsInfo->data[i] = NULL; + } +} + +void StringArray_AddFlag(StringArray *flagsInfo, const char *flag) { + // Check if we need to resize the array + if (flagsInfo->FlagsCount >= flagsInfo->AllocatedSize) { + StringArray_Resize(flagsInfo, flagsInfo->AllocatedSize * 2); + } + flagsInfo->data[flagsInfo->FlagsCount] = _strdup(flag); + flagsInfo->FlagsCount++; +} + +void HL_StringArray_Print(const StringArray *flagsInfo) { + for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { + if (flagsInfo->data[i] != NULL) { + wchar_t* wstr = convertToWideChar(flagsInfo->data[i]); + wprintf(L" %ls\n", wstr); + free(wstr); + } + } +} + +void HL_StringArray_Free(StringArray *flagsInfo) { + for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { + free(flagsInfo->data[i]); + } + free(flagsInfo->data); +} + +void StringArray_Remove(StringArray *flagsInfo, const char *flag) { + for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { + if (flagsInfo->data[i] && strcmp(flagsInfo->data[i], flag) == 0) { + free(flagsInfo->data[i]); + for (size_t j = i; j < flagsInfo->FlagsCount - 1; ++j) { + flagsInfo->data[j] = flagsInfo->data[j + 1]; + } + flagsInfo->data[flagsInfo->FlagsCount - 1] = NULL; + flagsInfo->FlagsCount--; + break; + } + } +} + +bool HL_StringArray_Contains(const StringArray *flagsInfo, const char *flag) { + for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { + if (flagsInfo->data[i] && strcmp(flagsInfo->data[i], flag) == 0) { + return true; + } + } + return false; +} + +// Resize FlagsInfo array +void StringArray_Resize(StringArray* flagsInfo, size_t newSize) { + // Count the number of non-null pointers + size_t nonNullCount = 0; + for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { + if (flagsInfo->data[i] != NULL) { + nonNullCount++; + } + } + + // Check if the new size is smaller than the number of non-null pointers + if (newSize < nonNullCount) { + fprintf(stderr, "Resize failed. New size is smaller than the number of non-null pointers.\n"); + return; + } + + char** temp = realloc(flagsInfo->data, newSize * sizeof(char*)); + if (temp != NULL) { + flagsInfo->data = temp; + if (newSize > flagsInfo->AllocatedSize) { + // Initialize new elements to NULL + for (size_t i = flagsInfo->AllocatedSize; i < newSize; ++i) { + flagsInfo->data[i] = NULL; + } + } + flagsInfo->AllocatedSize = newSize; + } else { + // Handle realloc failure + fprintf(stderr, "Realloc failed. Memory not reallocated.\n"); + exit(EXIT_FAILURE); + } +} diff --git a/HarmonyLinkLib/src/Utilities.c b/HarmonyLinkLib/src/Utilities.c new file mode 100644 index 0000000..25d0bf7 --- /dev/null +++ b/HarmonyLinkLib/src/Utilities.c @@ -0,0 +1,27 @@ +// Copyright (c) 2024 Jordon Brooks +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "Utilities.h" +#include + +wchar_t* convertToWideChar(const char* str) { + size_t len = 0; + mbstowcs_s(&len, NULL, 0, str, 0); // Get the length of the wide string (including null terminator) + wchar_t* wstr = (wchar_t*)malloc(len * sizeof(wchar_t)); + if (wstr == NULL) { + return NULL; // Handle memory allocation failure + } + mbstowcs_s(&len, wstr, len, str, len - 1); + return wstr; +} \ No newline at end of file