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

📄 dict_get_word.c

📁 在mtk平台上开发的电子词典源码
💻 C
📖 第 1 页 / 共 4 页
字号:
	
	FS_Seek(fpData, -16, FS_FILE_END);
	FS_Read(fpData, &s_code_startbyte, 4, &read_byte); //读取拼音目录在文件中的开始位置
	FS_Read(fpData, &e_index_startbyte, 4, &read_byte);	//读取解释索引在文件中的开始位置
	
	FS_Seek(fpData, e_index_startbyte+8*(idiom_id-1), FS_FILE_BEGIN);
	FS_Read(fpData, &idiom_info, 8, &read_byte); //读取成语解释索引信息
	
	FS_Seek(fpData, idiom_info.index, FS_FILE_BEGIN);
	memset(idiom_explain_buf, 0, 1024);
	FS_Read(fpData, idiom_explain_buf, idiom_info.explain_len, &read_byte);
	//读取成语解释和拼音编码
	memset(idiom_explain_buf+idiom_info.explain_len,0,8);	//手机有可能多读数据,对多读部分清0
	strcpy(idiom_explain, idiom_explain_buf+idiom_info.sound_num*2);
	//获得成语解释
	ps=(short *)idiom_explain_buf;					//以短整形形式指向数据缓冲,以便获得拼音编码
	
	//通过循环获得成语全部拼音
	for(i=0; i<idiom_info.sound_num; i++)
	{
		FS_Seek(fpData, s_code_startbyte + *ps * 8, FS_FILE_BEGIN); //根据编码确定对应拼音
		//在目录中的位置
		FS_Read(fpData, pch, 8, &read_byte);						//读取拼音
		pch = strchr( pch, 0 );
		*pch = ' ';
		pch++;
		ps++;
	}
	pch--;
	*pch='\n';
	
	FS_Close(fpData);							//关闭文件
	return 1;
}
////分类词汇接口
int Get_class_word
(
	unsigned char *Input,		////输入
	unsigned char *word_list,	////联想结果
	int class_mode,		////分类号
	int page			////0.正常联想 1.下翻页 2.上翻页
)
{
	int fp=0;
	unsigned long ii=0;
	unsigned long class_number=0;
	unsigned long start_byte=0, end_byte=0;
	unsigned long start_class_id=0, end_class_id, mid_class_id=0;
	unsigned char Input_word[128];
	unsigned char get_word[128];
	int flag=0;
	unsigned char *pz=NULL;
	unsigned int read_byte=0;
	////////////////////////////////

	memset(Input_word, 0, 128);
	memcpy(Input_word, Input, strlen((char *)Input));
	if(OrderStr((char *)Input_word, ENGLISHWORDLEN_LIMIT)==0){ return 0; }	////用户输入非法

	fp = MY_OPENFILE(DISK_TYPE, "dict\\class_word.bjld", FS_READ_ONLY);
	if(fp<0){ return 0; }

	////总的分类种数, 总的单词数
	FS_Seek(fp, 0-8, FS_FILE_END);
	FS_Read(fp, &class_number, 4, &read_byte);
	if((unsigned long)class_mode > class_number-1){ FS_Close(fp); return 0; }
	////当前分类索引
	memset(&class_word_info, 0, sizeof(struct CLASS_WORD_INFO));
	FS_Seek(fp, ((unsigned long)class_mode-class_number)*sizeof(struct CLASS_WORD_INFO)-8, FS_FILE_END);
	FS_Read(fp, &class_word_info, sizeof(struct CLASS_WORD_INFO), &read_byte);
	//////////////////////////

	ii=(unsigned long)(To_lower(Input_word[0])-'a');
	start_class_id = class_word_info.alpha_index[ii];
	if(class_word_info.alpha_index[ii+1]==0){ FS_Close(fp); return 0; }
	end_class_id = class_word_info.alpha_index[ii+1]-1;
	mid_class_id = (unsigned long)((start_class_id+end_class_id)/2);

	////折半查找
	while(1)
	{
		FS_Seek(fp, mid_class_id*8, FS_FILE_BEGIN);
		FS_Read(fp, &start_byte, 4, &read_byte);
		FS_Read(fp, &end_byte, 4, &read_byte);
		Get_word_by_byte(get_word, start_byte);

		flag = xumx_stricmp(Input_word, get_word);
		if(flag==0){ break; }////正好找到
		else if(flag>0)////偏右
		{
			if(start_class_id<mid_class_id){ start_class_id = mid_class_id; }
			else if(start_class_id==mid_class_id && start_class_id<end_class_id)
			{
				mid_class_id=end_class_id;
				start_class_id=end_class_id;
				continue;
			}
		}
		else{ end_class_id = mid_class_id; }////偏左

		if(end_class_id<=start_class_id)////确定了边界
		{
			if(flag>0)////右边
			{ mid_class_id=end_class_id+1; }
			else{ mid_class_id=end_class_id; }////左边
			break;
		}
		else{ mid_class_id = (unsigned long)((start_class_id+end_class_id)/2); }
	}////while(1)

	if(page==1)////下翻页
	{
		if(flag!=0 || mid_class_id+1>=class_word_info.alpha_index[26])
		{ FS_Close(fp); return 0; }
		mid_class_id++;
	}
	else if(page==2)////上翻页
	{
		if(flag!=0 || mid_class_id<=class_word_info.alpha_index[0])
		{ FS_Close(fp); return 0; }
		if(mid_class_id<class_word_info.alpha_index[0]+GET_WORD_MAX_NUM)
		{ mid_class_id=class_word_info.alpha_index[0]; }
		else{ mid_class_id-=GET_WORD_MAX_NUM; }
	}

	ii=0;
	pz=word_list;
	while(ii<GET_WORD_MAX_NUM && mid_class_id<class_word_info.alpha_index[26])
	{
		FS_Seek(fp, mid_class_id*8, FS_FILE_BEGIN);
		FS_Read(fp, &start_byte, 4, &read_byte);
		FS_Read(fp, &end_byte, 4, &read_byte);
		Get_word_by_byte(get_word, start_byte);////根据精确的数值,直接获得单词
		////添加到列表
		start_class_id = strlen((char *)get_word);
		memcpy(pz, get_word, start_class_id);
		pz+=start_class_id; *pz=0; pz++;
		note_word_info(get_word, start_byte, end_byte);

		mid_class_id++;
		ii++;
	}////while()
	FS_Close(fp);
	memset(pz, 0, 8);

	return ((int)ii);
}
//////////////////////////////////
void Get_word_by_byte( unsigned char *word, unsigned long start)
{
	int fp=0;
	unsigned int read_byte=0;
	
	fp = MY_OPENFILE(DISK_TYPE, (char *)ENG_CHN_DATA_FILE_NAME, FS_READ_ONLY);
	if(fp<0){ return; }
	
	FS_Seek(fp, start, FS_FILE_BEGIN);
	FS_Read(fp, word, 128, &read_byte);
	delete_tab(word, 128);
	FS_Close(fp);
}
/////////////////////////////////////////

