|
8 | 8 | * | Mikachu2333 <[email protected]> |
9 | 9 | * | |
10 | 10 | * Created On : <2023-08-28> |
11 | | - * Last Modified : <2025-08-20> |
| 11 | + * Last Modified : <2025-08-21> |
12 | 12 | * |
13 | 13 | * |
14 | 14 | * xy: 襄阳、咸阳 |
|
22 | 22 | #ifndef XY_H |
23 | 23 | #define XY_H |
24 | 24 |
|
25 | | -#define _XY_Version "v0.1.7.0-2025/08/20" |
| 25 | +#define _XY_Version "v0.1.7.0-2025/08/21" |
26 | 26 | #define _XY_Maintain_URL "https://github.com/RubyMetric/chsrc/blob/dev/lib/xy.h" |
27 | 27 | #define _XY_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc/blob/dev/lib/xy.h" |
28 | 28 |
|
|
44 | 44 | #include <stdlib.h> |
45 | 45 | #include <string.h> |
46 | 46 | #include <unistd.h> |
| 47 | +#include <dirent.h> // opendir() closedir() |
47 | 48 |
|
48 | 49 | #if defined(__STDC__) && __STDC_VERSION__ >= 202311 |
49 | 50 | #define XY_Deprecate_This(msg) [[deprecated(msg)]] |
|
56 | 57 | #endif |
57 | 58 |
|
58 | 59 |
|
59 | | -/* Global */ |
| 60 | +/* 全局变量 */ |
60 | 61 | bool xy_enable_color = true; |
61 | 62 |
|
| 63 | +/* 由 xy_init() 赋值 */ |
| 64 | +bool xy_on_windows = false; |
| 65 | +bool xy_on_linux = false; |
| 66 | +bool xy_on_macos = false; |
| 67 | +bool xy_on_bsd = false; |
| 68 | +bool xy_on_android = false; |
| 69 | + |
| 70 | +char *xy_os_devnull = NULL; |
| 71 | + |
62 | 72 | // #define NDEBUG |
63 | 73 |
|
64 | 74 | #ifdef _WIN32 |
65 | 75 | #define XY_Build_On_Windows 1 |
66 | 76 |
|
67 | | - #define xy_on_windows true |
68 | | - #define xy_on_linux false |
69 | | - #define xy_on_macos false |
70 | | - #define xy_on_bsd false |
71 | | - #define xy_os_devnull "nul" |
72 | 77 | #include <windows.h> |
73 | 78 | #include <shlobj.h> |
74 | | - #define xy_use_utf8() SetConsoleOutputCP (65001) |
75 | 79 |
|
76 | 80 | #elif defined(__linux__) || defined(__linux) |
77 | 81 | #define XY_Build_On_Linux 1 |
78 | 82 | #define XY_Build_On_Unix 1 |
79 | 83 |
|
80 | | - #define xy_on_windows false |
81 | | - #define xy_on_linux true |
82 | | - #define xy_on_macos false |
83 | | - #define xy_on_bsd false |
84 | | - #define xy_os_devnull "/dev/null" |
85 | | - #define xy_use_utf8() |
86 | | - |
87 | 84 | #elif defined(__APPLE__) |
88 | 85 | #define XY_Build_On_macOS 1 |
89 | 86 | #define XY_Build_On_Unix 1 |
90 | 87 |
|
91 | | - #define xy_on_windows false |
92 | | - #define xy_on_linux false |
93 | | - #define xy_on_macos true |
94 | | - #define xy_on_bsd false |
95 | | - #define xy_os_devnull "/dev/null" |
96 | | - #define xy_use_utf8() |
97 | | - |
98 | 88 | #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) |
99 | 89 | #define XY_Build_On_BSD 1 |
100 | 90 | #define XY_Build_On_Unix 1 |
101 | | - |
102 | | - #define xy_on_windows false |
103 | | - #define xy_on_linux false |
104 | | - #define xy_on_macos false |
105 | | - #define xy_on_bsd true |
106 | | - #define xy_os_devnull "/dev/null" |
107 | | - #define xy_use_utf8() |
108 | 91 | #endif |
109 | 92 |
|
110 | 93 | #define assert_str(a, b) assert (xy_streql ((a), (b))) |
@@ -661,7 +644,7 @@ _xy_log (int level, const char *prompt, const char *content) |
661 | 644 | } |
662 | 645 | else |
663 | 646 | { |
664 | | - // xy_assert ("CAN'T REACH!"); |
| 647 | + xy_unreached(); |
665 | 648 | } |
666 | 649 |
|
667 | 650 | if (to_stderr) |
@@ -747,7 +730,7 @@ _xy_log_brkt (int level, const char *prompt1, const char *prompt2, const char *c |
747 | 730 | } |
748 | 731 | else |
749 | 732 | { |
750 | | - // xy_assert ("CAN'T REACH!"); |
| 733 | + xy_unreached(); |
751 | 734 | } |
752 | 735 |
|
753 | 736 | if (to_stderr) |
@@ -1110,6 +1093,105 @@ xy_parent_dir (const char *path) |
1110 | 1093 |
|
1111 | 1094 |
|
1112 | 1095 |
|
| 1096 | +void |
| 1097 | +xy_detect_os () |
| 1098 | +{ |
| 1099 | + // C: |
| 1100 | + char *drive = getenv ("SystemDrive"); |
| 1101 | + if (drive) |
| 1102 | + { |
| 1103 | + char path[256]; |
| 1104 | + snprintf (path, sizeof (path), "%s\\Users", drive); |
| 1105 | + DIR *d = opendir (path); |
| 1106 | + if (d) |
| 1107 | + { |
| 1108 | + xy_on_windows = true; |
| 1109 | + closedir (d); |
| 1110 | + return; |
| 1111 | + } |
| 1112 | + } |
| 1113 | + |
| 1114 | + FILE *fp = fopen ("/proc/version", "r"); |
| 1115 | + if (fp) |
| 1116 | + { |
| 1117 | + char buf[256] = {0}; |
| 1118 | + fread (buf, 1, sizeof(buf) - 1, fp); |
| 1119 | + fclose (fp); |
| 1120 | + if (strstr (buf, "Android")) |
| 1121 | + { |
| 1122 | + xy_on_android = true; |
| 1123 | + return; |
| 1124 | + } |
| 1125 | + else if (strstr (buf, "Linux")) |
| 1126 | + { |
| 1127 | + xy_on_linux = true; |
| 1128 | + return; |
| 1129 | + } |
| 1130 | + } |
| 1131 | + |
| 1132 | + /* 判断 macOS */ |
| 1133 | + DIR *d = opendir ("/System/Applications"); |
| 1134 | + if (d) |
| 1135 | + { |
| 1136 | + closedir (d); |
| 1137 | + d = opendir ("/Library/Apple"); |
| 1138 | + if (d) |
| 1139 | + { |
| 1140 | + xy_on_macos = true; |
| 1141 | + closedir (d); |
| 1142 | + } |
| 1143 | + } |
| 1144 | + |
| 1145 | + /* 最后判断 BSD */ |
| 1146 | + fp = popen ("uname -s", "r"); |
| 1147 | + if (!fp) |
| 1148 | + { |
| 1149 | + if (opendir ("/etc/rc.d")) |
| 1150 | + { |
| 1151 | + closedir (d); |
| 1152 | + xy_on_bsd = true; |
| 1153 | + return; |
| 1154 | + } |
| 1155 | + } |
| 1156 | + else |
| 1157 | + { |
| 1158 | + char buf[256]; |
| 1159 | + fgets (buf, sizeof (buf), fp); |
| 1160 | + pclose (fp); |
| 1161 | + if (strstr (buf, "BSD") != NULL) |
| 1162 | + xy_on_bsd = true; |
| 1163 | + } |
| 1164 | + |
| 1165 | + assert (xy_on_windows || xy_on_linux || xy_on_android || xy_on_macos || xy_on_bsd); |
| 1166 | +} |
| 1167 | + |
| 1168 | + |
| 1169 | +void |
| 1170 | +xy_use_utf8 () |
| 1171 | +{ |
| 1172 | +#ifdef XY_Build_On_Windows |
| 1173 | + SetConsoleOutputCP (65001); |
| 1174 | +#endif |
| 1175 | +} |
| 1176 | + |
| 1177 | + |
| 1178 | +/** |
| 1179 | + * @note 该函数必须被首先调用,方能使用各个跨操作系统的函数 |
| 1180 | + */ |
| 1181 | +void |
| 1182 | +xy_init () |
| 1183 | +{ |
| 1184 | + xy_detect_os (); |
| 1185 | + |
| 1186 | + if (xy_on_windows) |
| 1187 | + xy_os_devnull = "nul"; |
| 1188 | + else |
| 1189 | + xy_os_devnull = "/dev/null"; |
| 1190 | + |
| 1191 | + xy_use_utf8 (); |
| 1192 | +} |
| 1193 | + |
| 1194 | + |
1113 | 1195 | /****************************************************** |
1114 | 1196 | * Container |
1115 | 1197 | ******************************************************/ |
|
0 commit comments