/* PR-QWK - QWK Importer for UMS
 * Copyright (C) 1998 J.Ross Nicoll
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <proto/dos.h>

#include <stdlib.h>
#include <string.h>

#include "pr-qwk.h"

#define Local extern
#define Prototype extern
#include "PR-QWK-protos.h"

Prototype LONG PutC(UBYTE);
Prototype STRPTR ReadLine(BPTR);
Prototype STRPTR ReadString(BPTR);

LONG PutC(UBYTE Char)
{
	return(FPutC(OutputHandle, Char));
}

/* ---------------------------------------------------------------------- */

STRPTR ReadLine(BPTR FromFile)
{
	STRPTR input;
	STRPTR output = NULL;
	TEXT inputSpace[101];
	ULONG outputLength = 0;

	input = (STRPTR)&inputSpace;

	if(FromFile == NULL) return(NULL);

	while(strchr(output, 10) == NULL)
	{
		outputLength = outputLength + 100;
		output = ReallocPoolVec(output, sizeof(char) * (outputLength + 1));
		memset(output + outputLength, 0, 1);
		if(output == NULL) return(NULL);
		FGets(FromFile, input, 100);
		strcat(output, input);
	}

	memset(strchr(output, 10), 0, 1);
	output = ReallocPoolVec(output, strlen(output) + 1);

	return(output);
}

/* ---------------------------------------------------------------------- */

STRPTR ReadString(BPTR FromFile)
{
	BOOL end = FALSE;
	STRPTR output = NULL;
	TEXT buffer[101];
	UBYTE readIn;
	ULONG base;
	ULONG outputSize = 0;

	if(FromFile == NULL) return(NULL);
	base = Seek(FromFile, 0, OFFSET_CURRENT);

	do
	{
		outputSize = outputSize + 100;
		output = ReallocPoolVec(output, outputSize + 1);
		if(output == NULL) return(NULL);
		readIn = FRead(FromFile, buffer, sizeof(TEXT), 100);
		buffer[readIn] = 0;
		strcpy(output + outputSize - 100, buffer);
	}while(strlen(buffer) == 100);

	output = ReallocPoolVec(output, strlen(output) + 1);
	Seek(FromFile, base + strlen(output) + 1, OFFSET_BEGINNING);

	return(output);
}
