CASIO DT930程序编写(例子程序头文件)

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

       

#include "itron.h"

#include "cmndef.h"

#include "bios1mac.h"

#include  "COM_APLM.H"

#include "clce800.h"



/* 定义的功能键值*/

#define		BS_KEY		8

#define		CLR_KEY		12

#define		ENT_KEY		13

#define		F1_KEY		45

#define		F2_KEY		29

#define		F3_KEY		28

#define		F4_KEY		16

#define		F5_KEY		32

#define		F6_KEY		26

#define		F7_KEY		27



#define		ENTERNEWLINE		"/x0d/x0a"



#define		PASSFILE	"pass.txt"



/* 对文件操作的宏定义 */

#define		E_NOFILE		1	/* 没有这个文件 */

#define		E_NOCOM			2	/* COM打开错误 */

#define		E_NOOPEN		3	/* 文件打开错误 */

#define		E_PWERR			4	/* 密码错误 */

/*  */

#define		COM_OPEN_ERR	1	/* COM口无法打开 */

#define		FILE_SEND_ERR	2	/* 文件发送失败 */

#define		FILE_REC_ERR	3	/* 文件接收失败 */



#define		E_NOFIND		1



H COM = COM0;

DAT_PWR_STR	pwr_str;		/* 这个定义为全局变量 */

CU_GRAPHSET tfGraphSet =

{

	CU_GRAPH_ON_1,

	5,

	1,

	CU_GRAPH_NM_FILE,

	3

};



/* 函数声明区 */

int CheckSN(void);

void display(B *DspStrin, H x, H y);		/* 正常显示 */

void display2(B *DspString, H x, H y);		/*  */

void display3(B *DspString, H x, H y);		/* 粗体显示 */

/*int getkey(void);	*/						/* 获得一个键 */

int getstring(int X, int Y, int iMix, int iMax, char *string);

int GetInt(int x, int y, int len, char *intstr, int type, int *iIsAdd);/*得到一个整数*/

int GetDouble(int x, int y, int len, char *doustr, int type, int *IsAdd);/*得到一个实数*/

void AddSpace(char *sStr, int iStrLen);

void DecSpace(char *sStr, int iStrLen);

void GetDate(char *sDate);



/* 文件传送 */

ER Com_open(void);

ER SendFile(B *srcName, B *tarDir);

ER RecvFile(B *srcName, B *tarDir);



/* 折半查找 */

ER binary_search(int fname, W pos, int fieldsize, int keypos, int keylen, B *code, B *sdata, W *lno);

/* 顺序查找 */

ER order_search(int fname, W pos, int fieldsize, int keypos, int keylen, B *code, B *sdata, W* lno);





void display(B *DspString, H x, H y)

{

	lcd_csr_put(y, x);

	/* lcd_string( dsp_ank, dsp_mode, (UB *)data, LCD_LF_OFF ); */

	cputstr(LCD_ATTR_NORMAL, (UB*)DspString);

	/* cputstr800(dsp_mode,(UB *)data); */

}



void display2(B *DspString, H x, H y)

{

	lcd_csr_put(y, x);

	cputstr(LCD_ATTR_REVERS, (UB*)DspString);

} 



void display3(B *DspString, H x, H y)

{

	lcd_csr_put(y, x);

	cputstr(LCD_ATTR_WIDTH, (UB*)DspString);

}



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

	函数说明:	检查SN号是否合法

	返 回 值:

		0		SN号检验不合法

		1		SN号检验合法

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

int CheckSN()

{

	if (!((dat_dealer_chk((UB*)"101105")==E_OK) || (dat_dealer_chk((UB*)"365331")==E_OK)))

		return 1;

	else

		return 0;

}



int getstring(int X, int Y, int iMix, int iMax, char *string)

{

	#ifndef	INPUTALL

	#define	INPUTALL	0

	return(read_str(X, Y, INPUTALL, string, iMax));

	#endif

}



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

	函数说明:	得到系统日期

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

void GetDate(char *sDate)

{

	struct day_tabl My_day;

	s_dateget(&My_day);

   	memset(sDate, 0x0, 11); 

   	sprintf(sDate, "%04.04d-%02.02d-%02.02d", My_day.year, My_day.month, My_day.day);

}



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

	函数说明:	获取一个整数

	变    量:

	[x,y]		x,y的坐标值

	[len]		获取的数值长度

	[doustr]	将获取的数值保存到该字段

	[type]		设置正、负号

		0	没有符号标志

		1	有符号标志

	[iIsAdd]	返回选择的正、负号

		0	返回负数

		1	返回正数

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

int GetInt(int x, int y, int len, char *intstr, int type, int *iIsAdd)

