added ConstBufferReader
This commit is contained in:
parent
ac5931ce26
commit
ba7176099f
149
inc/hgl/io/ConstBufferReader.h
Normal file
149
inc/hgl/io/ConstBufferReader.h
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
#ifndef HGL_IO_CONST_BUFFER_READER_INCLUDE
|
||||||
|
#define HGL_IO_CONST_BUFFER_READER_INCLUDE
|
||||||
|
|
||||||
|
#include<hgl/type/String.h>
|
||||||
|
#include<hgl/io/SeekOrigin.h>
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
namespace io
|
||||||
|
{
|
||||||
|
class ConstBufferReader
|
||||||
|
{
|
||||||
|
const void *buffer;
|
||||||
|
const uint8 *pointer;
|
||||||
|
int64 size;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
ConstBufferReader(const void *buf,const int64 s)
|
||||||
|
{
|
||||||
|
buffer=buf;
|
||||||
|
pointer=(uint8 *)buffer;
|
||||||
|
size=s;
|
||||||
|
}
|
||||||
|
|
||||||
|
ConstBufferReader(ConstBufferReader &cbr,const int64 s)
|
||||||
|
{
|
||||||
|
buffer=cbr.CurPointer();
|
||||||
|
pointer=(uint8 *)buffer;
|
||||||
|
size=s;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> bool Read(T &v)
|
||||||
|
{
|
||||||
|
if(size<sizeof(T))return(false);
|
||||||
|
|
||||||
|
v=*(T *)pointer;
|
||||||
|
|
||||||
|
pointer+=sizeof(T);
|
||||||
|
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T1,typename T2> bool CastRead(T2 &v)
|
||||||
|
{
|
||||||
|
if(size<sizeof(T1))return(false);
|
||||||
|
|
||||||
|
v=(T2)*(T1 *)pointer;
|
||||||
|
|
||||||
|
pointer+=sizeof(T1);
|
||||||
|
|
||||||
|
return(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> bool ReadArray(T *v,const int64 count)
|
||||||
|
{
|
||||||
|
if(size<count*sizeof(T))return(false);
|
||||||
|
|
||||||
|
memcpy(v,pointer,count*sizeof(T));
|
||||||
|
|
||||||
|
pointer+=count*sizeof(T);
|
||||||
|
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
template<typename T,typename S> int _ReadString(T *str)
|
||||||
|
{
|
||||||
|
if(!str)return(-1);
|
||||||
|
|
||||||
|
S str_len;
|
||||||
|
|
||||||
|
if(!Read<S>(str_len))return(-2);
|
||||||
|
|
||||||
|
if(!ReadArray<T>(str,(const int64)str_len))return(-3);
|
||||||
|
|
||||||
|
str[str_len]=0;
|
||||||
|
|
||||||
|
return str_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T,typename S> int _ReadString(String<T> &str)
|
||||||
|
{
|
||||||
|
S str_len;
|
||||||
|
|
||||||
|
if(!Read<S>(str_len))return(-2);
|
||||||
|
|
||||||
|
str.SetString((T *)pointer,str_len);
|
||||||
|
|
||||||
|
pointer+=str_len*sizeof(T);
|
||||||
|
|
||||||
|
return(str_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
template<typename T> int ReadTinyString (T *str){return _ReadString<T,uint8>(str);}
|
||||||
|
template<typename T> int ReadShortString(T *str){return _ReadString<T,uint16>(str);}
|
||||||
|
template<typename T> int ReadString (T *str){return _ReadString<T,uint32>(str);}
|
||||||
|
|
||||||
|
template<typename T> int ReadTinyString (hgl::String<T> &str){return _ReadString<T,uint8>(str);}
|
||||||
|
template<typename T> int ReadShortString(hgl::String<T> &str){return _ReadString<T,uint16>(str);}
|
||||||
|
template<typename T> int ReadString (hgl::String<T> &str){return _ReadString<T,uint32>(str);}
|
||||||
|
|
||||||
|
int64 GetSize()const{return size;}
|
||||||
|
int64 GetRemain()const{return size-(pointer-(uint8 *)buffer);}
|
||||||
|
|
||||||
|
int64 Tell()const{return pointer-(uint8 *)buffer;}
|
||||||
|
int64 Seek(const int64 offset,const SeekOrigin so=SeekOrigin::Begin)
|
||||||
|
{
|
||||||
|
int64 new_offset;
|
||||||
|
|
||||||
|
switch(so)
|
||||||
|
{
|
||||||
|
case SeekOrigin::Begin: new_offset=offset;break;
|
||||||
|
case SeekOrigin::Current: new_offset=pointer-(uint8 *)buffer+offset;break;
|
||||||
|
case SeekOrigin::End: new_offset=size+offset;break;
|
||||||
|
default:return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(new_offset>size)
|
||||||
|
return(-1);
|
||||||
|
|
||||||
|
pointer=(uint8 *)buffer+new_offset;
|
||||||
|
|
||||||
|
return(new_offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
int64 Skip(const int64 offset)
|
||||||
|
{
|
||||||
|
if(offset>size)
|
||||||
|
return(-1);
|
||||||
|
|
||||||
|
pointer+=offset;
|
||||||
|
|
||||||
|
return(offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
int64 Restart()
|
||||||
|
{
|
||||||
|
pointer=(uint8 *)buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
const void *CurPointer()const{return pointer;}
|
||||||
|
};//class ConstBufferReader
|
||||||
|
}//namespace io
|
||||||
|
}//namespace hgl
|
||||||
|
#endif//HGL_IO_CONST_BUFFER_READER_INCLUDE
|
@ -1,18 +1,12 @@
|
|||||||
#ifndef HGL_IO_SEEK_ACCESS_INCLUDE
|
#ifndef HGL_IO_SEEK_ACCESS_INCLUDE
|
||||||
#define HGL_IO_SEEK_ACCESS_INCLUDE
|
#define HGL_IO_SEEK_ACCESS_INCLUDE
|
||||||
|
|
||||||
#include<hgl/type/DataType.h>
|
#include<hgl/platform/Platform.h>
|
||||||
|
#include<hgl/io/SeekOrigin.h>
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
namespace io
|
namespace io
|
||||||
{
|
{
|
||||||
enum class SeekOrigin /// 资源偏移方向枚举
|
|
||||||
{
|
|
||||||
Begin=0, ///<从资源最开始处开始,offset必须大于0。移到资源的offset位置
|
|
||||||
Current, ///<从资源当前位置开始,移到资源的Position+offset位置
|
|
||||||
End ///<从资源的结束位置开始,offset必须小于0,表示结束前的字符数
|
|
||||||
};//enum SeekOrigin
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 定位访问功能基类
|
* 定位访问功能基类
|
||||||
*/
|
*/
|
||||||
|
16
inc/hgl/io/SeekOrigin.h
Normal file
16
inc/hgl/io/SeekOrigin.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#ifndef HGL_IO_SEEK_ORIGIN_INCLUDE
|
||||||
|
#define HGL_IO_SEEK_ORIGIN_INCLUDE
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
namespace io
|
||||||
|
{
|
||||||
|
enum class SeekOrigin /// 资源偏移方向枚举
|
||||||
|
{
|
||||||
|
Begin=0, ///<从资源最开始处开始,offset必须大于0。移到资源的offset位置
|
||||||
|
Current, ///<从资源当前位置开始,移到资源的Position+offset位置
|
||||||
|
End ///<从资源的结束位置开始,offset必须小于0,表示结束前的字符数
|
||||||
|
};//enum SeekOrigin
|
||||||
|
}//namespace io
|
||||||
|
}//namespace hgl
|
||||||
|
#endif//HGL_IO_SEEK_ORIGIN_INCLUDE
|
Loading…
x
Reference in New Issue
Block a user