/***************************************************************************
 * output.c:	Functions for printing the output.
 *
 * Part of...
 *
 *	FREE:	Display free space on your disk volumes.
 *		Author:  Daniel Jay Barrett, barrett@cs.jhu.edu.
 *
 * This program is Freely Distributable.  Make all the copies you want
 * and give them away.  Use this code in any way you like.
***************************************************************************/

#include "free.h"

/* Show the amount of free space on the volume. */

void ShowFree(char *volume, long chipRam, long fastRam, char out[])
{
	if (ValidDriveName(volume))
	{
		if (!StrCaseCmp(volume, "RAM:"))
			ShowFreeRam(chipRam, fastRam, out);
		else
			DoNormalDrive(volume, out);
	}
	else
		fprintf(stderr, "ERROR!  \"%s\" MUST END WITH '%c'.\n",
			volume, VOLUME_END_CHAR);
}


/* Copy "free RAM" information into the buffer "out[]".  We use
 * MakeFormatString() to make sure that the CHIP RAM amount is
 * properly indented to match other output... and then just hack
 * the FAST RAM and TOTAL RAM values onto the end.  If you have no FAST
 * RAM, you won't see these last two values. */

void ShowFreeRam(long chip, long fast, char out[])
{
	char tmp[BUFSIZ];	/* This should hold 1 line of output... */
	char formatString[FORMAT_LENGTH];

	MakeFormatString("Chip", formatString, 'd', NO_CR);
	sprintf(tmp, formatString, HI_ON, "Chip", HI_OFF, chip);
	Concat(out, tmp);

	if (fast)
	{
		sprintf(tmp, "   %s%s%s%10ld   %s%s%s%9ld",
			HI_ON, "Fast", HI_OFF, fast,
	   		HI_ON, "Total", HI_OFF, chip+fast);
		Concat(out, tmp);
	}

	Concat(out, "\n");
}


/* Find the free space on a normal disk drive, and append it to the
 * buffer "out[]".  We use MakeFormatString() for matching indenting. */

void DoNormalDrive(char *volume, char out[])
{
	long mem;
	char tmp[BUFSIZ];	/* This should hold 1 line of output... */
	char formatString[FORMAT_LENGTH];

	mem = GetMem(volume);

	if (mem != NO_DRIVE)
	{
		MakeFormatString(volume, formatString, 'd', CR);
		sprintf(tmp, formatString, HI_ON, volume, HI_OFF, mem);
	}
	else
	{
		MakeFormatString(volume, formatString, 's', CR);
		sprintf(tmp, formatString, HI_ON, volume, HI_OFF, NONE);
		}
	Concat(out, tmp);
}
