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,6 +12,41 @@ namespace hgl
*/ */
class TextInputStream 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: private:
InputStream *input_stream; ///<输入流 InputStream *input_stream; ///<输入流
@ -24,6 +59,8 @@ namespace hgl
ByteOrderMask bom; ///<BOM头 ByteOrderMask bom; ///<BOM头
ParseCallback *callback; ///<回调函数
private: private:
template<typename T> int Parse(const T *); template<typename T> int Parse(const T *);
@ -38,41 +75,12 @@ namespace hgl
SAFE_CLEAR_ARRAY(buffer); 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 * @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