{

	int  key, iIndex, ret;

	char tempint[20];

	iIndex = 0;

	ret = 0;

	memset(tempint, 0x0, 20);

	strcpy(tempint, intstr);

	*iIsAdd = 1;

	if (type==1)

		display("+", x, y);

	if (type==1)

		display(tempint, x+1, y);

	else

		display(tempint, x, y);

	while(1)

	{

		key = getkey();

		if (key==0x2D)

		{

			if (*iIsAdd==1)

				*iIsAdd = 0;

			else

				*iIsAdd = 1;

		}

		if (type==1)

		{

			if (*iIsAdd==1)

				display("+", x, y);

			else

				display("-", x, y);

		}

		if ((key==BS_KEY) && (iIndex>0))

		{

			tempint[iIndex] = 0x0;

			iIndex--;

			tempint[iIndex] = ' ';

		}

		if (key==CLR_KEY)

		{

			iIndex++;

			tempint[0] = '0';

			tempint[1] = 0x0;

			ret = -1;

			break;

		}

		if (key==ENT_KEY)

		{

			ret = 1;

			break;

		}

		if ((key>=0x30) && (key<=0x39) && (iIndex<len))

		{

			tempint[iIndex] = key;

			iIndex++;

		}

		if (type==1)

			display(tempint, x+1, y);

		else

			display(tempint, x, y);

	}

	strcpy(intstr, tempint);



	if (ret==1)

		return(ENT_KEY);

	else

		return(CLR_KEY);

}



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

	函数说明:	获取一个实数

	变    量:

	[x,y]		x,y的坐标值

	[len]		获取的数值长度

	[doustr]	将获取的数值保存到该字段

	[type]		设置正、负号

		0	没有符号标志

		1	有符号标志

	[iIsAdd]	返回选择的正、负号

		0	返回负数

		1	返回正数

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

int GetDouble(int x, int y, int len, char *doustr, int type, int *iIsAdd)

{

	int  key, iIndex, ret, idian;

	char tempdouble[20];

	iIndex = 0;

	ret = 0;

	idian = 0;

	memset(tempdouble, 0x0, 20);

	strcpy(tempdouble, doustr);

	*iIsAdd = 1;

	if (type==1) 

		display("+", x, y);

	if (type==1)

		display(tempdouble, x+1, y);

	else

		display(tempdouble, x, y);

	while(1)

	{

		key = getkey();

		if (key==0x2D)	/* F1 */

		{

			if (*iIsAdd==1)

				*iIsAdd = 0;

			else

				*iIsAdd = 1;

		}

		if (type==1)

		{

			if (*iIsAdd==1)

				display("+", x, y);

			else

				display("-", x, y);

	   	}

		if ((key==BS_KEY) && (iIndex>0))

		{

			tempdouble[iIndex] = 0x0;

			iIndex--;

			if (tempdouble[iIndex]==46)

				idian = 0;

			tempdouble[iIndex] = ' ';

		}

		if (key==CLR_KEY)

		{

			iIndex++;

			tempdouble[0] = '0';

			tempdouble[1] = 0x0;

			ret = -1;

			break;

		}

		if (key==ENT_KEY)

		{

			ret = 1;

			break;

		}

		if ((key>=0x30) && (key<=0x39) && (iIndex<len))

		{

			tempdouble[iIndex] = key;

			iIndex++;

		}

		if ((key==46) && (idian==0) && (iIndex<len))

		{

			tempdouble[iIndex] = key;

			idian = 1;

			iIndex++;

		}

		if (type==1)

			display(tempdouble, x+1, y);

		else

			display(tempdouble, x, y);

	}	/* end while */

	strcpy(doustr, tempdouble);

	

	if (ret==1)

		return(ENT_KEY);

	else

		return(CLR_KEY);	

}



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

	函数说明:		用空格填充字符串

	参数说明:

	sStr:			需要添加空格的字符串

	iStrLen:		定义字符串的长度

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

void AddSpace(char *sStr, int iStrLen)

{

	int i, j;

	i = strlen(sStr);

	if (i>iStrLen)

	{

		sStr[iStrLen] = 0x0;

	}

	else

	{

		for(j=i; j<iStrLen-1; j++)

			sStr[j] = ' ';

  		sStr[j] = 0x0;

	}

}



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

	函数说明:		去掉字符串尾部的空格

	参数说明:

	sStr:			需要去掉空格的字符串

	iStrLen:		定义字符串的长度

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

void DecSpace(char *sStr, int iStrLen)

{

	int i;

	for (i=iStrLen-2; i>0; i--)

	{

		if (sStr[i] != ' ')

		{

			sStr[++i] = 0x0;

			break;

		}

	}

}



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

/*                        文件传输模块                               */

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

ER Com_open()

