Linux下std::ifstream成员函数对应系统调用验证

news2024/9/21 1:16:35

最近在分析离线数据使用时的bug,发现代码中对std::ifstream成员函数使用存在疑问,所以就写了个简单测试程序来分析std::ifstream成员函数对应那些系统调用。

目录

1.gcount

2.seekg和tellg

3.read


代码如下:

test.cpp

#include <iostream>
#include <fstream>
#include <atomic>
#include <string>

using namespace std;

/*
1.fseek函数:C语言标准库函数
作用:设置文件读写位置
函数原型:int fseek(FILE *fp, LONG offset, int origin);
参数含义:
    fp 文件指针 
    offset 相对于origin规定的偏移位置量 
    origin 指针移动的起始位置,取值如下: 
        SEEK_SET 文件开始位置 
        SEEK_CUR 文件当前位置 
        SEEK_END 文件结束位置
返回值:
    成功,返回0,失败返回非0值,并设置error的值,可以用perror()函数输出错误。

2.ftell函数:C语言标准库函数
作用:用于得到文件位置指针当前位置相对于文件首的偏移字节数
原型:
#include <stdio.h>
long ftell(FILE *stream);
获取文件大小:首先将文件的当前位置移到文件的末尾,然后调用函数ftell()获得当前位置相对于文件首的位移,该位移值等于文件所含字节数。
fseek(fp, 0L,SEEK_END); 
len = ftell(fp); 

3.lseek函数:linux下系统调用
lseek: 用于改变一个文件读写指针位置的一个系统调用。指针位置可以是绝对的或者相对的。
    和fseek函数类似,但lseek返回的是一个off_t数值
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
offset为正则向文件末尾移动(向前移),为负数则向文件头部(向后移)。
返回值:
正确时返回文件读写指针距文件开头的字节大小,出错返回-1
lseek的作用是打开文件下一次读写的开始位置,因此还有以下两个作用
(1)拓展文件,不过一定要一次写的操作。迅雷等下载工具在下载文件时候先扩展一个空间然后再下载的。
int fd = open("test.txt",O_RDWR);
lseek(fd,0x1000, SEEK_SET);
write(fd, "a", 1);
close(fd);

(2)获取文件大小。
int fd = open("test.txt", O_RDWR);
printf("file size:%d \n", lseek(fd, 0, SEEK_END));
close(fd);

总结:lseek 功能包含 ftell + fseek

*/

/*
C++中seekp() seekg() tellp() tellg() 
seekp:设置输出文件流的文件流指针位置
tellp::获取输出文件流的文件流指针位置
seekg:设置输入文件流的文件流指针位置
tellg:获取输入文件流的文件流指针位置
函数原型:
ostream& seekp( streampos pos );
ostream& seekp( streamoff off, ios::seek_dir dir );
istream& seekg( streampos pos );
istream& seekg( streamoff off, ios::seek_dir dir );
ostream& tellp() 
istream& tellg() 
参数含义:
pos:新的文件流指针位置值
off:需要偏移的值
dir:搜索的起始位置
dir参数用于对文件流指针的定位操作上,代表搜索的起始位置
在ios中定义的枚举类型:
    enum seek_dir {beg, cur, end};
    含义如下:
        ios::beg:文件流的起始位置
        ios::cur:文件流的当前位置
        ios::end:文件流的结束位置

gcount(): 返回在对象上执行的最后一个未格式化的输入操作中提取的字符数,不涉及调用系统api
获取文件大小:
seekg(0, std::ios::end);
tellg();
*/

/*
iostream类评估流状态的4个功能函数:good(),bad(),fail(),和eof(),具体功能如下:
good():流没有遇到错误。
bad():数据流已遇到效应的流(即存储器分配失败,没有缓流等)。
fail():的完整性的错误:通常可恢复的错误(格式化/解析失败)。
eof():已达到文件结尾(EOF)字符。
*/

static std::atomic<int> count_num(0);

#define DB_IsBigEnd_FromRPHeaderAttr( HeaderAttr ) \
	((bool) (HeaderAttr & 0X00000001))

//typedef unsigned int uint_t;
using uint_t = unsigned int;
typedef unsigned char Byte;
typedef unsigned short	u_short;


#define DB_FORMAT_VER_MAX_LENGTH	(16)

#define DB_DATA_VER_MAX_LENGTH		(16)

#define DB_AUTHOR_MAX_LENGTH		(8)

#define DB_DATE_MAX_LENGTH			(8)

/* 小端到大端的变换宏 */
#define _V_GET_LITTLE_SHORT( x )	(short)( ( ( *((Byte*)x+1) ) << 8 ) | (*((Byte*)x)) )
#define _V_GET_LITTLE_LONG( x )		(int)( ( ( *((Byte*)x+3) ) << 24 ) | (( *((Byte*)x+2) ) << 16 ) \
        | (( *((Byte*)x+1) ) << 8 ) | ( *((Byte*)x) ) )
#define _V_GET_LITTLE_FLOAT( x ) (*((float *)(x)))

#define _V_GET_BIGENDDIAN_SHORT( x ) (short)( ( ( *((Byte*)x) ) << 8 ) | (*((Byte*)x+1)) )
#define _V_GET_BIGENDDIAN_LONG( x )	(int)( ( ( *((Byte*)x) ) << 24 ) | (( *((Byte*)x+1) ) << 16 ) \
    | (( *((Byte*)x+2) ) << 8 ) |  ( *((Byte*)x+3) ) )


typedef struct _DB_Header_t
{
	uint_t		unAttr;									
	uint_t		unHeaderSize;							
	uint_t		unFrameSize;							
	char		cFormatVer[DB_FORMAT_VER_MAX_LENGTH];	
	char		cDataVer[DB_DATA_VER_MAX_LENGTH];		
	char		cAuthor[DB_AUTHOR_MAX_LENGTH];		
	char		cDate[DB_DATE_MAX_LENGTH];			
	uint_t		unCalcRegionMaxSize;					
	uint_t		unInfoRegionMaxSize;					
	uint_t		unRPLinkShapePointMaxCnt;				
	uint_t		unNameMaxLength;						
	uint_t		unNameFrameOffset;						
	uint_t		unDistrictFrameOffset;					
	uint_t		unLevelCnt;								
	uint_t		unLevelRecordSize;						
	uint_t		pstLevelTable;							
} DB_Header_t ;

