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

#include "EspaceUC.h"
#include "TestAlloc.h"

EspaceUC::EspaceUC(int dx,int dy) {
  this->dx = dx;
  this->dy = dy;
  p =(unsigned char **) calloc(dy,sizeof(unsigned char *));
  testAllocAndExit(p,"new EspaceUC(int,int)");
  if ( p ) {
    for ( int i = 0 ; i < dy ; i++ ) {
      p[i] =(unsigned char *) calloc(dx+1,sizeof(unsigned char));
      testAllocAndExit(p[i],"new EspaceUC(int,int)",i); } }
}

EspaceUC::~EspaceUC(void) {
  for ( int i = 0 ; i < dy ; i++ )
    free(p[i]) ;
  free(p) ;
}

void EspaceUC::init(unsigned char v) {
  for ( int i = 0 ; i < dy ; i++ )
    memset(p[i],(int) v,(dx+1)*sizeof(unsigned char)) ;
}

unsigned char EspaceUC::cherche(int x,int y) {
  return(p[y][x]) ;
}

void EspaceUC::sauve(unsigned char c,int x,int y) {
  p[y][x] = c ;
}

long EspaceUC::getUsedMemory(void) {
  long cpt = sizeof(EspaceUC);
  cpt += dy*(dx+1)*sizeof(unsigned char) ;
  cpt += dy*sizeof(unsigned char *) ;
  return(cpt);
}

int EspaceUC::getUsedHandles(void) {
  int cpt = 1;
  cpt += dy ;
  cpt++ ;
  return(cpt);
}

