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

📄 funcpbook.c

📁 本人编写的无线电话程序,给予PIC18C801设计,包括了uCOS的移植以及菜单,自己设计的拼音注入法,完整地一级汉字库,希望对大家有所帮助
💻 C
字号:
#include "includes.h"
#include "file.h"
#include "filesys.h"
extern OS_MEM			*pMemSml;
extern INT8U rom usrpri_file[];
INT8U PB_SearchInsertP(INT16U rom  * pName, PHONEBOOK * pb);

//Before call this function, the pName shall be ended with a 0x0000, except for that the length is
//the maximum value(10). All the continings in field "Name" shall be unicode
INT8U MFunc_InsertPhoneBookItem(INT16U rom * pName, INT8U NameLen, INT8U rom * pNum, INT8U NumLen, INT8U Group){
	INT8U i,Index,IndexB, IndexC, fhdl;
	PHONEBOOK * pbp;	
	PHONEBOOK_ITEM * p;
	
	if(*pName == 0 || *pName >=0xfe00) return(0xff); //the name is illegal
	if(NameLen == 0) return(0xff);
	fhdl = file_open(usrpri_file, (INT8U rom **)(&pbp));

	pbp = &(((F_UserPrivate *)pbp)->PhoneBook);
	p = &(pbp->item[0]);
	//find a empty item
	for(i=0;i<PHONEBOOKLEN;i++){
		if(p->name[0] == 0xffff) break;
		p ++;
	}
	Index = i;
	//copy the name
	NameLen = (NameLen>10)? 10:NameLen;	//The maximum length of name is 10 unicode words
	for(i=0; i<NameLen; i++){
		p->name[i] = *pName;
		pName++;
	}
	if(i < 10) p->name[i] = 0x0000; //mark the end of "name"
	
	//copy the number
	NumLen = (NumLen>24)? 24:NumLen;	//The maximum length of number is 24
	app_a2c(pNum, NumLen, &(p->number[0]));

	p->group = Group;

	//find correct position to insert the item
	IndexB = PB_SearchInsertP(p->name,pbp);
	p->previous = pbp->item[IndexB].previous;
	p->next  = IndexB;
	IndexC = pbp->item[IndexB].previous;
	pbp->item[IndexC].next = Index;
	pbp->item[IndexB].previous = Index;

	file_save(	fhdl,
				offsetof(F_UserPrivate, PhoneBook) + offsetof(PHONEBOOK, item[Index]),
				sizeof(PHONEBOOK_ITEM));

	file_save(	fhdl,
				offsetof(F_UserPrivate, PhoneBook) + offsetof(PHONEBOOK, item[IndexB]) 
							+ offsetof(PHONEBOOK_ITEM, previous),
				sizeof(INT8U));
	file_save(	fhdl,
				offsetof(F_UserPrivate, PhoneBook) + offsetof(PHONEBOOK, item[IndexC])
							+ offsetof(PHONEBOOK_ITEM, next),
				sizeof(INT8U));
}

//Functin to delete a item of phone book. Return the index of next item. if the item to be deleted is the 
//last one, return the first item of the phonebook
INT8U MFunc_DeletePhoneBookItem(INT8U Index){
	PHONEBOOK_ITEM * p;
	PHONEBOOK	*	pbp;
	INT8U 	fhdl,idxp,idxn;

	fhdl = file_open(usrpri_file, (INT8U rom **)(&pbp));
	pbp = &(((F_UserPrivate *)pbp)->PhoneBook);
	p = &(pbp->item[Index]);

	if(p->name[0] == 0xffff){//the item to be deleted is empty, there is some error
		return(pbp->head);
	}
	p->name[0] = 0xffff;	//mark the item "EMPTY"
	//save the first 2 bytes of current item's "Name" field
	file_save(	fhdl,
				offsetof(F_UserPrivate, PhoneBook.item[Index].name[0]),
				sizeof(INT16U));
	idxp = p->previous;
	idxn = p->next;

	pbp->item[idxp].next = idxn;
	pbp->item[idxn].previous = idxp;

	file_save(	fhdl,
				offsetof(F_UserPrivate, PhoneBook.item[idxp].next),
				sizeof(pbp->item[idxp].next));
	file_save(	fhdl,
				offsetof(F_UserPrivate, PhoneBook.item[idxn].previous),
				sizeof(pbp->item[idxn].previous));
	return(idxn);
}



//This function search phone book items with name pointed by pName, if can't find, return the index of item 
//that "LARGER" than the input name, if find one, return the index of the item.
//This function can also be applied in phone book polling operation
INT8U PB_SearchInsertP(INT16U rom  * pName, PHONEBOOK * pb){
	INT8U pos,Index,i;
	INT16U rom * pNameSave;
	pNameSave = pName;
	pos = 0;
	Index = pb->head;
	for(;;){
		if(pb->item[Index].name[pos] == *pName){
			pos++;
			pName++;
			if(*pName == 0x0000) return(Index);
			if(pos == 10) return(Index);
		}
		else if(pb->item[Index].name[pos] > *pName){
			return (Index);
		}
		else {
			Index = pb->item[Index].next;
			pos = 0;
			pName = pNameSave;
			if(Index >= PHONEBOOKLEN+2) return (0xff); //reach the end of table
		}
	}
}

/**********************************************************
 * To search matched item according to the number. return *
 * the pointer to the name found. If don't find matched   *
 * item in the phonebook, return "NULL_PTR"               *
 **********************************************************/
INT16U rom * PB_SearchMatchItem(INT8U rom *pnum, INT8U len){
	INT8U fhdl,idx,i,err,result;
	INT8U rom * pSml;
	INT8U rom * pSave;
	PHONEBOOK * pbp;
	INT8U rom * p;
	
	fhdl = file_open(usrpri_file, (INT8U rom **)(&pbp));
	pbp = &(((F_UserPrivate *)pbp)->PhoneBook);
	idx = pbp->head;
	pSml = OSMemGet(pMemSml, &err);
	pSave = pSml;
	if(err != OS_NO_ERR) return((INT16U rom *)NULL_PTR);

	len = app_a2c(pnum, len, pSml);
	for(;;){
		if(pbp->item[idx].name[0]!=0 && pbp->item[idx].name[0]<0xfe00){
			p = pbp->item[idx].number;
			for(i=0;i<len;i++){
				if(*pSml == *p){
					p++;
					pSml++;
				}
				else break;
			}
			if(i == len) {
				OSMemPut(pMemSml,pSave);
				return(pbp->item[idx].name);
			}
		}
		idx = pbp->item[idx].next;
		pSml = pSave;
		if(idx == pbp->tail){
			OSMemPut(pMemSml,pSave);
			return((INT16U rom *)NULL_PTR);
		}
	}
}

⌨️ 快捷键说明

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