text: /*
  *   Copyright (C) 2010
  *   Chair of Process Control Engineering,
  *   D-52056 Aachen, Germany.
  *   All rights reserved.
  *
  *
  *   This file is part of the ACPLT/OV Package 
  *   Licensed under the Apache License, Version 2.0 (the "License");
  *   you may not use this file except in compliance with the License.
  *   You may obtain a copy of the License at
  *
  *       http://www.apache.org/licenses/LICENSE-2.0
  *
  *   Unless required by applicable law or agreed to in writing, software
  *   distributed under the License is distributed on an "AS IS" BASIS,
  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *   See the License for the specific language governing permissions and
  *   limitations under the License.
  */
  /******************************************************************************
  *
  *   FILE
  *   ----
  *   MID.c
  *
  *   History
  *   -------
  *   2010-11-30   File created
  *
  *******************************************************************************
  *
  *   This file is generated by the 'fb_builder' command
  *
  ******************************************************************************/
  
  
  #ifndef OV_COMPILE_LIBRARY_iec61131stdfb
  #define OV_COMPILE_LIBRARY_iec61131stdfb
  #endif
  
  
  #include "iec61131stdfb.h"
  #include "ov_macros.h"
  #include "ov_logfile.h"
  #include "ov_string.h"
  #include "ov_malloc.h"
  
  
  OV_DLLFNCEXPORT OV_RESULT iec61131stdfb_MID_IN_set(
      OV_INSTPTR_iec61131stdfb_MID          pobj,
      const OV_STRING  value
  ) {
      return ov_string_setvalue(&pobj->v_IN,value);
  }
  
  OV_DLLFNCEXPORT OV_RESULT iec61131stdfb_MID_L_set(
      OV_INSTPTR_iec61131stdfb_MID          pobj,
      const OV_UINT  value
  ) {
      pobj->v_L = value;
      return OV_ERR_OK;
  }
  
  OV_DLLFNCEXPORT OV_RESULT iec61131stdfb_MID_P_set(
      OV_INSTPTR_iec61131stdfb_MID          pobj,
      const OV_UINT  value
  ) {
      pobj->v_P = value;
      return OV_ERR_OK;
  }
  
  OV_DLLFNCEXPORT OV_STRING iec61131stdfb_MID_OUT_get(
      OV_INSTPTR_iec61131stdfb_MID          pobj
  ) {
      return pobj->v_OUT;
  }
  
  
  OV_DLLFNCEXPORT void iec61131stdfb_MID_destructor(OV_INSTPTR_ov_object pobj) {
  
  	OV_INSTPTR_iec61131stdfb_MID pinst = Ov_StaticPtrCast(iec61131stdfb_MID, pobj);
  	
  	ov_string_setvalue(&pinst->v_IN, "");
  	ov_string_setvalue(&pinst->v_OUT, "");
  	fb_functionblock_destructor(pobj);
  }
  
  
  OV_DLLFNCEXPORT void iec61131stdfb_MID_typemethod(
  	OV_INSTPTR_fb_functionblock	pfb,
  	OV_TIME						*pltc
  ) {
      /*    
      *   local variables
      */
  	
  	unsigned int i;
  	unsigned int in_length;
  	
  	OV_STRING p_char;
  	
      OV_INSTPTR_iec61131stdfb_MID pinst = Ov_StaticPtrCast(iec61131stdfb_MID, pfb);
  	
  	ov_string_setvalue(&pinst->v_OUT, "");	//free memory
  	
  	
  	if(!pinst->v_P) //exit, if position is 0
  		return;
  	
  	if(!pinst->v_L)	//exit, if length is 0
  		return;
  	
  	in_length = ov_string_getlength(pinst->v_IN);
  	
  	if(pinst->v_P <= in_length)
  	{
  		p_char = (OV_STRING) ov_malloc((pinst->v_L + 1) * sizeof(char));
  		if(p_char != NULL)
  		{
  			for(i=0; i < pinst->v_L; i++)
  				if(pinst->v_P - 1 + i < in_length)
  					p_char[i] = pinst->v_IN[(pinst->v_P - 1)+ i];
  				else
  				{
  					ov_logfile_warning("%s: next character exceeds input string, stopping operation", pinst->v_identifier);
  					break;
  				}
  			
  			p_char[i] = 0;
  			ov_string_setvalue(&pinst->v_OUT, p_char);
  			ov_free(p_char);
  		}
  		else
  		{
  			ov_logfile_error("%s: allocation of memory failed, no operation performed", pinst->v_identifier);
  			return;
  		}
  	}
  	else
  	{
  		ov_logfile_warning("%s: specified position is not part of the input string", pinst->v_identifier);
  	}
  	
  	
  	
      return;
  }
  
  