edit.c

来自「CNC 的开放码,EMC2 V2.2.8版」· C语言 代码 · 共 1,270 行 · 第 1/3 页

C
1,270
字号
					}				}				break;	#endif				default:				IsOk = FALSE;				ErrorMessageVarParser = "Unknown variable (on first character following %)";		}		if (IsOk)		{			if ( VarTypeFound )				*VarTypeFound = TypeFound;			if ( VarOffsetFound ) 				*VarOffsetFound = OffsetFound;			if ( pNumberOfChars!=NULL )				*pNumberOfChars = *pPtrScanPos - text;		}		else		{			if ( ErrorMessageVarParser==NULL )				ErrorMessageVarParser = "Unknown variable (global error)";		}	}	return IsOk;}/* Convert a string of arithmetic expression for the arithm_eval : *//* Variables becomes @type/offset@ *//* Then verify all the expression using arithm_eval *//* return pointer on the new string converted and verified if okay, else NULL */char * TextParserForArithmExpr(char * text, int TypeElement){	 static char NewExpr[100];	 char Buffer[20];	 char Temp [2];	int ItIsOk = TRUE;	int BeenHere = FALSE;	int HaveAUnit= FALSE;	int VarType;	int VarOffset;	int StringLength;	char * tmpptr = text;	char * bufptr = Buffer;	ErrorMessageVarParser = NULL;	strcpy(NewExpr,"");	strcpy(Buffer,"");		// we want to break the text into a "unit". The unit is between the math 	// symbols. The unit is check for a symbol, proper variable, decimal or text and converted. 		// if the text is empty then set an error which does nothing	if (*tmpptr == '\0') { ItIsOk=FALSE;}		do{		do{						 			switch (*tmpptr)			{			case '\0':// we have found the end-must be a unit to check				HaveAUnit= TRUE; 			    break;			case ' ':				ItIsOk= FALSE;				ErrorMessageVarParser="No spaces allowed.";				break;			case '=':case '<':case '>':case '*':case '/':case '|':case ',':			case '!':case '&':case '+':case '-':case ')':case '^':				if (BeenHere==TRUE) // been here before then save the math symbols					{ 						//printf("added %c to NewExpr\n",*tmpptr);						sprintf (Temp,"%c",*tmpptr);					  strcat(NewExpr,Temp);					  tmpptr ++;					}				else { 					//printf("set been here true\n");					   HaveAUnit=TRUE;// otherwise we have a unit to check					   BeenHere=TRUE;					 }				break;			case '(': // is this the beginning bracket of absolute, minimum, maximum or average(MOY) funtion? 					 if (strcmp(Buffer,"ABS")==0 || strcmp(Buffer,"MINI")|| strcmp(Buffer,"MAXI")||strcmp(Buffer,"MOY"))  					 	{	sprintf(Temp,"%c",*tmpptr);// add ( to our buffer 					 		strcat(Buffer,Temp);							strcat(NewExpr,Buffer); // add Buffer to NewExp							strcpy(Buffer,""); //clear buffer							tmpptr ++;   // next character						}					else					{						// printf("added %c to NewExpr NewExpr %s\n",*tmpptr,NewExpr); 					  sprintf (Temp,"%c",*tmpptr); // add it to NewExpr					  strcat(NewExpr,Temp);					  tmpptr ++;					}					break;					 default:					 //printf("added %c to Buffer\n",*tmpptr);					 sprintf(Temp,"%c",*tmpptr);// add it to our buffer to make a unit					 strcat(Buffer,Temp);					 tmpptr ++;					 BeenHere=FALSE;				break;				 }					 					 }	 			while (HaveAUnit == FALSE && ItIsOk == TRUE);			 		if (ItIsOk == TRUE) // we have a unit and no error		{			//printf("Have a unit! %s\n",Buffer);			if (*bufptr !='%') // if not a variable				{					char * VarName=ConvSymbolToVarName(bufptr);// check for symbols										if (VarName!=NULL)						{ 							//printf("converted symbol to variable %s,%s\n",Buffer,VarName);						  strcpy(Buffer,""); // empty buffer of symbols						  strcpy(Buffer,VarName); // copy the variable in						}				}			if (*bufptr !='%') // still not a variable?			 	{						//printf("checking for a number\n");					switch (*bufptr) // does it have to do with numbers dec or hex?					{								case '0':case '1':case '2':case '3':								case '4':case '5':case '6':case '7':								case '8':case '9':case '-':case '+':								case '(':case ')':case '$':case '\0': //or at the end								//printf("found number\n");									break;								default:								//printf("'%c' is not a number!\n",*bufptr);  										ItIsOk=FALSE;										ErrorMessageVarParser="Not a symbol or dec/Hex number";			                    	break;						}				}											if (*bufptr =='%' && *(bufptr+2) >='0' && *(bufptr+2)<='9') //now a variable?			{   				//printf("It's a variable!\n");				switch (*(bufptr+1)) // is it the right variable?				{						case 'W':case 'T':case 'M':case 'C':case 'X':							if (TextParserForAVar(bufptr,&VarType,&VarOffset,&StringLength,FALSE/*PartialNames allowed*/))					{						strcpy(Buffer,"");						sprintf(Buffer,"@%d/%d@",VarType,VarOffset);						strcat(NewExpr,Buffer);						strcpy(Buffer,"");						HaveAUnit=FALSE;						//printf("new expresion=%s\nbuffer=%s\n",NewExpr,Buffer);						if ( VarType<VAR_ARE_WORD )						{							//printf("variable atribute error 1");							ItIsOk = FALSE;							ErrorMessageVarParser = "Incompatible type of variable (must be an integer!)";						}					}					else					{   						//printf("variable atribute error 2");						ItIsOk = FALSE;					}										break;								default:						ItIsOk=FALSE;						ErrorMessageVarParser = "can only use W,T,M,C,X variable letters";					   break;				}			}		else		    {if (*tmpptr=='%')				{   ItIsOk=FALSE;					ErrorMessageVarParser = "Syntaxt error in variable";				}				else 				{				 if (ItIsOk== TRUE)				 	{ 							//printf("Adding %s to NewExpr_%s\n",Buffer,NewExpr);						strcat(NewExpr,Buffer);				  		strcpy(Buffer,"");											HaveAUnit=FALSE;					}				}			}		}	}	while (ItIsOk != FALSE && *tmpptr!='\0');// not at end and no error? 						//printf("Parser Arithm ; ItIsOk=%d ; OriExpr=%s ; NewExpr=%s\n",ItIsOk, text, NewExpr);		/* Verify expression converted */	if (ItIsOk)	{		if (TypeElement==ELE_OUTPUT_OPERATE)			ErrorMessageVarParser = VerifySyntaxForMakeCalc(NewExpr);		else			ErrorMessageVarParser = VerifySyntaxForEvalCompare(NewExpr);		if (ErrorMessageVarParser)			ItIsOk = FALSE;	}	/* Error Message */	if (ErrorMessageVarParser)	{		ShowMessageBox("Error",ErrorMessageVarParser,"Ok");	}	/* Give result */	if (ItIsOk)	{			//printf("returned %s\n",NewExpr);	//for debugging		return NewExpr;	}		else		return NULL;}void SaveElementProperties(){	int IdBase;	int Preset;	int NumRungToJump;	StrTimer * Timer;	StrMonostable * Monostable;	StrCounter * Counter;	char * NewArithmExpr;	int SubRoutineToCall;	int VarTypeEntered,VarNumEntered;#ifdef SEQUENTIAL_SUPPORT	int iCurrentLanguage = SectionArray[ InfosGene->CurrentSection ].Language;	if ( iCurrentLanguage==SECTION_IN_SEQUENTIAL )	{		SaveSeqElementProperties( );		return;	}#endif	if (EditDatas.ElementUnderEdit)	{		switch(EditDatas.ElementUnderEdit->Type)		{			case ELE_INPUT:			case ELE_INPUT_NOT:			case ELE_RISING_INPUT:			case ELE_FALLING_INPUT:			case ELE_OUTPUT:			case ELE_OUTPUT_NOT:			case ELE_OUTPUT_SET:			case ELE_OUTPUT_RESET:				if ( TextParserForAVar(GetProperty(0),&VarTypeEntered,&VarNumEntered,NULL,FALSE/*PartialNames*/) )				{					if ( VarTypeEntered>=VAR_ARE_WORD )					{						ShowMessageBox("Error","You must select a boolean variable !","Ok");					}					else					{						EditDatas.ElementUnderEdit->VarType = VarTypeEntered;						EditDatas.ElementUnderEdit->VarNum = VarNumEntered;					}				}				else				{					if (ErrorMessageVarParser)						ShowMessageBox("Error",ErrorMessageVarParser,"Ok");					else						ShowMessageBox( "Error", "Unknown variable...", "Ok" );				}				break;			case ELE_TIMER:				TextToNumber(GetProperty(0),0,NBR_TIMERS-1,&EditDatas.ElementUnderEdit->VarNum);				Timer = &TimerArray[EditDatas.ElementUnderEdit->VarNum];				IdBase = ConvBaseInTextToId(GetProperty(1));				Timer->Base = CorresDatasForBase[IdBase].ValueInMS;				strcpy(Timer->DisplayFormat,CorresDatasForBase[IdBase].DisplayFormat);				if (TextToNumber(GetProperty(2),0,999,&Preset))					Timer->Preset = Preset * Timer->Base;				break;			case ELE_MONOSTABLE:				TextToNumber(GetProperty(0),0,NBR_MONOSTABLES-1,&EditDatas.ElementUnderEdit->VarNum);				Monostable = &MonostableArray[EditDatas.ElementUnderEdit->VarNum];				IdBase = ConvBaseInTextToId(GetProperty(1));				Monostable->Base = CorresDatasForBase[IdBase].ValueInMS;				strcpy(Monostable->DisplayFormat,CorresDatasForBase[IdBase].DisplayFormat);				if (TextToNumber(GetProperty(2),0,999,&Preset))					Monostable->Preset = Preset * Monostable->Base;				break;			case ELE_COUNTER:				TextToNumber(GetProperty(0),0,NBR_COUNTERS-1,&EditDatas.ElementUnderEdit->VarNum);				Counter = &CounterArray[EditDatas.ElementUnderEdit->VarNum];				if (TextToNumber(GetProperty(1),0,9999,&Preset))					Counter->Preset = Preset;				break;			case ELE_OUTPUT_JUMP:				NumRungToJump = ConvLabelToNumRung(GetProperty(0));				if (NumRungToJump>=0)					EditDatas.ElementUnderEdit->VarNum = NumRungToJump;				break;			case ELE_OUTPUT_CALL:				if ( TextToNumber(GetProperty(0),0,NBR_SECTIONS-1,&SubRoutineToCall) )				{					if ( VerifyIfSubRoutineNumberExist( SubRoutineToCall ) )						EditDatas.ElementUnderEdit->VarNum = SubRoutineToCall;				}				break;			case ELE_COMPAR:				NewArithmExpr = TextParserForArithmExpr(GetProperty(0), ELE_COMPAR);				if (NewArithmExpr)					strcpy(EditArithmExpr[EditDatas.ElementUnderEdit->VarNum].Expr,NewArithmExpr);				break;			case ELE_OUTPUT_OPERATE:				NewArithmExpr = TextParserForArithmExpr(GetProperty(0), ELE_OUTPUT_OPERATE);				if (NewArithmExpr)					strcpy(EditArithmExpr[EditDatas.ElementUnderEdit->VarNum].Expr,NewArithmExpr);				break;		}		/* display back to show what we have really understand... */		LoadElementProperties(EditDatas.ElementUnderEdit);	}}/* For editing, we do not touch directly the current arithmexpressions. We work on the edit ones. It is only when theedited rung is applyed that we copy the expressions edited */void CopyCurrentArithmExpr(){	int NumExpr;	for (NumExpr=0; NumExpr<NBR_ARITHM_EXPR; NumExpr++)		strcpy(EditArithmExpr[NumExpr].Expr,ArithmExpr[NumExpr].Expr);}void ApplyNewArithmExpr(){	int NumExpr;	for (NumExpr=0; NumExpr<NBR_ARITHM_EXPR; NumExpr++)		strcpy(ArithmExpr[NumExpr].Expr,EditArithmExpr[NumExpr].Expr);}void CheckForFreeingArithmExpr(int PosiX,int PosiY){	int TypeElement = EditDatas.Rung.Element[PosiX][PosiY].Type;	if ( (TypeElement==ELE_COMPAR) || (TypeElement==ELE_OUTPUT_OPERATE) )	{		/* Freeing Expr */		EditArithmExpr[ EditDatas.Rung.Element[PosiX][PosiY].VarNum ].Expr[0] = '\0';	}}void CheckForAllocatingArithmExpr(int PosiX,int PosiY){	int TypeElement = EditDatas.Rung.Element[PosiX][PosiY].Type;	int NumExpr = 0;	int Found = FALSE;	if ( (TypeElement==ELE_COMPAR) || (TypeElement==ELE_OUTPUT_OPERATE) )	{		do		{			/* Expr free ? */			if (EditArithmExpr[ NumExpr ].Expr[0]=='\0')			{				Found = TRUE;				/* Allocate this expr for the operate/compar block ! */				EditDatas.Rung.Element[PosiX][PosiY].VarNum = NumExpr;			}			NumExpr++;		}		while( (NumExpr<NBR_ARITHM_EXPR) && (!Found) );	}}void InitBufferRungEdited( StrRung * pRung ){	int x,y;	for (y=0;y<RUNG_HEIGHT;y++)	{		for(x=0;x<RUNG_WIDTH;x++)		{			pRung->Element[x][y].Type = ELE_FREE;			pRung->Element[x][y].ConnectedWithTop = 0;			pRung->Element[x][y].VarType = 0;			pRung->Element[x][y].VarNum = 0;			pRung->Element[x][y].DynamicState = 0;		}	}	pRung->Used = TRUE;	pRung->PrevRung = -1;	pRung->NextRung = -1;}int GetNbrRungsDefined( void ){	int NbrRungsUsed = 0;	int ScanRung;	for( ScanRung=0; ScanRung<NBR_RUNGS; ScanRung++ )	{		if (RungArray[ScanRung].Used)			NbrRungsUsed++;	}	return NbrRungsUsed;}int FindFreeRung(){	int NumFree = -1;	int ScanRung = 0;	do	{		if (!RungArray[ScanRung].Used)			NumFree = ScanRung;		ScanRung++;	}	while( (NumFree==-1)&&(ScanRung<NBR_RUNGS) );	return NumFree;}

⌨️ 快捷键说明

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