// format_old_roman().

// General includes.
#include "cl_sysdep.h"

// Specification.
#include "cl_format.h"


// Implementation.

#include "cln/integer.h"
#include "cln/integer_io.h"
#include "cln/abort.h"

namespace cln {

void format_old_roman (std::ostream& stream, const cl_I& arg)
{
	if (!(0 < arg && arg < 5000)) {
		fprint(std::cerr, "format_old_roman: argument should be in the range 1 - 4999, not ");
		fprint(std::cerr, arg);
		fprint(std::cerr, ".\n");
		cl_abort();
	}
	var uintL value = cl_I_to_UL(arg);
	struct roman { char symbol; uintL value; };
	static const roman scale[7] = {
		{ 'I',    1 },
		{ 'V',    5 },
		{ 'X',   10 },
		{ 'L',   50 },
		{ 'C',  100 },
		{ 'D',  500 },
		{ 'M', 1000 },
	};
	for (int i = 6; value > 0 /* && i >= 0 */ ; i--) {
		var const roman * p = &scale[i];
		var uintL multiplicity = floor(value,p->value);
		value = value % p->value;
		while (multiplicity > 0) {
			fprintchar(stream,p->symbol);
			multiplicity--;
		}
	}
}

}  // namespace cln
