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

📄 controloutput.c

📁 这是linux下 MiniGUI的一个程序
💻 C
字号:
/*
**  Create date: 2005/06/13.
**  Created by: JuncofeniteXu.
**  DCU99Editor, the M$ Windows like Para Editor on MiniGUI.** 
**
**  $those code must finish three things:
** 	first, read data from parameter editor, and suppose all the data are right;
**	second, store data, make all data into fixed or canonical format put some into parameter-files;
**	third, calculate checksum, some data must showback for check. 
**  if find some lawless input, po-up some warnning, and return a fail flag,
**  else return a success flag.	
**
**  use ControlInput.h to declare the following function may be more criterial
**
**  
**  especially, if you want to use this in commerce, you must pay for it
**  tel:
*/

/*
**  TODO:
*/

#include <stdio.h>#include <stdlib.h>#include <string.h> 
#include <sys/types.h>#include <sys/stat.h>#include <sys/unistd.h>

#include <minigui/common.h>#include <minigui/minigui.h>#include <minigui/gdi.h>#include <minigui/window.h>#include <minigui/control.h>#include <minigui/mgext.h>

#include "DuplexEditor.h"

FILE *OpenOrCreate_File(char filefullname[NAME_MAX+PATH_MAX+1])
{
	//open or create file
	FILE *fp=fopen(filefullname,"r+");	if(!fp)
	{		fp=fopen(filefullname,"w+");				if(!fp)		{	      		perror( "Couldn't  create parameter data file" );	      		//return NULL;	      	}
      	}
      	
      	return fp;
}

void auto_step_forward(HWND hDlg, int ID, int min, int max)
{
	char temp[20];
	HWND hChildWnd;
	
	memset(temp, 0, 20);
	GetDlgItemText(hDlg, ID, temp, 20);
	
	if( atoi(temp) < min  || atoi(temp) > max)
	{
		return;
	}
	
	/*sprintf( temp, "%d", atoi(temp) + 1);	
	if( atoi(temp) > max)
	{
		sprintf( temp, "%d", min);
	}*/
	//just like that:
	sprintf( temp, "%d", (atoi(temp) + 1)%100);
	
	SetDlgItemText(hDlg, ID, temp);
	hChildWnd = GetDlgItem(hDlg, ID);
	UpdateWindow(hChildWnd, TRUE);
	SetFocus(hChildWnd);
	
}

/*
**  view char c[2] as 0xc[0]c[1]
**  and return the value of 0xc[0]c[1]
**  0xc[0]c[1] = c[0] * 16 + c[1]
**  eg:“EF”->0xEF(=239),"11"->0x11(=17)
*/
int atox(const char *nptr){	int val, tmph, tmpl;	char sglptr[2];	switch(nptr[0])	{		case 'a':		case 'A':			tmph = 10;			break;		case 'b':		case 'B':			tmph = 11;			break;		case 'c':		case 'C':			tmph = 12;			break;		case 'd':		case 'D':			tmph = 13;			break;		case 'e':		case 'E':			tmph = 14;			break;		case 'f':		case 'F':			tmph = 15;			break;		default:			sglptr[0] = nptr[0];			sglptr[1] = '\0';			tmph = atoi(sglptr);			break;	}	switch(nptr[1])	{		case 'a':		case 'A':			tmpl = 10;			break;		case 'b':		case 'B':			tmpl = 11;			break;		case 'c':		case 'C':			tmpl = 12;			break;		case 'd':		case 'D':			tmpl = 13;			break;		case 'e':		case 'E':			tmpl = 14;			break;		case 'f':		case 'F':			tmpl = 15;			break;		default:			sglptr[0] = nptr[1];			sglptr[1] = '\0';			tmpl = atoi(sglptr);			break;	}	return (val = tmph * 16 + tmpl);}

/*
**  view char c[4] as 0xc[0]c[1]c[2]c[3]
**  and return the value of 0xc[0]c[1]c[2]c[3]
**  0xc[0]c[1]c[2]c[3] = 0xc[0]c[1] * 256 + 0xc[2]c[3] 
**  eg:“FFFF”->0xFFFF(=65535),"0011"->0x0011(=17)
*/
unsigned int atox_EXT(const char *nptr)
{
	unsigned int checksum = atox(nptr);
	checksum *= 256;
	checksum += atox( nptr + 2);
	return checksum;
}

/*
**  the function char_to_X1X2, just like that:
**  char char_to_X1X2(unsigned char c,int bit)**  {	** 	char tmp;** 	**	if(bit==0)//high**		tmp=c/16;**	else//if(bit==1)  low**		tmp=c%16;**	if(tmp<10)**		tmp+=48;//0~9->'0'~'9'**	else **		tmp+=55;//10~15->'A'~'F'**	**	return tmp;
**  }
**  that's all right, but if the bits two long,you can use a lazy function:
**  if like that,you must use bit[0] = c%1024,bit[1] = (c/256)%16,bit[2] = (c%256)%16,bit[3] = c%16
*/
char UInt_to_X1X2X3X4(unsigned int checksum, int bit)
{
	unsigned char temp[5] = {0L};
	sprintf( temp, "%x", checksum);	
	//sprintf() may have some problem, bay cause 0xX0X1X1X3X4X5X6X7
	// the checksum may very small,such as 3,and need to change to "0003"	integrity_to_nbits(temp, 4);	return temp[bit];
}

//because the result between 0~65535,unsigned int 0~65535
unsigned int CalculateChecksum(const char *pch, BOOL flag_isMainKey)
{
	long int checksum = 0;	int i = 0;		if(flag_isMainKey)
	{
		checksum = atox_EXT(pch);		 	     //add for adjust		i = 4;					 	     //add for adjust		while( pch[i] != '\0')
		{
			checksum += atox_EXT( pch + i);
			//but here atox_EXT(), the string-length = 4 		
			i += 4;
			checksum = (65536 - checksum)%65536;	     //add for adjust			//this is the methord of old-pragram : jiazhuqi from 7 stares	 	}
		return (unsigned int)checksum;			     //add for adjust	}
	else
	{		checksum = atox(pch);		//printf("\n%ld(chksm)",checksum);		i = 2;		while( pch[i] != '\0')
		{
			checksum += atox_EXT(pch + i);
			//but here atox_EXT(), the string-length = 4 		
			i += 4;	 	}	}
	
	checksum = checksum%65536; 	checksum = 65536 - checksum;
	checksum = checksum%65536;	//printf("\n%ld",checksum);	//this sentenece mean just gain low 4 bits
	//if checksum = 0, 65536 - checksum = 65536 = 0x10000,but we just need 0x0000
	//because unsigned char c(0~255), if checksum = 256, c = (unsighed char )checksum = 0!
	
	return (unsigned int)checksum;
}

//*******************************************************main key**************************************************************
void MainKeyEditor_StoreChecksum(char Mkey[265])
{
	Mkey[260] = '\0';
		//the main-checksum may not use this methord to calculate	//???	//gain checksum
	unsigned int checksum = CalculateChecksum(Mkey,TRUE);
	
	// 0x0000 -> '0''0''0''0''0',0xFFFF(65536) -> 'F' 'F''F''F'	
	//high byte
	Mkey[260] = UInt_to_X1X2X3X4(checksum,0);
	Mkey[261] = UInt_to_X1X2X3X4(checksum,1);
	//low byte
	Mkey[262] = UInt_to_X1X2X3X4(checksum,2);
	Mkey[263] = UInt_to_X1X2X3X4(checksum,3);
		Mkey[264] = '\0';	
}

BOOL MainKeyEditor_StoreFinalData(char Mkey[265])
{
	FILE *fp = NULL;
	
	//store in files or special device
	
	//open or create file
	if( !(fp = OpenOrCreate_File(FULL_FILE_NAME)))
	{
		return FALSE;;
	}
	
	//write file
      	if(fwrite(Mkey,1,264,fp)<0)
      	{      		perror( "Write parameter data file fail" );     		fclose(fp);     		return FALSE;     	}
      	      	
      	fclose(fp);      	      	
      	return TRUE;
}

//*******************************************************assistant key*********************************************************
void AssistantKeyEditor_StoreInitialData(HWND hDlg, HWND hChildWnd[10], char Akey[47])
{
	char temp[20] = {0L};	
	int i = 0;
	
	memset(Akey, '\0', 47);	//gain the team-num o key
	memset(temp,'\0',20);
	GetDlgItemText(hDlg, duplex_KeyDistinguishWordEdit, temp, 20);
	i = atoi(temp);
	memset(temp,'\0',20);
	sprintf ( temp, "%x", i); 
	integrity_to_nbits(temp, 2);
	//0 -> '0''0',99 -> '6''3'
	Akey[0] = temp[0];
	Akey[1] = temp[1];
	
	//store key 0~9
	for( i = 0; i < 10; i ++)
	{
		memset(temp,'\0',20);
		GetWindowText(hChildWnd[i], temp, 20);
		
		Akey[4*i + 2] = temp[0];
		Akey[4*i + 3] = temp[1];
		Akey[4*i + 4] = temp[2];
		Akey[4*i + 5] = temp[3];
	}
	
	Akey[42] = '\0';}

void AssistantKeyEditor_StoreChecksum(char Akey[47])
{
	//gain checksum
	unsigned int checksum = CalculateChecksum(Akey, FALSE);
	
	//store checksum
	// 0x0000 -> '0''0''0''0''0',0xFFFF(65536) -> 'F' 'F''F''F'	
	//high byte
	Akey[42] = UInt_to_X1X2X3X4(checksum,0);
	Akey[43] = UInt_to_X1X2X3X4(checksum,1);
	//low byte
	Akey[44] = UInt_to_X1X2X3X4(checksum,2);
	Akey[45] = UInt_to_X1X2X3X4(checksum,3);
	
	Akey[46] = '\0';
}

BOOL AssistantKeyEditor_StoreFinalData(char Akey[47])
{
	FILE *fp = NULL;	
	//calculate the assistant key team-num: 0~99	
	int seek = atox(Akey);		//store in files or special device
	
	//open or create file
	if( !(fp = OpenOrCreate_File(FULL_FILE_NAME)))
	{
		return FALSE;;
	}
	
	//seek the location
	// 46 bytes per team 
      	seek *= 46; 
      	// the first 264 bytes: the space to store main-key parameter
      	seek += 264; 
      	fseek(fp, seek, SEEK_SET);
      	
      	//write file
      	if(fwrite(Akey,1,46,fp)<0)
      	{      		perror( "Write parameter data file fail" );     		fclose(fp);     		return FALSE;     	}
      	      	
      	fclose(fp);      	      	
      	return TRUE;
}

⌨️ 快捷键说明

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