typedef struct _DB_Level_t
{
	u_int	unAttr;							
	u_int	unCalcRegionManagerHeaderOffset;
	u_int	unUnCompressedCalcRegionManagerHeaderOffset;
	u_int	unCalcRegionManagerHeaderSize;	
	u_int	unInfoRegionManagerHeaderOffset;
	u_int	unInfoRegionManagerHeaderSize;	
	u_int	unAdjacentNodeManagerHeaderOffset;
	u_int	unAdjacentNodeManagerHeaderSize;
} DB_Level_t;

typedef struct _DB_NameFrame_t
{
	u_int		unFrameSize;	
	u_int		unStructSize;	
	u_int		unNameIdxCnt;	
	u_int		unNameIdxSize;	
	u_int		punIdxTable;	
	u_int		pusNameTable;	
} DB_NameFrame_t;

typedef struct _DB_DistrictFrame_t
{
	u_int					unFrameSize;		
	u_int					unStructSize;		
	u_int					unDistrictCnt;		
	u_int					unDistrictSize;		
	u_int					pstDistrictTable;	
} DB_DistrictFrame_t;

typedef struct _DB_AbsoluteNodeID_t
{
	u_int 	unNodeID ;			
	u_int 	unNodeExtendID ;
} DB_AbsoluteNodeID_t;

typedef struct _DB_AbsoluteLinkID_t
{
	u_int	unLinkIDPart1;	 
	u_int 	unLinkIDPart2;	 
	u_int 	unLinkAttr;	 	 	 
} DB_AbsoluteLinkID_t;

typedef struct _DB_AdjacentNode_t
{
	u_int					nLongitude ;
	u_int					nLatitude ;
	DB_AbsoluteNodeID_t	stNodeID ;
	DB_AbsoluteLinkID_t	stLinkID ;
	bool					bIsStartNode ;
} DB_AdjacentNode_t ;

typedef struct _DB_AdjacentNodeMgr_t
{
	u_int					unSize ;
	u_int					unStructSize ;
	u_int					unCnt ;
	u_int					unRecordSize;	
	u_int					pstAdjNodes;
} DB_AdjacentNodeMgr_t ;

typedef struct _DB_CalcRegion_ManagerHeader_t
{
	u_int					unFrameSize;		
	u_int					unRankInfo;			
	u_int					unRegionCnt;		
	u_int					unRegionIdxSize;	
	u_int					pstRegionIdxTable;	
} DB_CalcRegion_ManagerHeader_t;

typedef struct _DB_DistrictRecord_t
{
	u_short		usCityNameIdx;	
	u_short		usCountyNameIdx;
} DB_DistrictRecord_t;

typedef struct _DB_CalcRegion_Idx_t
{
	u_short	unRegionOffset;					
	u_short	unUnCompressedRegionOffset ;	
	u_short	unRegionSize;					
	u_short	unCompressedRegionSize ;		
} DB_CalcRegion_Idx_t;


//多个对象指向同一个文件操作
class DBFile
{
public:
    DBFile(std::ifstream *DBFileHandle);
    ~DBFile();

	int32_t seek_cur_to_end_size(std::ifstream *m_pDBFileHandle, int32_t start_seek_pos);

	static int32_t seek_cur_to_end_size(std::ifstream& pFile, int32_t start_seek_pos);

private:
    std::ifstream *m_pDBFileHandle;
};

DBFile::DBFile(std::ifstream *DBFileHandle)
{
    if(DBFileHandle) {
        count_num++;
        m_pDBFileHandle = DBFileHandle;
    }
}

DBFile::~DBFile()
{
    if(m_pDBFileHandle) {
        count_num--;
        if(count_num <= 0) {
            count_num = 0;
        }
        if (count_num <= 0 && m_pDBFileHandle->good() && m_pDBFileHandle->is_open()) {
            m_pDBFileHandle->close();
        }
    }
}

int32_t DBFile::seek_cur_to_end_size(std::ifstream *m_pDBFileHandle, int32_t start_seek_pos)
{
	if(!m_pDBFileHandle) {
		return 0;
	}
	m_pDBFileHandle->seekg(0, std::ios::end);
	if(!m_pDBFileHandle->good()) {
		return 0;
	}
	int32_t seekg_end_size = m_pDBFileHandle->tellg();

	m_pDBFileHandle->seekg(start_seek_pos, std::ios::beg);
	if(!m_pDBFileHandle->good()) {
		return 0;
	}
	int32_t seekg_cur_size = m_pDBFileHandle->tellg();

	return seekg_end_size - seekg_cur_size;
}

int32_t DBFile::seek_cur_to_end_size(std::ifstream& pFile, int32_t start_seek_pos)
{
    std::cout << 11111 << std::endl;
	pFile.seekg(0, std::ios::end);
	if(!pFile.good()) {
		return 0;
	}
    std::cout << 22222 << std::endl;
	int32_t seekg_end_size = pFile.tellg();
    std::cout << 33333 << std::endl;
	pFile.seekg(start_seek_pos, std::ios::beg);
    std::cout << 44444 << std::endl;
	if(!pFile.good()) {
		return 0;
	}
    std::cout << 55555 << std::endl;
	int32_t seekg_cur_size = pFile.tellg();
    std::cout << 666666 << std::endl;

	return seekg_end_size - seekg_cur_size;
}

