⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 table_dbase.cpp

📁 这是一个GPS相关的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
		//-------------------------------------------------
		// Initializations...

		fseek(hFile, 0, SEEK_SET);


		//-------------------------------------------------
		// Bytes 0-31: File Header...

		fread(&FileType			, sizeof(char),  1, hFile);	// 00		FoxBase+, FoxPro, dBaseIII+, dBaseIV, no memo	- 0x03
															//			FoxBase+, dBaseIII+ with memo					- 0x83
															//			FoxPro with memo								- 0xF5
															//			dBaseIV with memo								- 0x8B
															//			dBaseIV with SQL Table							- 0x8E
		fread(&LastUpdate		, sizeof(char),  3, hFile);	// 01-03	Last update, format YYYYMMDD   **correction: it is YYMMDD**
		fread(&nRecords			, sizeof(char),  4, hFile);	// 04-07	Number of records in file (32-bit number)
		fread(&nHeaderBytes		, sizeof(char),  2, hFile);	// 08-09	Number of bytes in header (16-bit number)
		fread(&nRecordBytes		, sizeof(char),  2, hFile);	// 10-11	Number of bytes in record (16-bit number)
		fread( buf				, sizeof(char),  2, hFile);	// 12-13	Reserved, fill with 0x00
		fread(&Transaction		, sizeof(char),  1, hFile);	// 14		dBaseIV flag, incomplete transaction
															//			Begin Transaction sets it to					- 0x01
															//			End Transaction or RollBack reset it to			- 0x00
		fread(&bEncrypted		, sizeof(char),  1, hFile);	// 15		Encryption flag, encrypted 0x01 else 0x00
															//			Changing the flag does not encrypt or decrypt the records
		fread( buf				, sizeof(char), 12, hFile);	// 16-27	dBaseIV multi-user environment use
		fread(&ProductionIdx	, sizeof(char),  1, hFile);	// 28		Production index exists - 0x01 else 0x00
		fread(&LanguageDrvID	, sizeof(char),  1, hFile);	// 29		dBaseIV language driver ID
		fread( buf				, sizeof(char),  2, hFile);	// 30-31	Reserved fill with 0x00


		//-------------------------------------------------
		// Bytes 32-n: Field Descriptor Array...

		while(	ftell(hFile) < (long)nHeaderBytes - 1 && !feof(hFile) )
		{
			FieldDesc	= (TFieldDesc *)SG_Realloc(FieldDesc, (nFields + 1) * sizeof(TFieldDesc));
			FieldDesc[nFields].Name[12]	= '\0';

			fread( FieldDesc[nFields].Name			, sizeof(char), 11, hFile);	// 0-10		Field Name ASCII padded with 0x00
			fread(&FieldDesc[nFields].Type			, sizeof(char),  1, hFile);	// 11		Field Type Identifier (see table)
			fread(&FieldDesc[nFields].Displacement	, sizeof(char),  4, hFile);	// 12-15	Displacement of field in record
			fread(&FieldDesc[nFields].Width			, sizeof(char),  1, hFile);	// 16		Field length in bytes
			fread(&FieldDesc[nFields].Decimals		, sizeof(char),  1, hFile);	// 17		Field decimal places
			fread( buf								, sizeof(char),  2, hFile);	// 18-19	Reserved
			fread(&FieldDesc[nFields].WorkAreaID	, sizeof(char),  1, hFile);	// 20		dBaseIV work area ID
			fread( buf								, sizeof(char), 10, hFile);	// 21-30	Reserved
			fread(&FieldDesc[nFields].ProductionIdx	, sizeof(char),  1, hFile);	// 31	 	Field is part of production index - 0x01 else 0x00

			nFields++;
		}


		//-------------------------------------------------
		// Byte n+1: Header Record Terminator (0x0D)...
		fread( buf				, sizeof(char),  1, hFile);

		if( buf[0] == 0x0d )
		{
			Init_Record();
			Move_First();

			Result	= true;
		}
	}

	//-----------------------------------------------------
	if( !Result )
	{
		fclose(hFile);
		hFile	= NULL;
		bOpen	= false;
		Close();
	}

	return( Result );
}

