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

#include "EspaceC.h"
#include "TestAlloc.h"

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

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

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

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

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

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

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