class DBParser
{
public:
	DBParser() ;
	virtual ~DBParser();
    int Init (const std::string DBPathFileName);
    int GetHeaderSize (u_int* punHeaderSize);
    int GetHeader (u_int unHeaderSize, DB_Header_t* pstHeaderInfo);
    int GetNameFrameSize (u_int* punNameFrameSize);
    int GetNameFrame (
				u_int unOffset,
				u_int unFrameSize,
				DB_NameFrame_t* pstNameFrame);
    int GetDistrictFrameSize (
                    u_int* punDistrictFrameSize);

    int GetDistrictFrame (
                    u_int unOffset,
                    u_int unFrameSize,
                    DB_DistrictFrame_t* pstDistrictFrame);

    int GetAdjacentNodeManager (
                        u_int unMgrOffset,
                        u_int unMgrSize,
                        DB_AdjacentNodeMgr_t* pstAdjNodeMgr);

    int GetCalcRegionManagerHeader( u_int unHeaderOffset,
                                    u_int unHeaderSize,
                                    DB_CalcRegion_ManagerHeader_t* pstManagerHeader);

private:
    std::ifstream		m_hDBFileHandle ;
    bool		        m_bIsBigEnd ;
    std::string         m_rp_db_file_path;
    std::atomic_bool	m_rp_db_file_install;
};

DBParser::DBParser() {
    m_rp_db_file_path = "";
    m_rp_db_file_install = false;
}

DBParser::~DBParser() {
    m_rp_db_file_install = false;
    if (m_hDBFileHandle.is_open()) {
        m_hDBFileHandle.close() ;
	}
}


int DBParser::Init (const std::string DBPathFileName)
{
    m_hDBFileHandle.open(DBPathFileName , std::ios::in|std::ios::binary);
    if (!m_hDBFileHandle.is_open()) {
        return -1;
    }
    DBFile dbFile(&m_hDBFileHandle);
    unsigned int unHeaderAttr = 0;
    m_hDBFileHandle.read((char*)&unHeaderAttr, sizeof(unHeaderAttr)) ;
    if (!m_hDBFileHandle.good()) {
        return -2;
    }
    std::cout << "gcount = " << m_hDBFileHandle.gcount() << std::endl;
    m_bIsBigEnd = DB_IsBigEnd_FromRPHeaderAttr(unHeaderAttr) ;
    unsigned int unHeaderSize = 0;
    std::cout << 99999 << std::endl;
    m_hDBFileHandle.read((char*)&unHeaderSize, sizeof(unHeaderSize));
    std::cout << 101010 << std::endl;
    if (m_hDBFileHandle.gcount() != sizeof(unHeaderSize)) {
        return -3;
    }
    std::cout << "gcount = " << m_hDBFileHandle.gcount() << std::endl;
    if (unHeaderSize > 1024 * 1024) // header size 不应该大于 1M
    {
	    return -4;
    }
	
    DB_Header_t* header = new DB_Header_t; // malloc(unHeaderSize);
    if (NULL == header) {
		return -5;
    }

    m_hDBFileHandle.seekg(0, std::ios::beg); //seek to begin

    m_hDBFileHandle.read((char*)header, unHeaderSize);

    if (m_hDBFileHandle.gcount() != unHeaderSize) {
		free(header);
        return -3;
    }

    m_hDBFileHandle.seekg(0, std::ios::end);

    ulong fileSize = m_hDBFileHandle.tellg();
    u_int frameSize = header->unFrameSize;
    free(header);
    if (frameSize != (u_int)fileSize) {
        return -6;
    }

    m_rp_db_file_path = DBPathFileName;
    m_rp_db_file_install = true;
    m_hDBFileHandle.close();

    return 0;
}

int DBParser::GetHeaderSize (u_int* punHeaderSize)
{
    if (punHeaderSize == NULL) {
        return -1;
    }
    if((true == m_rp_db_file_install) && (!m_hDBFileHandle.is_open()))
    {
        m_hDBFileHandle.open(m_rp_db_file_path , std::ios::in|std::ios::binary);
    }
    DBFile oRpFile(&m_hDBFileHandle);

    if (!m_hDBFileHandle.is_open())
	{
		*punHeaderSize = 0;
		return -2 ;
	}

    int size = DBFile::seek_cur_to_end_size(m_hDBFileHandle, 4);

    if (!m_hDBFileHandle.good()) {
        return -3;
    }
    if(size < sizeof(*punHeaderSize)){
        return -4;
    }
    std::cout << 77777 << std::endl;
    m_hDBFileHandle.read ((char*)punHeaderSize, sizeof (*punHeaderSize)) ;
    std::cout << "gcount = " << m_hDBFileHandle.gcount() << std::endl;
    std::cout << 88888 << std::endl;
    if (!m_hDBFileHandle.good()) {
        return false;
    }

	if (m_bIsBigEnd)
	{
		*punHeaderSize = _V_GET_LITTLE_LONG(punHeaderSize) ;
	}

    return 0;
}

