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

📄 atbpbfs.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 2 页
字号:
*******************************************************************************/


PB_RET	FS_pb_WriteRec(SHORT phonebook_id, SHORT phys_index, T_PB_RECORD *record)
{
	SHORT charIndex;
	UBYTE *recordstore;
	SHORT recIndex;
	USHORT *unicode;
	T_FFS_SIZE size;

	/* Make sure record exists */

	if (phys_index>=fsdata.records_max)
		return PB_RECDOESNOTEXIST;

	/* Copy alpha tag */

	recordstore = (UBYTE *)mfwAlloc(fsdata.record_size);

	unicode = (USHORT *)recordstore;

	for (charIndex=0; charIndex<record->alpha.length; charIndex++)
	{
		unicode[charIndex] = record->alpha.data[charIndex];
	}

	unicode[charIndex] = 0;

	/* Copy number */

	memcpy(&recordstore[fsdata.alpha_max*sizeof(USHORT)], record->number, fsdata.number_max/2);

	/* Copy ton/npi */

	recordstore[fsdata.alpha_max*sizeof(USHORT)+fsdata.number_max/2] = record->ton_npi;

	/* Open the file */

	recIndex = FS_file_OpenForRec(phonebook_id, phys_index);

	/* Find the record in the file */

	FFS_seek(fsdata.file, fsdata.record_size*recIndex, FFS_SEEK_SET);

	/* Write record */

	size = FFS_write(fsdata.file, (void *)recordstore, fsdata.record_size);

	mfwFree(recordstore, fsdata.record_size);

#ifdef BLOCKING
	return PB_OK;
#else
	return PB_EXCT;
#endif

}


/*******************************************************************************

 $Function:    	FS_pb_DeleteRec

 $Description:	Deletes a record at a physical position index.

 $Returns:		PB_OK			Action completed OK.
				PB_EXCT			Action currently executing, callback awaited.
				PB_FILEWRITEFAIL File write encountered an error

 $Arguments:	phonebook_id	The phonebook identifier
				phys_index		Physical index of the record to delete.

*******************************************************************************/

PB_RET	FS_pb_DeleteRec(SHORT phonebook_id, SHORT phys_index)
{
	UBYTE *recordstore;
	char filename[30];
	SHORT fileIndex;
	SHORT recIndex;
	T_FFS_RET ffsret;
	T_FFS_SIZE size;

	/* Make sure record exists */

	if (phys_index>=fsdata.records_max)
		return PB_RECDOESNOTEXIST;

	/* Allocate null buffer */

	recordstore = (UBYTE *)mfwAlloc(fsdata.record_size);
	memset(recordstore, 0xFF, fsdata.record_size);

	/* Open the file */

	recIndex = FS_file_OpenForRec(phonebook_id, phys_index);

	/* Find the record in the file */

	FFS_seek(fsdata.file, fsdata.record_size*recIndex, FFS_SEEK_SET);

	/* Write record */

	size = FFS_write(fsdata.file, (void *)recordstore, fsdata.record_size);

	mfwFree(recordstore, fsdata.record_size);

#ifdef BLOCKING
	return PB_OK;
#else
	return PB_EXCT;
#endif
}


/*******************************************************************************

 $Function:    	FS_pb_Finished

 $Description:	Operation is over; close the last file.

 $Returns:		PB_OK		Action completed OK.
				PB_ERROR	Error.

 $Arguments:	phonebook_id	The phonebook identifier


*******************************************************************************/

PB_RET	FS_pb_Finished(SHORT phonebook_id)
{
	T_FFS_RET ret;

	if (fsdata.fileID!=-1 && fsdata.file)
	{
		ret = FFS_close(fsdata.file);
	}

	fsdata.file = NULL;
	fsdata.fileID = -1;

	return PB_OK;
}


/*******************************************************************************

 $Function:    	FS_pb_ReadTables

 $Description:	Read in index table files

 $Returns:		PB_OK		Action completed OK.
				PB_ERROR	Error.

 $Arguments:	phonebook_id	The phonebook identifier
				records_used	Number of entries in phonebook
				name_table		Table of entries ordered by name
				number_table	Table of entries ordered by number

*******************************************************************************/

PB_RET	FS_pb_ReadTables(SHORT phonebook_id, SHORT *records_used, SHORT *name_table, SHORT *number_table)
{
	T_FFS_RET ret;
	T_FFS_FD file;
	T_FFS_SIZE size;

	UBYTE writing;

	tracefunction("FS_pb_ReadTables");

	/* Check if file wrote OK */

	writing = 2;	/* Dummy value to check for errors */
	size = FFS_file_read("/mmi/pbverify", (void *)&writing, sizeof(UBYTE));
	trace_P1("Try to read file /mmi/pbverify, size = %d", size);

	if (size<0)
	{
		ret = FFS_file_write("/mmi/pbverify", (void *)&writing, sizeof(UBYTE), FFS_O_CREATE);
		trace_P1("Try to create file /mmi/pbverify, ret = %d", ret);
	}

	trace_P1("Value of writing = %d", writing);

	if (writing==TRUE)		/* Operation was aborted while reading tables file */
	{
		trace("***ERROR - tables not written properly");
		return PB_FILEREADFAIL;
	}

	/* Read in tables file */

	file = FFS_open("/mmi/pbtables", FFS_O_RDWR);
	trace_P1("Try to open file /mmi/pbtables, result %d", file);

	if (file<0)
	{
		return PB_FILEREADFAIL;
	}

	FFS_read(file, (void *)records_used, sizeof(SHORT));
	FFS_read(file, (void *)name_table, fsdata.records_max*sizeof(SHORT));
	FFS_read(file, (void *)number_table, fsdata.records_max*sizeof(SHORT));

	FFS_close(file);

	return PB_OK;
}


