소스 검색

refactor & feat: 添加ReaderFile模块

SongZihuan 3 년 전
부모
커밋
a551458ef3

+ 20 - 0
include/runtime/rt-exception.h

@@ -0,0 +1,20 @@
+#ifndef AFUN_RT_EXCEPTION_H
+#define AFUN_RT_EXCEPTION_H
+#include "aFuntool.h"
+
+namespace aFunrt {
+    class aFunrtException : public aFuntool::aFunException {
+    public:
+        inline explicit aFunrtException(const std::string &msg);
+    };
+
+    class readerFileOpenError : public aFunrtException {
+    public:
+        inline explicit readerFileOpenError(const aFuntool::FilePath &file);
+    };
+
+}
+
+#include "rt-exception.inline.h"
+
+#endif //AFUN_RT_EXCEPTION_H

+ 17 - 0
include/runtime/rt-exception.inline.h

@@ -0,0 +1,17 @@
+#ifndef AFUN_RT_EXCEPTION_INLINE_H
+#define AFUN_RT_EXCEPTION_INLINE_H
+
+#include "rt-exception.h"
+
+namespace aFunrt {
+    inline aFunrtException::aFunrtException(const std::string &msg) : aFunException{msg} {
+
+    }
+
+    inline readerFileOpenError::readerFileOpenError(const aFuntool::FilePath &file)
+        : aFunrtException("Reader cannot open file: " + file) {
+
+    }
+}
+
+#endif //AFUN_RT_EXCEPTION_INLINE_H

+ 9 - 1
include/runtime/rt-reader.h

@@ -6,13 +6,21 @@ namespace aFunrt {
     class ReaderString : public aFuncore::Reader {
     public:
         inline ReaderString(std::string str_, const aFuntool::FilePath &path_);
-
         size_t readText(char *dest, size_t read_len, ReadMode &mode) override;
     private:
         std::string str;
         size_t index;
         size_t len;
     };
+
+    class ReaderFile : public aFuncore::Reader {
+    public:
+        inline explicit ReaderFile(const aFuntool::FilePath &path_) noexcept(false);
+        size_t readText(char *dest, size_t read_len, ReadMode &mode) override;
+    private:
+        FILE *file;
+        bool no_first;
+    };
 }
 
 #include "rt-reader.inline.h"

+ 9 - 0
include/runtime/rt-reader.inline.h

@@ -1,6 +1,7 @@
 #ifndef AFUN_RT_READER_INLINE_H
 #define AFUN_RT_READER_INLINE_H
 #include "rt-reader.h"
+#include "rt-exception.h"
 
 namespace aFunrt {
     inline ReaderString::ReaderString(std::string str_, const aFuntool::FilePath &path_)
@@ -8,6 +9,14 @@ namespace aFunrt {
         index = 0;
         len = str.size();
     }
+
+    ReaderFile::ReaderFile(const aFuntool::FilePath &path_)
+        : Reader{path_, 0} {
+        file = aFuntool::fileOpen(path_, "rb");
+        if (file == nullptr)
+            throw readerFileOpenError(path_);
+        no_first = false;
+    }
 }
 
 #endif //AFUN_RT_READER_INLINE_H

+ 10 - 8
include/tool/tool-stdio.h

