improved ParseCallback of TextInputStream

This commit is contained in:
2023-07-17 20:26:45 +08:00
parent aeae7b89ab
commit e3bf1e0cd3
2 changed files with 87 additions and 35 deletions

View File

@@ -1,5 +1,6 @@
#pragma once
#include<hgl/type/DataType.h>
#include<hgl/type/String.h>
#include<hgl/io/InputStream.h>
namespace hgl
@@ -14,34 +15,49 @@ namespace hgl
{
public:
template<typename T>
struct ParseCallback
{
protected:
String<T> tmp;
public:
virtual bool OnBOM(const ByteOrderMask &){return true;} ///<读取到BOM头的回调函数
virtual bool OnLine(const T *text,const int line){return true;}
/**
* 读取到一行文本的回调函数(ansi/utf8)
* 读取到一行文本的回调函数
* @param text 读取到的文本内容
* @param len 读取到的文本字长度
* @param line_end 当前行是否结束
*/
virtual bool OnLine(const char *text,const int len,const bool line_end){return true;}
virtual bool OnLine(const T *text,const int len,const bool line_end)
{
if(!line_end)
{
tmp.Strcat(text,len);
return true;
}
else
{
if(tmp.IsEmpty())
return OnLine(text,len);
/**
* 读取到一行文本的回调函数(utf16le/utf16be)
* @param text 读取到的文本内容
* @param len 读取到的文本字长度
* @param line_end 当前行是否结束
*/
virtual bool OnLine(const u16char *text,const int len,const bool line_end){return true;}
tmp.Strcat(text,len);
/**
* 读取到一行文本的回调函数(utf32le/utf32be)
* @param text 读取到的文本内容
* @param len 读取到的文本字长度
* @param line_end 当前行是否结束
*/
virtual bool OnLine(const u32char *text,const int len,const bool line_end){return true;}
const bool result=OnLine(tmp.c_str(),tmp.Length());
tmp.Clear();
return(result);
}
}
};
struct EventCallback
{
virtual bool OnEnd(){return true;} ///<读取结束的回调函数
virtual bool OnReadError(){return true;} ///<读取错误的回调函数
virtual bool OnParseError(){return true;} ///<解析错误的回调函数
@@ -60,11 +76,16 @@ namespace hgl
ByteOrderMask bom; ///<BOM头
ByteOrderMask default_bom; ///<缺省BOM在没有BOM头时使用
ParseCallback *callback; ///<回调函数
ParseCallback<char> *callback_u8; ///<回调函数
ParseCallback<u16char> *callback_u16; ///<回调函数
ParseCallback<u32char> *callback_u32; ///<回调函数
EventCallback *event_callback; ///<事件回调函数
private:
template<typename T> int Parse(const T *);
template<typename T> int Parse(const T *,ParseCallback<T> *);
int TextBlockParse(); ///<文本块解析
@@ -78,12 +99,20 @@ namespace hgl
void SetDefaultBOM(const ByteOrderMask &bo){default_bom=bo;} ///<设置缺省BOM头}
template<typename T> void SetParseCallback(ParseCallback<T> *pc); ///<设置回调函数
void SetEventCallback(EventCallback *ec){event_callback=ec;}
/**
* 运行并解晰文本
* @param pc 解晰结果回调函数
* @return 解析出的文本行数
*/
virtual int Run(ParseCallback *pc);
virtual int Run();
};//class TextInputStream
/**
*
*/
}//namespace io
}//namespace hgl