#ifdef MSDOS
#include "dmsmgr.hpp"
#include <io.h>

dmsmgr::dmsmgr(unsigned long the_size, unsigned long num_elements) // numelements defaults to 1
{
	installed = FALSE;
	errcode = 0;
	thesize = the_size;
	numelements = num_elements;
	return;
}

dmsmgr::~dmsmgr()
{
	if (fp)
		fclose (fp);
}

void
dmsmgr::alloc_dmb()
{
	fp = tmpfile();
	if (fp)		{
		installed = TRUE;
		chsize (fileno (fp), thesize * numelements);
	} else	{
		installed = FALSE;
		errcode = errno;
	}
	return;
}

char* dmsmgr::dmserr()
{
	static char errstring[26];

	sprintf (errstring, "DMS Disk Error #%d.", errcode);
	return errstring;
}

Boolean dmsmgr::stow (char far* send, size_t listnum)
{
size_t done;

	if (!is_installed())
		return FALSE;
	done = fseek (fp, listnum * thesize, SEEK_SET);
	if (done)	{
		errcode = errno;
		return FALSE;
	}
	done = fwrite (send, thesize, 1, fp);
	if (done == thesize)
		return TRUE;
	else	{
		errcode = errno;
		return FALSE;
	}
}

Boolean dmsmgr::fetch (char far* receive, size_t listnum)
{
size_t done;

	if (!is_installed())
		return FALSE;
	done = fseek (fp, listnum * thesize, SEEK_SET);
	if (done)	{
		errcode = errno;
		return FALSE;
	}
	done = fread (receive, thesize, 1, fp);
	if (done == thesize)
		return TRUE;
	else	{
		errcode = errno;
		return FALSE;
	}
}
#endif /* MSDOS */
