From 41621b2af3483e7aad9ce31ceec5de7a3f083cc4 Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Mon, 27 May 2024 01:07:19 +0100 Subject: [PATCH] Add more C stuff --- HarmonyLinkLib/include/Structs/CPUInfo.h | 42 +++++++++-- HarmonyLinkLib/src/Structs/CPUInfo.c | 88 ++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 6 deletions(-) diff --git a/HarmonyLinkLib/include/Structs/CPUInfo.h b/HarmonyLinkLib/include/Structs/CPUInfo.h index ae0532a..3620f77 100644 --- a/HarmonyLinkLib/include/Structs/CPUInfo.h +++ b/HarmonyLinkLib/include/Structs/CPUInfo.h @@ -12,21 +12,51 @@ // See the License for the specific language governing permissions and // limitations under the License. +// include/Structs/CPUInfo.h + #pragma once +#include #include #include "Core.h" -typedef struct -{ +typedef struct { + char** data; // Array of strings (flags) + size_t FlagsCount; // Number of flags +} FlagsInfo; + +typedef struct { char* VendorID; - bool Model_Name; + char* Model_Name; unsigned int Physical_Cores; unsigned int Logical_Cores; - char[] Flags; - + FlagsInfo flagsInfo; } FCPUInfo; -//HARMONYLINKLIB_API void FBattery_print(const FCPUInfo* self); +// 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); + +// Print FlagsInfo +HARMONYLINKLIB_API void HL_FlagsInfo_Print(const FlagsInfo* flagsInfo); + +// 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); + +// 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); diff --git a/HarmonyLinkLib/src/Structs/CPUInfo.c b/HarmonyLinkLib/src/Structs/CPUInfo.c index 703f260..182abe0 100644 --- a/HarmonyLinkLib/src/Structs/CPUInfo.c +++ b/HarmonyLinkLib/src/Structs/CPUInfo.c @@ -14,3 +14,91 @@ #include "Structs/CPUInfo.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, + unsigned int logicalCores, size_t flagsCount) { + cpuInfo->VendorID = strdup(vendorID); + cpuInfo->Model_Name = strdup(modelName); + cpuInfo->Physical_Cores = physicalCores; + cpuInfo->Logical_Cores = logicalCores; + FlagsInfo_Init(&cpuInfo->flagsInfo, flagsCount); +} + +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 FlagsInfo *flagsInfo) { + for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { + if (flagsInfo->data[i] != NULL) { + wprintf(L" %s\n", flagsInfo->data[i]); + } + } +} + +void HL_FCPUInfo_Print(const FCPUInfo *cpuInfo) { + wprintf(L"VendorID: %s\n", cpuInfo->VendorID); + wprintf(L"Model Name: %s\n", cpuInfo->Model_Name); + 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); +} + +void HL_FlagsInfo_Free(FlagsInfo *flagsInfo) { + for (size_t i = 0; i < flagsInfo->FlagsCount; ++i) { + free(flagsInfo->data[i]); + } + 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; +} + +void HL_FCPUInfo_Free(FCPUInfo *cpuInfo) { + free(cpuInfo->VendorID); + free(cpuInfo->Model_Name); + HL_FlagsInfo_Free(&cpuInfo->flagsInfo); +}