The callback function of TextInputStream create independent callback structure

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-07-13 14:45:03 +08:00
parent d27b8d75fa
commit 8d829b9690
2 changed files with 51 additions and 37 deletions

View File

@ -12,32 +12,10 @@ namespace hgl
*/ */
class TextInputStream class TextInputStream
{ {
private:
InputStream *input_stream; ///<输入流
uint8 *buffer; ///<缓冲区
int32 buffer_size; ///<缓冲区大小
int32 cur_buf_size; ///<当前缓冲区大小
int64 stream_pos,stream_size; ///<流当前位置/大小
ByteOrderMask bom; ///<BOM头
private:
template<typename T> int Parse(const T *);
int TextBlockParse(); ///<文本块解析
public: public:
TextInputStream(InputStream *i,const int buf_size=HGL_SIZE_1MB); struct ParseCallback
virtual ~TextInputStream()
{ {
SAFE_CLEAR_ARRAY(buffer);
}
virtual bool OnBOM(const ByteOrderMask &){return true;} ///<读取到BOM头的回调函数 virtual bool OnBOM(const ByteOrderMask &){return true;} ///<读取到BOM头的回调函数
/** /**
@ -67,12 +45,42 @@ namespace hgl
virtual bool OnEnd(){return true;} ///<读取结束的回调函数 virtual bool OnEnd(){return true;} ///<读取结束的回调函数
virtual bool OnReadError(){return true;} ///<读取错误的回调函数 virtual bool OnReadError(){return true;} ///<读取错误的回调函数
virtual bool OnParseError(){return true;} ///<解析错误的回调函数 virtual bool OnParseError(){return true;} ///<解析错误的回调函数
};
private:
InputStream *input_stream; ///<输入流
uint8 *buffer; ///<缓冲区
int32 buffer_size; ///<缓冲区大小
int32 cur_buf_size; ///<当前缓冲区大小
int64 stream_pos,stream_size; ///<流当前位置/大小
ByteOrderMask bom; ///<BOM头
ParseCallback *callback; ///<回调函数
private:
template<typename T> int Parse(const T *);
int TextBlockParse(); ///<文本块解析
public:
TextInputStream(InputStream *i,const int buf_size=HGL_SIZE_1MB);
virtual ~TextInputStream()
{
SAFE_CLEAR_ARRAY(buffer);
}
/** /**
* *
* @param pc
* @return * @return
*/ */
virtual int Run(); virtual int Run(ParseCallback *pc);
};//class TextInputStream };//class TextInputStream
}//namespace io }//namespace io
}//namespace hgl }//namespace hgl

View File

@ -15,6 +15,7 @@ namespace hgl
stream_size=input_stream->Available(); stream_size=input_stream->Available();
bom=ByteOrderMask::NONE; bom=ByteOrderMask::NONE;
callback=nullptr;
} }
template<typename T> int TextInputStream::Parse(const T *p) template<typename T> int TextInputStream::Parse(const T *p)
@ -28,7 +29,7 @@ namespace hgl
{ {
if(*p=='\n') if(*p=='\n')
{ {
OnLine(sp,p-sp,true); callback->OnLine(sp,p-sp,true);
++line_count; ++line_count;
++p; ++p;
sp=p; sp=p;
@ -39,7 +40,7 @@ namespace hgl
++p; ++p;
if(*p=='\n') if(*p=='\n')
{ {
OnLine(sp,p-sp-1,true); callback->OnLine(sp,p-sp-1,true);
++line_count; ++line_count;
++p; ++p;
sp=p; sp=p;
@ -51,7 +52,7 @@ namespace hgl
if(sp<end) if(sp<end)
{ {
OnLine(sp,end-sp,false); callback->OnLine(sp,end-sp,false);
++line_count; ++line_count;
} }
@ -89,10 +90,13 @@ namespace hgl
return Parse<char>((char *)p); return Parse<char>((char *)p);
} }
int TextInputStream::Run() int TextInputStream::Run(ParseCallback *pc)
{ {
if(!pc)return(-2);
if(!input_stream)return(-1); if(!input_stream)return(-1);
callback=pc;
int64 read_size; int64 read_size;
int result; int result;
@ -109,7 +113,7 @@ namespace hgl
if(cur_buf_size!=read_size) if(cur_buf_size!=read_size)
{ {
OnReadError(); callback->OnReadError();
return(-1); return(-1);
} }
@ -117,7 +121,7 @@ namespace hgl
if(result<0) if(result<0)
{ {
OnReadError(); callback->OnReadError();
return(result); return(result);
} }
@ -126,6 +130,8 @@ namespace hgl
stream_pos+=cur_buf_size; stream_pos+=cur_buf_size;
} }
callback->OnEnd();
return line_count; return line_count;
} }
}//namespace io }//namespace io