
#include "RobboLito_def.h"
#include "RobboTotalBase.h"
#include <string.h>

boolean TOTAL_INIT = FALSE;
boolean REGISTERED = FALSE;
void TotalBaseInit ()
{
  int pi1, pi2, pi3, pi4;
  int i;
  TOTAL_INIT = TRUE;
  InitTotalBaseUtility ();
  NUM_TOTAL_BASES = 0;
  for (pi1 = 0; pi1 < 16; pi1++)
    for (pi2 = 0; pi2 < 16; pi2++)
      for (pi3 = 0; pi3 < 16; pi3++)
        for (pi4 = 0; pi4 < 16; pi4++)
          TotalBaseTable[pi1][pi2][pi3][pi4] = -1;
  TABLE_TOTAL_BASES = malloc (MAX_TABLES * sizeof (RobboTotalBase));
  for (i = 0; i < MAX_TABLES; i++)
    (TABLE_TOTAL_BASES + i)->Fdata = NULL;
  for (i = 0; i < MAX_TABLES; i++)
    (TABLE_TOTAL_BASES + i)->indici = NULL;
  InitInitTotalBaseCache (1);
  TOT_INDICI = 0;
}

int RegisterRobboTotalBases (char *string)
{
  char DIR[1024];
  if (!TOTAL_INIT)
    TotalBaseInit ();
  DeRegisterRobboTotalBases ();
  strcpy (TOTAL_BASE_DIRECTORY, string);
  sprintf (DIR, "%s", TOTAL_BASE_DIRECTORY);
  GetTotalBase (DIR);
  sprintf (DIR, "%s/3", TOTAL_BASE_DIRECTORY); /* for the WINDOWS: \ ? */
  GetTotalBase (DIR);
  sprintf (DIR, "%s/4", TOTAL_BASE_DIRECTORY);
  GetTotalBase (DIR);
  sprintf (DIR, "%s/5", TOTAL_BASE_DIRECTORY);
  GetTotalBase (DIR);
  sprintf (DIR, "%s/6", TOTAL_BASE_DIRECTORY);
  GetTotalBase (DIR);
  sprintf (DIR, "%s/51", TOTAL_BASE_DIRECTORY);
  GetTotalBase (DIR);
  sprintf (DIR, "%s/42", TOTAL_BASE_DIRECTORY);
  GetTotalBase (DIR);
  sprintf (DIR, "%s/33", TOTAL_BASE_DIRECTORY);
  GetTotalBase (DIR);
  sprintf (DIR, "%s/Z", TOTAL_BASE_DIRECTORY);
  GetTotalBase (DIR);
  sprintf (DIR, "%s/345Z", TOTAL_BASE_DIRECTORY);
  GetTotalBase (DIR);
  sprintf (DIR, "%s/6Z", TOTAL_BASE_DIRECTORY);
  GetTotalBase (DIR);
  SEND ("info string "
        "%d RobboTotalBases registered with " TYPE_64_BIT " indici %dmb\n",
        NUM_TOTAL_BASES, TOT_INDICI, (int) ((TOT_INDICI * 4) >> 20));
  REGISTERED = TRUE;
  return TRUE;
}

int DeRegisterRobboTotalBases ()
{
  int pi1, pi2, pi3, pi4, i;
  RobboTotalBase* tb;
  for (i = 0; i < NUM_TOTAL_BASES; i++)
    {
      tb = (TABLE_TOTAL_BASES + i);
      if (tb->indici)
        free (tb->indici);
      if (tb->Fdata)
        fclose (tb->Fdata);
      tb->Fdata = NULL;
    }
  NUM_TOTAL_BASES = 0;
  TOT_INDICI = 0;
  for (pi1 = 0; pi1 < 16; pi1++)
    for (pi2 = 0; pi2 < 16; pi2++)
      for (pi3 = 0; pi3 < 16; pi3++)
        for (pi4 = 0; pi4 < 16; pi4++)
          TotalBaseTable[pi1][pi2][pi3][pi4] = -1;
  SetTotalBaseCache (CURRENT_TOTAL_BASE_CACHE);
  REGISTERED = FALSE;
  return TRUE;
}

int TotalUsage ()
{
  return REGISTERED;
}
