/*
***************************************************************************
*
* Datei:
*    RSysSpecialList.c
*
* Inhalt:
*    void RSysAmigaGuideRefList(void);
*    void SysBrokerList(void);
*    void SysAvailFontsList(void);
*
* Bemerkungen:
*    Spezielle Listen, die unter keine der anderen Kategorien fallen.
*
* Erstellungsdatum:
*    07-Jul-93    Rolf Böhme
*
* Änderungen:
*    07-Jul-93    Rolf Böhme    Erstellung
*
***************************************************************************
*/

#include "RSysDebug.h"
#include "RSysFunc.h"


void
RSysAmigaGuideRefList(void)
{
   struct List listbuff, *list = &listbuff;
   struct Node *node;
   struct XRef *xrefnode;
   LONG key;
   register int i;
   UBYTE *fpart, filename[16];
   char *XRefTypes[] = {
      "LINK", "FUNC", "COMM", "INCL", "MACR",
      "STRU", "FIEL", "TYPD", "DEFI"
   };

   PrintHeader(AGUIDEXREF, NULL);

   EmptyListView();

   countentries = 0;

   if (AmigaGuideBase = (struct AmigaGuideBase *)OpenLibrary ((STRPTR)"amigaguide.library", 33))
   {
      key = LockAmigaGuideBase(NULL);

      if (GetAmigaGuideAttr(AGA_XRefList, NULL, (ULONG *)&list))
      {
         countentries = CountNodes(list);

         if (!NoEntries())
         {
            Entries = AllocScrollEntries(countentries);

            for(node = list->lh_Head, i = 0; node->ln_Succ && (i < countentries);
                node = node->ln_Succ, i++)
            {
               xrefnode = (struct XRef *)node;

               fpart = FilePart(xrefnode->xr_File);

               if(fpart != xrefnode->xr_File)
                  sprintf((char *)filename,"+%-14s.14s",fpart);
               else
                  strncpy((char *)filename, (char *)xrefnode->xr_File, 15);

               sprintf(Entries[i].se_Entry, EntryAttr[32].ea_dataformat,
                       xrefnode->xr_Name, XRefTypes[node->ln_Type],
                       filename, xrefnode->xr_Line);
            }

            CreateEntryList(SORT);
         }
      }

      UnlockAmigaGuideBase(key);

      CloseLibrary((struct Library *)AmigaGuideBase);
   }
   else
      ErrorHandle(LIBRARY_ERR, OPEN_FAIL, KILL);

   PrintStatistics();

   return;
}

/*
 * Das sind die neuen Funktionen, die ich mit pragmas einbinde. Damit
 * entfällt auch gleich das Prototyping. Alle Daten können mit dem
 * Exchange-Programm überprüft werden. Die Funktionen sind in den fd-Files
 * als private gekennzeichnet, was aber kein Prob sein sollte :-) Den
 * Namen und die Übergaberegisterbelegung entnahm ich dem Program
 * LIBSearcher v2.01 von Joep Grooten (lib201).
 */

/*
 * FindBroker() gibt die Adresse des Brokers zurück, dessen Name in a0
 * übergeben werden muß.
#pragma amicall(CxBase, 108, FindBroker(a0))
struct Node *FindBroker(STRPTR name);
 */

/*
 * CopyBrokerList() kopiert den Listenkopf der Knoten mit den Commodities
 * in eine zu übergebene mit MEMF_PUBLIC allozierte Liste. Offenbar gibt
 * die Funktion die Anzahl der eingetragenen Commodities zurück. Die Liste
 * scheint eine einfache normale struct List zu sein, ebenso wie die
 * Knoten. Jedenfalls gibts beim Auslesen keine Probleme.
#pragma amicall(CxBase, 186, CopyBrokerList(a0))
LONG  CopyBrokerList(struct List *bList);
 */

/*
 * FreeBrokerList() scheint den Inhalt der allozierten Liste wieder
 * freizugeben. Allerdings sind Seiteneffekte zu vermuten. Vielleicht wird
 * auch die Commodities-Liste gekillt!
#pragma amicall(CxBase, 192, FreeBrokerList(a0))
void  FreeBrokerList(struct List *bList);
 */

#define MAX_BROKER_ENTRIES 40

