From 8d829b9690afbf308aa07e674b02d7aafb8f69cf Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Thu, 13 Jul 2023 14:45:03 +0800 Subject: [PATCH] The callback function of TextInputStream create independent callback structure --- inc/hgl/io/TextInputStream.h | 70 ++++++++++++++++++++---------------- src/IO/TextInputStream.cpp | 18 ++++++---- 2 files changed, 51 insertions(+), 37 deletions(-) diff --git a/inc/hgl/io/TextInputStream.h b/inc/hgl/io/TextInputStream.h index 0411e12..c22f391 100644 --- a/inc/hgl/io/TextInputStream.h +++ b/inc/hgl/io/TextInputStream.h @@ -12,6 +12,41 @@ namespace hgl */ class TextInputStream { + public: + + struct ParseCallback + { + virtual bool OnBOM(const ByteOrderMask &){return true;} ///<读取到BOM头的回调函数 + + /** + * 读取到一行文本的回调函数(ansi/utf8) + * @param text 读取到的文本内容 + * @param len 读取到的文本字长度 + * @param line_end 当前行是否结束 + */ + virtual bool OnLine(const char *text,const int len,const bool line_end){return true;} + + /** + * 读取到一行文本的回调函数(utf16le/utf16be) + * @param text 读取到的文本内容 + * @param len 读取到的文本字长度 + * @param line_end 当前行是否结束 + */ + virtual bool OnLine(const u16char *text,const int len,const bool line_end){return true;} + + /** + * 读取到一行文本的回调函数(utf32le/utf32be) + * @param text 读取到的文本内容 + * @param len 读取到的文本字长度 + * @param line_end 当前行是否结束 + */ + virtual bool OnLine(const u32char *text,const int len,const bool line_end){return true;} + + virtual bool OnEnd(){return true;} ///<读取结束的回调函数 + virtual bool OnReadError(){return true;} ///<读取错误的回调函数 + virtual bool OnParseError(){return true;} ///<解析错误的回调函数 + }; + private: InputStream *input_stream; ///<输入流 @@ -24,6 +59,8 @@ namespace hgl ByteOrderMask bom; /// int Parse(const T *); @@ -38,41 +75,12 @@ namespace hgl SAFE_CLEAR_ARRAY(buffer); } - virtual bool OnBOM(const ByteOrderMask &){return true;} ///<读取到BOM头的回调函数 - - /** - * 读取到一行文本的回调函数(ansi/utf8) - * @param text 读取到的文本内容 - * @param len 读取到的文本字长度 - * @param line_end 当前行是否结束 - */ - virtual bool OnLine(const char *text,const int len,const bool line_end){return true;} - - /** - * 读取到一行文本的回调函数(utf16le/utf16be) - * @param text 读取到的文本内容 - * @param len 读取到的文本字长度 - * @param line_end 当前行是否结束 - */ - virtual bool OnLine(const u16char *text,const int len,const bool line_end){return true;} - - /** - * 读取到一行文本的回调函数(utf32le/utf32be) - * @param text 读取到的文本内容 - * @param len 读取到的文本字长度 - * @param line_end 当前行是否结束 - */ - virtual bool OnLine(const u32char *text,const int len,const bool line_end){return true;} - - virtual bool OnEnd(){return true;} ///<读取结束的回调函数 - virtual bool OnReadError(){return true;} ///<读取错误的回调函数 - virtual bool OnParseError(){return true;} ///<解析错误的回调函数 - /** * 运行并解晰文本 + * @param pc 解晰结果回调函数 * @return 解析出的文本行数 */ - virtual int Run(); + virtual int Run(ParseCallback *pc); };//class TextInputStream }//namespace io }//namespace hgl diff --git a/src/IO/TextInputStream.cpp b/src/IO/TextInputStream.cpp index efe15f9..4eaf1e0 100644 --- a/src/IO/TextInputStream.cpp +++ b/src/IO/TextInputStream.cpp @@ -15,6 +15,7 @@ namespace hgl stream_size=input_stream->Available(); bom=ByteOrderMask::NONE; + callback=nullptr; } template int TextInputStream::Parse(const T *p) @@ -28,7 +29,7 @@ namespace hgl { if(*p=='\n') { - OnLine(sp,p-sp,true); + callback->OnLine(sp,p-sp,true); ++line_count; ++p; sp=p; @@ -39,7 +40,7 @@ namespace hgl ++p; if(*p=='\n') { - OnLine(sp,p-sp-1,true); + callback->OnLine(sp,p-sp-1,true); ++line_count; ++p; sp=p; @@ -51,7 +52,7 @@ namespace hgl if(spOnLine(sp,end-sp,false); ++line_count; } @@ -89,10 +90,13 @@ namespace hgl return Parse((char *)p); } - int TextInputStream::Run() + int TextInputStream::Run(ParseCallback *pc) { + if(!pc)return(-2); if(!input_stream)return(-1); + callback=pc; + int64 read_size; int result; @@ -109,7 +113,7 @@ namespace hgl if(cur_buf_size!=read_size) { - OnReadError(); + callback->OnReadError(); return(-1); } @@ -117,7 +121,7 @@ namespace hgl if(result<0) { - OnReadError(); + callback->OnReadError(); return(result); } @@ -126,6 +130,8 @@ namespace hgl stream_pos+=cur_buf_size; } + callback->OnEnd(); + return line_count; } }//namespace io