/*******************************************************************************

 $Function:    	FS_pb_WriteTables

 $Description:	Update index table files

 $Returns:		PB_OK		Action completed OK.
				PB_FILEWRITEFAIL

 $Arguments:	phonebook_id	The phonebook identifier
				records_used	Number of entries in phonebook
				name_table		Table of entries ordered by name
				number_table	Table of entries ordered by number

*******************************************************************************/

PB_RET	FS_pb_WriteTables(SHORT phonebook_id, SHORT records_used, SHORT *name_table, SHORT *number_table)
{
	T_FFS_RET ret;
	T_FFS_FD file;
	UBYTE writing;

	tracefunction("FS_pb_WriteTables");

	/* Indicate that file writing is in progress */

	writing = TRUE;
	ret = FFS_file_write("/mmi/pbverify", (void *)&writing, sizeof(UBYTE), FFS_O_TRUNC);
	trace_P1("1. Try to write file /mmi/pbverify, result = %d", ret);

	/* Update tables file */

	file = FFS_open("/mmi/pbtables", FFS_O_RDWR);
	trace_P1("Try to open file /mmi/pbtables, result %d", file);

	if (file<0)
	{
		file = FFS_open("/mmi/pbtables", FFS_O_RDWR | FFS_O_CREATE);
		trace_P1("Create file /mmi/pbtables, result %d", file);
		if (file<0)
		{
			trace("** Cannot create file - flash phonebook not available **");
			return PB_FILEWRITEFAIL;
		}
	}

	FFS_write(file, (void *)&records_used, sizeof(SHORT));
	FFS_write(file, (void *)name_table, fsdata.records_max*sizeof(SHORT));
	FFS_write(file, (void *)number_table, fsdata.records_max*sizeof(SHORT));

	FFS_close(file);

	/* Indicate that file was written OK */

	writing = FALSE;
	ret = FFS_file_write("/mmi/pbverify", (void *)&writing, sizeof(UBYTE), FFS_O_TRUNC);
	trace_P1("2. Try to write file /mmi/pbverify, result = %d", ret);

	return PB_OK;
}


/*******************************************************************************

 $Function:    	FS_file_GetIndex

 $Description:	For a given physical index, return the file and record number.

 $Returns:		None

 $Arguments:	phys_index		The physical record index
 				fileIndex		Pointer to variable to store file index
 				recIndex		Pointer to variable to store record index


*******************************************************************************/

void FS_file_GetIndex(SHORT phys_index, SHORT *fileIndex, SHORT *recIndex)
{
	*fileIndex = phys_index/PB_RECS_PER_FILE;
	*recIndex = phys_index % PB_RECS_PER_FILE;
	return;
}

/*******************************************************************************

 $Function:    	FS_file_GetName

 $Description:	For a given file index, return the filename.

 $Returns:		None

 $Arguments:	filename		Pointer to string to store the filename
 				phonebook_id	The identifier of the phonebook
 				fileIndex		The index of the file


*******************************************************************************/

void FS_file_GetName(char *filename, SHORT phonebook_id, SHORT fileIndex)
{
	sprintf(filename, "/mmi/pb%d", fileIndex);
	return;
}

/*******************************************************************************

 $Function:    	FS_file_OpenForRec

 $Description:	Open the appropriate file to access a specific record

 $Returns:		None

 $Arguments:	phonebook_id	The phonebook identifier
 				phys_index		The physical record index


*******************************************************************************/

SHORT FS_file_OpenForRec(SHORT phonebook_id, SHORT phys_index)
{
	SHORT fileIndex;
	SHORT recIndex;
	char filename[30];
	T_FFS_RET ffsret;

	/* Open file if it is not already open */

	FS_file_GetIndex(phys_index, &fileIndex, &recIndex);

	if (fsdata.fileID!=fileIndex || fsdata.file==NULL)
	{
		/* Close currently open file */
		if (fsdata.file!=NULL)
		{
			ffsret = FFS_close(fsdata.file);
			fsdata.file = NULL;
			fsdata.fileID = -1;
		}

		FS_file_GetName(filename, phonebook_id, fileIndex);

		fsdata.file = FFS_open(filename, FFS_O_RDWR);
		trace_P2("Try to open file %s, result %d",filename, fsdata.file);
		fsdata.fileID = fileIndex;
	}

	return recIndex;
}

⌨️ 快捷键说明

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