/*
 *      Copyright (c) 2000 Paul Campbell/VeriFarm Inc
 *
 *  This program 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 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "veriuser.h"
#include "acc_user.h"
#include <string.h>
#ifndef NULL
#define NULL 0
#endif

//
//	data:	0 - $strlen
//		1 - $strcat
//		2 - $strcpy
//		3 - $strchr
//		4 - $strrchr
//		5 - $strcmp
//		6 - $strstr
//		7 - $strfetch
//		

int
check_strings(int data, int reason)
{
	int count = tf_nump();
	switch (data) {
	case 0:	if (count != 1)
			tf_message(ERR_ERROR, "User", "TFARG", "Wrong number of parameters to $strlen"); 
		break;
	case 1:	if (count != 2)
			tf_message(ERR_ERROR, "User", "TFARG", "Wrong number of parameters to $strcat"); 
		break;
	case 2:	if (count != 2)
			tf_message(ERR_ERROR, "User", "TFARG", "Wrong number of parameters to $strcpy"); 
		break;
	case 3:	if (count != 2)
			tf_message(ERR_ERROR, "User", "TFARG", "Wrong number of parameters to $strchr"); 
		break;
	case 4:	if (count != 2)
			tf_message(ERR_ERROR, "User", "TFARG", "Wrong number of parameters to $strrchr"); 
		break;
	case 5:	if (count != 2)
			tf_message(ERR_ERROR, "User", "TFARG", "Wrong number of parameters to $strcmp"); 
		break;
	case 6:	if (count != 2)
			tf_message(ERR_ERROR, "User", "TFARG", "Wrong number of parameters to $strstr"); 
		break;
	case 7:	if (count != 2)
			tf_message(ERR_ERROR, "User", "TFARG", "Wrong number of parameters to $strfetch"); 
		break;
	}
	return(1);
}

int
call_strings(int data, int reason)
{
	char	tmp[65536];
	int count = tf_nump();
	s_setval_value s;
	char *cp, *cp2;
	int i;
	s_setval_delay d;

	switch (data) {
	case 0:
		cp = acc_fetch_tfarg_str(1);		// strlen
		i = (cp == NULL ? 0 : strlen(cp));
		tf_putp(0, i);
		break;
	case 1: cp = acc_fetch_tfarg_str(1);		//strcat
		if (cp) {
			strncpy(tmp, cp, sizeof(tmp));	// take a copy from the string buffer
			tmp[sizeof(tmp)-1] = 0;
			cp = acc_fetch_tfarg_str(2);
			if (cp) {
				strncat(tmp, cp, sizeof(tmp)-strlen(tmp));
				tmp[sizeof(tmp)-1] = 0;
			}
		} else {
			tmp[0] = 0;
		}
		s.value.str = tmp;
		s.format = accStringVal;
		d.model = accNoDelay;
		acc_set_value(acc_handle_tfarg(1), &s, &d);
		tf_putp(0, 0);
		break;

	case 2: cp = acc_fetch_tfarg_str(2);		// strcpy
		if (cp) {
			strncpy(tmp, cp, sizeof(tmp));	// take a copy from the string buffer
			tmp[sizeof(tmp)-1] = 0;
		} else {
			tmp[0] = 0;
		}
		s.value.str = tmp;
		s.format = accStringVal;
		d.model = accNoDelay;
		acc_set_value(acc_handle_tfarg(1), &s, &d);
		tf_putp(0, 0);
		break;

	case 3: cp = acc_fetch_tfarg_str(2);		// strchr
		if (cp && *cp) {
			i = *cp;
			cp = acc_fetch_tfarg_str(1);
			if (cp) {
				cp2 = strchr(cp, i);
				if (cp2 == NULL) {
					i =-1;
				} else {
					i = cp2-cp;
				}
			} else {
				i = -1;
			}
		} else {
			i = -1;
		}
		tf_putp(0, i);
		break;

	case 4: cp = acc_fetch_tfarg_str(2);		// strrchr
		if (cp && *cp) {
			i = *cp;
			cp = acc_fetch_tfarg_str(1);
			if (cp) {
				cp2 = strrchr(cp, i);
				if (cp2 == NULL) {
					i =-1;
				} else {
					i = cp2-cp;
				}
			} else {
				i = -1;
			}
		} else {
			i = -1;
		}
		tf_putp(0, i);
		break;

	case 5:		
		cp =  tf_getcstringp(1);
		if (cp) {
			strncpy(tmp, cp, sizeof(tmp));	
			tmp[sizeof(tmp)-1] = 0;
			cp =  tf_getcstringp(2);
			if (cp) {
				i = strcmp(tmp, cp);
			} else {
				i = 0;
			}
		} else {
			i = 0;
		}
		tf_putp(0, i);
		break;
	case 6: cp = acc_fetch_tfarg_str(2);		// strstr
		if (cp) {
			strncpy(tmp, cp, sizeof(tmp));	
			tmp[sizeof(tmp)-1] = 0;
			i = *cp;
			cp = acc_fetch_tfarg_str(1);
			if (cp) {
				cp2 = strstr(cp, tmp);
				if (cp2 == NULL) {
					i =-1;
				} else {
					i = cp2-cp;
				}
			} else {
				i = -1;
			}
		} else {
			i = -1;
		}
		tf_putp(0, i);
	case 7: i = acc_fetch_tfarg_int(2);		// strfetch
		cp = acc_fetch_tfarg_str(1);
		if (cp) {
			if (i < 0 || strlen(cp) <= i) {
				i = -1;
			} else {
				i = cp[i]&0xff;
			}
		} else {
			i = -1;
		}
		tf_putp(0, i);
		break;

	}
	return(0);
}