int DBParser::GetHeader (u_int unHeaderSize,
	                    DB_Header_t* pstHeaderInfo)
{
	u_int	i = 0;
	Byte*	pbtHeaderInfo = (Byte*)pstHeaderInfo;
	DB_Level_t *pstLevelTable;

	if (unHeaderSize == 0 || pstHeaderInfo == NULL) {
        return -1;
    }

    if((true == m_rp_db_file_install) && (!m_hDBFileHandle.is_open()))
    {
        m_hDBFileHandle.open(m_rp_db_file_path , std::ios::in|std::ios::binary);
    }
    DBFile oRpFile(&m_hDBFileHandle);

    if (!m_hDBFileHandle.is_open()) {
        return -2;
    }
    m_hDBFileHandle.seekg(0, std::ios::beg);
    if (!m_hDBFileHandle.good()) {
        return -3;
    }
    /*
    char tmpBuf[9016] = {0};
    m_hDBFileHandle.read(tmpBuf, unHeaderSize);
    */
    std::cout << "begin gcount  gcount" << std::endl;
    m_hDBFileHandle.read((char*)pbtHeaderInfo, unHeaderSize);
    std::cout << "gcount = " << m_hDBFileHandle.gcount() << std::endl;
    std::cout << "begin gcount  gcount" << std::endl;
    if (!m_hDBFileHandle.good()) {
        return -3;
    } 

	if (m_bIsBigEnd)
	{
		pstHeaderInfo->unAttr = _V_GET_LITTLE_LONG (&pstHeaderInfo->unAttr) ;
		pstHeaderInfo->unHeaderSize = _V_GET_LITTLE_LONG (&pstHeaderInfo->unHeaderSize) ;
		pstHeaderInfo->unFrameSize = _V_GET_LITTLE_LONG (&pstHeaderInfo->unFrameSize) ;
		pstHeaderInfo->unCalcRegionMaxSize = _V_GET_LITTLE_LONG (&pstHeaderInfo->unCalcRegionMaxSize) ;
		pstHeaderInfo->unInfoRegionMaxSize = _V_GET_LITTLE_LONG (&pstHeaderInfo->unInfoRegionMaxSize) ;
		pstHeaderInfo->unRPLinkShapePointMaxCnt = _V_GET_LITTLE_LONG (&pstHeaderInfo->unRPLinkShapePointMaxCnt) ;
		pstHeaderInfo->unNameMaxLength = _V_GET_LITTLE_LONG (&pstHeaderInfo->unNameMaxLength) ;
		pstHeaderInfo->unNameFrameOffset = _V_GET_LITTLE_LONG (&pstHeaderInfo->unNameFrameOffset) ;
		pstHeaderInfo->unDistrictFrameOffset = _V_GET_LITTLE_LONG (&pstHeaderInfo->unDistrictFrameOffset) ;
		pstHeaderInfo->unLevelCnt = _V_GET_LITTLE_LONG (&pstHeaderInfo->unLevelCnt) ;
		pstHeaderInfo->unLevelRecordSize = _V_GET_LITTLE_LONG (&pstHeaderInfo->unLevelRecordSize) ;
		pstHeaderInfo->pstLevelTable = _V_GET_LITTLE_LONG (&pstHeaderInfo->pstLevelTable) ;
	}

	pstLevelTable = (DB_Level_t*)(pbtHeaderInfo + (u_int)(pstHeaderInfo->pstLevelTable));

    if(pstLevelTable == NULL) {
        return -4;
    }

	if (m_bIsBigEnd)
	{
		for (i = 0; i < pstHeaderInfo->unLevelCnt ; i++)
		{
			pstLevelTable->unAttr =
				_V_GET_LITTLE_LONG (&pstLevelTable->unAttr) ;
			pstLevelTable->unCalcRegionManagerHeaderOffset =
				_V_GET_LITTLE_LONG (&pstLevelTable->unCalcRegionManagerHeaderOffset) ;
			pstLevelTable->unUnCompressedCalcRegionManagerHeaderOffset =
				_V_GET_LITTLE_LONG (&pstLevelTable->unUnCompressedCalcRegionManagerHeaderOffset) ;
			pstLevelTable->unCalcRegionManagerHeaderSize =
				_V_GET_LITTLE_LONG (&pstLevelTable->unCalcRegionManagerHeaderSize) ;
			pstLevelTable->unInfoRegionManagerHeaderOffset =
				_V_GET_LITTLE_LONG (&pstLevelTable->unInfoRegionManagerHeaderOffset) ;
			pstLevelTable->unInfoRegionManagerHeaderSize =
				_V_GET_LITTLE_LONG (&pstLevelTable->unInfoRegionManagerHeaderSize) ;
			pstLevelTable->unAdjacentNodeManagerHeaderOffset =
				_V_GET_LITTLE_LONG (&pstLevelTable->unAdjacentNodeManagerHeaderOffset) ;
			pstLevelTable->unAdjacentNodeManagerHeaderSize =
				_V_GET_LITTLE_LONG (&pstLevelTable->unAdjacentNodeManagerHeaderSize) ;

			pstLevelTable = (DB_Level_t*)((Byte*)pstLevelTable + pstHeaderInfo->unLevelRecordSize );
		}
	}

	return 0;
}

int DBParser::GetNameFrameSize(u_int* punNameFrameSize)
{
    DB_Header_t stHeaderInfo ;
	DB_NameFrame_t stNameFrame ;

	if (punNameFrameSize == 0) {
        return -1;
    }
    if((true == m_rp_db_file_install) && (!m_hDBFileHandle.is_open()))
    {
        m_hDBFileHandle.open(m_rp_db_file_path , std::ios::in|std::ios::binary);
    }
    DBFile oRpFile(&m_hDBFileHandle);
    if (!m_hDBFileHandle.is_open()) {
        return -1;
    }

    m_hDBFileHandle.seekg (0, std::ios::beg);
    if (!m_hDBFileHandle.good()) {
        return -2;
    }

    m_hDBFileHandle.read((char*)&stHeaderInfo, sizeof(stHeaderInfo)) ;
    if (stHeaderInfo.unNameFrameOffset == 0) {
        return -3;
    }
	if (m_bIsBigEnd)
	{
		stHeaderInfo.unNameFrameOffset = _V_GET_LITTLE_LONG (&stHeaderInfo.unNameFrameOffset) ;
	}
    m_hDBFileHandle.seekg(stHeaderInfo.unNameFrameOffset, std::ios::beg);
    if (!m_hDBFileHandle.good()) {
        return -2;
    }

    m_hDBFileHandle.read((char*)&stNameFrame.unFrameSize, sizeof(stNameFrame.unFrameSize)) ;
    if (stNameFrame.unFrameSize == 0) {
        return -3;
    }
	*punNameFrameSize = stNameFrame.unFrameSize ;

	if (m_bIsBigEnd)
	{
		*punNameFrameSize = _V_GET_LITTLE_LONG (punNameFrameSize) ;
	}

    return 0;
}


