improved ParseCallback of TextInputStream
This commit is contained in:
@@ -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
|
||||
|
@@ -16,10 +16,18 @@ namespace hgl
|
||||
|
||||
bom=ByteOrderMask::NONE;
|
||||
default_bom=ByteOrderMask::UTF8;
|
||||
callback=nullptr;
|
||||
|
||||
callback_u8=nullptr;
|
||||
callback_u16=nullptr;
|
||||
callback_u32=nullptr;
|
||||
event_callback=nullptr;
|
||||
}
|
||||
|
||||
template<typename T> int TextInputStream::Parse(const T *p)
|
||||
template<> void TextInputStream::SetParseCallback(ParseCallback<char> *pc){callback_u8=pc;}
|
||||
template<> void TextInputStream::SetParseCallback(ParseCallback<u16char> *pc){callback_u16=pc;}
|
||||
template<> void TextInputStream::SetParseCallback(ParseCallback<u32char> *pc){callback_u32=pc;}
|
||||
|
||||
template<typename T> int TextInputStream::Parse(const T *p,ParseCallback<T> *pc)
|
||||
{
|
||||
const T *sp=(const T *)p;
|
||||
const T *end=(const T *)(buffer+cur_buf_size);
|
||||
@@ -30,7 +38,7 @@ namespace hgl
|
||||
{
|
||||
if(*p=='\n')
|
||||
{
|
||||
callback->OnLine(sp,p-sp,true);
|
||||
pc->OnLine(sp,p-sp,true);
|
||||
++line_count;
|
||||
++p;
|
||||
sp=p;
|
||||
@@ -40,12 +48,12 @@ namespace hgl
|
||||
{
|
||||
if(p[1]=='\n')
|
||||
{
|
||||
callback->OnLine(sp,p-sp,true);
|
||||
pc->OnLine(sp,p-sp,true);
|
||||
p+=2;
|
||||
}
|
||||
else
|
||||
{
|
||||
callback->OnLine(sp,p-sp,true);
|
||||
pc->OnLine(sp,p-sp,true);
|
||||
++p;
|
||||
}
|
||||
|
||||
@@ -58,7 +66,7 @@ namespace hgl
|
||||
|
||||
if(sp<end)
|
||||
{
|
||||
callback->OnLine(sp,end-sp,false);
|
||||
pc->OnLine(sp,end-sp,false);
|
||||
++line_count;
|
||||
}
|
||||
|
||||
@@ -92,20 +100,30 @@ namespace hgl
|
||||
}
|
||||
|
||||
if(bom==ByteOrderMask::UTF16LE||bom==ByteOrderMask::UTF16BE)
|
||||
return Parse<u16char>((u16char *)p);
|
||||
{
|
||||
if(!callback_u16)return(-1);
|
||||
return Parse<u16char>((u16char *)p,callback_u16);
|
||||
}
|
||||
else
|
||||
if(bom==ByteOrderMask::UTF32LE||bom==ByteOrderMask::UTF32BE)
|
||||
return Parse<u32char>((u32char *)p);
|
||||
{
|
||||
if(!callback_u32)return(-1);
|
||||
return Parse<u32char>((u32char *)p,callback_u32);
|
||||
}
|
||||
else
|
||||
return Parse<char>((char *)p);
|
||||
{
|
||||
if(!callback_u8)return(-1);
|
||||
return Parse<char>((char *)p,callback_u8);
|
||||
}
|
||||
}
|
||||
|
||||
int TextInputStream::Run(ParseCallback *pc)
|
||||
int TextInputStream::Run()
|
||||
{
|
||||
if(!pc)return(-2);
|
||||
if(!input_stream)return(-1);
|
||||
|
||||
callback=pc;
|
||||
|
||||
if(!callback_u8
|
||||
&&!callback_u16
|
||||
&&!callback_u32)return(-2);
|
||||
|
||||
int64 read_size;
|
||||
|
||||
@@ -123,7 +141,9 @@ namespace hgl
|
||||
|
||||
if(cur_buf_size!=read_size)
|
||||
{
|
||||
callback->OnReadError();
|
||||
if(event_callback)
|
||||
event_callback->OnReadError();
|
||||
|
||||
return(-1);
|
||||
}
|
||||
|
||||
@@ -131,7 +151,9 @@ namespace hgl
|
||||
|
||||
if(result<0)
|
||||
{
|
||||
callback->OnReadError();
|
||||
if(event_callback)
|
||||
event_callback->OnReadError();
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
@@ -140,7 +162,8 @@ namespace hgl
|
||||
stream_pos+=cur_buf_size;
|
||||
}
|
||||
|
||||
callback->OnEnd();
|
||||
if(event_callback)
|
||||
event_callback->OnEnd();
|
||||
|
||||
return line_count;
|
||||
}
|
||||
|
Reference in New Issue
Block a user