home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / dropbox-1.1.lha / DropBox / src / database.c next >
Encoding:
C/C++ Source or Header  |  1993-09-05  |  10.1 KB  |  577 lines

  1. /** DoRev Header ** Do not edit! **
  2. *
  3. * Name             :  database.c
  4. * Copyright        :  Copyright 1993 Steve Anichini. All Rights Reserved.
  5. * Creation date    :  11-Jun-93
  6. * Translator       :  SAS/C 5.1b
  7. *
  8. * Date       Rev  Author               Comment
  9. * ---------  ---  -------------------  ----------------------------------------
  10. * 03-Jul-93    5  Steve Anichini       Near total rewrite of database functions
  11. * 01-Jul-93    4  Steve Anichini       Made CopyDBNode() a macro
  12. * 01-Jul-93    3  Steve Anichini       Added support for SOURCEDIR 
  13.                                         and SOURCEFILE for CreateCommand()
  14. * 21-Jun-93    2  Steve Anichini       First Release.
  15. * 12-Jun-93    1  Steve Anichini       Beta Release 1.0
  16. * 11-Jun-93    0  Steve Anichini       None.
  17. *
  18. *** DoRev End **/
  19.  
  20.  
  21. #include "DropBox.h"
  22.  
  23. /* State Machine Defines */
  24. #define SM_START 1
  25. #define SM_ACHAR 2
  26. #define SM_LBRA  3
  27. #define SM_RBRA  4
  28. #define SM_BCHAR 5
  29. #define SM_CHAR  6
  30. #define SM_END   7
  31. #define SM_ERROR 8
  32.  
  33. struct List *DataBase = NULL;
  34.  
  35. struct List *MyNewList(ULONG type)
  36. {
  37.     struct List *temp = NULL;
  38.     
  39.     if(!(temp = (struct List *) AllocVec(sizeof(struct List), MEMF_PUBLIC)))
  40.         return NULL;
  41.         
  42.     NewList(temp);
  43.     
  44.     temp->lh_Type = type;
  45.     
  46.     return temp;
  47. }
  48.  
  49. void InitDB()
  50. {    
  51.     if(!(DataBase = MyNewList(NT_DBNODE)))
  52.         leave(NO_DATABASE);            
  53. }
  54.  
  55. void CleanList(struct List *list)
  56. {
  57.     struct Node *temp = NULL;
  58.  
  59.     if(list)
  60.     {
  61.         while(!IsEmpty(list))
  62.             if(temp = RemoveNode(NULL,list))
  63.                 FreeNode(temp);
  64.         
  65.         FreeVec((UBYTE *)list);
  66.     }    
  67. }
  68.  
  69. void FreeNode(struct Node *nd)
  70. {
  71.     switch(nd->ln_Type)
  72.     {
  73.         case NT_PATNODE:
  74.             FreeVec((UBYTE *)nd);
  75.             break;
  76.             
  77.         case NT_DBNODE:
  78.             CleanList(((struct DBNode *)nd)->db_Pats);
  79.             FreeVec((UBYTE *)nd);
  80.             break;
  81.             
  82.         default:
  83.             break;
  84.     }
  85. }    
  86.                 
  87. struct Node *NewNode(ULONG type)
  88. {
  89.     struct Node *temp;
  90.     struct List *list;
  91.  
  92.     switch(type)
  93.     {
  94.         case NT_DBNODE:
  95.             if(temp = (struct Node *) AllocVec(sizeof(struct DBNode), MEMF_PUBLIC))
  96.                 if(list = MyNewList(NT_PATNODE))
  97.                     FillDBNode((struct DBNode *) temp, "--Unamed--", "", "",
  98.                             "", DFLG_SUPOUTPUT|DFLG_SUPINPUT, list);
  99.                 else
  100.                 {
  101.                     FreeVec((UBYTE *)temp);
  102.                     temp = NULL;
  103.                 }
  104.             break;
  105.             
  106.         case NT_PATNODE:
  107.             if(temp = (struct Node *) AllocVec(sizeof(struct PatNode), MEMF_PUBLIC))
  108.                 FillPatNode((struct PatNode *)temp, "#?.", PFLG_NOFLAG);
  109.             break;
  110.     
  111.         default:
  112.             temp = NULL;
  113.             break;
  114.     }
  115.     
  116.     return temp;
  117. }    
  118. void InsertNode(struct Node *nd, struct Node *where, struct List *list)
  119. {
  120.     if(where->ln_Pred)
  121.          Insert(list, nd, where->ln_Pred);
  122.     else
  123.         AddHead(list, nd);
  124. }
  125.  
  126. struct Node *RemoveNode(struct Node *node, struct List *list)
  127. {
  128.     if(node)
  129.     {
  130.         Remove(node);
  131.         
  132.         return node;
  133.     }
  134.     else
  135.         return RemTail(list);
  136. }
  137.  
  138. void FillDBNode(struct DBNode *node, char *name, char *dest,
  139.                         char *com, char *template, ULONG flags, struct List *pats)
  140. {
  141.     strcpy(node->db_Name, name);
  142.         
  143.     node->db_Nd.ln_Type = NT_DBNODE;
  144.     node->db_Nd.ln_Pri = 0;
  145.     node->db_Nd.ln_Name = node->db_Name;
  146.         
  147.     strcpy(node->db_Pat, "") ; /* Null string for compat. */
  148.     strcpy(node->db_Dest, dest);
  149.     strcpy(node->db_Com, com);
  150.     strcpy(node->db_Template, template);
  151.         
  152.     node->db_Flags = flags;
  153.     node->db_Pats = pats;
  154. }    
  155.  
  156. void FillPatNode(struct PatNode *node, char *pat, ULONG flags)
  157. {
  158.     strcpy(node->pat_Str, pat);
  159.     
  160.     node->pat_Nd.ln_Type = NT_PATNODE;
  161.     node->pat_Nd.ln_Pri = 0;
  162.     node->pat_Nd.ln_Name = node->pat_Str;
  163.     
  164.     node->pat_Flags = flags;
  165.     node->pat_Reserved = 0;
  166. }
  167.  
  168. void CopyDBNode(struct DBNode *dest, struct DBNode *src)
  169. {
  170.     struct List *list = NULL;
  171.     struct PatNode *pn = NULL, *pn2;
  172.     
  173.     FillDBNode(dest, dest->db_Name,src->db_Dest,
  174.         src->db_Com, src->db_Template,src->db_Flags, NULL);
  175.     
  176.     if(src->db_Pats)
  177.         if(list = MyNewList(NT_PATNODE))
  178.         {
  179.             pn = (struct PatNode *) src->db_Pats->lh_Head;
  180.             
  181.             while(pn->pat_Nd.ln_Succ)
  182.             {
  183.                 if(pn2 = (struct PatNode *)NewNode(NT_PATNODE))
  184.                 {
  185.                     FillPatNode(pn2, pn->pat_Str, pn->pat_Flags);
  186.                     AddTail(list, (struct Node *)pn2);
  187.                 }
  188.                 
  189.                 pn = (struct PatNode *)pn->pat_Nd.ln_Succ;
  190.             }
  191.             
  192.             dest->db_Pats = list;
  193.         }
  194. }
  195.     
  196. struct List *FindDBNode(char *file)
  197. {
  198.     struct DBNode *temp;
  199.     struct PatNode *temp2;
  200.     char pat[DEFLEN];
  201.     struct List *list = NULL;
  202.     struct DBNode *new = NULL;
  203.     BOOL found;
  204.     
  205.     if(!(list = MyNewList(NT_DBNODE)))
  206.         return NULL;
  207.     
  208.     temp = (struct DBNode *) DataBase->lh_Head;
  209.         
  210.     while(temp->db_Nd.ln_Succ)
  211.     {
  212.         if(temp->db_Pats)
  213.         {
  214.             temp2 = (struct PatNode *) temp->db_Pats->lh_Head;
  215.             
  216.             found = FALSE;
  217.             while(!found && (temp2->pat_Nd.ln_Succ))
  218.             {
  219.                 ParsePatternNoCase(temp2->pat_Str, pat, DEFLEN);
  220.                 
  221.                 if(MatchPatternNoCase(pat, file))
  222.                 {
  223.                     found = TRUE;
  224.                     
  225.                     if(!(new = (struct DBNode *) NewNode(NT_DBNODE)))
  226.                     {
  227.                         if(IsEmpty(list))
  228.                         {
  229.                             CleanList(list);
  230.                             list = NULL;
  231.                         }
  232.                         return list;
  233.                     }
  234.                     
  235.                     CopyDBNode(new, temp);
  236.                     strcpy(new->db_Name, temp->db_Name);
  237.                     AddTail(list, (struct Node *)new);
  238.                 }
  239.                 else
  240.                     temp2 = (struct PatNode *) temp2->pat_Nd.ln_Succ;
  241.             }
  242.             
  243.         }
  244.     
  245.         temp = (struct DBNode *) temp->db_Nd.ln_Succ;
  246.     }
  247.     
  248.     if(!IsEmpty(list))
  249.         return list;
  250.     else
  251.     {
  252.         CleanList(list);
  253.         return NULL;
  254.     }
  255. }
  256.  
  257. int SetState(int cur, char c)
  258. {
  259.     switch(cur)
  260.     {
  261.         case SM_START:
  262.             if(c != '[')
  263.             {
  264.                 if(c != ']')
  265.                     return SM_ACHAR;
  266.                 else
  267.                     return SM_ERROR;
  268.             }
  269.             else
  270.                 return SM_LBRA;
  271.             break;
  272.  
  273.         case SM_ACHAR:
  274.             if(c == '[')
  275.                 return SM_LBRA;
  276.             else
  277.                 if(c == ']')
  278.                     return SM_ERROR;
  279.                 else
  280.                     if(c != '\0')
  281.                         return SM_ACHAR;
  282.                     else
  283.                         return SM_END;
  284.             break;
  285.  
  286.         case SM_LBRA:
  287.             if((c != '[') && (c != ']'))
  288.                 return SM_BCHAR;
  289.             else
  290.                 return SM_ERROR;
  291.             break;
  292.  
  293.         case SM_BCHAR:
  294.             if(c != ']')
  295.                 return SM_ERROR;
  296.             else
  297.                 return SM_RBRA;
  298.             break;
  299.  
  300.         case SM_RBRA:
  301.             if((c != '[') && (c != ']'))
  302.             {
  303.                 if(c != '\0')
  304.                     return SM_CHAR;
  305.                 else
  306.                     return SM_END;
  307.             }
  308.             else
  309.                 return SM_ERROR;
  310.             break;
  311.  
  312.         case SM_ERROR:
  313.             return SM_ERROR;
  314.             break;
  315.     }
  316.  
  317.     return SM_START;
  318. }
  319.  
  320. ULONG ParseToken(struct DBNode *node, struct WBArg *warg, char *com, char *tok)
  321. {
  322.     char *temp, reallytemp[2];
  323.     int state;
  324.     char storage[DEFLEN], dir[DEFLEN];
  325.  
  326.     NameFromLock(warg->wa_Lock, dir, DEFLEN);
  327.     AddPart(dir, warg->wa_Name, DEFLEN);
  328.     strcpy(storage, dir);
  329.     *(PathPart(storage)) = '\0'; /*For SOURCEDIR */
  330.  
  331.     temp = tok;
  332.     reallytemp[1] = '\0';
  333.  
  334.     if(*temp)
  335.         state = SM_START;
  336.     else
  337.         state = SM_END;
  338.  
  339.     while(state != SM_END)
  340.     {
  341.         switch(state)
  342.         {
  343.             case SM_START:
  344.                 break;
  345.  
  346.             case SM_ACHAR:
  347.                 reallytemp[0] = *temp;
  348.                 strcat(com, reallytemp);
  349.             case SM_LBRA:
  350.                 temp++;
  351.                 break;
  352.  
  353.             case SM_BCHAR:
  354.                 if(!strnicmp(temp, COM,strlen(COM)))
  355.                 {
  356.                     strcat(com, "\"");
  357.                     strcat(com, node->db_Com);
  358.                     strcat(com, "\"");
  359.                     temp += strlen(COM);
  360.                 }
  361.                 else
  362.                     if(!strnicmp(temp, DEST1, strlen(DEST1)))
  363.                     {
  364.                         strcat(com, "\"");
  365.                         strcat(com, node->db_Dest);
  366.                         if(node->db_Flags&DFLG_CREATE)
  367.                         {
  368.                             // Pray the stack doesn't overflow!!!
  369.                             char *t2, name[DEFLEN], thetemp[DEFLEN]; 
  370.                             
  371.                             strcpy(name, warg->wa_Name);
  372.                             t2 = strrchr(name, '.');
  373.                             if(t2)
  374.                                 *t2 = '\0';
  375.                             strcpy(thetemp, "");
  376.                             AddPart(thetemp, name, DEFLEN*2);
  377.                             AddPart(thetemp, "a", DEFLEN*2);
  378.                             t2 = FilePart(thetemp);
  379.                             if(t2)
  380.                                 *t2 = '\0';
  381.                             strcat(com, thetemp);
  382.                         }
  383.                         strcat(com, "\"");
  384.                         temp += strlen(DEST1);
  385.                     }
  386.                     else
  387.                         if(!strnicmp(temp, SOURCE, strlen(SOURCE)))
  388.                         {
  389.                             if(!strnicmp(temp, SOURCEDIR, strlen(SOURCEDIR)))
  390.                             {
  391.                                 strcat(com, "\"");
  392.                                 strcat(com, storage);
  393.                                 strcat(com, "\"");
  394.                                 temp += strlen(SOURCEDIR);
  395.                             }
  396.                             else
  397.                                 if(!strnicmp(temp, SOURCEFILE, strlen(SOURCEFILE)))
  398.                                 {
  399.                                     strcat(com, "\"");
  400.                                     strcat(com, warg->wa_Name);
  401.                                     strcat(com, "\"");
  402.                                     temp += strlen(SOURCEFILE);
  403.                                 }
  404.                                 else /* SOURCE */
  405.                                 {
  406.                                     strcat(com, "\"");
  407.                                     strcat(com, dir);
  408.                                     strcat(com, "\"");
  409.                                     temp += strlen(SOURCE);
  410.                                 }
  411.                         }
  412.                         else
  413.                             return PT_COMUNKNOWN;
  414.                 break;
  415.                 
  416.             case SM_RBRA:
  417.                 temp++;
  418.                 break;
  419.                     
  420.             case SM_CHAR:
  421.                 strcat(com, temp);
  422.                 break;
  423.                 
  424.             case SM_ERROR:
  425.                 return PT_BADTOKEN;
  426.                 
  427.         } /* end of switch */
  428.         
  429.         state = SetState(state, *temp);
  430.     }
  431.     
  432.     strcat(com, " ");
  433.     
  434.     return NO_ERROR;
  435. }
  436.             
  437. ULONG CreateCommand(struct DBNode *node, struct WBArg *warg, char *com)
  438. {
  439.     char str[DEFLEN];
  440.     char *tok[64];
  441.     int numtoks = 0, i;
  442.     ULONG err = NO_ERROR;
  443.     
  444.     strcpy(str, node->db_Template);
  445.     
  446.     tok[numtoks] = strtok(str, " ");
  447.     while(tok[numtoks])
  448.     {
  449.         numtoks++;
  450.         tok[numtoks] = strtok(NULL, " ");
  451.     }
  452.     
  453.     strcpy(com, "");
  454.     
  455.     for(i = 0; i < numtoks; i++)
  456.         if(err = ParseToken(node, warg, com, tok[i]))
  457.             return err;
  458.     
  459.     return err;
  460. }
  461.  
  462. struct Node *OrdToPtr(UWORD ord, struct List *list)
  463. {
  464.     int i = 0;
  465.     struct Node *temp;
  466.     BOOL found = FALSE;
  467.     
  468.     temp = list->lh_Head;
  469.     
  470.     while(temp->ln_Succ && !found)
  471.         if(i == ord)
  472.             found = TRUE;
  473.         else
  474.         {
  475.             i++;
  476.             temp = temp->ln_Succ;
  477.         }
  478.         
  479.     if(!found)
  480.         return NULL;
  481.     else
  482.         return temp;
  483. }
  484.     
  485. UWORD PtrToOrd(struct Node *ptr, struct List *list)
  486. {
  487.     UWORD i = 0;
  488.     struct Node *temp;
  489.     BOOL found = FALSE;
  490.     
  491.     temp = list->lh_Head;
  492.     
  493.     while(temp->ln_Succ && !found)
  494.         if( ptr == temp)
  495.             found = TRUE;
  496.         else
  497.         {
  498.             i++;
  499.             temp = temp->ln_Succ;
  500.         }
  501.         
  502.     if(!found)
  503.         return ~0;
  504.     else
  505.         return i;
  506. }
  507.         
  508. ULONG CountNodes(struct List *list)
  509. {
  510.     ULONG i = 0;
  511.     struct Node *temp;
  512.     
  513.     temp = list->lh_Head;
  514.     
  515.     while(temp->ln_Succ)
  516.     {
  517.         i++;
  518.         temp = temp->ln_Succ;
  519.     }
  520.         
  521.     return i;
  522. }
  523.  
  524. ULONG Sort(struct List **list)
  525. {
  526.     struct List *new = NULL;
  527.     struct Node *nd = NULL, *nd2 = NULL, *pred;
  528.     BOOL found;
  529.     
  530.     if(!(new = MyNewList((*list)->lh_Type)))
  531.         return NO_MEM;
  532.     else
  533.     {
  534.         nd = (*list)->lh_Head;
  535.         
  536.         while(nd->ln_Succ)
  537.         {
  538.             Remove(nd);
  539.             
  540.             if(IsEmpty(new))
  541.                 AddHead(new, nd);
  542.             else
  543.             {
  544.                 pred = NULL;
  545.                 nd2 = new->lh_Head;
  546.                 found = FALSE;
  547.                 
  548.                 while(nd2->ln_Succ && !found)
  549.                     if(stricmp(nd->ln_Name, nd2->ln_Name) <= 0)
  550.                     {
  551.                         Insert(new, nd, pred);
  552.                         found = TRUE;
  553.                     }
  554.                     else
  555.                     {
  556.                         pred = nd2;
  557.                         nd2 = nd2->ln_Succ;
  558.                     }
  559.                     
  560.                 if(!found)
  561.                     AddTail(new, nd);
  562.             }
  563.             
  564.             nd =  (*list)->lh_Head;
  565.             
  566.         }
  567.         
  568.         FreeVec((UBYTE *)(*list));
  569.         *list = new;
  570.         
  571.         return NO_ERROR;
  572.     }
  573. }
  574.                 
  575.                 
  576.                         
  577.