int DBParser::GetNameFrame (u_int unOffset,
				            u_int unFrameSize,
				            DB_NameFrame_t* pstNameFrame)
{
	u_int	unTempOffset = 0;
	u_int	unWordsCnt ;
	Byte*	pbyNameFrame = (Byte*)pstNameFrame ;
	u_int	*punIdxTable;
	u_short	*pusNameTable;

	if (unOffset == 0 || unFrameSize == 0 || pstNameFrame == NULL) {
        return -1;
    }

    if((true == m_rp_db_file_install) && (!m_hDBFileHandle.is_open()))
    {
        m_hDBFileHandle.open(m_rp_db_file_path , std::ios::in|std::ios::binary);
    }
    DBFile oRpFile(&m_hDBFileHandle);
    if (!m_hDBFileHandle.is_open()) {
        return -2;
    }

    m_hDBFileHandle.seekg (unOffset, std::ios::beg);
    if (!m_hDBFileHandle.good()) {
        return -3;
    }

    m_hDBFileHandle.read ((char*)pbyNameFrame, unFrameSize) ;
    if (pstNameFrame->unFrameSize == 0 ||
		pstNameFrame->unNameIdxCnt == 0) {
            return -4;
    }
	if (m_bIsBigEnd)
	{
		pstNameFrame->unFrameSize = _V_GET_LITTLE_LONG (&pstNameFrame->unFrameSize) ;
		pstNameFrame->unStructSize = _V_GET_LITTLE_LONG (&pstNameFrame->unStructSize) ;
		pstNameFrame->unNameIdxCnt = _V_GET_LITTLE_LONG (&pstNameFrame->unNameIdxCnt) ;
		pstNameFrame->unNameIdxSize = _V_GET_LITTLE_LONG (&pstNameFrame->unNameIdxSize) ;
	}
	unTempOffset = pstNameFrame->unStructSize;

	if (unTempOffset == 0 || unTempOffset % 4 != 0
		|| unTempOffset != (u_int)pstNameFrame->punIdxTable) {
            return -4;
    }
	punIdxTable = (u_int*)(pbyNameFrame + (u_int)pstNameFrame->punIdxTable) ;
	if (m_bIsBigEnd)
	{
		for ( u_int i = 0; i < pstNameFrame->unNameIdxCnt; ++i )
		{
			*punIdxTable = _V_GET_LITTLE_LONG(punIdxTable);
			punIdxTable = (u_int*)( (Byte*)punIdxTable + pstNameFrame->unNameIdxSize );
		}
	}

	unTempOffset += sizeof(u_int) * pstNameFrame->unNameIdxCnt ;
	unWordsCnt = unFrameSize - unTempOffset ;
	if (unWordsCnt == 0 || unWordsCnt % sizeof(u_short) != 0) {
        return -4;
    }
	unWordsCnt /= sizeof(u_short) ;
	pusNameTable = (u_short*)(pbyNameFrame + (u_int)pstNameFrame->pusNameTable);
	if (m_bIsBigEnd)
	{
		for ( u_int i = 0; i < unWordsCnt; ++i )
		{
			*pusNameTable = _V_GET_LITTLE_SHORT( pusNameTable );
			++pusNameTable;
		}
	}

	return 0;
}

int DBParser::GetDistrictFrameSize (
				u_int* punDistrictFrameSize)
{
	DB_Header_t stHeaderInfo ;
	DB_DistrictFrame_t stDistrictFrame ;

	if (punDistrictFrameSize == 0) {
        return -1;
    }
    if((true == m_rp_db_file_install) && (!m_hDBFileHandle.is_open()))
    {
        m_hDBFileHandle.open(m_rp_db_file_path , std::ios::in|std::ios::binary);
    }
    DBFile oRpFile(&m_hDBFileHandle);
    if (!m_hDBFileHandle.is_open()) {
        return -4;
    }

    m_hDBFileHandle.seekg (0, std::ios::beg);
    if (!m_hDBFileHandle.good()) {
        return -4;
    }

    m_hDBFileHandle.read ((char*)&stHeaderInfo, sizeof(stHeaderInfo));
    if (stHeaderInfo.unDistrictFrameOffset == 0) {
        return -4;
    }
	if (m_bIsBigEnd)
	{
		stHeaderInfo.unDistrictFrameOffset = _V_GET_LITTLE_LONG (&stHeaderInfo.unDistrictFrameOffset) ;
	}

    m_hDBFileHandle.seekg (stHeaderInfo.unDistrictFrameOffset, std::ios::beg);
    if (!m_hDBFileHandle.good()) {
        return -4;
    }
    m_hDBFileHandle.read ((char*)&stDistrictFrame.unFrameSize, sizeof(stDistrictFrame.unFrameSize)) ;
    if (stDistrictFrame.unFrameSize == 0) {
        return -4;
    }

	*punDistrictFrameSize = stDistrictFrame.unFrameSize;
	if (m_bIsBigEnd)
	{
		*punDistrictFrameSize = _V_GET_LITTLE_LONG (punDistrictFrameSize) ;
	}

	return 0;
}

