You like light mode? Ew!
The SNBP library is a comprehensive utility library designed for the Bassil language project. It provides a wide range of functions for string manipulation, Windows API interaction, file handling, and console output formatting.
Version: 0.7.4
Author:
Last Updated: 12/9/2024 (DD/MM/YYYY)
License: MIT License
All functions and classes in this library are encapsulated within the SNBP
namespace.
std::vector<std::string> splitString(const std::string &s, const std::string &delimiter = " ")
Description: Splits a string into a vector of substrings based on a specified delimiter.
Parameters:
s
: The input string to be split.delimiter
: The string that serves as the delimiter for splitting. Default is a space.Return Value: A vector containing the substrings resulting from splitting the input string.
Example:
std::string input = "apple,banana,cherry"; std::vector<std::string> result = SNBP::splitString(input, ","); // result now contains {"apple", "banana", "cherry"}
std::string& ltrim(std::string &s) std::string& rtrim(std::string &s) std::string& trim(std::string &s)
Description: These functions trim whitespace from strings.
ltrim
: Removes leading whitespace.rtrim
: Removes trailing whitespace.trim
: Removes both leading and trailing whitespace.Parameters:
s
: The string to be trimmed (passed by reference).Return Value: A reference to the modified string, allowing for method chaining.
Example:
std::string str = " Hello, World! "; SNBP::trim(str); // str is now "Hello, World!"
int CreateWinAPI32MessageBox(const std::string &_title, const std::string &_message, int _type)
Description: Creates a Windows API 32-bit message box.
Parameters:
_title
: The title of the message box._message
: The content of the message box._type
: The type of the message box, determining which buttons are displayed.Return Value: The ID of the button clicked by the user.
Note: The _type
parameter can have the following values:
Example:
int result = SNBP::CreateWinAPI32MessageBox("Warning", "Are you sure?", 5); if (result == IDYES) { // User clicked Yes } else { // User clicked No }
int generalLog(const std::string &str, bool isPrintTrue = true)
Description: Logs a message to a file and optionally prints it to the console.
Parameters:
str
: The message to be logged.isPrintTrue
: Flag to indicate if the message should be printed to the console. Default is true.Return Value: Returns 0 on successful logging, 1 if there was an error opening the log file.
Note: The log file is located at "C:/coding-projects/CPP-Dev/bassil/output/logs.log".
Example:
int result = SNBP::generalLog("Application started", true); if (result != 0) { std::cerr << "Failed to write to log file" << std::endl; }
int enableAnsiInConsole()
Description: Enables ANSI escape sequence processing for the console output, allowing the use of colored text and other formatting in the console.
Return Value: Returns 0 on success, 1 if there was an error enabling ANSI support.
Note: This function should be called once at the beginning of the program if ANSI support is needed.
Example:
if (SNBP::enableAnsiInConsole() == 0) { std::cout << "\033[31mThis text is red\033[0m" << std::endl; } else { std::cerr << "Failed to enable ANSI support" << std::endl; }
The dhi
(Detailed Hardware Information) namespace provides functions to retrieve various hardware details on Windows systems.
struct HardwareInfo { std::wstring category; std::vector<std::pair<std::wstring, std::wstring>> properties; };
Description: This structure holds information about a specific hardware category.
category
: The name of the hardware category (e.g., "System Information", "Processor Information").properties
: A vector of key-value pairs representing the properties of the hardware.getSystemInfo(WMIService &wmi)
: Retrieves general system information.getProcessorInfo(WMIService &wmi)
: Retrieves processor information.getMemoryInfo(WMIService &wmi)
: Retrieves memory information.getDiskInfo(WMIService &wmi)
: Retrieves disk information.getGraphicsInfo(WMIService &wmi)
: Retrieves graphics card information.Note: These functions require a WMIService
object, which handles the Windows Management Instrumentation (WMI) queries.
int SNPBinit()
Description: Initializes the SNBP library, setting up version information and other necessary configurations.
Return Value: Returns 0 on successful initialization.
Note: This function should be called at the beginning of your program before using any other SNBP functions.
Example:
if (SNBP::SNPBinit() == 0) { std::cout << "SNBP library initialized successfully" << std::endl; } else { std::cerr << "Failed to initialize SNBP library" << std::endl; }
The SNBP library uses a combination of return values and exceptions for error handling:
std::runtime_error
: Thrown for general runtime errors, such as file I/O failures or Windows API errors.std::invalid_argument
: Thrown when a function receives invalid input parameters.std::out_of_range
: Thrown when attempting to access elements outside the valid range, e.g., in file reading operations.Example:
try { std::string content = SNBP::readFileToString("config.txt"); std::cout << "File content: " << content << std::endl; } catch (const std::runtime_error& e) { std::cerr << "Error reading file: " << e.what() << std::endl; SNBP::generalLog("File read error: " + std::string(e.what())); }
The SNBP library is not inherently thread-safe. When using SNBP functions in a multi-threaded environment, consider the following:
generalLog
, readFileToString
) should be synchronized when accessed from multiple threads.dhi
namespace use COM, which requires proper initialization in a multi-threaded environment.Example of thread-safe logging:
#include <mutex> std::mutex logMutex; void threadSafeLog(const std::string& message) { std::lock_guard<std::mutex> lock(logMutex); SNBP::generalLog(message); }
splitString
and trim
may create copies of strings. For large strings or frequent operations, consider using string views or references where possible.dhi
namespace, can be relatively slow. Cache results when appropriate rather than calling these functions repeatedly.enableAnsiInConsole
) may have a small performance impact on console output. Only enable if needed.Example of optimized string splitting:
void processSplitString(const std::string& input, const std::string& delimiter) { size_t start = 0; size_t end = input.find(delimiter); while (end != std::string::npos) { processSubstring(input.substr(start, end - start)); start = end + delimiter.length(); end = input.find(delimiter, start); } processSubstring(input.substr(start)); }
SNBP::SNPBinit()
at the start of your program.const std::string&
for input parameters to avoid unnecessary copying.isAnsiEnabledInConsole()
.Example of good practice:
int main() { if (SNBP::SNPBinit() != 0) { std::cerr << "Failed to initialize SNBP library" << std::endl; return 1; } if (SNBP::enableAnsiInConsole() == 0 && SNBP::isAnsiEnabledInConsole()) { std::cout << SNBP::colorText("ANSI colors enabled", "#00FF00") << std::endl; } else { std::cout << "ANSI colors not supported" << std::endl; } try { // Use SNBP functions here } catch (const std::exception& e) { SNBP::generalLog("Error occurred: " + std::string(e.what())); return 1; } return 0; }
The SNBP library is primarily designed for Windows environments. Key compatibility points:
The SNBP library is an open-source project. Contributions and feedback are welcome.
For more information, visit the SNBP GitHub repository.