Browse Source

fix: 修复tool_logger的错误

SongZihuan 3 năm trước cách đây
mục cha
commit
41be176dc4

+ 0 - 1
include/tool/log.hpp

@@ -100,7 +100,6 @@ namespace aFuntool {
         struct LogNode *pop();
     };
 
-
     extern LogFactory log_factory;
 }
 

+ 2 - 2
include/tool/path.hpp

@@ -13,8 +13,8 @@ namespace aFuntool {
 #else
 
 namespace aFuntool {
-    const char *SEP = "/";
-    const char SEP_CH = '/';
+    static const char *SEP = "/";
+    static const char SEP_CH = '/';
 }
 
 #endif

+ 1 - 1
include/tool/stdio_.hpp

@@ -69,7 +69,7 @@ namespace aFuntool {
 
 namespace aFuntool {
     static int fgetc_stdin(){ return fgetc(stdout); }
-    static int fgets_stdin_(char *buf, int len, FILE *file){ fgets(buf, len, file); }
+    static int fgets_stdin_(char *buf, int len, FILE *file){ return fgets(buf, len, file) != nullptr; }
     static int fungetc_stdin(char ch){ return ungetc(ch, stdin); }
 
     static int fputs_stdout(const char *str){ return fputs(str, stdout); }

+ 5 - 5
src/tool/file.cpp

@@ -51,7 +51,7 @@ static int get_stat(aFun_stat &stat_, const std::string &path_){
     re = _wstat64(tmp, &stat_);
     free(tmp);  // 如果 path 为nullptr, 则释放最新生成的 wchat_t
 #else
-    re = stat(path_, stat_);
+    re = stat(path_.c_str(), &stat_);
 #endif
     return re;
 }
@@ -133,12 +133,12 @@ std::string aFuntool::getFilePathName(const std::string &path){
 std::string aFuntool::getFilePath(const std::string &path, int dep){
     std::string::size_type point = path.size();
     for (int i = 0; i < dep; i++) {
-        auto tmp = path.rfind('.', point);
+        auto tmp = path.rfind(SEP_CH, point);
         if (tmp == std::string::npos)
             break;
         point = tmp;
     }
-    return path.substr(point);
+    return path.substr(0, point);
 }
 
 /**
@@ -207,8 +207,8 @@ std::string aFuntool::getExedir(int dep) {
     return re;
 #else
     ssize_t ret =  readlink("/proc/self/exe", exepath, 217);  // 预留一位给NUL
-    if (ret == -1 || STR_LEN(exepath) == 0)
-        return nullptr;
+    if (ret == -1 || strlen(exepath) == 0)
+        return "";
     return getFilePath(exepath, dep + 1);
 #endif
 }

+ 4 - 4
src/tool/stdio_.cpp

@@ -490,7 +490,7 @@ int aFuntool::fgets_stdin(char **dest, int len) {
  *
  * 参考自: https://gist.github.com/SuperH-0630/a4190b89d21c349a8d6882ca71453ae6
  */
-bool aFuntool::checkStdin(void) {
+bool aFuntool::checkStdin() {
     if (!isatty(fileno(stdin)))
         return true;
     bool re = false;
@@ -500,7 +500,7 @@ bool aFuntool::checkStdin(void) {
     fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
 
     int ch = fgetc(stdin);
-    CLEAR_FERROR(stdin);
+    clear_ferror(stdin);
 
     if (ch != EOF) {
         ungetc(ch, stdin);
@@ -512,7 +512,7 @@ bool aFuntool::checkStdin(void) {
     return re;
 }
 
-bool aFuntool::fclear_stdin(void) {
+bool aFuntool::fclear_stdin() {
     if (!isatty(fileno(stdin)))
         return true;
 
@@ -523,7 +523,7 @@ bool aFuntool::fclear_stdin(void) {
     int ch;
     do {
         ch = fgetc(stdin);
-        CLEAR_FERROR(stdin);
+        clear_ferror(stdin);
     } while (ch != EOF);
 
     fcntl(STDIN_FILENO, F_SETFL, oldf);

+ 2 - 1
test/src/CMakeLists.txt

@@ -23,4 +23,5 @@ add_new_test(tool_md5 COMMAND "$<TARGET_FILE:tool_md5>" "${TOOL_MD5_ANS}" "${CMA
 add_new_test(tool_logger COMMAND "$<TARGET_FILE:tool_logger>")
 add_new_test(tool_exit COMMAND "$<TARGET_FILE:tool_exit>")
 add_new_test(tool_hash COMMAND "$<TARGET_FILE:tool_hash>")
-set_test_label(tool tool_mem tool_byte tool_dlc tool_regex tool_md5)
+add_new_test(tool_utf COMMAND "$<TARGET_FILE:tool_utf>")
+set_test_label(tool tool_mem tool_byte tool_dlc tool_regex tool_md5 tool_utf)

+ 5 - 2
test/src/tool_logger.cpp

@@ -3,9 +3,12 @@ using namespace aFuntool;
 
 int main(int argc, char **argv){
     std::string base_path = getExedir(1);
-    if (base_path.empty())
+    if (base_path.empty()) {
+        printf("Not Exe Dir\n");
         aFunExit(0);
+    }
 
-    log_factory.initLogSystem(base_path);
+    setlocale(LC_ALL, "");
+    log_factory.initLogSystem(base_path + SEP + "aFunlog");
     aFunExit(0);
 }

+ 30 - 0
test/src/tool_utf.cpp

@@ -0,0 +1,30 @@
+#include "tool.hpp"
+#include <clocale>
+using namespace aFuntool;
+
+int main(int argc, char **argv) {
+    wchar_t *tmp;
+    const wchar_t *tmp2 = L"你好";
+    const char *tmp3 = "你好";
+
+    setlocale(LC_ALL, "");
+
+    convertWideByte(&tmp, tmp3, CP_UTF8);
+
+    std::wcout << tmp << std::endl;
+    std::wcout << tmp2 << std::endl;
+
+    for (int i = 0; i < wcslen(tmp); i++)
+        printf("%x ", tmp[i]);
+    printf("\n");
+
+    for (int i = 0; i < wcslen(tmp2); i++)
+        printf("%x ", tmp2[i]);
+    printf("\n");
+
+    for (int i = 0; i < strlen(tmp3); i++)
+        printf("%x ", (unsigned int)tmp3[i]);
+    printf("\n");
+
+    return 0;
+}