int DBParser::GetDistrictFrame (
				u_int unOffset,
				u_int unFrameSize,
				DB_DistrictFrame_t* pstDistrictFrame)
{
	DB_DistrictRecord_t *pstDistrictTable;
	Byte*	pbyDistrictFrame = (Byte*)pstDistrictFrame ;

	if (unOffset == 0 || unFrameSize == 0 || pstDistrictFrame == NULL) {
        return  -1; 
    } 
    if((true == m_rp_db_file_install) && (!m_hDBFileHandle.is_open()))
    {
        m_hDBFileHandle.open(m_rp_db_file_path , std::ios::in|std::ios::binary);
    }
    DBFile oRpFile(&m_hDBFileHandle);
    if (!m_hDBFileHandle.is_open()) {
        return  -4;
    }

    m_hDBFileHandle.seekg (unOffset, std::ios::beg);
    if (!m_hDBFileHandle.good()) {
        return  -4;
    }

    m_hDBFileHandle.read ((char*)pbyDistrictFrame, unFrameSize) ;
    if (pstDistrictFrame->unFrameSize == 0 ||
		pstDistrictFrame->unDistrictCnt == 0) {
            return  -4;
    }

	if (m_bIsBigEnd)
	{
		pstDistrictFrame->unFrameSize = _V_GET_LITTLE_LONG (&pstDistrictFrame->unFrameSize) ;
		pstDistrictFrame->unStructSize = _V_GET_LITTLE_LONG (&pstDistrictFrame->unStructSize) ;
		pstDistrictFrame->unDistrictCnt = _V_GET_LITTLE_LONG (&pstDistrictFrame->unDistrictCnt) ;
		pstDistrictFrame->unDistrictSize = _V_GET_LITTLE_LONG (&pstDistrictFrame->unDistrictSize) ;
	}
	if( pstDistrictFrame->unStructSize == 0
		|| pstDistrictFrame->unStructSize % 4 != 0
		|| pstDistrictFrame->unStructSize != (u_int)(pstDistrictFrame->pstDistrictTable)) {
        return -4;
    }
		
	pstDistrictTable = (DB_DistrictRecord_t*)
		( pbyDistrictFrame + (u_int)(pstDistrictFrame->pstDistrictTable) );
	if (m_bIsBigEnd)
	{
		for ( u_int i = 0; i < pstDistrictFrame->unDistrictCnt; ++i )
		{
			pstDistrictTable->usCityNameIdx = _V_GET_LITTLE_SHORT( &(pstDistrictTable->usCityNameIdx) );
			pstDistrictTable->usCountyNameIdx = _V_GET_LITTLE_SHORT( &(pstDistrictTable->usCountyNameIdx) );

			pstDistrictTable = (DB_DistrictRecord_t*)
				( (Byte*)pstDistrictTable + pstDistrictFrame->unDistrictSize );
		}
	}

	return 0;
}

int DBParser::GetAdjacentNodeManager (
					u_int unMgrOffset,
					u_int unMgrSize,
					DB_AdjacentNodeMgr_t* pstAdjNodeMgr)
{

	u_int	i = 0;
	Byte*	pbtAdjNodeMgr = (Byte*)pstAdjNodeMgr ;
	DB_AdjacentNode_t *pstAdjacentNode;

	if (unMgrOffset == 0 || unMgrSize == 0 ||
		pstAdjNodeMgr == NULL) {
        return  -1;
    } 
    if((true == m_rp_db_file_install) && (!m_hDBFileHandle.is_open()))
    {
        m_hDBFileHandle.open(m_rp_db_file_path , std::ios::in|std::ios::binary);
    }
    DBFile oRpFile(&m_hDBFileHandle);
    if (!m_hDBFileHandle.is_open()) {
        return  -4;
    }

    m_hDBFileHandle.seekg(unMgrOffset, std::ios::beg);
    if (!m_hDBFileHandle.good()) {
        return  -4;
    }
    m_hDBFileHandle.read ((char*)pbtAdjNodeMgr, unMgrSize) ;
    if (!m_hDBFileHandle.good()) {
        return  -4;
    }

	if (m_bIsBigEnd)
	{
		pstAdjNodeMgr->unSize = _V_GET_LITTLE_LONG (&pstAdjNodeMgr->unSize) ;
		pstAdjNodeMgr->unStructSize = _V_GET_LITTLE_LONG (&pstAdjNodeMgr->unStructSize) ;
		pstAdjNodeMgr->unCnt = _V_GET_LITTLE_LONG (&pstAdjNodeMgr->unCnt) ;
		pstAdjNodeMgr->unRecordSize = _V_GET_LITTLE_LONG (&pstAdjNodeMgr->unRecordSize) ;
		pstAdjNodeMgr->pstAdjNodes = _V_GET_LITTLE_LONG (&pstAdjNodeMgr->pstAdjNodes) ;
	}

	pstAdjacentNode = (DB_AdjacentNode_t*)( pbtAdjNodeMgr + (u_int)(pstAdjNodeMgr->pstAdjNodes) );

	if ( m_bIsBigEnd )
	{
		for ( i = 0; i < pstAdjNodeMgr->unCnt; ++i )
		{
			pstAdjacentNode->nLongitude = _V_GET_LITTLE_LONG (
				&(pstAdjacentNode->nLongitude) );
			pstAdjacentNode->nLatitude = _V_GET_LITTLE_LONG (
				&(pstAdjacentNode->nLatitude) );
			pstAdjacentNode->stNodeID.unNodeID = _V_GET_LITTLE_LONG (
				&(pstAdjacentNode->stNodeID.unNodeID) );
			pstAdjacentNode->stNodeID.unNodeExtendID = _V_GET_LITTLE_LONG (
				&(pstAdjacentNode->stNodeID.unNodeExtendID) );

			pstAdjacentNode = (DB_AdjacentNode_t*)
				( (Byte*)pstAdjacentNode + pstAdjNodeMgr->unRecordSize );
		}
	}

	return 0;
}