@@ -72,14 +72,16 @@ namespace aFuntool {
         PrintFunction *func;
     public:
         inline explicit OutStream(PrintFunction *func_);
-        inline OutStream &operator<<(int8_t a);
-        inline OutStream &operator<<(int16_t a);
-        inline OutStream &operator<<(int32_t a);
-        inline OutStream &operator<<(int64_t a);
-        inline OutStream &operator<<(uint8_t a);
-        inline OutStream &operator<<(uint16_t a);
-        inline OutStream &operator<<(uint32_t a);
-        inline OutStream &operator<<(uint64_t a);
+        inline OutStream &operator<<(signed char a);
+        inline OutStream &operator<<(short a);
+        inline OutStream &operator<<(int a);
+        inline OutStream &operator<<(long a);
+        inline OutStream &operator<<(long long a);
+        inline OutStream &operator<<(unsigned char a);
+        inline OutStream &operator<<(unsigned short a);
+        inline OutStream &operator<<(unsigned int a);
+        inline OutStream &operator<<(unsigned long a);
+        inline OutStream &operator<<(unsigned long long a);
         inline OutStream &operator<<(float a);
         inline OutStream &operator<<(double a);
         inline OutStream &operator<<(long double a);

+ 18 - 8
include/tool/tool-stdio.inline.h

@@ -101,46 +101,56 @@ namespace aFuntool {
 
     }
 
-    inline OutStream &OutStream::operator<<(int8_t a) {
+    inline OutStream &OutStream::operator<<(signed char a) {
         func(0, "%c", a);
         return *this;
     }
 
-    inline OutStream &OutStream::operator<<(int16_t a) {
+    inline OutStream &OutStream::operator<<(short a) {
         func(0, "%d", a);
         return *this;
     }
 
-    inline OutStream &OutStream::operator<<(int32_t a) {
+    inline OutStream &OutStream::operator<<(int a) {
         func(0, "%d", a);
         return *this;
     }
 
-    inline OutStream &OutStream::operator<<(int64_t a) {
+    inline OutStream &OutStream::operator<<(long a) {
         func(0, "%ld", a);
         return *this;
     }
 
-    inline OutStream &OutStream::operator<<(uint8_t a) {
+    inline OutStream &OutStream::operator<<(long long a) {
+        func(0, "%lld", a);
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(unsigned char a) {
         func(0, "%c", a);
         return *this;
     }
 
-    inline OutStream &OutStream::operator<<(uint16_t a) {
+    inline OutStream &OutStream::operator<<(unsigned short a) {
         func(0, "%u", a);
         return *this;
     }
 
-    inline OutStream &OutStream::operator<<(uint32_t a) {
+    inline OutStream &OutStream::operator<<(unsigned int a) {
         func(0, "%u", a);
         return *this;
     }
 
-    inline OutStream &OutStream::operator<<(uint64_t a) {
+    inline OutStream &OutStream::operator<<(unsigned long a) {
         func(0, "%lu", a);
         return *this;
     }
 
+    inline OutStream &OutStream::operator<<(unsigned long long a) {
+        func(0, "%llu", a);
+        return *this;
+    }
+
     inline OutStream &OutStream::operator<<(const char *a){
         func(0, "%s", a);
         return *this;

+ 2 - 2
src/core/code.cpp

@@ -29,7 +29,7 @@ namespace aFuncore {
      */
     void Code::display() const{
         if (code->type != ByteCode::code_start) {
-            errorLog(aFunCoreLogger, "Code dsplay all did not with `start`");
+            errorLog(aFunCoreLogger, "Code display all did not with `start`");
             return;
         }
 
@@ -371,7 +371,7 @@ RETURN_FALSE:
      * 显式代码块内容
      */
     void Code::ByteCode::display() const{
-        aFuntool::cout << (prefix == aFuntool::NUL ? '-' : prefix) << "[father: " << father << "] type=" << type << " " << this;
+        aFuntool::cout << (char)(prefix == aFuntool::NUL ? '-' : prefix) << "[father: " << father << "] type=" << type << " " << this;
         if (type == code_element)
             aFuntool::cout << " element: " << data.element << "\n";
         else if (type == code_block)

+ 31 - 0
src/runtime/rt-reader.cpp

@@ -14,4 +14,35 @@ namespace aFunrt {
         index += read_len;
         return read_len;
     }
+
+    size_t ReaderFile::readText(char *dest, size_t read_len, aFuncore::Reader::ReadMode &mode) {
+        if (!no_first) {
+            no_first = true;
+            char ch;
+            if (fread(&ch, sizeof(char), 1, file) != 1) {
+                mode = read_mode_finished;
+                return 0;
+            }
+
+            if (ch == (char)0xEF) {
+                /* 处理BOM编码 */
+                char ch_[2];
+                if (fread(ch_, sizeof(char), 2, file) != 2 || ch_[0] != (char)0xBB || ch_[1] != (char)0xBF) {
+                    mode = read_mode_error;
+                    return 0;
+                }
+//                writeTrackLog(aFunRTLogger, "Parser utf-8 with BOM");
+            } else {
+                ungetc(ch, file);
+//                writeTrackLog(aFunRTLogger, "Parser utf-8 without BOM");
+            }
+        }
+
+        size_t len_r =  fread(dest, sizeof(char), read_len, file);
+        if (aFuntool::clear_ferror(file)) {
+            mode = read_mode_error;
+        } else if (feof(file))
+            mode = read_mode_finished;
+        return len_r;
+    }
 }

+ 27 - 0
test/src/core-syntactic.cpp

@@ -5,6 +5,9 @@ const char *str = "{if true [HelloWorld (10)]}\n";
 const char *str2 = "{if true [HelloWorld (10)\n";
 
 int main() {
+    std::string md5_1;
+    std::string md5_2;
+
     {
         auto reader = aFunrt::ReaderString(str, "str");
         auto parser = aFuncore::Parser(reader);
@@ -13,6 +16,8 @@ int main() {
         if (!ret)
             return 1;
         code.display();
+        md5_1 = code.getMD5_v1();
+        aFuntool::cout << "Code1 md5: %s" << md5_1 << "\n";
     }
 
     {
@@ -26,5 +31,27 @@ int main() {
             printf("Event %d, %d\n", event.type, event.line);
         }
     }
+
+    {
+        auto file = aFuntool::fileOpen("test.aun", "w");
+        if (file == nullptr) {
+            printf("Cannot open file test.aun\n");
+            return 1;
+        }
+        fprintf(file, "%s", str);
+        aFuntool::fileClose(file);
+
+        auto reader = aFunrt::ReaderFile("test.aun");
+        auto parser = aFuncore::Parser(reader);
+        auto code = aFuncore::Code("test.aun");
+        bool ret = parser.parserCode(code);
+        if (!ret)
+            return 1;
+        code.display();
+        md5_2 = code.getMD5_v1();
+        aFuntool::cout << "Code2 md5: %s" << md5_2 << "\n";
+    }
+
+    std::cout << "Is md5_1 == md5_2? " << (md5_1 == md5_2) << "\n";
     return 0;
 }