CMCore/inc/hgl/io/TextInputStream.h

115 lines
4.4 KiB
C
Raw Normal View History

2023-07-13 14:30:08 +08:00
#pragma once
#include<hgl/type/DataType.h>
#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
{
public:
template<typename T>
struct ParseCallback
{
protected:
String<T> tmp;
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;}
/**
*
* @param text
* @param len
* @param line_end
*/
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);
}
}
};
struct EventCallback
{
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; ///<输入流
uint8 *buffer; ///<缓冲区
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头时使用
ParseCallback<char> *callback_u8; ///<回调函数
ParseCallback<u16char> *callback_u16; ///<回调函数
ParseCallback<u32char> *callback_u32; ///<回调函数
EventCallback *event_callback; ///<事件回调函数
2023-07-13 14:30:08 +08:00
private:
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);
virtual ~TextInputStream()
{
SAFE_CLEAR_ARRAY(buffer);
}
2023-07-13 21:11:33 +08:00
void SetDefaultBOM(const ByteOrderMask &bo){default_bom=bo;} ///<设置缺省BOM头}
template<typename T> void SetParseCallback(ParseCallback<T> *pc); ///<设置回调函数
void SetEventCallback(EventCallback *ec){event_callback=ec;}
2023-07-13 14:30:08 +08:00
/**
*
* @param pc
2023-07-13 14:30:08 +08:00
* @return
*/
virtual int Run();
2023-07-13 14:30:08 +08:00
};//class TextInputStream
}//namespace io
}//namespace hgl