/*
   ***************************************************************************
   *
   * Datei:
   *      RSysFlush.c
   *
   * Inhalt:
   *
   *      --- Globale Routinen ---
   *
   *    void RSysFlushAll ( void );
   *    void RSysFlushAllFonts ( void );
   *    void RSysFlushAllLibraries ( void );
   *
   *      --- Lokale  Routinen ---
   *
   *
   * Bemerkungen:
   *      Speicherfreigabe durch Beseitigen von nicht benutzten
   *      Fonts und Libraries.
   *
   * Erstellungsdatum:
   *      07-Jul-93     Rolf Böhme
   *
   * Änderungen:
   *      07-Jul-93     Rolf Böhme        Erstellung
   *
   ***************************************************************************
 */

#include "RSys.h"
#include "protos.h"

 /*
  * FlushAllLibraries() entfernt alle geschlossenen Libraries aus
  * dem System
  */
void
RSysFlushAllLibraries (void)
{
  LIBRARY *result;
  NODE *node;
  int old_lib_cnt = 0;		/* GMD */
  int new_lib_cnt = 0;		/* GMD */
  int diff_lib_cnt = 0;		/* GMD */
  int flush_lib_cnt = 0;	/* GMD */
  char buf[80];

  HandleHelp (MN_RSysFlushAllLibraries);

  Forbid ();
  for (node = SysBase->LibList.lh_Head; node->ln_Succ;
       node = node->ln_Succ)
    {
      ++old_lib_cnt;
      result = (LIBRARY *) node;

      if (result->lib_OpenCnt == 0)
	{
	  ++flush_lib_cnt;
	  RemLibrary (result);
	}
    }
  Permit ();

  /*
     * now see how many libraries flushed themselves
   */
  if (flush_lib_cnt)
    {
      Forbid ();
      for (node = SysBase->LibList.lh_Head; node->ln_Succ;
	   node = node->ln_Succ)
	{
	  ++new_lib_cnt;
	}
      Permit ();
    }

  diff_lib_cnt = old_lib_cnt - new_lib_cnt;
  if (flush_lib_cnt == 0)
    {
      sprintf (buf, "No flushable libraries");
    }
  else
    {
      sprintf (buf, "Flushable libraries = %d, actual flushed = %d",
	       flush_lib_cnt, diff_lib_cnt);
    }

  PrintInfo (buf, SPEAK, (2 * SEC));

  RefreshList (LastID);
/*
   PrintStatistics ();
 */

  return;
}

/* 
 * Flush Devices (GMD)
 */

void
RSysFlushAllDevices (void)
{
  LIBRARY *result;
  NODE *node;
  int old_devs_cnt = 0;		/* GMD */
  int new_devs_cnt = 0;		/* GMD */
  int diff_devs_cnt = 0;	/* GMD */
  int flush_devs_cnt = 0;	/* GMD */
  char buf[80];

  HandleHelp (MN_RSysFlushAllLibraries);

  Forbid ();
  for (node = SysBase->DeviceList.lh_Head; node->ln_Succ;
       node = node->ln_Succ)
    {
      ++old_devs_cnt;
      result = (LIBRARY *) node;

      if (result->lib_OpenCnt == 0)
	{
	  ++flush_devs_cnt;
	  RemDevice ((DEVICE *) result);
	}
    }
  Permit ();

  /*
     * now see how many devices flushed themselves
   */
  if (flush_devs_cnt)
    {
      Forbid ();
      for (node = SysBase->DeviceList.lh_Head; node->ln_Succ;
	   node = node->ln_Succ)
	{
	  ++new_devs_cnt;
	}
      Permit ();
    }

  diff_devs_cnt = old_devs_cnt - new_devs_cnt;
  if (flush_devs_cnt == 0)
    {
      sprintf (buf, "No flushable devices");
    }
  else
    {
      sprintf (buf, "Flushable devices = %d, actual flushed = %d",
	       flush_devs_cnt, diff_devs_cnt);
    }

  PrintInfo (buf, SPEAK, (2 * SEC));

  RefreshList (LastID);

  PrintStatistics ();

  return;
}


 /*
  * RSysFlushAllFonts() entfernt alle unbenutzten Diskfonts aus
  * dem System
  */
void
RSysFlushAllFonts (void)
{
  TEXTFONT *tf;
  NODE *node;
  int old_font_cnt = 0;		/* GMD */
  int new_font_cnt = 0;		/* GMD */
  int diff_font_cnt = 0;	/* GMD */
  char buf[80];

  HandleHelp (MN_RSysFlushAllFonts);

  Forbid ();
  for (node = GfxBase->TextFonts.lh_Head; node->ln_Succ;
       node = node->ln_Succ)
    {
      tf = (TEXTFONT *) node;

      if ((tf->tf_Accessors == 0) && (tf->tf_Flags & FPF_DISKFONT))
	{
	  ++old_font_cnt;
	  RemFont (tf);
	  Remove (node);
	}
    }
  Permit ();

  /*
     * now see how many fonts were  flushed
   */

  Forbid ();
  for (node = GfxBase->TextFonts.lh_Head; node->ln_Succ;
       node = node->ln_Succ)
    {
      tf = (TEXTFONT *) node;

      if ((tf->tf_Accessors == 0) && (tf->tf_Flags & FPF_DISKFONT))
	{
	  ++new_font_cnt;
	  RemFont (tf);
	  Remove (node);
	}
    }
  Permit ();

  diff_font_cnt = old_font_cnt - new_font_cnt;
  sprintf (buf, "%d fonts flushed", diff_font_cnt);

  PrintInfo (buf, SPEAK, (2 * SEC));

  RefreshList (LastID);

  PrintStatistics ();

  return;
}

void
RSysFlushAll (void)
{
  RSysFlushAllLibraries ();
  RSysFlushAllDevices ();
  RSysFlushAllFonts ();

  return;
}
