/*                      QBCC Project

This source file is licensed under the GNU Lesser Public Licence which should be 
included with the distribution of this source.

*/
// Input Functions


#include "../include/conlib.h"
#include "../include/stringh.h"


/* Standard INPUT function */
void INPUT(char *fmt,...)
{
	int UseNewline = FALSE;
	int UsePrompt = FALSE;
	va_list p;
	string *Str;
	int *Integer;
	float *Single;
	char *promptstr;
	char InputStr[1024];
	char *format;
	char *token;
	
	va_start(p, fmt);

	format = fmt;

	do {    
		// Prompt String
		if(*fmt == 'p')
		{
			promptstr = (char *)va_arg(p, char *);
			printf("%s",promptstr);
			UsePrompt = TRUE;
		}
	} while (*(++fmt) != '\0');

	if(!UsePrompt)
		printf("? ");

	scanf("%[^\n]",InputStr);
	token = strtok(InputStr,",");

	while(token != NULL)
	{
		if(*format == 'p')
			format++;
		if(*format == 's')
		{
			Str = (string *)va_arg(p,string *);
			Str->ptr = realloc(Str->ptr, strlen(token) + 1);
			strcpy(Str->ptr, token);
			Str->length = strlen(token);
		}
		if(*format == 'i')
		{
			Integer = (int *)va_arg(p,int *);
			*Integer = atoi(token);
		}
		if(*format == 'f')
		{
			Single = (float *)va_arg(p,double *);
			*Single = (float)atof(token);
		}

		token = strtok(NULL,",");
		format++;
		if(*format == '\0')
			break;
	}	
}