Przeglądaj źródła

refactor & feat: 新增环境变量

SongZihuan 3 lat temu
rodzic
commit
d35f278238

+ 0 - 2
include/core/code.hpp

@@ -1,8 +1,6 @@
 #ifndef AFUN_CODE_H
 #ifndef AFUN_CODE_H
 #define AFUN_CODE_H
 #define AFUN_CODE_H
-#include "iostream"
 #include "tool.hpp"
 #include "tool.hpp"
-#include "exception.hpp"
 
 
 namespace aFuncore {
 namespace aFuncore {
     typedef enum CodeType {
     typedef enum CodeType {

+ 36 - 0
include/core/env-var.hpp

@@ -0,0 +1,36 @@
+#ifndef AFUN_ENV_VAR_HPP
+#define AFUN_ENV_VAR_HPP
+#include "tool.hpp"
+
+namespace aFuncore {
+    static const size_t ENV_VAR_HASH_SIZE = 100;  // 环境变量哈希表大小
+
+    class EnvVarSpace {  // 环境变量
+
+        struct EnvVar {  // 环境变量
+            std::string name;
+            std::string str;
+            int32_t num = 0;  // 可以同时记录字符串和数字
+            struct EnvVar *next = nullptr;
+        };
+
+        size_t count;
+        EnvVar *var[ENV_VAR_HASH_SIZE] {};
+        pthread_rwlock_t lock;
+
+    public:
+        EnvVarSpace();
+        ~EnvVarSpace();
+
+        [[nodiscard]]  size_t getCount() const {return count;}
+
+        bool findString(const std::string &name, std::string &str) const;
+        bool findNumber(const std::string &name, int32_t &num) const;
+
+        void setString(const std::string &name, const std::string &str);
+        void setNumber(const std::string &name, int32_t num);
+    };
+}
+
+
+#endif //AFUN_ENV_VAR_HPP

+ 0 - 14
include/core/exception.hpp

@@ -1,14 +0,0 @@
-#ifndef AFUN_EXCEPTION_HPP
-#define AFUN_EXCEPTION_HPP
-#include "iostream"
-
-namespace aFuncore {
-    class AttributesError : public std::exception {
-        std::string msg;
-    public:
-        explicit AttributesError(const std::string &attributes) {msg = (std::string("Get attributes error: ") + attributes);}
-        virtual const char *what() {return msg.c_str();}
-    };
-}
-
-#endif //AFUN_EXCEPTION_HPP

+ 0 - 0
include/tool/log_m.hpp → include/tool/log-m.hpp


+ 1 - 1
include/tool/log.hpp

@@ -3,7 +3,7 @@
 #include <iostream>
 #include <iostream>
 #include "aFunToolExport.h"
 #include "aFunToolExport.h"
 #include "macro.hpp"
 #include "macro.hpp"
-#include "log_m.hpp"
+#include "log-m.hpp"
 #include "iostream"
 #include "iostream"
 
 
 using namespace aFuntool;
 using namespace aFuntool;

+ 0 - 0
include/tool/time_s.hpp → include/tool/time_.hpp


+ 1 - 1
include/tool/tool.hpp

@@ -46,7 +46,7 @@ namespace aFuntool {
 #include "path.hpp"
 #include "path.hpp"
 #include "regex.hpp"
 #include "regex.hpp"
 #include "str.hpp"
 #include "str.hpp"
-#include "time_s.hpp"
+#include "time_.hpp"
 #include "log.hpp"
 #include "log.hpp"
 
 
 #include "pthread.h"
 #include "pthread.h"

+ 69 - 0
src/core/env-var.cpp

@@ -0,0 +1,69 @@
+#include "env-var.hpp"
+using namespace aFuncore;
+using namespace aFuntool;
+
+aFuncore::EnvVarSpace::EnvVarSpace() {  // NOLINT lock 通过 pthread_rwlock_init 初始化
+    count = 0;
+    pthread_rwlock_init(&lock, nullptr);
+}
+
+aFuncore::EnvVarSpace::~EnvVarSpace() {
+    for (auto &i : var) {
+        for (auto tmp = i; tmp != nullptr; tmp = tmp->next)
+            delete tmp;
+    }
+    pthread_rwlock_destroy(&lock);
+}
+
+bool EnvVarSpace::findString(const std::string &name, std::string &str) const{
+    size_t index = time33(name) % ENV_VAR_HASH_SIZE;
+    for (auto tmp = var[index]; tmp != nullptr; tmp = tmp->next) {
+        if (name == tmp->name) {
+            str = tmp->str;
+            return true;
+        }
+    }
+    return false;
+}
+
+bool EnvVarSpace::findNumber(const std::string &name, int32_t &num) const{
+    size_t index = time33(name) % ENV_VAR_HASH_SIZE;
+    for (auto tmp = var[index]; tmp != nullptr; tmp = tmp->next) {
+        if (name == tmp->name) {
+            num = tmp->num;
+            return true;
+        }
+    }
+    return false;
+}
+
+void EnvVarSpace::setString(const std::string &name, const std::string &str){
+    size_t index = time33(name) % ENV_VAR_HASH_SIZE;
+    auto tmp = &var[index];
+    for (NULL; *tmp != nullptr; tmp = &((*tmp)->next)) {
+        if (name == (*tmp)->name) {
+            (*tmp)->str = str;
+            return;
+        }
+    }
+
+    (*tmp) = new EnvVar;
+    (*tmp)->name = name;
+    (*tmp)->str = str;
+}
+
+void EnvVarSpace::setNumber(const std::string &name, int32_t num){
+    size_t index = time33(name) % ENV_VAR_HASH_SIZE;
+    auto tmp = &var[index];
+    for (NULL; *tmp != nullptr; tmp = &((*tmp)->next)) {
+        if (name == (*tmp)->name) {
+            (*tmp)->num = num;
+            return;
+        }
+    }
+
+    (*tmp) = new EnvVar;
+    (*tmp)->name = name;
+    (*tmp)->num = num;
+}
+

+ 2 - 1
test/src/CMakeLists.txt

@@ -27,4 +27,5 @@ 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)
 set_test_label(tool tool_mem tool_byte tool_dlc tool_regex tool_md5 tool_utf)
 
 
 add_new_test(core_init COMMAND "$<TARGET_FILE:core_init>")
 add_new_test(core_init COMMAND "$<TARGET_FILE:core_init>")
-add_new_test(core_code COMMAND "$<TARGET_FILE:core_code>")
+add_new_test(core_code COMMAND "$<TARGET_FILE:core_code>")
+add_new_test(core-env-var COMMAND "$<TARGET_FILE:core-env-var>")

+ 21 - 0
test/src/core-env-var.cpp

@@ -0,0 +1,21 @@
+#include "env-var.hpp"
+using namespace aFuncore;
+using namespace aFuntool;
+
+int main() {
+    auto *evs = new EnvVarSpace();
+
+    evs->setString("a", "string");
+    evs->setNumber("a", 20);
+
+    std::string s;
+    int32_t n;
+
+    evs->findString("a", s);
+    evs->findNumber("a", n);
+
+    std::cout << "a = " << s << ", " << n << std::endl;
+
+    delete evs;
+    return 0;
+}