Source file: /~heha/messtech/matdde.zip/source/testargs/testargs.c

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ Library zum Testen der an die DDE-Funktionen uebergebenen Argumente

#include <windows.h>
#include <string.h>		//fuer strcmp()

#define szBufSize 50

#define DLLMEX
#include "..\header\mex.h"

#pragma hdrstop


//Prototypes
BOOL mxIsScalar( Matrix* pm );


//wird hier nur intern benutzt
BOOL mxIsScalar( Matrix* pm )
{
	if ( (mxGetM(pm) == 1) && (mxGetN(pm) == 1) )
		return TRUE;
	else return FALSE;

}
                      


void ErrorBeep()
{
	DWORD tick, l;

	tick = GetTickCount();
	do {
	  	MessageBeep(0);
		for (l=0; l<8000; l++);
	} while (GetTickCount()-tick < 300);
}



//fuer ddeinit.dll
BOOL TestServiceArg( Matrix* service )
{
	if ( !mxIsString(service) || mxGetM(service) != 1 )
	{
		mexPrintf( "???  The service argument must be an one row string.\r\n" );
		ErrorBeep();

		return FALSE;
	}
	return TRUE;
}



//fuer ddeinit.dll
BOOL TestTopicArg( Matrix* topic )
{
	if ( !mxIsString(topic) || mxGetM(topic) != 1 )
	{
		mexPrintf( "???  The topic argument must be an one row string.\r\n" );
		ErrorBeep();

		return FALSE;
	}
	return TRUE;
}



//fuer ddepoke.dll
BOOL TestMatrixArg( Matrix* matx )
{
	if ( mxIsString(matx) )
	{
		mexPrintf( "???  The matrix argument must be numeric.\r\n" );
		ErrorBeep();

		return FALSE;
	}
	return TRUE;
}



//fuer ddeadv.dll
BOOL TestCallbackArg( Matrix* callback )
{
	if ( !mxIsString(callback) || mxGetM(callback) > 1 )
	{
		mexPrintf( "???  The callback argument must be an empty or one row string.\r\n" );
		ErrorBeep();

		return FALSE;
	}
	return TRUE;
}



//fuer ddeadv.dll
/*
//alt:
char* TestUpmtxArg( Matrix* upmtx )
{
	if ( !mxIsString(upmtx) || mxGetM(upmtx) > 1)
	{
		mexPrintf( "???  The upmtx argument must be an empty or one row string.\r\n" );
		ErrorBeep();

		return (char*)-1;
	}
	else if ( mxGetN(upmtx) > 0 )		//wenn upmtx ungleich ''
	{
		int n = (int)mxGetN(upmtx);
		char* szDestMatrix = (char*) mxCalloc( n + 1, sizeof(char) );
		//
		mxGetString( upmtx, szDestMatrix, n );

		return szDestMatrix;	
	}

	return NULL;						//warm link herstellen
}
*/
//besser:
BOOL TestUpmtxArg( Matrix* upmtx )
{
	if ( !mxIsString(upmtx) || mxGetM(upmtx) > 1)
	{
		mexPrintf( "???  The upmtx argument must be an empty or one row string.\r\n" );
		ErrorBeep();
    
    	return FALSE;
	}
	return TRUE;
}



//fuer ddeexec.dll
BOOL TestCommandArg( Matrix* command)
{
    if ( !mxIsString(command) || mxGetM(command) != 1 || mxGetN(command) < 1 )
	{
		mexPrintf( "???  The command argument must be an one row string.\r\n" );
		ErrorBeep();

		return FALSE;
	}

	return TRUE;
}



//fuer alle anderen dlls
BOOL TestIOArgs( int in, int minIn, int maxIn )
{
	if ( in < minIn || in > maxIn )
	{
		char* szBuffer = (char*)mxCalloc( szBufSize, sizeof(char) );

		if ( minIn == maxIn )
		{
			wsprintf( szBuffer, "???  Function requires %d input argument(s).\r\n", minIn );
			mexPrintf( szBuffer );
			ErrorBeep();

			return FALSE;
		}

		else
		{
			wsprintf( szBuffer, "???  Function requires %d to %d input arguments.\r\n", minIn, maxIn );
			mexPrintf( szBuffer );
			ErrorBeep();

			return FALSE;
		}

		mxFree( szBuffer );
	}

	return TRUE;
}



