home *** CD-ROM | disk | FTP | other *** search
/ ST-Computer Leser 1998 October / STC_CD_10_1998.iso / BASE / BGH / C___1 / BGH.CXX < prev    next >
C/C++ Source or Header  |  1998-09-26  |  10KB  |  386 lines

  1. #include <aes.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <osbind.h>
  5. #include <string.h>
  6. #include <portab.h>
  7.  
  8. #include "bgh.h"
  9.  
  10. #define BGH_MAGIC 0x23424748
  11.  
  12. typedef struct
  13. {
  14.     LONG    Magic;            /* #BGH */
  15.     LONG    resvd[3];
  16. } BGH_Head;
  17.  
  18. typedef struct bgh_gruppe
  19. {
  20.     WORD                        Index;
  21.     struct bgh_gruppe*    Next;
  22.     WORD                        First;
  23.     BYTE                        StringTyp;
  24. } BGH_Gruppe;
  25.  
  26. typedef struct bgh_object
  27. {
  28.     WORD    Index;
  29.     WORD    Next;
  30.     BYTE    StringTyp;
  31.     BYTE    Link;
  32. } BGH_Object;
  33.  
  34.  
  35. static WORD WPEEK(void* Word)
  36. {
  37.     register UBYTE* wordp = (UBYTE*)Word;
  38.     return (wordp[0] << 8 | wordp[1]);
  39. }
  40. static inline void WPOKE(void* Word, WORD Wert)
  41. {
  42.     register UBYTE* wordp = (UBYTE*)Word;
  43.     wordp[0] = Wert >> 8;
  44.     wordp[1] = Wert;
  45. }
  46. static LONG LPEEK(void* Long)
  47. {
  48.     register UBYTE* longp = (UBYTE*)Long;
  49.     return (longp[0] << 24 | longp[1] << 16 | longp[2] << 8 | longp[3]);
  50. }
  51. static inline void LPOKE(void* Long, LONG Wert)
  52. {
  53.     register UBYTE* longp = (UBYTE*)Long;
  54.     longp[0] = Wert >> 24;
  55.     longp[1] = Wert >> 16;
  56.     longp[2] = Wert >> 8;
  57.     longp[3] = Wert;
  58. }
  59.  
  60.  
  61. static void* MyMxalloc(LONG Ammount, WORD Mode)
  62. {
  63.     void* result = 0;
  64.  
  65.     void *svStack;      /* Supervisorstack */
  66.     LONG sRAM, sRAMg;   /* ST-RAM          */
  67.     LONG aRAM, aRAMg;   /* Alternate RAM   */
  68.  
  69.  
  70.     /*
  71.     // Beispieltabelle moeglicher Werte:
  72.     //           | newfashion  | oldfashion
  73.     // sRAM aRAM | sRAMg aRAMg | sRAMg aRAMg
  74.     //   1    0  |   1     0   |   1     1
  75.     //   0    2  |   0     2   |   2     2
  76.     //   1    2  |   1     2   |   3     3
  77.     */
  78.  
  79.     svStack = (void*)Super(0);  /* unterbinde Taskwechsel */
  80.  
  81.     sRAM  = (LONG) Mxalloc( -1, 0);
  82.     sRAMg = (LONG) Mxalloc( -1, 0x40); /* im Fehlerfall Mxalloc( -1, 3) */
  83.     aRAM  = (LONG) Mxalloc( -1, 1);
  84.     aRAMg = (LONG) Mxalloc( -1, 0x41); /* im Fehlerfall Mxalloc( -1, 3) */
  85.  
  86.     Super( svStack);  /* erlaube Taskwechsel */
  87.  
  88.     if(sRAM == -32)
  89.         result = (void*)Malloc(Ammount);  /* Mxalloc() ist nicht implementiert */
  90.     else
  91.     {
  92.         if (((sRAM + aRAM) == sRAMg) && ((sRAM + aRAM) == aRAMg))
  93.             result = (void*)Mxalloc(Ammount, Mode & 3);  /* oldfashion Mxalloc() */
  94.         else
  95.             result = (void*)Mxalloc(Ammount, Mode);
  96.     }
  97.     return result;
  98. }
  99.  
  100. BGH::BGH(const char* Name)
  101. {
  102.     char Char, * Read, * LastText=0, * egal;
  103.     WORD i, Section, LastTextTyp=0;
  104.     LONG FileHandle, FileLen;
  105.  
  106.     BGH_Head*        Head = 0;
  107.     BGH_Gruppe*        LastGruppen[4];        /* für Gruppen-Verkettung */
  108.     BGH_Gruppe*        LastGruppe    = 0;        /* für 1.Objekt           */
  109.     BGH_Object*        LastObject    = 0;        /* für Objekt-Verkettung  */
  110.     BGH_Gruppe**     Sectionen;
  111.     BGH_Gruppe*        NewGruppe;
  112.     BGH_Object*        NewObject;
  113.  
  114.  
  115.     FileHandle = Fopen(Name, 0);                /* Datei öffnen */
  116.     if(FileHandle > 0)
  117.     {
  118.         FileLen = Fseek(0, FileHandle, 2);    /* Dateilänge ermitteln */
  119.         Fseek(0, FileHandle, 0);
  120.         if(FileLen > sizeof(BGH_Head))        /* Filelänge > Headerlänge */
  121.         {
  122.             Read = (char*) MyMxalloc(FileLen+1, 0x43);    /* Speicher anforderm     */
  123.             if(Read && FileLen == Fread(FileHandle, FileLen, Read)) /* Datei laden */
  124.             {
  125.                 Read[FileLen] = 0;                    /* Eine NULL anhängen */
  126.                 Head = (BGH_Head*) Read;
  127.                 if(Head->Magic == BGH_MAGIC)        /* ist es eine BGH-Datei ? */
  128.                 {
  129.                      Sectionen = (BGH_Gruppe**)Head;
  130.                     for(i=0; i<4; i++)
  131.                     {
  132.                         Sectionen[i]    =        /* Verkettungen zurücksetzen */
  133.                         LastGruppen[i]    = 0;
  134.                     }
  135.                     Read += sizeof(BGH_Head);        /* Header überspringen */
  136.  
  137.                     while(*Read)                        /* solange NULL nicht erreicht */
  138.                     {
  139.                         while(*Read && *Read != 0xa && *Read != 0xd)
  140.                             Read++;                                    /* CR oder LF suchen   */
  141.                         while(*Read == 0xa || *Read == 0xd)
  142.                             *Read++ = 0;                            /* CR und LF ausNULLen */
  143.                         if(*Read == '#')        /* ist 1.Zeichen nach CR/LF = '#' */
  144.                         {
  145.                             i = 0;
  146.                             switch(*++Read)   /* dann teste 2.Zeichen */
  147.                             {
  148.                                 case 'M':                        /* vergleichs SectionsID */
  149.                                     egal        = "More ";        /* vergleiche mit "More " */
  150.                                     i            = 5;
  151.                                     Section    = 0;
  152.                                     break;
  153.                                 case 'D':
  154.                                     egal        = "Dial ";        /* vergleiche mit "Dial " */
  155.                                     i            = 5;
  156.                                     Section    = 1;
  157.                                     break;
  158.                                 case 'A':
  159.                                     egal        = "Alert ";        /* vergleiche mit "Alert " */
  160.                                     i            = 6;
  161.                                     Section    = 2;
  162.                                     break;
  163.                                 case 'U':
  164.                                     egal        = "User ";        /* vergleiche mit "User " */
  165.                                     i            = 5;
  166.                                     Section    = 3;
  167.                                     break;
  168.                             }
  169.                             if(i && !strncmp(Read, egal, i)) /* SectionsId gefundeen */
  170.                             {
  171.                                 LastGruppe = 0;        /* wenn Fehler keine Objekte */
  172.                                 Read += i;                /* SectionsId überspringen   */
  173.                                 egal = Read;         /* Position 1. Ziffer merken */
  174.                                 while(*Read >= '0' && *Read <= '9')
  175.                                     Read++;                /* Ende der Zahl suchen      */
  176.                                 if(*Read == 0xa || *Read == ' ' || *Read == 0xd || !*Read)
  177.                                 {
  178.                                     if(*Read) *Read++ = 0;    /* Zahl mit NULL abschlie₧en    */
  179.  
  180.                                     if(Read - egal >= 3)    /* mindestens 3 Ziffern            */
  181.                                     {
  182.                                         NewGruppe    = (BGH_Gruppe*) ((ULONG)Read - sizeof(BGH_Gruppe));
  183.                                         /* GruppenStructur initialisieren */
  184.                                         WPOKE(&NewGruppe->Index, atoi(egal));
  185.                                         LPOKE(&NewGruppe->Next, 0);
  186.                                         WPOKE(&NewGruppe->First, 0);
  187.  
  188.                                         if(*Read == '>' && Read[1] >= '0' && Read[1] <= '9')
  189.                                         {
  190.                                             LastTextTyp = 1;
  191.                                             WPOKE(LastText, atoi(&Read[1]));
  192.                                             WPOKE(Read, atoi(&Read[1]));
  193.                                             NewGruppe->StringTyp = 2;
  194.                                             Read += 2;
  195.                                         }
  196.                                         else
  197.                                         {
  198.                                             LastTextTyp                = 0;
  199.                                             LastText                    = Read;
  200.                                             NewGruppe->StringTyp = 0;
  201.                                         }
  202.  
  203.                                         /* GruppenStructur verketten */
  204.                                         if(LastGruppen[Section])
  205.                                             LPOKE(&LastGruppen[Section]->Next, (LONG)NewGruppe);
  206.                                         else
  207.                                             Sectionen[Section]    = NewGruppe;
  208.                                         LastGruppen[Section]        =
  209.                                         LastGruppe                    = NewGruppe;
  210.                                         LastObject                    = 0;
  211.                                     }
  212.                                 }
  213.                             }
  214.                             else
  215.                             {
  216.                                 if(LastGruppe)    /* war schon eine Gruppe ? */
  217.                                 {
  218.                                     egal = ++Read;    /* ObjektStructur initialisieren */
  219.                                     while(*Read >= '0' && *Read <= '9')
  220.                                         Read++;
  221.                                     if(*Read == ' ' && Read - egal >= 3)
  222.                                     {
  223.                                         *Read++ = 0;
  224.  
  225.                                         NewObject                    = (BGH_Object*) ((ULONG)Read - sizeof(BGH_Object));
  226.                                         WPOKE(&NewObject->Index, atoi(egal));
  227.                                         WPOKE(&NewObject->Next, 0);
  228.                                         NewObject->StringTyp = 0;
  229.                                         switch(*Read)
  230.                                         {
  231.                                         case '^':
  232.                                             if(Read[1] == 0xa || Read[1] == 0xd || !Read[1])
  233.                                             {
  234.                                                 if(LastTextTyp)
  235.                                                 {
  236.                                                     WPOKE(&NewObject->Link, WPEEK(LastText));
  237.                                                     NewObject->StringTyp = 2;
  238.                                                 }
  239.                                                 else
  240.                                                 {
  241.                                                     WPOKE(&NewObject->Link, (ULONG)LastText - (ULONG)NewObject);
  242.                                                     NewObject->StringTyp = 1;
  243.                                                 }
  244.                                                 Read++;
  245.                                             }
  246.                                             break;
  247.                                         case '>':
  248.                                             if(Read[1] >= '0' && Read[1] <= '9')
  249.                                             {
  250.                                                 LastTextTyp = 1;
  251.                                                 WPOKE(LastText, atoi(&Read[1]));
  252.                                                 WPOKE(&NewObject->Link, atoi(&Read[1]));
  253.                                                 NewObject->StringTyp = 2;
  254.  
  255.                                                 Read++;
  256.                                             }
  257.                                             break;
  258.                                         default:
  259.                                             break;
  260.                                         }
  261.                                         if(!NewObject->StringTyp)
  262.                                         {
  263.                                             LastTextTyp    = 0;
  264.                                             LastText        = Read;
  265.                                         }
  266.  
  267.                                         /* ObjektStructur verketten */
  268.                                         if(LastObject)
  269.                                             WPOKE(&LastObject->Next, (ULONG)NewObject - (ULONG)LastObject);
  270.                                         else
  271.                                             WPOKE(&LastGruppe->First, (ULONG)NewObject - (ULONG)LastGruppe);
  272.                                         LastObject                    = NewObject;
  273.                                     }
  274.                                 }
  275.                             }
  276.                         }
  277.                     }
  278.                 }
  279.             }
  280.         }
  281.         Fclose(FileHandle);
  282.     }
  283.     MemoryBase = (void*) Head;
  284. }
  285.  
  286. BGH::~BGH()
  287. {
  288.     if(MemoryBase)    Mfree(MemoryBase);    /* Speicher freigeben            */
  289. }
  290.  
  291. LONG BGH::operator signed()
  292. {
  293.     return (LONG) MemoryBase;
  294. }
  295.  
  296. char*    BGH::GetHelpString(WORD Type, WORD Gruppe, WORD Index)
  297. {
  298.     BGH_Gruppe**     Sectionen    = (BGH_Gruppe**) MemoryBase;
  299.     char*                result        = 0;
  300.     BGH_Gruppe*        GruppeP;
  301.     BGH_Object*        ObjectP;
  302.  
  303.  
  304.     if(Sectionen && Type >= 0 && Type <= 3)
  305.     {
  306.         GruppeP = Sectionen[Type];        /* 1. Gruppe        */
  307.         while(GruppeP)                                /* Gruppe testen    */
  308.         {
  309.             if(WPEEK(&GruppeP->Index) == Gruppe)        /* Gruppe gefunden        */
  310.             {
  311.                 if(Index == -1)                    /* ist Index = -1 dann    */
  312.                 {
  313.                     result = ((char*)GruppeP + sizeof(BGH_Gruppe)); /* Gruppen-Help */
  314.                     if(GruppeP->StringTyp)
  315.                         result = GetHelpString(0, 0, WPEEK(result));
  316.                 }
  317.                 else                                    /* sonst */
  318.                 {
  319.                     if(WPEEK(&GruppeP->First))
  320.                         ObjectP = (BGH_Object*)((LONG)GruppeP + WPEEK(&GruppeP->First));
  321.                     else
  322.                         ObjectP = 0;                        /* 1. Objekt            */
  323.                     while(ObjectP)                            /* Objekt testen        */
  324.                     {
  325.                         if(WPEEK(&ObjectP->Index) == Index)    /* Objekt gefunden    */
  326.                         {
  327.                             switch(ObjectP->StringTyp)
  328.                             {
  329.                             case 1:
  330.                                 result = ((char*)ObjectP + WPEEK(&ObjectP->Link));
  331.                                 break;
  332.                             case 2:
  333.                                 result = GetHelpString(0, 0, WPEEK(&ObjectP->Link));
  334.                                 break;
  335.                             default:
  336.                                 result = ((char*)ObjectP + sizeof(BGH_Object));
  337.                                 break;
  338.                             }
  339.                             break;
  340.                         }
  341.                         if(WPEEK(&ObjectP->Next))                    /* nächstes Objekt    */
  342.                             ObjectP = (BGH_Object*)((LONG)ObjectP + WPEEK(&ObjectP->Next));
  343.                         else
  344.                             ObjectP = 0;
  345.                     }
  346.                 }
  347.                 break;
  348.             }
  349.             if(LPEEK(&GruppeP->Next))                                /* nächste Gruppe        */
  350.                 GruppeP = (BGH_Gruppe*)LPEEK(&GruppeP->Next);
  351.             else
  352.                 GruppeP = 0;
  353.         }
  354.     }
  355.     return result;
  356. }
  357.  
  358. void    BGH::Action(WORD Mx, WORD My, WORD Typ, WORD Gruppe, WORD Index)
  359. {
  360.     WORD BubbleId;
  361.     WORD Msg[8];
  362.  
  363.     char* HelpString = GetHelpString(Typ, Gruppe, Index);
  364.  
  365.     if(HelpString)
  366.     {
  367.         BubbleId = appl_find("BUBBLE  ");
  368.  
  369.         if(BubbleId >= 0)
  370.         {
  371.             Msg[0] = 0xbabb; /*BUBBLEGEM_SHOW;*/
  372.             Msg[1] = gl_apid;
  373.             Msg[2] = 0;
  374.             Msg[3] = Mx;
  375.             Msg[4] = My;
  376.             Msg[5] = (WORD)(((LONG)HelpString >> 16) & 0x0000ffff);
  377.             Msg[6] = (WORD)((LONG)HelpString & 0x0000ffff);
  378.             Msg[7] = 0;
  379.             if (appl_write(BubbleId, 16, Msg) == 0)
  380.             {
  381.             /* Fehler */
  382.             }
  383.         }
  384.     }
  385. }
  386.