int DBParser::GetCalcRegionManagerHeader (
					u_int unHeaderOffset,
				    u_int unHeaderSize,
				    DB_CalcRegion_ManagerHeader_t* pstManagerHeader)
{
	u_int	i = 0;
	Byte*	pbtCalcRegionManagerHeader = (Byte*)pstManagerHeader ;
	DB_CalcRegion_Idx_t *pstRegionIdxTable;

	if (unHeaderOffset == 0 || unHeaderSize == 0 ||
		pstManagerHeader == NULL) {
        return  -1;
    }
    if((true == m_rp_db_file_install) && (!m_hDBFileHandle.is_open()))
    {
        m_hDBFileHandle.open(m_rp_db_file_path , std::ios::in|std::ios::binary);
    }
    DBFile oRpFile(&m_hDBFileHandle);
    if (!m_hDBFileHandle.is_open()) {
        return  -4;
    }

    m_hDBFileHandle.seekg (unHeaderOffset, std::ios::beg);
    if (!m_hDBFileHandle.good()) {
        return  -4;
    }
    m_hDBFileHandle.read ((char*)pbtCalcRegionManagerHeader, unHeaderSize) ;
    if (!m_hDBFileHandle.good()) {
        return  -4;
    }

	if (m_bIsBigEnd)
	{
		pstManagerHeader->unFrameSize = _V_GET_LITTLE_LONG (&pstManagerHeader->unFrameSize) ;
		pstManagerHeader->unRankInfo = _V_GET_LITTLE_LONG (&pstManagerHeader->unRankInfo) ;
		pstManagerHeader->unRegionCnt = _V_GET_LITTLE_LONG (&pstManagerHeader->unRegionCnt) ;
		pstManagerHeader->unRegionIdxSize = _V_GET_LITTLE_LONG (&pstManagerHeader->unRegionIdxSize) ;
		pstManagerHeader->pstRegionIdxTable = _V_GET_LITTLE_LONG (&pstManagerHeader->pstRegionIdxTable) ;
	}

	pstRegionIdxTable = (DB_CalcRegion_Idx_t*)
		(pbtCalcRegionManagerHeader + (u_int)(pstManagerHeader->pstRegionIdxTable) );

	if (m_bIsBigEnd)
	{
		for (i = 0; i < pstManagerHeader->unRegionCnt ; i++)
		{
			pstRegionIdxTable->unRegionOffset = _V_GET_LITTLE_LONG (
				&(pstRegionIdxTable->unRegionOffset) );
			pstRegionIdxTable->unUnCompressedRegionOffset = _V_GET_LITTLE_LONG (
				&(pstRegionIdxTable->unUnCompressedRegionOffset) );
			pstRegionIdxTable->unRegionSize = _V_GET_LITTLE_LONG (
				&(pstRegionIdxTable->unRegionSize) );
			pstRegionIdxTable->unCompressedRegionSize = _V_GET_LITTLE_LONG (
				&(pstRegionIdxTable->unCompressedRegionSize) );

			pstRegionIdxTable = (DB_CalcRegion_Idx_t*)
				( (Byte*)pstRegionIdxTable + pstManagerHeader->unRegionIdxSize );
		}
	}

	return 0;
}

int main() {
    std::cout << "start test ..." << std::endl;
    std::string usPathFileName = "/home/tiger/cpp/fstream/dbfile/103.rp";
    DBParser dbParser;
    dbParser.Init (usPathFileName);
    u_int unHeaderSize = 0;
    u_int unNameFrameSize = 0;
    u_int unDistrictFrameSize = 0;
    std::cout << dbParser.GetHeaderSize(&unHeaderSize) << " " << unHeaderSize << std::endl;
    if (unHeaderSize > 0) {
        DB_Header_t stHeaderInfo;
        u_int size = sizeof(stHeaderInfo);
        std::cout << size << "   " << unHeaderSize << std::endl;
        unHeaderSize > size ? unHeaderSize = size : 0;
        std::cout << dbParser.GetHeader(unHeaderSize, &stHeaderInfo) << std::endl;
        std::cout << stHeaderInfo.cFormatVer << std::endl;
	    std::cout << stHeaderInfo.cDataVer << std::endl;
	    std::cout << stHeaderInfo.cAuthor << std::endl;
	    std::cout << stHeaderInfo.cDate << std::endl;
    }
    std::cout << dbParser.GetNameFrameSize(&unNameFrameSize) << " " << unNameFrameSize << std::endl;
    if (unNameFrameSize > 0) {
		DB_NameFrame_t pstNameFrame;
        //dbParser.GetNameFrame(, , );
    }
    std::cout << dbParser.GetDistrictFrameSize(&unDistrictFrameSize) << " " << unDistrictFrameSize << std::endl;
    return 0;
}

编译执行:

g++ -g -o test test.cpp 

strace -ff -o ./out  ./test 

 

1.gcount

原始代码:

 

  系统调用:

 

 结论: 

可以看到gcount是上一次读取字节数,没有对应系统api调用

2.seekg和tellg

 

 结论:

seekg和tellg对应系统调用为lseek

3.read

原始代码:

系统调用:

 结论:

read对应系统调用为read,而且可以知道即使读取4个字节,底层系统api调用是将整个文件进行读取

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/697788.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

python中如何使用正则表达式提取数据

这篇文章主要介绍了python中如何使用正则表达式提取数据问题。具有很好的参考价值&#xff0c;希望对大家有所帮助。如有错误或未考虑完全的地方&#xff0c;望不吝赐教。 正则表达式是一个特殊的字符序列&#xff0c;它能帮助你方便的检查一个字符串是否与某种模式匹配。 re…

mac m4a转mp3怎么转?

mac m4a转mp3怎么操作&#xff1f;小编之前编写了在windows系统电脑上进行音频格式转换的教程和方法&#xff0c;帮助了不少的小伙伴。最近有一个粉丝朋友向我求助&#xff0c;因为自己使用的是苹果mac电脑&#xff0c;有没有什么方法可以在上面将m4a音频格式转换成mp3。因为使…

从加密转型AI:追求可持续性发展还是盲目跟风?

很多批评者曾说&#xff0c;加密行业充斥着流行语&#xff0c;总是在追逐下一个新趋势&#xff0c;甚至会因为过度追求短期利润而忽视了可持续性发展的重要性。在大多数情况下&#xff0c;他们似乎是对的。 上周末&#xff0c;国内最早也是最大的比特币论坛巴比特宣布转型AI赛道…

ClickHouse单节点安装配置

创建目录 mkdir /opt/clickhouse 将文件复制到目录 cp /opt/clickhouse-*.rpm /opt/module/clickhouse/ 在/clickhouse目录下解压安装文件 rpm2cpio clickhouse-client-21.7.3.14-2.noarch.rpm | cpio -idmv rpm2cpio clickhouse-common-static-21.7.3.14-2.x86_64.rpm | …

OpenCV学习笔记 | 边缘检测Canny算法复现 | Python