BOOL TestChannelArg( Matrix* channel )
{
	
	if ( mxIsString(channel) || !mxIsScalar(channel) )
	{
		mexPrintf( "???  The channel argument must be a numeric scalar.\r\n" );
		ErrorBeep();

		return FALSE;
	}
	
	if ( !mxGetScalar(channel) )		//ungleich Null!
	{
		mexPrintf( "???  The channel argument must be a valid channel handle.\r\n" );
		ErrorBeep();

		return FALSE;
	}
	
	return TRUE;
}



BOOL TestItemArg( Matrix* item )
{
    if ( !mxIsString(item) || mxGetM(item) != 1 )
	{
		mexPrintf( "???  The item argument must be an one row string.\r\n" );
		ErrorBeep();

		return FALSE;
	}

	return TRUE;
}



WORD TestFormatArg( Matrix* format, BOOL strchk, BOOL* isString )
{
	WORD	retVal;
	double	stringArg;			//zweites Format-Argument

	WORD	cbFormat = (WORD)mxGetScalar( format );		//erstes Format-Argument

	if ( strchk )	//Test, ob Stringmatrix
	{
	    if ( mxIsString(format) || mxGetM(format) != 1 || mxGetN(format) != 2 )
		{
			mexPrintf( "???  The clipboard format argument must be a two-element one-row numeric matrix.\r\n" );
			ErrorBeep();
			return (WORD)-1;
		}

		hmemcpy( (char*)&stringArg, (char huge*)mxGetPr(format) + sizeof(double)/* zweite Zahl */, sizeof(double) );			
		
		if ( !(stringArg == (double)0 || stringArg == (double)1) )	//nur 0 oder 1 erlaubt
		{
			mexPrintf( "???  The second format argument is not valid.\r\n" );
			ErrorBeep();
			return (WORD)-1;
		}
        
        if ( cbFormat == (WORD)1 )	//Text-Format
		{
			if ( stringArg == (double)1 )	
				*isString = TRUE;		//String-Matrix
			else
				*isString = FALSE;		//Zahlen-Matrix
			
			return CF_TEXT;				//als Text uebertragen
		}
		else						//kein Text-Format
		{
			*isString = FALSE;				//nur Zahlen-Matrix moeglich

	        if ( stringArg == (double)1 )	//String-Matrix
			{
				mexPrintf( "???  For string-resultant matrix CF_TEXT format is required.\r\n" );
				ErrorBeep();
				return (WORD)-1;
			}
		}
	}
	else	//kein Test auf Ergebnisformat - fuer ddepoke.dll und ddeunadv.dll
	{   
	    if ( mxIsString(format) || mxGetM(format) != 1 )
		{
			mexPrintf( "???  The clipboard format argument must be a numeric scalar.\r\n" );
			ErrorBeep();
			return (WORD)-1;
		}
	}
        
    if ( cbFormat != (WORD)1 )	//kein Text-Format - Excel-Format?
	{
		char* szBuffer = (char*)mxCalloc( szBufSize, sizeof(char) );

		GetClipboardFormatName( cbFormat, szBuffer, szBufSize );

		if ( _stricmp (szBuffer, "XlTable") == 0 )	//_strlwr(szBuffer) == "xltable" ?
			retVal = cbFormat;

		else
		{
			mexPrintf( "???  Only XlTable and CF_TEXT format are currently supported.\r\n" );
			ErrorBeep();
			retVal = (WORD)-1;
		}

		mxFree( szBuffer );
		return retVal;
	}
	else
		return CF_TEXT;			//Text-Format
}



DWORD TestTimeoutArg( Matrix* timeout )
{
	if ( mxIsString(timeout) || !mxIsScalar(timeout) )
	{
		mexPrintf( "???  The timeout argument must be a numeric scalar.\r\n" );
		ErrorBeep();

		return (DWORD)-1;
	}
    else
    {
		double time = mxGetScalar( timeout );
		
		if ( time <= (double)0 )
		{
			mexPrintf( "???  The timeout value must be greater then 0.\r\n" );
			ErrorBeep();

			return (DWORD)-1;
		}
		return (DWORD)time;
	}
}


Detected encoding: ASCII (7 bit)2