2023-07-13 14:30:08 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
#include<hgl/type/DataType.h>
|
2023-07-17 20:26:45 +08:00
|
|
|
|
#include<hgl/type/String.h>
|
2023-07-13 14:30:08 +08:00
|
|
|
|
#include<hgl/io/InputStream.h>
|
|
|
|
|
|
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace io
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 文本输入流<br>
|
|
|
|
|
* 它与TextOutputStream并无对应关注,主要作用是方便超大文本的读取与解晰。
|
|
|
|
|
*/
|
|
|
|
|
class TextInputStream
|
|
|
|
|
{
|
2023-07-13 14:45:03 +08:00
|
|
|
|
public:
|
|
|
|
|
|
2023-07-17 20:26:45 +08:00
|
|
|
|
template<typename T>
|
2023-07-13 14:45:03 +08:00
|
|
|
|
struct ParseCallback
|
|
|
|
|
{
|
2023-07-17 20:26:45 +08:00
|
|
|
|
protected:
|
2023-07-13 14:45:03 +08:00
|
|
|
|
|
2023-07-17 20:26:45 +08:00
|
|
|
|
String<T> tmp;
|
2023-07-13 14:45:03 +08:00
|
|
|
|
|
2023-07-17 20:26:45 +08:00
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
virtual bool OnBOM(const ByteOrderMask &){return true;} ///<读取到BOM头的回调函数
|
|
|
|
|
|
2023-07-27 18:52:40 +08:00
|
|
|
|
virtual bool OnLine(const T *text,const int len){return true;}
|
2023-07-13 14:45:03 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2023-07-17 20:26:45 +08:00
|
|
|
|
* 读取到一行文本的回调函数
|
2023-07-13 14:45:03 +08:00
|
|
|
|
* @param text 读取到的文本内容
|
|
|
|
|
* @param len 读取到的文本字长度
|
|
|
|
|
* @param line_end 当前行是否结束
|
|
|
|
|
*/
|
2023-07-17 20:26:45 +08:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
tmp.Strcat(text,len);
|
|
|
|
|
|
|
|
|
|
const bool result=OnLine(tmp.c_str(),tmp.Length());
|
|
|
|
|
|
|
|
|
|
tmp.Clear();
|
|
|
|
|
return(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-07-13 14:45:03 +08:00
|
|
|
|
|
2023-07-17 20:26:45 +08:00
|
|
|
|
struct EventCallback
|
|
|
|
|
{
|
2023-07-13 14:45:03 +08:00
|
|
|
|
virtual bool OnEnd(){return true;} ///<读取结束的回调函数
|
|
|
|
|
virtual bool OnReadError(){return true;} ///<读取错误的回调函数
|
|
|
|
|
virtual bool OnParseError(){return true;} ///<解析错误的回调函数
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-13 14:30:08 +08:00
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
InputStream *input_stream; ///<输入流
|
|
|
|
|
|
2023-10-10 01:12:38 +08:00
|
|
|
|
char *buffer; ///<缓冲区
|
2023-07-13 14:30:08 +08:00
|
|
|
|
int32 buffer_size; ///<缓冲区大小
|
|
|
|
|
int32 cur_buf_size; ///<当前缓冲区大小
|
|
|
|
|
|
|
|
|
|
int64 stream_pos,stream_size; ///<流当前位置/大小
|
|
|
|
|
|
|
|
|
|
ByteOrderMask bom; ///<BOM头
|
|
|
|
|
|
2023-07-13 21:11:33 +08:00
|
|
|
|
ByteOrderMask default_bom; ///<缺省BOM,在没有BOM头时使用
|
2023-07-17 20:26:45 +08:00
|
|
|
|
|
|
|
|
|
ParseCallback<char> *callback_u8; ///<回调函数
|
|
|
|
|
ParseCallback<u16char> *callback_u16; ///<回调函数
|
|
|
|
|
ParseCallback<u32char> *callback_u32; ///<回调函数
|
|
|
|
|
|
|
|
|
|
EventCallback *event_callback; ///<事件回调函数
|
2023-07-13 14:45:03 +08:00
|
|
|
|
|
2023-07-13 14:30:08 +08:00
|
|
|
|
private:
|
|
|
|
|
|
2023-07-17 20:26:45 +08:00
|
|
|
|
template<typename T> int Parse(const T *,ParseCallback<T> *);
|
2023-07-13 14:30:08 +08:00
|
|
|
|
|
|
|
|
|
int TextBlockParse(); ///<文本块解析
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
TextInputStream(InputStream *i,const int buf_size=HGL_SIZE_1MB);
|
2023-10-10 01:12:38 +08:00
|
|
|
|
TextInputStream(char *buf,const int buf_size);
|
2023-07-13 14:30:08 +08:00
|
|
|
|
virtual ~TextInputStream()
|
|
|
|
|
{
|
2023-10-10 01:12:38 +08:00
|
|
|
|
if(input_stream) //有input_stream证明是从流加载的,需要删除临时缓冲区
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR_ARRAY(buffer);
|
|
|
|
|
}
|
2023-07-13 14:30:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 21:11:33 +08:00
|
|
|
|
void SetDefaultBOM(const ByteOrderMask &bo){default_bom=bo;} ///<设置缺省BOM头}
|
|
|
|
|
|
2023-07-17 20:26:45 +08:00
|
|
|
|
template<typename T> void SetParseCallback(ParseCallback<T> *pc); ///<设置回调函数
|
|
|
|
|
|
|
|
|
|
void SetEventCallback(EventCallback *ec){event_callback=ec;}
|
|
|
|
|
|
2023-07-13 14:30:08 +08:00
|
|
|
|
/**
|
|
|
|
|
* 运行并解晰文本
|
2023-07-13 14:45:03 +08:00
|
|
|
|
* @param pc 解晰结果回调函数
|
2023-07-13 14:30:08 +08:00
|
|
|
|
* @return 解析出的文本行数
|
|
|
|
|
*/
|
2023-07-17 20:26:45 +08:00
|
|
|
|
virtual int Run();
|
2023-07-13 14:30:08 +08:00
|
|
|
|
};//class TextInputStream
|
|
|
|
|
}//namespace io
|
|
|
|
|
}//namespace hgl
|