{

	ER ercd;

	CU_RSPRM tfRsPrm;



	cu_stopKeySet(CU_FNC_1);

	ercd = Ir_SetWinMode(SET_WIN2K);/* 有这行代码可以支持4M的传输:DT980,HA-60 IO */

	ercd = cu_open(COM , CU_B115K, &tfRsPrm, CU_MODE_HT);

	return ercd;

}



ER RecvFile(B *srcName, B *tarDir)

{

	ER ercd;



	ercd = Com_open();

	if (ercd != E_OK)

	{

		cu_stopKeySet(CU_FNC_NON);

		return ercd;

	}



	ercd = cu_fileRecv(COM, CU_TRANS_NORMAL, (B*)srcName, (B*)tarDir, CU_PROTECT_INVALID, &tfGraphSet);



	cu_stopKeySet(CU_FNC_NON);

	ercd = cu_close(COM, CU_CLOSE_NORMAL);



	return ercd;

}



ER SendFile(B *srcName, B *tarDir)

{

	ER ercd;

	B SendFileName[16];

	int i;



	ercd = Com_open();

	if (ercd != E_OK) 

	{

		cu_stopKeySet(CU_FNC_NON);

		return ercd;

	}

	memset(SendFileName, 0x0, sizeof(SendFileName)/sizeof(B));

	if (strlen(srcName) < 15)

	{

		for (i=0; i<15; i++)

		{

			if (srcName[i] == '.')

				break;

		}

		strncpy(SendFileName, srcName, i);

		strncat(SendFileName, "        ", (11-i));

		strncat(SendFileName, &srcName[i], 4);

	}

	else

	{

		strcpy(SendFileName, srcName);

	}



	ercd = cu_fileSend(COM, CU_TRANS_NORMAL, (B*)SendFileName, (B*)tarDir, CU_PROTECT_INVALID, &tfGraphSet);



	cu_stopKeySet(CU_FNC_NON);

	ercd = cu_close(COM, CU_CLOSE_NORMAL);

	return ercd;

}



int CheckPassword(char *password)

{

	ER ret, fPASSFILE, i, iPassOk;

	B sTemp[20], OldPass[20];

	W lno;

	if ((fPASSFILE = open(PASSFILE, O_RDONLY))== E_LOWERR)

	{

		return (E_NOOPEN); /* No Files */

	}

	memset(OldPass, 0x0, sizeof(OldPass));

	read(fPASSFILE, OldPass, 6);

	close(fPASSFILE);

	if (strcmp(OldPass, password)==0)

	{

		return (0);

	}

	else

	{

		return (E_PWERR);

	}

}



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

	函数说明:	折半查找

	参    数:

	[fname]		文件指针

	[start_adr]	文件查找的开始位置

	[fieldsize]	一行文本的长度

	[keypos]	查找字段的位置

	[keylen]	查找字段的长度

	[code]		查找字段的内容

	[sdata]		返回查到到的字段

	[fpos]		查找到的字段的偏移量

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

ER binary_search(int fname, W pos, int fieldsize, int keypos, int keylen, B *code, B *sdata, W *lno)

{

	long filelength;

	long head, trail;

	long offset;

	int i, j;

	int Count=0, end=0;

	

	head = pos;

	filelength = lseek(fname, 0, 2);

	/* 计算记录条数 */

	trail = filelength / fieldsize - 1;

	while(1)

	{

		/* 计算偏移量 */

		offset = ((head + trail) / 2) * fieldsize;

		lseek(fname, offset, 0);

		read(fname, sdata, fieldsize);

		for (i=keypos, j=0; j<keylen; i++, j++)

		{

			if (sdata[i]==code[j])

			{

				Count++;

				continue;

			}

			else if (sdata[i] > code[j])

			{

				trail = (head + trail) / 2 - 1;

				if (head >= trail)

					end++;

				Count = 0;

				break;

			}

			else if (sdata[i] < code[j])

			{

				head = (head + trail) / 2 + 1;

				if (head >= trail)

					end++;

				Count = 0;

				break;

			}

		}



		if (end==2 || Count==keylen)

			break;

	}

	if (Count == keylen)

	{

		*lno = offset;

		return E_OK;

	}

	else

	{

		return E_NOFIND;

	}

}



ER order_search(int fname, W pos, int fieldsize, int keypos, int keylen, B *code, B *sdata, W* lno)

{

	ER err = 0;

	W offset = pos;

	

	err = lseek(fname, pos, 0);

	if (err==E_LOWERR)

		return E_PRM;



	while(1)

	{

		err = read(fname, sdata, fieldsize);

		if(err==E_LOWERR)

			return E_NOFIND;

		if(err<fieldsize)

			return E_NOFIND;



		if (strncmp(&sdata[keypos], code, keylen)==0)

		{

			*lno = offset;

			return E_OK;

		}

		offset += fieldsize;

	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值