//---------------------------------------------------------
void CSG_Table_DBase::Init_Record(void)
{
	int		iField, iPos;

	Record		= (char *)SG_Realloc(Record		, nRecordBytes	* sizeof(char));
	FieldOffset	= (int  *)SG_Realloc(FieldOffset	, nFields		* sizeof(int )); 

	for(iField=0, iPos=1; iField<nFields; iField++)
	{
		FieldOffset[iField]	= iPos;
		iPos	+= FieldDesc[iField].Width;
	}
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
int CSG_Table_DBase::Get_File_Position(void)
{
	return( hFile ? ftell(hFile) : 0 );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CSG_Table_DBase::Move_First(void)
{
	bool	Result	= false;

	if( bOpen )
	{
		Flush_Record();

		fseek(hFile, nHeaderBytes, SEEK_SET);

		if( fread(Record, nRecordBytes, sizeof(char), hFile) == 1 )
		{
			Result	= true;
		}

		fseek(hFile, nHeaderBytes, SEEK_SET);
	}

	return( Result );
}

//---------------------------------------------------------
bool CSG_Table_DBase::Move_Next(void)
{
	bool	Result	= false;

	if( bOpen )
	{
		Flush_Record();

		fseek(hFile, nRecordBytes, SEEK_CUR);

		if( fread(Record, nRecordBytes, sizeof(char), hFile) == 1 )
		{
			Result	= true;
		}

		fseek(hFile, -nRecordBytes, SEEK_CUR);
	}

	return( Result );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
void CSG_Table_DBase::Add_Record(void)
{
	if( bOpen )
	{
		bRecModified	= true;

		memset(Record, 0, nRecordBytes);

		fseek(hFile, 0, SEEK_END);
		fwrite(Record, nRecordBytes, sizeof(char), hFile);
		fseek(hFile, -nRecordBytes, SEEK_END);

		nRecords++;

		nFileBytes		+= nRecordBytes;
	}
}

//---------------------------------------------------------
void CSG_Table_DBase::Flush_Record(void)
{
	if( bOpen && !bReadOnly && bRecModified )
	{
		bRecModified	= false;
		fwrite(Record, nRecordBytes, sizeof(char), hFile);
		fseek(hFile, -nRecordBytes, SEEK_CUR);
	}
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
int CSG_Table_DBase::asInt(int iField)
{
	char	*s;
	int		Result	= 0;

	if( bOpen && iField >= 0 && iField < nFields )
	{
		if( FieldDesc[iField].Type == DBF_FT_NUMERIC )
		{
			s		= (char *)SG_Calloc(FieldDesc[iField].Width + 1, sizeof(char));
			memcpy(s, Record + FieldOffset[iField], FieldDesc[iField].Width);
			Result	= atoi(s);
			SG_Free(s);
		}
	}

	return( Result );
}

//---------------------------------------------------------
double CSG_Table_DBase::asDouble(int iField)
{
	char	*s;
	double	Result	= 0;

	if( bOpen && iField >= 0 && iField < nFields )
	{
		if( FieldDesc[iField].Type == DBF_FT_NUMERIC )
		{
			s		= (char *)SG_Calloc(FieldDesc[iField].Width + 1, sizeof(char));
			memcpy(s, Record + FieldOffset[iField], FieldDesc[iField].Width);
			Result	= atof(s);
			SG_Free(s);
		}
	}

	return( Result );
}

//---------------------------------------------------------
char * CSG_Table_DBase::asString(int iField)
{
	int		i;

	if( bOpen && iField >= 0 && iField < nFields )
	{
		i					= FieldDesc[iField].Width;
		Result_String		= (char *)SG_Realloc(Result_String, (i + 1) * sizeof(char));
		memcpy(Result_String, Record + FieldOffset[iField], FieldDesc[iField].Width);

		Result_String[i--]	= '\0';

		while( i >= 0 && Result_String[i] == ' ' )
		{
			Result_String[i--]	= '\0';
		}

		return( Result_String );
	}

	return( "" );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CSG_Table_DBase::Set_Value(int iField, double Value)
{
	static char	s[256]	= "";

	int		n;

	if( bOpen && iField >= 0 && iField < nFields && FieldDesc[iField].Width > 0 )
	{
		if( FieldDesc[iField].Type == DBF_FT_NUMERIC )
		{
			bRecModified	= true;

			if( FieldDesc[iField].Decimals > 0 )
			{
				sprintf(s, "%.*f", FieldDesc[iField].Decimals, Value);
			}
			else
			{
				sprintf(s, "%d", (int)Value);
			}

			if( (n = strlen(s)) > FieldDesc[iField].Width )
			{
				n	= FieldDesc[iField].Width;
			}

			memset(Record + FieldOffset[iField], ' ', FieldDesc[iField].Width);
			memcpy(Record + FieldOffset[iField], s	, n);

			return( true );
		}
	}

	return( false );
}

//---------------------------------------------------------
bool CSG_Table_DBase::Set_Value(int iField, const char *Value)
{
	int		n;

	if( bOpen && iField >= 0 && iField < nFields && FieldDesc[iField].Width > 0 )
	{
		if( FieldDesc[iField].Type == DBF_FT_CHARACTER && Value )
		{
			bRecModified	= true;

			if( (n = strlen(Value)) > FieldDesc[iField].Width )
			{
				n	= FieldDesc[iField].Width;
			}

			memset(Record + FieldOffset[iField], ' '	, FieldDesc[iField].Width);
			memcpy(Record + FieldOffset[iField], Value	, n);

			return( true );
		}
	}

	return( false );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -