include	"../lib/mctable.h"

$for (csilrdxp)
# MCT_PUT - Put value randomly (generic)

procedure mct_put$t (table, row, col, value)

pointer	table			# table descriptor
int	row			# row number
int	col			# column number
PIXEL	value			# data value

int	offset
pointer	base
errchk	mct_indef()

begin
	# Check pointer and magic number.
	if (table == NULL)
	    call error (0, "mct_put: Null table pointer")
	if (MCT_MAGIC (table) != MAGIC)
	    call error (0, "mct_put: Bad magic number")

	# Check the table type.
	if (MCT_TYPE (table) != TY_PIXEL)
	    call error (0, "mct_put: Wrong table type")

	# Test row and column values.
	if (row < 1)
	    call error (0, "mct_put: Row number less than one")
	if (col < 1 || col > MCT_MAXCOL (table))
	    call error (0, "mct_put: Column out of range")

	# Reallocate space if necessary.
	if (row > MCT_MAXROW (table)) {

	    # Compute offset of new area.
	    offset = MCT_MAXROW (table) * MCT_MAXCOL (table)

	    # Recompute new number of rows and reallocate buffer.
	    MCT_MAXROW (table) = MCT_MAXROW (table) + MCT_INCROWS (table)
	    call realloc (MCT_DATA (table), 
		MCT_MAXROW (table) * MCT_MAXCOL (table), TY_PIXEL)

	    # Compute base address of new area and clear it with INDEF.
	    base = MCT_DATA (table) + offset
	    call mct_indef (table, base,
			    MCT_INCROWS (table) * MCT_MAXCOL (table))
	}

	# Update row and column counter, only if the new entries are beyond
	# the old limits.

	if (row > MCT_NPROWS (table)) {
	    MCT_NPROWS (table) = row
	    MCT_NPCOLS (table) = col
	} else if (col > MCT_NPCOLS (table))
	    MCT_NPCOLS (table) = col

	# Enter variable.
	base = MCT_DATA (table) + (row - 1) * MCT_MAXCOL (table)
	$if (datatype == p)
	MEMP[base + col - 1] = value
	$else
	Mem$t[base + col - 1] = value
	$endif
end
$endfor