摘要 OpenCV中的边缘检测是指在图像中检测出明显的边缘轮廓线&#xff0c;可以通过计算图像中每个像素的梯度来实现。Canny算法是一种常用的边缘检测算法&#xff0c;它主要通过连续的操作来寻找边缘&#xff0c;包括对图像去噪、计算图像梯度、非极大值抑制和双阈值处理等步骤…

第36节:cesium 下雨效果(含源码+视频)

结果示例: 完整源码: <template><div class="viewer"><vc-viewer @ready="ready" :logo="false"><!

[ JVM ] 常用参数 优化参考

基础概念回顾 JDK、JRE、JVM的关系&#xff08;JDK>JRE>JVM&#xff09; JDK JRE 开发工具 、JRE JVM 类库&#xff0c;具体关系如下图&#xff1a; JDK&#xff08;Java Development Kit&#xff09; 用于开发 Java 应用程序的软件开发工具集合&#xff0c;包括 了 …

【接口mock工具】推荐一个好用的api接口关联,接口mock,文档管理的工具-yapi

【接口mock工具】推荐一个好用的api接口关联&#xff0c;接口mock&#xff0c;文档管理的工具-yapi 能满足的功能 &#xff1a; 1. 接口mock 2. 接口文档生成 3. 接口自动化测试 4. 接口自动化测试结果可以发送到你的企业微信上面 5. 接口管理可以按照分组&#xff0c;分项…

python数据分析之利用多种机器学习方法实现文本分类、情感预测

大家好&#xff0c;我是带我去滑雪&#xff01; 文本分类是一种机器学习和自然语言处理&#xff08;NLP&#xff09;任务&#xff0c;旨在将给定的文本数据分配到预定义的类别或标签中。其目标是为文本数据提供自动分类和标注&#xff0c;使得可以根据其内容或主题进行组织、排…

初见RNN(第七次组会)

初见RNN&#xff08;第七次组会&#xff09; 序列模型、马尔可夫假设循环神经网络 序列模型、马尔可夫假设 循环神经网络

[rocketmq] 浅谈结构

rocketmq 结构 NameServer &#xff1a;几乎是无状态节点&#xff0c;可横向扩展&#xff0c;节点之间无消息同步&#xff0c;主要负责对源数据的管理&#xff0c;包括对于Topic和路由信息的管理。 每个 Broker 在启动的时候会到 NameServer 注册&#xff0c;Producer 在发送消…

【UEFI实战】UEFI图形显示(从像素到字符)

GraphicsConsoleDxe 在【UEFI实战】UEFI图形显示&#xff08;显示驱动&#xff09;中已经介绍了如何使用显卡驱动安装的GOP来进行像素级别的显示&#xff0c;本文介绍的内容是对像素的包装&#xff0c;最终变成普通字符的输出。 模块简述 本模块将原本的GOP包装成了字符输出…

MySQL数据库——主从复制优化及读写分离

目录 主从复制优化主服务器配置优化redo log&#xff08;事务日志&#xff09;的刷盘策略从服务器配置优化 搭建MySQL读写分离 主从复制优化 主服务器配置优化 vim /etc/my.cnf expire_logs_days7 #设置二进制日志文件过期时间&#xff0c;默认值为0&#xff0c;表示logs不…

Vue2生命周期

Vue2生命周期 1、概念2、生命周期过程2.1 流程图示2.2 三阶段 3、钩子函数详解3.1 beforeCreate() 创建前3.2 created()创建后3.3 beforeMount() 挂载前3.4 mounted()挂载完成3.5 beforeUpdate() 更新前3.6 updated() 更新后3.7 beforeDestroy() 销毁前3.8 destroyed() 销毁完成…

在群晖上安装运行Airflow

本文是应网友 &#xff1a; 要求折腾的&#xff1b; 什么是 Airflow &#xff1f; Apache Airflow 是一个开源平台&#xff0c;用于开发、调度和监控面向批处理的工作流。Airflow 的可扩展 Python 框架使您能够构建与几乎任何技术连接的工作流。Web 界面有助于管理工作流程的状…

RabbitMQ灵活运用,怎么理解五种消息模型

RabbitMQ灵活运用&#xff0c;怎么理解五种消息模型 简介一、AMQP协议二、交换机类型与默认交换机1. 交换机的四种类型2. 默认交换机 三、五种模式速览1. 一对一简单模式2. work模式&#xff08;轮询&#xff09;3. 发布/订阅模式4. 路由模式&#xff08;自称direct模式&#x…

Android 应用自动开启辅助(无障碍)功能并使用辅助(无障碍)功能

一.背景 由于最近的项目需要开启无障碍功能然后实现对应的功能需求,但是由于需求是需要安装后就开启辅助功能,不要在繁琐的在设置中开启辅助功能,所以需要如何在应用中开启辅助功能。 二.前提条件 将普通应用转换成系统应用,然后将系统的framework.jar包放到应用中并且可以…

vscode配置task.json和launch.json启动调试

首先说一下参考博文&#xff1a; 文章标题“VScode 调试教程 tasks.json和launch.json的设置&#xff08;超详细&#xff09;” 地址&#xff1a;https://blog.csdn.net/qq_59084325/article/details/125662393 官方文档太官方&#xff0c;其他人的文档也看过&#xff0c;单独…

微信小程序浏览docx,pdf等文件在线预览使用wx.openDocument

wx.downloadFile({ url: fileUrl,//pdf链接success(res) {wx.openDocument({ //打开文档filePath: res.tempFilePath,fileType: "pdf",//文档类型showMenu: true,success: function (res) {wx.showToast({title: 打开文档成功,})},fail: function (res) {wx.showToas…

Stable Diffusion使用“面部修复”时报TypeError: ‘NoneType‘ object is not subscriptable错

问题 Stable Diffusion使用“面部修复”时报TypeError: ‘NoneType’ object is not subscriptable错 解决方案 下载【detection_Resnet50_Final.pth】和【parsing_parsenet.pth】到【repositories\CodeFormer\weights\facelib】目录下&#xff0c;并重新运行项目即可。 ht…