home *** CD-ROM | disk | FTP | other *** search
/ YPA: Your Privacy Assured / YPA.ISO / other_goodies / utilities / memomaster.lha / MM2.1 / source / listfunc.c < prev    next >
C/C++ Source or Header  |  1994-09-12  |  8KB  |  295 lines

  1. /*  Functions for creating and manipulating the list of memos
  2.  *
  3.  *  Functions in this file are :-
  4.  *
  5.  *    struct MinList *LoadData()  \   Not yet included/complete
  6.  *    int SaveData()              /
  7.  *    struct MinNode *MoveBackOne(struct MinNode *p)
  8.  *    struct MinNode *MoveBackBlock(struct MinNode *p)
  9.  *    struct MinNode *MoveEnd(struct MinNode *p)
  10.  *    struct MinNode *MoveFwdOne(struct MinNode *p)
  11.  *    struct MinNode *MoveFwdBlock(struct MinNode *p)
  12.  *    void DisplayBlock()
  13.  *    void Toggle(int t_gadget)
  14.  *
  15.  *        (to the memo currently top of the display)
  16.  *  The pointer ^ is generally held as a pointer to a MinNode structure since
  17.  *    this makes the code clearer and more efficient(?) since it does not
  18.  *    need to be cast into this form from the Memo_In_Mem form all over the
  19.  *    place.
  20.  *
  21.  *  Use them like so:-
  22.  *    struct MinNode *x,*DisplayFirst;
  23.  *    x=MoveFwdBlock(DisplayFirst);
  24.  *    if (x != DisplayFirst)
  25.  *    {
  26.  *    DisplayFirst=x;
  27.  *    DisplayBlock(DisplayFirst);
  28.  *    }
  29.  */
  30.  
  31. #include <string.h>
  32. #include <lists.h>
  33. #include <exec/types.h>
  34. #include <exec/nodes.h>
  35. #include <exec/lists.h>
  36. #include <exec/memory.h>
  37.  
  38. #include <clib/alib_protos.h>
  39.  
  40. #include "mm2.h"
  41. #include <stdio.h>
  42.  
  43. /* Prototypes */
  44.  
  45. Prototype struct MinList *LoadData(void);
  46. Prototype int SaveData(void);
  47. Prototype struct MinNode *MoveBackOne(struct MinNode *);
  48. Prototype struct MinNode *MoveBackBlock(struct MinNode *);
  49. Prototype struct MinNode *MoveFwdOne(struct MinNode *);
  50. Prototype struct MinNode *MoveFwdBlock(struct MinNode *);
  51. Prototype void Display_Block(struct MinNode *);
  52. Prototype void Toggle(int);
  53. Prototype struct MinNode *MoveEnd(struct MinNode *);
  54.  
  55. extern struct MinNode *DisplayFirst;
  56. extern struct Remember *RK;
  57. extern struct MinList *MemListPtr;
  58.  
  59. /* ==========================================================================
  60.  * LoadData() - version using exec list handling
  61.  */
  62.  
  63. struct MinList *LoadData()
  64.   {
  65.   FILE *fp;
  66.   struct MinList *mlp;
  67.   struct Memo_Item mi;
  68.   struct MI_Mem *p;
  69.   int OK;
  70.  
  71.   mlp=(struct MinList *)AllocRemember(&RK,sizeof(struct MinList),MEMF_PUBLIC);
  72.   if (mlp == NULL) return mlp;  /* Failed. No memory to start list  */
  73.   NewList((struct List *)mlp);
  74.   fp=fopen("PROGDIR:memodata.dat","r");
  75.  
  76.   if (fp == NULL ) return mlp;  /* File isn't there. Return empty list */
  77.   OK=fread((char *)&mi,sizeof(struct Memo_Item),1,fp);
  78.   while (OK)
  79.     {
  80.     p=(struct MI_Mem *)AllocRemember(&RK,sizeof(struct MI_Mem),MEMF_PUBLIC);
  81.     if (p == NULL)
  82.       {
  83.       /* Take some ABORT!!!! type action here due to loss of memory */
  84.       }
  85.     memcpy((char *)&(p->mim_MI),(char *)&mi,sizeof(struct Memo_Item) );
  86.     p->mim_Select = 0;    /*  All memos start off deselected  */
  87.     p->mim_MI.mi_Text[60]='\0';
  88.     AddTail((struct List *)mlp,(struct Node *)p);
  89.     OK=fread((char *)&mi,sizeof(struct Memo_Item),1,fp);
  90.     }
  91.   fclose(fp);
  92.   return mlp;
  93.   }
  94.  
  95. /*=====================================================================
  96.  * SaveData()
  97.  *
  98.  * Writes structures in list to file memodata.dat . Previous version of
  99.  *   memodata.dat is renamed .bak
  100.  *
  101.  * !!!!!!! Currently no error checking !!!!!!!
  102.  */
  103. SaveData()
  104.   {
  105.   FILE *fp;
  106.   int Save_Status=1;     /* Assume success */
  107.   int errr;
  108.   struct MinNode *n;
  109.   struct MI_Mem *m;
  110.  
  111.   errr=remove("PROGDIR:memodata.bak");
  112.   errr=rename("PROGDIR:memodata.dat","PROGDIR:memodata.bak");
  113.   if (LISTEMPTY) return Save_Status;
  114.   fp=fopen("PROGDIR:memodata.dat","w");
  115.   n=MemListPtr->mlh_Head;
  116.   while (n->mln_Succ)
  117.     {
  118.     m=(struct MI_Mem *)n;
  119.     errr=fwrite((char *)&(m->mim_MI),sizeof(struct Memo_Item),1,fp);
  120.     n = n->mln_Succ;
  121.     }
  122.   fclose(fp);
  123.   return Save_Status;
  124.   }
  125.  
  126.  
  127.  
  128. /*  =========================================================================
  129.  *  Move the 'current' memo back one
  130.  */
  131. struct MinNode *MoveBackOne(struct MinNode *p)
  132.   {
  133.   struct MinNode *n;
  134.  
  135.   if (n = GetPred(p))
  136.     return n;
  137.   else
  138.     return p;
  139.   }
  140.  
  141. /*  =========================================================================
  142.  *Move the 'current' memo back a block
  143.  */
  144.  
  145. struct MinNode *MoveBackBlock(struct MinNode *p)
  146.   {
  147.     struct MinNode *n;
  148.     int num;
  149.  
  150.     n = p;
  151.     num = MEMOS_IN_BLOCK-1;
  152.     while (num)
  153.     {
  154.       if (n)
  155.         n = GetPred(n);
  156.       num--;
  157.     }
  158.     if (n)
  159.       return n;
  160.     else
  161.       return GetHead(MemListPtr);
  162.   }
  163.  
  164. /*=========================================================================
  165.  * Move the 'current' memo forward one. More complicated than at first apparent
  166.  *   because the current pointer is the one at the start of the block and the
  167.  *   end of the list has to be checked relative to the last in the block.
  168.  *   Actually move the current pointer MEMOS_IN_BLOCK+1 forward (or until
  169.  *   the end of the list) and then MEMOS_IN_BLOCK backward (or the number
  170.  *   moved forwards if smaller)  */
  171.  
  172. struct MinNode *MoveFwdOne(struct MinNode *p)
  173.   {
  174.   int cf;
  175.  
  176.   cf=0;
  177.   while ((cf<MEMOS_IN_BLOCK+1) &&  (p->mln_Succ))
  178.     {
  179.     p=p->mln_Succ;
  180.     cf++;
  181.     }
  182.   cf=MIN(cf,MEMOS_IN_BLOCK);
  183.   while (cf > 0)
  184.     {
  185.     p=p->mln_Pred;
  186.     cf--;
  187.     }
  188.   return p;
  189.   }
  190.  
  191. /*=========================================================================
  192.  * Move the 'current' memo forward one block.
  193.  *   Actually move the current pointer MEMOS_IN_BLOCK*2 forward (or until
  194.  *   the end of the list) and then MEMOS_IN_BLOCK backward (or the number
  195.  *   moved forwards if smaller)  */
  196.  
  197. struct MinNode *MoveFwdBlock(struct MinNode *p)
  198.   {
  199.   int cf;
  200.  
  201.   cf=0;
  202.   while ((cf < MEMOS_IN_BLOCK+MEMOS_IN_BLOCK) && (p->mln_Succ))
  203.     {
  204.     p=p->mln_Succ;
  205.     cf++;
  206.     }
  207.   cf=MIN(cf,MEMOS_IN_BLOCK);
  208.   while (cf > 0)
  209.     {
  210.     p=p->mln_Pred;
  211.     cf--;
  212.     }
  213.   return p;
  214.   }
  215. /*========================================================================
  216.  *  Makes succesive calls to Display_One() to display a block of memos.
  217.  *    Param is pointer to a node structure attached to first memo
  218.  *    to be displayed. Value of parameter is DisplayFirst which may need to
  219.  *    be reset prior to call to Display_Block(). If param is NULL then list
  220.  *    must be empty so display all blank memos
  221.  =========================================================================*/
  222. void Display_Block(struct MinNode *m)
  223.   {
  224.   int p;
  225.  
  226.   p=0;
  227.   if (m)
  228.     {
  229. /*    while ( (p<MEMOS_IN_BLOCK) && ( m != NULL ) ) */
  230.     while ( (p<MEMOS_IN_BLOCK) && ( m->mln_Succ ) )
  231.     {
  232.     Display_One( p, (struct MI_mem *)m );
  233.     p++;
  234.     m=m->mln_Succ;
  235.     }
  236.     }
  237.  
  238.   while ( p<MEMOS_IN_BLOCK )
  239.     {
  240.     Display_Blank( p );
  241.     p++;
  242.     }
  243.   }
  244. /*=========================================================================
  245.  * If memo display gadget clicked on is currently showing a memo, toggle
  246.  *   the memos selected indicator and redisplay.
  247.  */
  248. void Toggle(int t_gadget)
  249.   {
  250.   int g;
  251.   struct MinNode *n;
  252.  
  253.   n=DisplayFirst;
  254.   for (g=0 ; g < t_gadget ; g++)
  255.     {
  256.     n=n->mln_Succ;
  257.     if (!n) break;
  258.     }
  259.   if (n)
  260.     {
  261.     if ( ((struct MI_Mem *)n)->mim_Select == 0) /* ie if not currently selected */
  262.       ((struct MI_Mem *)n)->mim_Select=1;
  263.     else
  264.       ((struct MI_Mem *)n)->mim_Select=0;
  265.     Display_One(g,(struct MI_Mem *)n);
  266.     }
  267.   }
  268.  
  269.  
  270. /*=========================================================================
  271.  * Move the 'current' memo forward to end
  272.  *   Actually move the current pointer forward to
  273.  *   the end of the list) and then MEMOS_IN_BLOCK backward
  274.  */
  275.  
  276. struct MinNode *MoveEnd(struct MinNode *p)
  277.   {
  278.     struct MinNode *n;
  279.     int b;
  280.  
  281.     n = GetTail(MemListPtr);
  282.     b = MEMOS_IN_BLOCK-1;
  283.     while (b > 0)
  284.     {
  285.       if (n)
  286.         n = GetPred(n);
  287.       b--;
  288.     }
  289.     if (n)
  290.       return n;
  291.     else
  292.       return GetHead(MemListPtr);
  293.   }
  294.  
  295.