-
Notifications
You must be signed in to change notification settings - Fork 0
/
SNBP.cpp
1565 lines (1458 loc) · 55.5 KB
/
SNBP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file SNBP.cpp
* @brief Comprehensive implementation of utility functions for the Bassil language project.
*
* This file contains a collection of utility functions that provide various
* string manipulation, Windows API interaction, file handling, and console
* output formatting capabilities. These utilities are designed to support
* the core functionality of the Bassil language project.
*
* @note This file requires Windows-specific headers and may not be
* compatible with non-Windows environments.
*
* @author Nerd Bear
* @date 31 August 2024
* @version 0.7.4
*
* @copyright Copyright (c) 2024 Bassil
*
* @see SNBP.h
*/
#include "C:/coding-projects/CPP-Dev/bassil/src/headers/SNBP.h"
#include <algorithm>
#include <stdexcept>
#include <fstream>
#include <limits>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <sstream>
#include <iomanip>
#include <string_view>
#include <regex>
#include <strsafe.h>
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <wbemidl.h>
#include <shlobj.h>
#include <comdef.h>
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "shell32.lib")
/**
* @brief Fallback implementation of StringCchCopyW if not available in the system headers.
*
* This macro provides a simple implementation of StringCchCopyW using wcscpy_s
* if the function is not available in the included headers. It ensures
* compatibility across different Windows environments.
*
* @param dest Destination wide-character string buffer.
* @param destSize Size of the destination buffer in characters.
* @param src Source wide-character string to be copied.
*
* @note This macro should only be used if StringCchCopyW is not defined.
*/
#ifndef StringCchCopyW
#define StringCchCopyW(dest, destSize, src) wcscpy_s(dest, destSize, src)
#endif
namespace SNBP
{
// Version of SNBP
std::string version = "Currently not defined, please run SNBP::init() to initialize the package";
// The author of SNBP
std::string author = "Currently not defined, please run SNBP::init() to initialize the package";
// The date when this version of SNBP was created
std::string date = "Currently not defined, please run SNBP::init() to initialize the package";
// The license that SNBP is currently under
std::string license = "Currently not defined, please run SNBP::init() to initialize the package";
/**
* @brief Splits a string into a vector of substrings based on a specified delimiter.
*
* This function takes an input string and a delimiter, and splits the input
* string into multiple substrings wherever the delimiter is found. The
* resulting substrings are stored in a vector and returned.
*
* @param s The input string to be split.
* @param delimiter The string that serves as the delimiter for splitting.
*
* @return std::vector<std::string> A vector containing the substrings resulting
* from splitting the input string.
*
* @note The delimiter itself is not included in any of the resulting substrings.
* @note If the delimiter is not found in the input string, the entire input
* string will be returned as a single element in the vector.
* @note Empty substrings are included in the result if multiple delimiters
* are found consecutively.
*
* @see std::string::find
* @see std::string::substr
*
* @par Example:
* @code
* std::string input = "apple,banana,cherry";
* std::vector<std::string> result = SNBP::split_string(input, ",");
* // result now contains {"apple", "banana", "cherry"}
* @endcode
*/
std::vector<std::string> splitString(const std::string &s, const std::string &delimiter = " ")
{
std::vector<std::string> tokens;
size_t start = 0, end = 0;
while ((end = s.find(delimiter, start)) != std::string::npos)
{
tokens.push_back(s.substr(start, end - start));
start = end + delimiter.length();
}
tokens.push_back(s.substr(start));
return tokens;
}
/**
* @brief Converts a standard string to a wide string.
*
* This function takes a standard string (std::string) and converts it to
* a wide string (std::wstring). It supports both UTF-8 and ASCII encodings,
* determined by the _isUtf8Enabled flag.
*
* @param _string The input string to be converted.
* @param _isUtf8Enabled Flag to indicate if UTF-8 encoding should be used.
* If true, the function uses UTF-8 encoding; if false, it uses ASCII.
* Default value is true.
*
* @return std::wstring The resulting wide string after conversion.
*
* @note This function uses the Windows API function MultiByteToWideChar for
* the conversion process.
* @note The function first calculates the required buffer size, then
* allocates the buffer and performs the conversion.
* @note If the conversion fails, an empty wide string is returned.
*
* @see MultiByteToWideChar (Windows API)
*
* @par Example:
* @code
* std::string narrowStr = "Hello, 世界";
* std::wstring wideStr = SNBP::StringToWString(narrowStr, true);
* @endcode
*/
std::wstring StringToWString(const std::string &_string, bool _isUtf8Enabled)
{
int len = MultiByteToWideChar(_isUtf8Enabled ? CP_UTF8 : CP_ACP, 0, _string.c_str(), -1, NULL, 0);
std::wstring wstr(len - 1, 0);
MultiByteToWideChar(_isUtf8Enabled ? CP_UTF8 : CP_ACP, 0, _string.c_str(), -1, &wstr[0], len);
return wstr;
}
/**
* @brief Converts a standard string to a wide character string (LPCWSTR).
*
* This function takes a standard string (std::string) and converts it to
* a wide character string (LPCWSTR). It's particularly useful when working
* with Windows API functions that require LPCWSTR parameters.
*
* @param _string The input string to be converted.
*
* @return LPCWSTR The resulting wide character string after conversion.
*
* @note This function internally uses the StringToWString function for conversion.
* @note The returned LPCWSTR points to a static std::wstring member. This means
* the pointer is valid only until the next call to this function.
* @warning The returned pointer should not be stored or used after subsequent
* calls to this function, as it may be invalidated.
*
* @see StringToWString
*
* @par Example:
* @code
* std::string narrowStr = "Hello, World!";
* LPCWSTR wideStr = SNBP::StringToLPCWSTR(narrowStr);
* // Use wideStr with Windows API functions
* @endcode
*/
LPCWSTR StringToLPCWSTR(const std::string &_string)
{
static std::wstring wstr;
wstr = StringToWString(_string);
return wstr.c_str();
}
/**
* @brief Creates a Windows API 32-bit message box.
*
* This function displays a message box using the Windows API. It allows
* customization of the title, message content, and the type of buttons
* displayed in the message box.
*
* @param _title The title of the message box.
* @param _message The content of the message box.
* @param _type The type of the message box, determining which buttons are displayed.
* Valid values are:
* - 1: MB_ABORTRETRYIGNORE (Abort, Retry, Ignore buttons)
* - 2: MB_OKCANCEL (OK, Cancel buttons)
* - 3: MB_CANCELTRYCONTINUE (Cancel, Try Again, Continue buttons)
* - 4: MB_YESNOCANCEL (Yes, No, Cancel buttons)
* - 5: MB_YESNO (Yes, No buttons)
* - 6: MB_OK (OK button)
*
* @return int The ID of the button clicked by the user. The return value
* depends on the type of message box displayed.
*
* @throw std::runtime_error if an invalid message box type is provided.
*
* @note This function uses the Windows API function MessageBoxW for displaying
* the message box.
* @note The title and message are converted to wide strings using StringToLPCWSTR.
*
* @see MessageBoxW (Windows API)
* @see StringToLPCWSTR
*
* @par Example:
* @code
* int result = SNBP::CreateWinAPI32MessageBox("Warning", "Are you sure?", 5);
* if (result == IDYES) {
* // User clicked Yes
* } else {
* // User clicked No
* }
* @endcode
*/
int CreateWinAPI32MessageBox(const std::string &_title, const std::string &_message, int _type)
{
#ifdef _WINDOWS_
UINT _popupType;
switch (_type)
{
case 1:
_popupType = MB_ABORTRETRYIGNORE;
break;
case 2:
_popupType = MB_OKCANCEL;
break;
case 3:
_popupType = MB_CANCELTRYCONTINUE;
break;
case 4:
_popupType = MB_YESNOCANCEL;
break;
case 5:
_popupType = MB_YESNO;
break;
case 6:
_popupType = MB_OK;
break;
default:
throw std::runtime_error("Unknown Windows API 32-BIT-VERSION POPUP MESSAGE type");
}
return MessageBoxW(NULL, StringToLPCWSTR(_message), StringToLPCWSTR(_title), _popupType);
#else
std::cerr << "Windows API 32-BIT-VERSION not available\n";
exit(-1);
return -1;
#endif
}
/**
* @brief Creates a Windows API balloon notification.
*
* This function displays a balloon notification (also known as a toast notification)
* using the Windows API. It allows customization of the title, message content,
* and the type of notification icon displayed.
*
* @param _title The title of the notification.
* @param _message The content of the notification.
* @param _type The type of the notification, determining the icon displayed:
* - 0: NIIF_INFO (Information icon)
* - 1: NIIF_ERROR (Error icon)
* - 2: NIIF_WARNING (Warning icon)
* - 3: NIIF_NONE (No icon)
*
* @throw std::runtime_error if an invalid notification type is provided.
*
* @note This function uses the Windows API function Shell_NotifyIconW for
* displaying the notification.
* @note The notification is displayed using the system tray (notification area).
* @note The notification will automatically disappear after a system-defined timeout.
* @note This function does not create a persistent notification icon in the system tray.
*
* @see Shell_NotifyIconW (Windows API)
* @see NOTIFYICONDATAW (Windows API structure)
*
* @par Example:
* @code
* SNBP::CreateWinAPI32BallonNotification("Update Available", "A new version is ready to install.", 0);
* @endcode
*/
void CreateWinAPI32BallonNotification(const std::string &_title, const std::string &_message, int _type)
{
#ifdef _WINDOWS_
NOTIFYICONDATAW nid = {};
nid.cbSize = sizeof(NOTIFYICONDATAW);
nid.hWnd = NULL;
nid.uFlags = NIF_INFO | NIF_ICON | NIF_TIP;
switch (_type)
{
case 0:
nid.dwInfoFlags = NIIF_INFO;
nid.uID = 1001;
break;
case 1:
nid.dwInfoFlags = NIIF_ERROR;
nid.uID = 1002;
break;
case 2:
nid.dwInfoFlags = NIIF_WARNING;
nid.uID = 1003;
break;
case 3:
nid.dwInfoFlags = NIIF_NONE;
nid.uID = 1004;
break;
default:
throw std::runtime_error("Unknown Notification type");
}
nid.hIcon = LoadIcon(NULL, IDI_INFORMATION);
StringCchCopyW(nid.szTip, ARRAYSIZE(nid.szTip), L"My App Notification");
StringCchCopyW(nid.szInfo, ARRAYSIZE(nid.szInfo), StringToWString(_message).c_str());
StringCchCopyW(nid.szInfoTitle, ARRAYSIZE(nid.szInfoTitle), StringToWString(_title).c_str());
nid.uTimeout = 2000;
Shell_NotifyIconW(NIM_ADD, &nid);
Shell_NotifyIconW(NIM_MODIFY, &nid);
#else
std::cerr << "Windows API 32-BIT-VERSION not available\n";
exit(-1);
#endif
}
/**
* @brief Retrieves the maximum screen size for a given monitor.
*
* This function returns the dimensions of the specified monitor in a Windows
* multi-monitor setup. It's useful for determining the available screen space
* on a particular monitor.
*
* @param monitorIndex The index of the monitor (0-based).
*
* @return RECT A structure containing the screen dimensions:
* - left: The x-coordinate of the upper-left corner of the rectangle.
* - top: The y-coordinate of the upper-left corner of the rectangle.
* - right: The x-coordinate of the lower-right corner of the rectangle.
* - bottom: The y-coordinate of the lower-right corner of the rectangle.
*
* @throw std::runtime_error if the monitor index does not exist or if unable to get monitor info.
*
* @note This function uses Windows API functions EnumDisplayDevices and GetMonitorInfo.
* @note The function assumes that monitor indices are contiguous and start from 0.
* @note For single-monitor systems, use index 0 to get the primary monitor dimensions.
*
* @see EnumDisplayDevices (Windows API)
* @see GetMonitorInfo (Windows API)
* @see MONITORINFO (Windows API structure)
*
* @par Example:
* @code
* try {
* RECT screenSize = SNBP::GetMaximizedScreenSize(0); // Primary monitor
* std::cout << "Width: " << (screenSize.right - screenSize.left) << std::endl;
* std::cout << "Height: " << (screenSize.bottom - screenSize.top) << std::endl;
* } catch (const std::runtime_error& e) {
* std::cerr << "Error: " << e.what() << std::endl;
* }
* @endcode
*/
RECT GetMaximizedScreenSize(int monitorIndex)
{
#ifdef _WINDOWS_
DISPLAY_DEVICE dd = {sizeof(dd)};
int deviceIndex = 0;
while (EnumDisplayDevices(NULL, deviceIndex, &dd, 0))
{
if (deviceIndex == monitorIndex)
{
HMONITOR hMonitor = MonitorFromPoint({0, 0}, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO monitorInfo = {sizeof(MONITORINFO)};
if (GetMonitorInfo(hMonitor, &monitorInfo))
{
return monitorInfo.rcMonitor;
}
else
{
throw std::runtime_error("Failed to get monitor info");
}
}
deviceIndex++;
}
throw std::runtime_error("Monitor index does not exist");
#else
std::cerr << "Windows API 32-BIT-VERSION not available\n";
exit(-1)
#endif
}
/**
* @brief Trims leading whitespace from a string.
*
* This function removes all leading whitespace characters from the input string.
* Whitespace characters include spaces, tabs, line feeds, carriage returns, etc.
*
* @param s The string to be trimmed. This parameter is passed by reference and modified in-place.
*
* @return std::string& A reference to the modified string, allowing for method chaining.
*
* @note This function modifies the original string; it does not create a copy.
* @note The function uses the standard C++ isspace() function to determine what constitutes whitespace.
*
* @see std::isspace
* @see rtrim
* @see trim
*
* @par Example:
* @code
* std::string str = " Hello, World!";
* SNBP::ltrim(str);
* std::cout << str; // Outputs: "Hello, World!"
* @endcode
*/
std::string <rim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch)
{ return !std::isspace(ch); }));
return s;
}
/**
* @brief Trims trailing whitespace from a string.
*
* This function removes all trailing whitespace characters from the input string.
* Whitespace characters include spaces, tabs, line feeds, carriage returns, etc.
*
* @param s The string to be trimmed. This parameter is passed by reference and modified in-place.
*
* @return std::string& A reference to the modified string, allowing for method chaining.
*
* @note This function modifies the original string; it does not create a copy.
* @note The function uses the standard C++ isspace() function to determine what constitutes whitespace.
* @note The function uses reverse iterators for efficient trimming from the end of the string.
*
* @see std::isspace
* @see ltrim
* @see trim
*
* @par Example:
* @code
* std::string str = "Hello, World! ";
* SNBP::rtrim(str);
* std::cout << str; // Outputs: "Hello, World!"
* @endcode
*/
std::string &rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch)
{ return !std::isspace(ch); })
.base(),
s.end());
return s;
}
/**
* @brief Trims both leading and trailing whitespace from a string.
*
* This function removes all leading and trailing whitespace characters from the input string.
* It combines the functionality of ltrim() and rtrim() into a single function call.
*
* @param s The string to be trimmed. This parameter is passed by reference and modified in-place.
*
* @return std::string& A reference to the modified string, allowing for method chaining.
*
* @note This function modifies the original string; it does not create a copy.
* @note The function uses the standard C++ isspace() function to determine what constitutes whitespace.
* @note This function is equivalent to calling ltrim() followed by rtrim().
*
* @see std::isspace
* @see ltrim
* @see rtrim
*
* @par Example:
* @code
* std::string str = " Hello, World! ";
* SNBP::trim(str);
* std::cout << str; // Outputs: "Hello, World!"
* @endcode
*/
std::string &trim(std::string &s)
{
return ltrim(rtrim(s));
}
/**
* @brief Logs a message to a file and optionally prints it.
*
* This function writes a log message to a specified file and optionally prints it to the console.
* It's useful for maintaining a record of program execution and debugging.
*
* @param str The message to be logged.
* @param isPrintTrue Flag to indicate if the message should be printed to the console.
* If true, the message is both logged to the file and printed to the console.
* If false, the message is only logged to the file. Default is true.
*
* @return int 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/Run-0001/logs.log".
* @note The function opens the file in append mode, so new log entries are added to the end of the file.
* @note Each log entry is written on a new line.
*
* @see std::ofstream
*
* @par Example:
* @code
* int result = SNBP::generalLog("Application started", true);
* if (result != 0) {
* std::cerr << "Failed to write to log file" << std::endl;
* }
* @endcode
*/
int generalLog(const std::string &str, bool isPrintTrue)
{
if (!isPrintTrue)
{
return 0;
}
std::ofstream outputFile("C:/coding-projects/CPP-Dev/bassil/output/logs.log", std::ios::app);
if (!outputFile.is_open())
{
std::cerr << "[generalLog] Failed to open file." << std::endl;
return 1;
}
outputFile << str << "\n";
outputFile.close();
return 0;
}
int clearFile(const std::string &filename)
{
std::ofstream outputFile(filename, std::ios::trunc);
if (!outputFile.is_open())
{
std::cerr << "[clear_logs] Failed to open file." << std::endl;
return 1;
}
outputFile.close();
return 0;
}
/**
* @brief Reads the entire content of a file into a string.
*
* This function opens the specified file and reads its entire content into a string.
* It's useful for loading configuration files, templates, or any text-based file
* into memory for further processing.
*
* @param filename The path to the file to be read.
*
* @return std::string The content of the file as a string.
*
* @throw std::runtime_error if unable to open the file.
*
* @note The function uses std::istreambuf_iterator for efficient reading of the entire file.
* @note Large files may consume significant memory. Use with caution for very large files.
*
* @see std::ifstream
* @see std::istreambuf_iterator
*
* @par Example:
* @code
* try {
* std::string content = SNBP::readFileToString("config.txt");
* std::cout << "File content: " << content << std::endl;
* } catch (const std::runtime_error& e) {
* std::cerr << "Error: " << e.what() << std::endl;
* }
* @endcode
*/
std::string readFileToString(const std::string &filename)
{
std::ifstream file(filename);
if (!file.is_open())
{
throw std::runtime_error("[readFileToString] Unable to open file");
}
return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
}
/**
* @brief Enables ANSI escape sequences for console output.
*
* This function enables ANSI escape sequence processing for the console output,
* allowing the use of colored text and other formatting in the console.
* It's typically used in Windows environments where ANSI support is not enabled by default.
*
* @throw std::runtime_error if unable to get or set console mode.
*
* @note This function uses Windows API functions GetStdHandle, GetConsoleMode, and SetConsoleMode.
* @note ANSI escape sequences are used for text formatting, colors, cursor movement, etc.
* @note This function should be called once at the beginning of the program if ANSI support is needed.
*
* @see GetStdHandle (Windows API)
* @see GetConsoleMode (Windows API)
* @see SetConsoleMode (Windows API)
*
* @par Example:
* @code
* try {
* SNBP::enableAnsiInConsole();
* std::cout << "\033[31mThis text is red\033[0m" << std::endl;
* } catch (const std::runtime_error& e) {
* std::cerr << "Error: " << e.what() << std::endl;
* }
* @endcode
*/
int enableAnsiInConsole()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode = 0;
if (!GetConsoleMode(hConsole, &mode))
{
return 1;
}
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hConsole, mode))
{
return 1;
}
return 0;
}
/**
* @brief Checks if ANSI escape sequences are enabled in the console.
*
* This function determines whether ANSI escape sequence processing is currently
* enabled for the console output. It's useful for conditional formatting or
* for deciding whether to use ANSI escape sequences in console output.
*
* @return bool Returns true if ANSI escape sequences are enabled, false otherwise.
*
* @note This function uses Windows API functions GetStdHandle and GetConsoleMode.
* @note The function checks for the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag in the console mode.
*
* @see GetStdHandle (Windows API)
* @see GetConsoleMode (Windows API)
* @see ENABLE_VIRTUAL_TERMINAL_PROCESSING
*
* @par Example:
* @code
* if (SNBP::isAnsiEnabledInConsole()) {
* std::cout << "\033[32mANSI sequences are supported!\033[0m" << std::endl;
* } else {
* std::cout << "ANSI sequences are not supported." << std::endl;
* }
* @endcode
*/
bool isAnsiEnabledInConsole()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode = 0;
if (!GetConsoleMode(hConsole, &mode))
{
return false;
}
return (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0;
}
/**
* @brief Validates a hex color code.
*
* This function checks if a given string represents a valid hexadecimal color code.
* A valid hex color code starts with a '#' followed by exactly 6 hexadecimal digits.
*
* @param colorCode The string to be validated as a hex color code.
*
* @return bool Returns true if the color code is a valid hex color, false otherwise.
*
* @note The function is case-insensitive, so both uppercase and lowercase letters are accepted.
* @note The function uses a regular expression for validation.
*
* @see std::regex
* @see std::regex_match
*
* @par Example:
* @code
* std::cout << SNBP::isValidHexColor("#FF00FF") << std::endl; // Outputs: 1 (true)
* std::cout << SNBP::isValidHexColor("#G12345") << std::endl; // Outputs: 0 (false)
* std::cout << SNBP::isValidHexColor("FF00FF") << std::endl; // Outputs: 0 (false, missing #)
* @endcode
*/
bool isValidHexColor(const std::string &colorCode)
{
static const std::regex hexPattern("^#([A-Fa-f0-9]{6})$");
return std::regex_match(colorCode, hexPattern);
}
/**
* @brief Applies a hex color code to a given text string using ANSI escape sequences.
*
* This function takes a text string and a hex color code, and returns the text
* formatted with ANSI escape sequences to display the text in the specified color.
*
* @param text The text to be colored.
* @param colorCode The hex color code to apply (e.g., "#FF0000" for red).
*
* @return std::string The text formatted with ANSI escape sequences for the specified color.
*
* @note The function first validates the hex color code using isValidHexColor().
* @note If the color code is invalid, the function returns an error message instead of the formatted text.
* @note The function converts the hex color to RGB values for use in the ANSI escape sequence.
* @note The returned string includes a reset sequence (\033[0m) at the end to prevent color bleeding.
*
* @see isValidHexColor()
* @see std::stoi()
*
* @par Example:
* @code
* std::string coloredText = SNBP::colorText("Hello, World!", "#00FF00");
* std::cout << coloredText << std::endl; // Outputs "Hello, World!" in green
* @endcode
*/
std::string colorText(const std::string &text, const std::string &colorCode)
{
if (!isValidHexColor(colorCode))
{
return "Invalid color code!";
}
int r = std::stoi(colorCode.substr(1, 2), nullptr, 16);
int g = std::stoi(colorCode.substr(3, 2), nullptr, 16);
int b = std::stoi(colorCode.substr(5, 2), nullptr, 16);
return "\033[38;2;" + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b) + "m" + text + "\033[0m";
}
/**
* @brief Formats text as bold using ANSI escape sequences.
*
* This function takes a text string and returns it formatted as bold
* using ANSI escape sequences.
*
* @param text The text to be formatted as bold.
*
* @return std::string The bold-formatted text.
*
* @throw std::runtime_error if ANSI is not enabled in the console.
*
* @note The function checks if ANSI is enabled in the console before applying formatting.
* @note The returned string includes a reset sequence (\033[0m) at the end to prevent style bleeding.
*
* @see isAnsiEnabledInConsole()
*
* @par Example:
* @code
* try {
* std::string boldText = SNBP::boldText("Important Message");
* std::cout << boldText << std::endl; // Outputs "Important Message" in bold
* } catch (const std::runtime_error& e) {
* std::cerr << "Error: " << e.what() << std::endl;
* }
* @endcode
*/
std::string boldText(const std::string &text)
{
if (!isAnsiEnabledInConsole())
{
throw std::runtime_error("ANSI is not enabled in the console. Call enableAnsiInConsole() first.");
}
return "\033[1m" + text + "\033[0m";
}
/**
* @brief Formats text as italic using ANSI escape sequences.
*
* This function takes a text string and returns it formatted as italic
* using ANSI escape sequences.
*
* @param text The text to be formatted as italic.
*
* @return std::string The italic-formatted text.
*
* @throw std::runtime_error if ANSI is not enabled in the console.
*
* @note The function checks if ANSI is enabled in the console before applying formatting.
* @note The returned string includes a reset sequence (\033[0m) at the end to prevent style bleeding.
* @note Not all console fonts support italic text. The result may vary depending on the console used.
*
* @see isAnsiEnabledInConsole()
*
* @par Example:
* @code
* try {
* std::string italicText = SNBP::italicText("Emphasized text");
* std::cout << italicText << std::endl; // Outputs "Emphasized text" in italic
* } catch (const std::runtime_error& e) {
* std::cerr << "Error: " << e.what() << std::endl;
* }
* @endcode
*/
std::string italicText(const std::string &text)
{
if (!isAnsiEnabledInConsole())
{
throw std::runtime_error("ANSI is not enabled in the console. Call enableAnsiInConsole() first.");
}
return "\033[3m" + text + "\033[0m";
}
/**
* @brief Formats text with an underline using ANSI escape sequences.
*
* This function takes a text string and returns it formatted with an underline
* using ANSI escape sequences.
*
* @param text The text to be underlined.
*
* @return std::string The underlined text.
*
* @throw std::runtime_error if ANSI is not enabled in the console.
*
* @note The function checks if ANSI is enabled in the console before applying formatting.
* @note The returned string includes a reset sequence (\033[0m) at the end to prevent style bleeding.
*
* @see isAnsiEnabledInConsole()
*
* @par Example:
* @code
* try {
* std::string underlinedText = SNBP::underlineText("Important link");
* std::cout << underlinedText << std::endl; // Outputs "Important link" with an underline
* } catch (const std::runtime_error& e) {
* std::cerr << "Error: " << e.what() << std::endl;
* }
* @endcode
*/
std::string underlineText(const std::string &text)
{
if (!isAnsiEnabledInConsole())
{
throw std::runtime_error("ANSI is not enabled in the console. Call enableAnsiInConsole() first.");
}
return "\033[4m" + text + "\033[0m";
}
/**
* @brief Applies multiple text formatting options using ANSI escape sequences.
*
* This function takes a text string and applies multiple formatting options
* (bold, italic, underline, and color) using ANSI escape sequences.
*
* @param text The text to be formatted.
* @param bold Apply bold formatting if true.
* @param italic Apply italic formatting if true.
* @param underline Apply underline formatting if true.
* @param colorCode Hex color code to apply (e.g., "#FF0000" for red).
*
* @return std::string The formatted text.
*
* @throw std::runtime_error if ANSI is not enabled in the console.
*
* @note The function checks if ANSI is enabled in the console before applying formatting.
* @note The color is applied first, followed by bold, italic, and underline formatting.
* @note The returned string includes a reset sequence (\033[0m) at the end to prevent style bleeding.
* @note If the color code is invalid or empty, no color will be applied.
*
* @see isAnsiEnabledInConsole()
* @see colorText()
*
* @par Example:
* @code
* try {
* std::string formattedText = SNBP::formatText("Formatted Text", true, false, true, "#00FF00");
* std::cout << formattedText << std::endl; // Outputs "Formatted Text" in green, bold, and underlined
* } catch (const std::runtime_error& e) {
* std::cerr << "Error: " << e.what() << std::endl;
* }
* @endcode
*/
std::string formatText(const std::string &text, bool bold, bool italic, bool underline, const std::string &colorCode)
{
if (!isAnsiEnabledInConsole())
{
throw std::runtime_error("ANSI is not enabled in the console. Call enableAnsiInConsole() first.");
}
std::string result;
if (!colorCode.empty())
{
result += colorText("", colorCode); // Apply color
}
if (bold)
result += "\033[1m";
if (italic)
result += "\033[3m";
if (underline)
result += "\033[4m";
result += text + "\033[0m"; // Add text and reset formatting
return result;
}
/**
* @brief Removes all ANSI escape sequences from a string.
*
* This function takes a string that may contain ANSI escape sequences and
* removes all such sequences, returning the plain text content.
*
* @param text The text containing ANSI escape sequences.
*
* @return std::string The text with all ANSI escape sequences removed.
*
* @note This function uses a regular expression to identify and remove ANSI escape sequences.
* @note The function will remove all sequences starting with '\033[' and ending with a letter.
*
* @see std::regex
* @see std::regex_replace
*
* @par Example:
* @code
* std::string coloredText = "\033[31mRed Text\033[0m \033[1mBold Text\033[0m";
* std::string plainText = SNBP::stripAnsiEscapeCodes(coloredText);
* std::cout << plainText << std::endl; // Outputs: "Red Text Bold Text"
* @endcode
*/
std::string stripAnsiEscapeCodes(const std::string &text)
{
static const std::regex ansiEscapePattern("\033\\[[0-9;]*[A-Za-z]");
return std::regex_replace(text, ansiEscapePattern, "");
}
/**
* @brief Truncates a string to a specified length, adding an ellipsis if truncated.
*
* This function takes an input string and a maximum length, and returns a string
* that is no longer than the specified length. If the input string is longer than
* the maximum length, it is truncated and an ellipsis (...) is appended.
*
* @param text The input text to truncate.
* @param maxLength The maximum length of the truncated string, including the ellipsis.
*
* @return std::string The truncated string.
*
* @note If the input string is shorter than or equal to maxLength, it is returned unchanged.
* @note The ellipsis (...) counts towards the maxLength.
* @note If maxLength is less than 4, the function will still attempt to add the ellipsis,
* which may result in a string longer than maxLength.
*
* @par Example:
* @code
* std::string longText = "This is a very long string that needs truncating";
* std::string truncated = SNBP::truncateString(longText, 20);
* std::cout << truncated << std::endl; // Outputs: "This is a very lon..."
* @endcode
*/
std::string truncateString(const std::string &text, size_t maxLength)
{
if (text.length() <= maxLength)
{
return text;
}
return text.substr(0, maxLength - 3) + "...";
}
/**
* @brief Centers a string within a field of a given width.
*
* This function takes a string and centers it within a field of a specified width.
* If the string is shorter than the specified width, padding characters are added
* on both sides to center the string. If the string is longer than or equal to the
* specified width, it is returned unchanged.
*
* @param text The text to be centered.
* @param width The width of the field in which to center the text.
* @param fillChar The character to use for padding (default is space).
*
* @return std::string The centered string.
*
* @note If the difference between the field width and the string length is odd,
* the extra padding character is added to the right side.
* @note If the string is longer than the specified width, no truncation occurs.
*
* @par Example:
* @code
* std::string centered = SNBP::centerString("Hello", 11, '-');
* std::cout << centered << std::endl; // Outputs: "---Hello---"
* @endcode
*/
std::string centerString(const std::string &text, size_t width, char fillChar)
{