edit.c

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

C
1,270
字号
/* Classic Ladder Project *//* Copyright (C) 2001-2006 Marc Le Douarain *//* http://www.multimania.com/mavati/classicladder *//* http://www.sourceforge.net/projects/classicladder *//* May 2001 *//* ------ *//* Editor *//* ------ *//* This part of the editor is the one who will not change even if if we use *//* another gui instead of gtk... who know? *//* ------------------------------------------------------------- *//* This library is free software; you can redistribute it and/or *//* modify it under the terms of the GNU Lesser General Public *//* License as published by the Free Software Foundation; either *//* version 2.1 of the License, or (at your option) any later version. *//* This library is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *//* Lesser General Public License for more details. *//* You should have received a copy of the GNU Lesser General Public *//* License along with this library; if not, write to the Free Software *//* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include <gtk/gtk.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "classicladder.h"#include "global.h"#include "drawing.h"#include "edit.h"#include "editproperties_gtk.h"#include "calc.h"#include "files.h"#include "arithm_eval.h"#include "classicladder_gtk.h"#include "manager.h"#ifdef SEQUENTIAL_SUPPORT#include "edit_sequential.h"#endif#include "symbols.h"/* This array give for each special elements the size used */#define TYPEELERULE 0#define XSIZEELERULE 1#define YSIZEELERULE 2static short int RulesForSpecialElements[][3] =            { {ELE_TIMER , 2, 2 } ,              {ELE_MONOSTABLE, 2, 2 },              {ELE_COUNTER, 2, 4 },              {ELE_COMPAR, 3, 1 },              {ELE_OUTPUT_OPERATE, 3, 1 },              {-1, -1, -1}/*end*/ };// pointer on a string error explanation...char * ErrorMessageVarParser = NULL;int ConvBaseInTextToId(char * text){	int ScanBase = 0;	int BaseFound = BASE_SECS;	do	{		if (strcmp(text,CorresDatasForBase[ScanBase].ParamSelect)==0)			BaseFound = ScanBase;		ScanBase++;	}	while(ScanBase<NBR_TIMEBASES);	return BaseFound;}int ConvLabelToNumRung(char * LabelName){	int ScanRung = 0;	int RungFound = -1;	int Done = FALSE;	ScanRung = InfosGene->FirstRung;	do	{		if (strcmp(RungArray[ScanRung].Label,LabelName)==0)			RungFound = ScanRung;		if (ScanRung == InfosGene->LastRung)			Done = TRUE;		else			ScanRung = RungArray[ScanRung].NextRung;	}	while(!Done);	return RungFound;}void LoadElementProperties(StrElement * Element){	char TextToWrite[100];	int NumParam;	StrTimer * Timer;	StrMonostable * Monostable;	StrCounter * Counter;	for(NumParam=0;NumParam<NBR_PARAMS_PER_OBJ;NumParam++)		SetProperty(NumParam,"---","");	if (Element)	{		switch(Element->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:				strcpy(TextToWrite,DisplayInfo(Element->VarType,Element->VarNum));				SetProperty(0,"Variable",TextToWrite);				break;			case ELE_OUTPUT_JUMP:				SetProperty(0,"JumpToLabel",RungArray[Element->VarNum].Label);				break;			case ELE_OUTPUT_CALL:				sprintf(TextToWrite,"%d",Element->VarNum);				SetProperty(0,"Sub-Routine",TextToWrite);				break;			case ELE_TIMER:				Timer = &TimerArray[Element->VarNum];				sprintf(TextToWrite,"%d",Element->VarNum);				SetProperty(0,"TimerNbr",TextToWrite);				sprintf(TextToWrite,"%s",CorresDatasForBase[ ConvBaseInMilliSecsToId(Timer->Base) ].ParamSelect);				SetProperty(1,"Base",TextToWrite);				sprintf(TextToWrite,"%d",Timer->Preset/Timer->Base);				SetProperty(2,"Preset",TextToWrite);				break;			case ELE_MONOSTABLE:				Monostable = &MonostableArray[Element->VarNum];				sprintf(TextToWrite,"%d",Element->VarNum);				SetProperty(0,"MonostNbr",TextToWrite);				sprintf(TextToWrite,"%s",CorresDatasForBase[ ConvBaseInMilliSecsToId(Monostable->Base) ].ParamSelect);				SetProperty(1,"Base",TextToWrite);				sprintf(TextToWrite,"%d",Monostable->Preset/Monostable->Base);				SetProperty(2,"Preset",TextToWrite);				break;			case ELE_COUNTER:				Counter = &CounterArray[Element->VarNum];				sprintf(TextToWrite,"%d",Element->VarNum);				SetProperty(0,"CounterNbr",TextToWrite);				sprintf(TextToWrite,"%d",Counter->Preset);				SetProperty(1,"Preset",TextToWrite);				break;			case ELE_COMPAR:			case ELE_OUTPUT_OPERATE:				strcpy(TextToWrite,DisplayArithmExpr(EditArithmExpr[Element->VarNum].Expr,0));				SetProperty(0,"Expression",TextToWrite);				break;		}	}}/* Convert a string to a number if >=Min and <=Max *//* return TRUE if okay */int TextToNumber(char * text,int ValMin,int ValMaxi,int *ValFound){	int IsOk = TRUE;	int Value;	Value = atoi(text);	if ( (Value<ValMin) || (Value>ValMaxi) )		IsOk = FALSE;	if (IsOk)		*ValFound = Value;	return IsOk;}/* like the preceding one, but pointer advance ! */int TextToNumberAndAdvance(char ** text,int ValMin,int ValMaxi,int *ValFound){	int IsOk = TRUE;	int Value = 0;	char * pScan = *text;	char NumberFound = FALSE;	while( *pScan>='0' && *pScan<='9' )	{		Value = Value*10+(*pScan-'0');		pScan++;		NumberFound = TRUE;	}	if ( !NumberFound || (Value<ValMin) || (Value>ValMaxi) )		IsOk = FALSE;	if (IsOk)	{		*ValFound = Value;		*text = pScan;	}	return IsOk;}/* Convert a string to variable (type/offset) *//* if pNumberOfChars not NULL, give length of the string found *//* return TRUE if okay */int TextParserForAVar( char * text,int * VarTypeFound,int * VarOffsetFound, int * pNumberOfChars, char PartialNames ){	int TypeFound = 0;	int OffsetFound;	int IsOk = TRUE;	char * pScanPos = text; // content will be modified when advancing!	char * * pPtrScanPos = &pScanPos;	// if not commencing per '%', search the corresponding symbol !	if ( *pScanPos!='%' )	{		char * VarName = ConvSymbolToVarName( pScanPos );		if ( VarName==NULL )		{			IsOk = FALSE;			ErrorMessageVarParser = "Unknown symbol for variable name";		}		else		{			pScanPos = VarName;		}	}	if ( IsOk )	{		// pass the first character '%'		(*pPtrScanPos)++;		switch(**pPtrScanPos)		{			case 'B':				TypeFound = VAR_MEM_BIT;				(*pPtrScanPos)++;				if (!TextToNumberAndAdvance(pPtrScanPos,0,NBR_BITS-1,&OffsetFound))				{					IsOk = FALSE;					ErrorMessageVarParser = "Unknown variable (number value out of bound)";				}				break;				case 'T':				(*pPtrScanPos)++;				if (!TextToNumberAndAdvance(pPtrScanPos,0,NBR_TIMERS-1,&OffsetFound))				{					IsOk = FALSE;					ErrorMessageVarParser = "Unknown variable (number value out of bound)";				}				else				{					if ( PartialNames==FALSE )					{						if ( **pPtrScanPos!=VAR_ATTRIBUTE_SEP )						{							IsOk = FALSE;							ErrorMessageVarParser = "Unknown variable (missing '.' character before attribute)";						}						else						{							(*pPtrScanPos)++;							switch(**pPtrScanPos)							{								case 'D':									TypeFound = VAR_TIMER_DONE;									break;								case 'R':									TypeFound = VAR_TIMER_RUNNING;									break;								case 'P':									TypeFound = VAR_TIMER_PRESET;									break;								case 'V':									TypeFound = VAR_TIMER_VALUE;									break;								default:									IsOk = FALSE;									ErrorMessageVarParser = "Unknown variable (unknown attribute)";									break;							}							(*pPtrScanPos)++;						}					}				}				break;				case 'M':				(*pPtrScanPos)++;				if (!TextToNumberAndAdvance(pPtrScanPos,0,NBR_MONOSTABLES-1,&OffsetFound))				{					IsOk = FALSE;					ErrorMessageVarParser = "Unknown variable (number value out of bound)";				}				else				{					if ( PartialNames==FALSE )					{						if ( **pPtrScanPos!=VAR_ATTRIBUTE_SEP )						{							IsOk = FALSE;							ErrorMessageVarParser = "Unknown variable (missing '.' character before attribute)";						}						else						{							(*pPtrScanPos)++;							switch(**pPtrScanPos)							{								case 'R':									TypeFound = VAR_MONOSTABLE_RUNNING;									break;								case 'P':									TypeFound = VAR_MONOSTABLE_PRESET;									break;								case 'V':									TypeFound = VAR_MONOSTABLE_VALUE;									break;								default:									IsOk = FALSE;									ErrorMessageVarParser = "Unknown variable (unknown attribute)";									break;							}							(*pPtrScanPos)++;						}					}				}				break;				case 'C':				(*pPtrScanPos)++;				if (!TextToNumberAndAdvance(pPtrScanPos,0,NBR_COUNTERS-1,&OffsetFound))				{					IsOk = FALSE;					ErrorMessageVarParser = "Unknown variable (number value out of bound)";				}				else				{					if ( PartialNames==FALSE )					{						if ( **pPtrScanPos!=VAR_ATTRIBUTE_SEP )						{							IsOk = FALSE;							ErrorMessageVarParser = "Unknown variable (missing '.' character before attribute)";						}						else						{							(*pPtrScanPos)++;							switch(**pPtrScanPos)							{								case 'D':									TypeFound = VAR_COUNTER_DONE;									break;								case 'E':									TypeFound = VAR_COUNTER_EMPTY;									break;								case 'F':									TypeFound = VAR_COUNTER_FULL;									break;								case 'P':									TypeFound = VAR_COUNTER_PRESET;									break;								case 'V':									TypeFound = VAR_COUNTER_VALUE;									break;								default:									IsOk = FALSE;									ErrorMessageVarParser = "Unknown variable (unknown attribute)";									break;							}							(*pPtrScanPos)++;						}					}				}				break;				case 'I':				TypeFound = VAR_PHYS_INPUT;				(*pPtrScanPos)++;				if (!TextToNumberAndAdvance(pPtrScanPos,0,NBR_PHYS_INPUTS-1,&OffsetFound))				{					IsOk = FALSE;					ErrorMessageVarParser = "Unknown variable (number value out of bound)";				}				break;				case 'Q':				TypeFound = VAR_PHYS_OUTPUT;				(*pPtrScanPos)++;				if (!TextToNumberAndAdvance(pPtrScanPos,0,NBR_PHYS_OUTPUTS-1,&OffsetFound))				{					IsOk = FALSE;					ErrorMessageVarParser = "Unknown variable (number value out of bound)";				}				break;				case 'W':				TypeFound = VAR_MEM_WORD;				(*pPtrScanPos)++;				if (!TextToNumberAndAdvance(pPtrScanPos,0,NBR_WORDS-1,&OffsetFound))				{					IsOk = FALSE;					ErrorMessageVarParser = "Unknown variable (number value out of bound)";				}				break;		#ifdef SEQUENTIAL_SUPPORT			case 'X':				TypeFound = VAR_STEP_ACTIVITY; //per default, but it could change later...				(*pPtrScanPos)++;				if (!TextToNumberAndAdvance(pPtrScanPos,0,NBR_STEPS-1,&OffsetFound))				{					IsOk = FALSE;					ErrorMessageVarParser = "Unknown variable (number value out of bound)";				}				else				{					// verify if there is a '.' after number so that it must be the X..,V form					// instead of X... form.					if ( **pPtrScanPos==VAR_ATTRIBUTE_SEP )					{						(*pPtrScanPos)++;						if ( **pPtrScanPos=='V' )						{							TypeFound = VAR_STEP_TIME;						}						else						{							IsOk = FALSE;							ErrorMessageVarParser = "Unknown variable (unknown attribute)";						}

⌨️ 快捷键说明

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