////背单词(按 ID 号产生一个单词,并得到它的解释)
int Get_WordAndExplain_by_ID
(
	unsigned char *word,	////产生的单词(至少64 byte)
	unsigned char *phonetic,		////音标
	unsigned char *explain,	////此单词的解释(至少512 byte)
	int class_mode,			////0 ~ 14, 共15种分类
	unsigned long word_ID	////单词的 ID 号
	)
{
	int fp=0;
	unsigned long class_number=0;
	unsigned long start_byte=0, end_byte=0;
	unsigned char Get_word[128];
	unsigned int byte_num=0;
	//////////////////////
	
	if(word==NULL || explain==NULL){ return 0; }
	
	fp = MY_OPENFILE(DISK_TYPE, (char *)CLASS_DATA_FILE_NAME, FS_READ_ONLY);
	if(fp<0){ return 0; }
	
	////总的分类种数, 总的单词数
	FS_Seek(fp, 0-8, FS_FILE_END);
	FS_Read(fp, &class_number, 4, &byte_num);
	///////////////////////////
	
	if((unsigned long)class_mode > class_number-1)////默认是普通词库
	{
		FS_Close(fp);
		
		fp = MY_OPENFILE(DISK_TYPE, (char *)ENG_CHN_DATA_FILE_NAME, FS_READ_ONLY);
		if(fp<0){ return 0; }
		
		////字符索引
		FS_Seek(fp, 0-4, FS_FILE_END);
		FS_Read(fp, &alpha_long, 4, &byte_num);
		////搜索索引
		FS_Seek(fp, 0-4*alpha_long-sizeof(struct WORD_AREA), FS_FILE_END);
		FS_Read(fp, Word_Area_List, sizeof(struct WORD_AREA), &byte_num);
		area_long = Word_Area_List[0].Start_ID;
		////data 索引
		FS_Seek(fp, 0-4*alpha_long-sizeof(struct WORD_AREA)*area_long-4, FS_FILE_END);
		FS_Read(fp, &data_long, 4, &byte_num);
		FS_Close(fp);
		word_ID= word_ID%(data_long-2);
		get_word_by_ID(word_ID, word);////根据ID号取单词
		get_english_word_explain(word, phonetic, explain);////取解释
		return 1;
	}////普通词库
	
	////分类词库
	////当前分类索引
	FS_Seek(fp, 0+((unsigned long)class_mode-class_number)*sizeof(struct CLASS_WORD_INFO)-8, FS_FILE_END);
	FS_Read(fp, &class_word_info, sizeof(struct CLASS_WORD_INFO), &byte_num);
	word_ID = word_ID%(class_word_info.alpha_index[26]-class_word_info.alpha_index[0]);
	FS_Seek(fp, (class_word_info.alpha_index[0]+word_ID)*8, FS_FILE_BEGIN);
	FS_Read(fp, &start_byte, 4, &byte_num);
	FS_Read(fp, &end_byte, 4, &byte_num);
	FS_Close(fp);
	
	Get_word_by_byte(Get_word, start_byte);////得到单词
	note_word_info(Get_word, start_byte, end_byte);////纪录信息
	memcpy(word, Get_word, strlen((char *)Get_word)+4);
	get_english_word_explain(word, phonetic, explain);
	return 1;
}
////该函数用来获得汉字的拼音和解释
int get_chn_explain
( 
	chn_info *s_chn_info,			//输入的汉字结构信息
	char *chn_sound1,				//输出的字母拼音
	char *chn_sound2,				//输出的汉语拼音
	char *explain					//输出的汉字解释
)
{
	int fpData=0; //数据文件指针
	char *strBuf_explain=idiom_list_buf; //解释缓冲
	int i=0, kk=0;
	short sound_index[16]; //拼音编码
	long sound_code1_index=0; //字母拼音目录在文件中的开始位置
	long sound_code2_index=0; //汉字拼音目录在文件中的开始位置
	char *ps1=chn_sound1,*ps2=chn_sound2;
	unsigned int read_byte=0;

	char text_buf[128];
	char *pt1=NULL, *pt2=NULL;
	unsigned int explain_cur=0;
	unsigned int text_len=0;
	char *sound_pt1=chn_sound1, *sound_pt2=chn_sound2;
	//////////////////////////////////////

	//判断参数是否为空指针
	if(chn_sound1==NULL || chn_sound2==NULL || explain==NULL || s_chn_info==NULL )
	{
		return 0;
	}

	fpData = MY_OPENFILE(DISK_TYPE, "dict\\chinese_data.bjld", FS_READ_ONLY);
	if(fpData<0){ return 0; }

	FS_Seek(fpData, s_chn_info->index, FS_FILE_BEGIN);
	FS_Read(fpData, strBuf_explain, s_chn_info->data_len, &read_byte);
											//根据汉字结构信息,读取汉字解释和拼音编码数据
	memset(strBuf_explain+s_chn_info->data_len,0,4);//手机有可能多读数据,对多读部分清0
	memcpy((char *)sound_index, strBuf_explain, s_chn_info->sound_num*2);
											//从数据缓冲中获得拼音编码
	//memcpy(explain, strBuf_explain+s_chn_info->sound_num*2, 
				//s_chn_info->data_len-s_chn_info->sound_num*2);//从数据缓冲中获得解释
	FS_Seek(fpData, 0-4*4, FS_FILE_END);
	FS_Read(fpData, &sound_code1_index, 4, &read_byte);	//读取字母拼音目录在文件中的开始位置
	FS_Read(fpData, &sound_code2_index, 4, &read_byte);	//读取汉字拼音目录在文件中的开始位置

	//得到汉字所有拼音
	for(i=0;i<s_chn_info->sound_num;i++)
	{
		FS_Seek(fpData, sound_code1_index+sound_index[i]*8, FS_FILE_BEGIN);//确定编码所对应字母拼音在文件中位置
		FS_Read(fpData, ps1, 8, &read_byte);				//读取字母拼音
		ps1+=8;
		FS_Seek(fpData, sound_code2_index+sound_index[i]*8, FS_FILE_BEGIN);//确定编码所对应汉字拼音在文件中位置
		FS_Read(fpData, ps2, 8, &read_byte);				//读取汉字拼音
		ps2+=8;
	}

	FS_Close(fpData);//关闭文件

	////整理解释
	explain_cur=0;
	//部首
	memcpy(explain, "部首: ", 6);
	memcpy(explain+6, s_chn_info->part, 2);
	memcpy(explain+8, "\n", 1);
	explain_cur = 9;
	//笔画数
	memset(text_buf, 0, 64);
	sprintf(text_buf, "笔画数: %ld\n", s_chn_info->strokes);
	text_len = strlen(text_buf);
	memcpy(explain+explain_cur, text_buf, text_len);
	explain_cur += text_len;
	//拼音及解释
	pt1=strBuf_explain+s_chn_info->sound_num*2;
	pt2=pt1;
	for(i=0;i<s_chn_info->sound_num;i++)
	{
		//拼音
		memset(text_buf, 0, 64);
		sprintf(text_buf, "%s\n%s\n", sound_pt1, sound_pt2);
		text_len = strlen(text_buf);
		memcpy(explain+explain_cur, text_buf, text_len);
		explain_cur += text_len;

		sound_pt1+=8;
		sound_pt2+=8;

		//解释
		kk=1;
		while(*pt1!=0 && *pt1!='\n')
		{
			pt2=pt1;
			while(*pt2!=0 && *pt2!='\n'){ pt2++; }
			if(*pt2=='\n'){ pt2++; }

			memset(text_buf, 0, 32);
			sprintf(text_buf, "%d.", kk);
			text_len = strlen(text_buf);
			memcpy(explain+explain_cur, text_buf, text_len);
			explain_cur += text_len;

			memcpy(explain+explain_cur, pt1, (unsigned int)(pt2-pt1));
			explain_cur += (unsigned int)(pt2-pt1);

			pt1=pt2;
			kk++;
		}////while()

		if(*pt1=='\n'){ pt1++; }
	}////for(i)

	////////////
	return 1;
}
////该函数用来获得汉字的结构信息,输入汉字--input,输出汉字结构信息.
int get_chn_info(char *input, pchn_info ps_chn_info)
{
	int fpData=0; //数据文件指针
	long chn_num=0;  //汉字总数
	long num_count=0; //个数计算
//	chn_info chinese_info[256]; //汉字结构信息数组,及每次读取数据信息缓冲
	int i=0;
	unsigned int read_byte=0;
	////////////////////////////////////////
	
	//判断输入输出是否为空指针,如为空结束函数
	if( input == NULL || ps_chn_info == NULL )
	{
		return 0;
	}
	
	fpData = MY_OPENFILE(DISK_TYPE, "dict\\chinese_data.bjld", FS_READ_ONLY);
	if(fpData<0){ return 0; }
	FS_Seek(fpData, 0-4, FS_FILE_END);
	FS_Read(fpData, &chn_num, 4, &read_byte);	//读取汉字总个数
	FS_Seek(fpData, 0-(16+chn_num*12), FS_FILE_END);
	
	//该循环用来获得该汉字结构信息
	while( chn_num > num_count )
	{
		memset(chinese_info,0,3072);
		FS_Read(fpData, chinese_info, 3072, &read_byte);	//读取256个汉字结构信息
		//在256个结构信息中查找
		for(i=0; i<256 && chn_num > num_count; i++)
		{
			//判断是否为要查找的汉字信息
			if(memcmp( input, chinese_info[i].word ,2) == 0)
			{
				*ps_chn_info = chinese_info[i];		//如找到,返回汉字信息
				FS_Close(fpData);		//关闭文件
				return 1;							//结束该函数
			}
			++num_count;
		}	
	}
	
	FS_Close(fpData);					//如没找到,关闭数据文件
	return 0;
}
#endif

⌨️ 快捷键说明

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