/* libsizzle/validate.h - parameter validation macros
 
  Copyright (C) 2000 Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
 
  This is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.
  
  This software 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
  General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this package; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA.  */

#ifndef __SIZZLE_VALIDATE_H__
#define __SIZZLE_VALIDATE_H__

#if HAVE_CONFIG_H
# include <config.h>
#endif 

#if __SIZZLE_IMPORT__
# define SIZZLE_API __declspec (dllimport)
#elif __SIZZLE_EXPORT__ 
# define SIZZLE_API __declspec (dllexport)
#else
# define SIZZLE_API
#endif

#include "result.h"
#include "eval.h"

/* Evaluates the expression @var{type_ok}, and if it returns 0, an
   error will be returned from the current function, including the
   argument number @var{arg_num} and the expected type @var{expected}.  */
#define CHECK_TYPE(arg_num, type_ok, expected)				    \
  do									    \
    {									    \
      if (!(type_ok))							    \
	{								    \
          *result = zzz_make_wta_exception                                  \
            (zzz_make_list				                    \
	     (zzz_make_string (zzz_invalid_arg_type, -1),                   \
	      zzz_make_integer (arg_num),	                            \
	      zzz_make_string (zzz_in_call_to, -1),	                    \
	      zzz_make_string (s_func_name, -1),                            \
	      zzz_make_string ("', ", -1), 	                            \
              zzz_make_string (expected, -1),				    \
	      zzz_make_string (zzz_expected, -1),			    \
	      NULL));				                            \
	  return RESULT_EXCEPTION;					    \
	}								    \
    }									    \
  while (0)

/* Evaluates the expression @var{range_ok}, and if it returns 0, an
   error will be returned from the current function, including the
   argument number @var{arg_num} and the expected type @var{expected}.  */
#define CHECK_RANGE(arg_num, range_ok, expected)			    \
  do									    \
    {									    \
      if (!(range_ok))							    \
	{								    \
          *result = zzz_make_wta_exception                                  \
            (zzz_make_list				                    \
	     (zzz_make_string (zzz_invalid_arg_range, -1),                  \
	      zzz_make_integer (arg_num),	                            \
	      zzz_make_string (zzz_in_call_to, -1),	                    \
	      zzz_make_string (s_func_name, -1),                            \
	      zzz_make_string ("', ", -1), 	                            \
              zzz_make_string (expected, -1),				    \
	      zzz_make_string (zzz_expected, -1),			    \
	      NULL));				                            \
	  return RESULT_EXCEPTION;					    \
	}								    \
    }									    \
  while (0)

#define CHECK_SAFE_FILENAME(no, filename, msg)		\
  do							\
    {							\
      CHECK_TYPE (no, string_len (filename) == 		\
		      strlen (string_val (filename)),	\
		  msg);					\
    }							\
  while (0);

#endif /* not __SIZZLE_VALIDATE_H__ */
