home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / term33so.lha / termFastMacros.c < prev    next >
C/C++ Source or Header  |  1993-04-30  |  8KB  |  415 lines

  1. /*
  2. **    termFastMacros.c
  3. **
  4. **    Fast! macros support routines
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* NewFastMacro(STRPTR Macro,STRPTR Code):
  13.      *
  14.      *    Create a new fast! macro node.
  15.      */
  16.  
  17. struct MacroNode *
  18. NewFastMacro(STRPTR Macro,STRPTR Code)
  19. {
  20.     if(FastMacroCount + 1 < 10000)
  21.     {
  22.         struct MacroNode *Node;
  23.  
  24.         if(Node = (struct MacroNode *)AllocVec(sizeof(struct MacroNode) + 21 + 257,MEMF_ANY|MEMF_CLEAR))
  25.         {
  26.             Node -> mn_Macro    = (STRPTR)(Node + 1);
  27.             Node -> mn_Code        = &Node -> mn_Macro[21];
  28.  
  29.             strcpy(Node -> mn_Macro,Macro);
  30.             strcpy(Node -> mn_Code ,Code);
  31.  
  32.             return(Node);
  33.         }
  34.     }
  35.  
  36.     return(NULL);
  37. }
  38.  
  39.     /* RemFastMacro(struct MacroNode *Node):
  40.      *
  41.      *    Remove and deallocate a fast! macro node.
  42.      */
  43.  
  44. VOID
  45. RemFastMacro(struct MacroNode *Node)
  46. {
  47.     Remove((struct Node *)Node);
  48.  
  49.     FreeVec(Node);
  50. }
  51.  
  52.     /* GetFastMacro(LONG Offset):
  53.      *
  54.      *    Get a fast! macro node from the global list by
  55.      *    its index number.
  56.      */
  57.  
  58. struct MacroNode *
  59. GetFastMacro(LONG Offset)
  60. {
  61.     struct MacroNode    *Node;
  62.     LONG             i;
  63.  
  64.     Node = (struct MacroNode *)FastMacroList . lh_Head;
  65.  
  66.     for(i = 0 ; i < Offset ; i++)
  67.     {
  68.         if(!Node -> mn_Succ -> mn_Succ)
  69.             return(NULL);
  70.  
  71.         Node = Node -> mn_Succ;
  72.     }
  73.  
  74.     return(Node);
  75. }
  76.  
  77.     /* ClearFastMacroList(struct List *List):
  78.      *
  79.      *    Remove all nodes from the global fast! macro list
  80.      *    and free them on the way.
  81.      */
  82.  
  83. VOID
  84. ClearFastMacroList(struct List *List)
  85. {
  86.     struct Node *Node,*NextNode;
  87.  
  88.     Node = List -> lh_Head;
  89.  
  90.     while(NextNode = Node -> ln_Succ)
  91.     {
  92.         Remove(Node);
  93.  
  94.         FreeVec(Node);
  95.  
  96.         Node = NextNode;
  97.     }
  98. }
  99.  
  100.     /* GetFastMacroOffset(struct MacroNode *MacroNode):
  101.      *
  102.      *    Get the index number of a given node in the
  103.      *    global fast! macro list.
  104.      */
  105.  
  106. LONG
  107. GetFastMacroOffset(struct MacroNode *MacroNode)
  108. {
  109.     struct MacroNode    *Node;
  110.     LONG             Offset = 0;
  111.  
  112.     Node = (struct MacroNode *)FastMacroList . lh_Head;
  113.  
  114.     while(Node -> mn_Succ)
  115.     {
  116.         if(Node == MacroNode)
  117.             return(Offset);
  118.  
  119.         Offset++;
  120.  
  121.         Node = Node -> mn_Succ;
  122.     }
  123.  
  124.     return(~0);
  125. }
  126.  
  127.     /* MoveList(struct List *From,struct List *To):
  128.      *
  129.      *    Move the contents of a list to a different list.
  130.      */
  131.  
  132. VOID __regargs
  133. MoveList(struct List *From,struct List *To)
  134. {
  135.     struct Node *Node,*NextNode;
  136.  
  137.     Node = From -> lh_Head;
  138.  
  139.     while(NextNode = Node -> ln_Succ)
  140.     {
  141.         Remove(Node);
  142.  
  143.         AddTail(To,Node);
  144.  
  145.         Node = NextNode;
  146.     }
  147. }
  148.  
  149.     /* SaveFastMacros(STRPTR Name):
  150.      *
  151.      *    Save the fast! macro list to a file.
  152.      */
  153.  
  154. BYTE
  155. SaveFastMacros(STRPTR Name)
  156. {
  157.     struct IFFHandle    *Handle;
  158.     BYTE             Success = FALSE;
  159.  
  160.     if(Handle = (struct IFFHandle *)AllocIFF())
  161.     {
  162.         if(Handle -> iff_Stream = Open(Name,MODE_NEWFILE))
  163.         {
  164.             InitIFFasDOS(Handle);
  165.  
  166.             if(!OpenIFF(Handle,IFFF_WRITE))
  167.             {
  168.                 if(!PushChunk(Handle,ID_TERM,ID_CAT,IFFSIZE_UNKNOWN))
  169.                 {
  170.                     if(!PushChunk(Handle,ID_TERM,ID_FORM,IFFSIZE_UNKNOWN))
  171.                     {
  172.                         if(!PushChunk(Handle,0,ID_VERS,IFFSIZE_UNKNOWN))
  173.                         {
  174.                             struct TermInfo TermInfo;
  175.  
  176.                             TermInfo . Version    = TermVersion;
  177.                             TermInfo . Revision    = TermRevision;
  178.  
  179.                             if(WriteChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) == sizeof(struct TermInfo))
  180.                             {
  181.                                 if(PopChunk(Handle))
  182.                                     Success = FALSE;
  183.                                 else
  184.                                 {
  185.                                     if(!PushChunk(Handle,0,ID_WIND,sizeof(struct IBox)))
  186.                                     {
  187.                                         struct IBox SizeBox;
  188.  
  189.                                         if(FastWindow)
  190.                                         {
  191.                                             FastWindowLeft        = FastWindow -> LeftEdge;
  192.                                             FastWindowTop        = FastWindow -> TopEdge;
  193.                                             FastWindowHeight    = FastWindow -> Height;
  194.                                         }
  195.  
  196.                                         if(FastWindowHeight == -1)
  197.                                             SizeBox . Height = Window -> WScreen -> WBorTop + Window -> WScreen -> Font -> ta_YSize + 1 + 2 + SZ_Height(LISTVIEW_KIND,10,0) + 2 + 8;
  198.                                         else
  199.                                             SizeBox . Height = FastWindowHeight;
  200.  
  201.                                         if(FastWindowLeft == -1)
  202.                                             SizeBox . Left = Window -> WScreen -> Width - (6 + SZ_Width(LISTVIEW_KIND,NULL,19,NULL) + 6);
  203.                                         else
  204.                                             SizeBox . Left = FastWindowLeft;
  205.  
  206.                                         if(FastWindowTop == -1)
  207.                                             SizeBox . Top = Window -> WScreen -> WBorTop + Window -> WScreen -> Font -> ta_YSize + 1;
  208.                                         else
  209.                                             SizeBox . Top = FastWindowTop;
  210.  
  211.                                         if(WriteChunkBytes(Handle,&SizeBox,sizeof(struct IBox)) == sizeof(struct IBox))
  212.                                         {
  213.                                             if(PopChunk(Handle))
  214.                                                 Success = FALSE;
  215.                                             else
  216.                                             {
  217.                                                 struct MacroNode *Node;
  218.  
  219.                                                 Node = (struct MacroNode *)FastMacroList . lh_Head;
  220.  
  221.                                                 while(Node -> mn_Succ)
  222.                                                 {
  223.                                                     if(!PushChunk(Handle,ID_TERM,ID_FORM,IFFSIZE_UNKNOWN))
  224.                                                     {
  225.                                                         if(!PushChunk(Handle,0,ID_FAST,IFFSIZE_UNKNOWN))
  226.                                                         {
  227.                                                             if(WriteChunkBytes(Handle,Node -> mn_Macro,20) != 20)
  228.                                                             {
  229.                                                                 Success = FALSE;
  230.  
  231.                                                                 break;
  232.                                                             }
  233.                                                             else
  234.                                                             {
  235.                                                                 if(WriteChunkBytes(Handle,Node -> mn_Code,256) != 256)
  236.                                                                 {
  237.                                                                     Success = FALSE;
  238.  
  239.                                                                     break;
  240.                                                                 }
  241.                                                                 else
  242.                                                                 {
  243.                                                                     if(PopChunk(Handle))
  244.                                                                     {
  245.                                                                         Success = FALSE;
  246.  
  247.                                                                         break;
  248.                                                                     }
  249.                                                                     else
  250.                                                                         Success = TRUE;
  251.                                                                 }
  252.                                                             }
  253.                                                         }
  254.  
  255.                                                         if(Success)
  256.                                                         {
  257.                                                             if(PopChunk(Handle))
  258.                                                             {
  259.                                                                 Success = FALSE;
  260.  
  261.                                                                 break;
  262.                                                             }
  263.                                                         }
  264.                                                     }
  265.  
  266.                                                     Node = Node -> mn_Succ;
  267.                                                 }
  268.                                             }
  269.                                         }
  270.                                     }
  271.                                 }
  272.                             }
  273.                         }
  274.  
  275.                         if(PopChunk(Handle))
  276.                             Success = FALSE;
  277.                     }
  278.  
  279.                     if(PopChunk(Handle))
  280.                         Success = FALSE;
  281.                 }
  282.  
  283.                 CloseIFF(Handle);
  284.             }
  285.  
  286.             Close(Handle -> iff_Stream);
  287.         }
  288.  
  289.         FreeIFF(Handle);
  290.     }
  291.  
  292.     if(Success)
  293.         AddProtection(Name,FIBF_EXECUTE);
  294.     else
  295.         DeleteFile(Name);
  296.  
  297.     return(Success);
  298. }
  299.  
  300.     /* LoadFastMacros(STRPTR Name):
  301.      *
  302.      *    Restore the fast! macro list from a file.
  303.      */
  304.  
  305. BYTE
  306. LoadFastMacros(STRPTR Name)
  307. {
  308.     STATIC ULONG Stops[6] =
  309.     {
  310.         ID_TERM,ID_VERS,
  311.         ID_TERM,ID_FAST,
  312.         ID_TERM,ID_WIND
  313.     };
  314.  
  315.     struct List __aligned     NewFastMacroList;
  316.     LONG             NewFastMacroCount = 0;
  317.     struct IFFHandle    *Handle;
  318.     BYTE             Success = FALSE;
  319.     struct MacroNode    *Node;
  320.     struct ContextNode    *Chunk;
  321.     struct IBox         SizeBox;
  322.  
  323.     NewList(&NewFastMacroList);
  324.  
  325.     if(Handle = AllocIFF())
  326.     {
  327.         if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
  328.         {
  329.             InitIFFasDOS(Handle);
  330.  
  331.             if(!OpenIFF(Handle,IFFF_READ))
  332.             {
  333.                 if(!StopChunks(Handle,Stops,3))
  334.                 {
  335.                     while(!ParseIFF(Handle,IFFPARSE_SCAN))
  336.                     {
  337.                         Chunk = CurrentChunk(Handle);
  338.  
  339.                         if(Chunk -> cn_ID == ID_VERS)
  340.                         {
  341.                             struct TermInfo TermInfo;
  342.  
  343.                             if(ReadChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) == sizeof(struct TermInfo))
  344.                             {
  345.                                 if((TermInfo . Version > TermVersion) || (TermInfo . Version == TermVersion && TermInfo . Revision > TermRevision) || (TermInfo . Version == 1 && TermInfo . Revision < 6))
  346.                                     break;
  347.                             }
  348.                             else
  349.                                 break;
  350.                         }
  351.  
  352.                         if(Chunk -> cn_ID == ID_WIND)
  353.                         {
  354.                             if(ReadChunkBytes(Handle,&SizeBox,sizeof(struct IBox)) == sizeof(struct IBox))
  355.                             {
  356.                                 FastWindowLeft        = SizeBox . Left;
  357.                                 FastWindowTop        = SizeBox . Top;
  358.                                 FastWindowHeight    = SizeBox . Height;
  359.                             }
  360.                             else
  361.                                 break;
  362.                         }
  363.  
  364.                         if(Chunk -> cn_ID == ID_FAST)
  365.                         {
  366.                             if(Node = NewFastMacro("",""))
  367.                             {
  368.                                 if(ReadChunkBytes(Handle,Node -> mn_Macro,20) == 20)
  369.                                 {
  370.                                     if(ReadChunkBytes(Handle,Node -> mn_Code,256) == 256)
  371.                                     {
  372.                                         AddTail(&NewFastMacroList,(struct Node *)Node);
  373.  
  374.                                         NewFastMacroCount++;
  375.  
  376.                                         Success = TRUE;
  377.                                     }
  378.                                     else
  379.                                         break;
  380.                                 }
  381.                                 else
  382.                                     break;
  383.                             }
  384.                             else
  385.                                 break;
  386.                         }
  387.                     }
  388.  
  389.                     if(Success)
  390.                     {
  391.                         if(NewFastMacroList . lh_Head -> ln_Succ)
  392.                         {
  393.                             ClearFastMacroList(&FastMacroList);
  394.  
  395.                             MoveList(&NewFastMacroList,&FastMacroList);
  396.  
  397.                             FastMacroCount = NewFastMacroCount;
  398.                         }
  399.                     }
  400.                     else
  401.                         ClearFastMacroList(&NewFastMacroList);
  402.                 }
  403.  
  404.                 CloseIFF(Handle);
  405.             }
  406.  
  407.             Close(Handle -> iff_Stream);
  408.         }
  409.  
  410.         FreeIFF(Handle);
  411.     }
  412.  
  413.     return(Success);
  414. }
  415.