void
SysBrokerList(void)
{
   struct Node *node,
        *nodes[MAX_BROKER_ENTRIES],
        *StartNode,
        *foundBroker = NULL;
   char  name[MAX_BROKER_ENTRIES][22];
   int   pri[MAX_BROKER_ENTRIES],
         i,
         cnt;
   extern CxObj *broker;

   PrintHeader(COMMODITIES, NULL);

   EmptyListView();

      /*
       * Analog zu FindName() oder FindPort() kann auch hier ein
       * Schutzprotokoll nötig sein. Da Commodities auch mit Interrupts
       * arbeiten müssen (InputHandler!), sollte gleich mit
       * Disable()/Enable() hingelangt werden!
       */
   Disable();
/*   foundBroker = FindBroker((STRPTR) "RSys");*/

   foundBroker = (struct Node *)broker;

   for (node = foundBroker; node->ln_Pred; node = node->ln_Pred)
      StartNode = node;

   for (node = StartNode, cnt = 0;
        node->ln_Succ && (cnt < MAX_BROKER_ENTRIES);
        node = node->ln_Succ)
   {
         /*
          * Vitale Daten kopieren; der Type ist unbekannt! Damit
          * dennoch die Namen ausgegeben werden können, übergebe ich
          * einfach den Node-Typen.
          */
      savestrcpy(name[cnt], node, 22, (int)node->ln_Type);

      pri[cnt]       = node->ln_Pri;
      nodes[cnt++]   = node;
   }

   Enable();

   countentries = cnt;

   if(NoEntries())
      return;

   Entries = AllocScrollEntries(countentries);

   for (i = 0; i < cnt; i++)
   {
      sprintf(Entries[i].se_Entry,
              EntryAttr[26].ea_dataformat,
              name[i],
              pri[i]);

      AddNodeToList(i, SORT);
   }

   PrintStatistics();

   return;
}

   /*
    * SysAvailFontsList() listet alle Fonts im Verzeichnis
    * FONTS: auf
    */
void
SysAvailFontsList(void)
{
   char  Style[MAXSHOW];
   char *Type[5] =
   {
      "-", "ROM", "DISK", "-", "SCAL"
   };
   struct AvailFontsHeader *afh;
   struct AvailFonts *AF;
   int   extrabytes,
         initialsize = sizeof(struct AvailFontsHeader),
         i;

   DPOS;

   PrintHeader(ALLFONTS, NULL);

   EmptyListView();

   PrintInfo("Read all Fonts", NO_SPEAK, 0);

   do
   {
      afh = (struct AvailFontsHeader *)
            MyAllocVec(initialsize, MEMF_CLEAR | MEMF_ANY, KILL);

      if (afh)
      {
         extrabytes = AvailFonts((STRPTR) afh, initialsize, AFF_DISK);
         if (extrabytes)
         {
            MyFreeVec(afh);
            initialsize += extrabytes;
         }
      }
   }
   while (extrabytes);

   countentries = afh->afh_NumEntries;

   if (NoEntries())
      return;

   Entries = AllocScrollEntries(countentries);

   AF = (struct AvailFonts *) & (afh[1]);

   for (i = 0; i < countentries; i++)
   {
      strcpy(Style, "       ");

      if (!(AF[i].af_Attr.ta_Style & FS_NORMAL))
         Style[0] = 'N';

      if (AF[i].af_Attr.ta_Style & FSF_UNDERLINED)
         Style[1] = 'U';

      if (AF[i].af_Attr.ta_Style & FSF_BOLD)
         Style[2] = 'B';

      if (AF[i].af_Attr.ta_Style & FSF_ITALIC)
         Style[3] = 'I';

      if (AF[i].af_Attr.ta_Style & FSF_EXTENDED)
         Style[4] = 'E';

      if (AF[i].af_Attr.ta_Style & FSF_COLORFONT)
         Style[5] = 'C';

      if (AF[i].af_Attr.ta_Style & FSF_TAGGED)
         Style[6] = 'T';

      Style[7] = STRINGEND;

      sprintf(Entries[i].se_Entry, EntryAttr[17].ea_dataformat,
              AF[i].af_Attr.ta_Name,
              Type[AF[i].af_Type],
              AF[i].af_Attr.ta_YSize,
              Style);

      AddNodeToList(i, SORT);
   }

   MyFreeVec(afh);

   PrintStatistics();

   return;
}
