home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / BBS / MISC / XSRC_117.ZIP / LKUPVAR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-01  |  6.8 KB  |  191 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*   XBBS SOURCE CODE copyright (c) 1990 by M. Kimes                        */
  4. /*   All Rights Reserved                                                    */
  5. /*                                                                          */
  6. /*    For complete details of the licensing restrictions, please refer      */
  7. /*    to the License agreement, which is published in its entirety in       */
  8. /*    the in the file LICENSE.XBS.                                          */
  9. /*                                                                          */
  10. /*    USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE      */
  11. /*    XBBS LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF            */
  12. /*    THIS AGREEMENT IN ANY OF THE AFOREMENTIONED FILES, OR IF YOU DO       */
  13. /*    NOT HAVE THESE FILES, YOU SHOULD IMMEDIATELY CONTACT M. KIMES         */
  14. /*    AT THE ADDRESS LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO USE   */
  15. /*    THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE XBBS LICENSING     */
  16. /*    AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE ABLE TO REACH WITH      */
  17. /*    M. KIMES                                                              */
  18. /*                                                                          */
  19. /*                                                                          */
  20. /* You can contact M. Kimes at the following address:                       */
  21. /*                                                                          */
  22. /* M. Kimes                         1:380/16.0@FidoNet                      */
  23. /* 542 Merrick                      (318)222-3455 data                      */
  24. /* Shreveport, LA  71104                                                    */
  25. /*                                                                          */
  26. /*                                                                          */
  27. /* Please feel free to contact me at any time to share your comments about  */
  28. /* my software and/or licensing policies.                                   */
  29. /*                                                                          */
  30. /*--------------------------------------------------------------------------*/
  31. /******************************************************************/
  32. /* This module manages a linked list of variable names and values */
  33. /*   XBBS replaces the variable name where encountered with the   */
  34. /*   variable's value.  Unknown variables return "" (null string) */
  35. /******************************************************************/
  36.  
  37. #include "msg.h"       /* Function declarations & misc BS */
  38. #include "xext.h"      /* External variables */
  39.  
  40. /* Error codes for bbserr global variable */
  41.  
  42.  
  43. #define NOERR       0       /* 's cool, m'man */
  44. #define NOVARS      1       /* No variables in linked list */
  45. #define NOTVAR      2       /* Name given isn't a var in list */
  46. #define NOMEM       3       /* Out of memory */
  47. #define NOFILE      4       /* Can't read a file */
  48. #define SYNTAX      5       /* Syntax error */
  49.  
  50. #define mymalloc mmalloc
  51. #define myfree ffree
  52. #define bbserr level        /* For XBBS 1.xx; 2.xx uses bbserr */
  53.  
  54. struct _variable {      /* Structure of the variables */
  55.     char *name;
  56.     char *str;
  57.     struct _variable *next;     /* Next var in list */
  58.     struct _variable *prior;    /* Previous var in list */
  59. };
  60.  
  61. struct _variable *head=NULL;    /* First var */
  62. struct _variable *tail=NULL;    /* Last var */
  63.  
  64. struct _variable * pascal find_var (char *name);
  65.  
  66.  
  67.  
  68. char * pascal lookup_var (char *name) {    /* Finds var, returns string */
  69.  
  70.     struct _variable *info;
  71.  
  72.     info=find_var(name);
  73.     if(!info) return "";
  74.     else return info->str;
  75. }
  76.  
  77.  
  78.  
  79. static struct _variable * pascal find_var (char *name) {      /* Finds var, returns struct ptr */
  80.  
  81.     struct _variable *info;
  82.  
  83.     if(!name) {
  84.         bbserr=NOVARS;
  85.         return NULL;
  86.     }
  87.     bbserr=NOERR;
  88.     if(head==NULL) {
  89.         bbserr=NOVARS;
  90.         return NULL;      /* No variables defined */
  91.     }
  92.     info=head;
  93.     while(info) {
  94.         if(!stricmp(info->name,name)) {
  95.             return info;
  96.         }
  97.         info=info->next;
  98.     }
  99.     bbserr=NOTVAR;
  100.     return NULL;
  101. }
  102.  
  103.  
  104.  
  105. void pascal delete_var (char *name) {   /* Delete named var */
  106.  
  107.     struct _variable *info;
  108.  
  109.     bbserr=NOERR;
  110.     info=find_var(name);
  111.     if(info) {                          /* It exists; delete it */
  112.         if(info->prior) info->prior->next=info->next; /* Remove from link */
  113.         else {
  114.             head=info->next;            /* Next is new first item */
  115.             if(head) info->prior=NULL;  /* ...else no vars left */
  116.         }
  117.         if(info->next) info->next->prior=info->prior;   /* Remove from link */
  118.         if(info->name) myfree(info->name);  /* Release allocated memory */
  119.         if(info->str) myfree(info->str);
  120.         if(info)myfree(info);
  121.     }
  122. }
  123.  
  124.  
  125.  
  126. void pascal assign_var (char *name,char *str) { /* Handles new and old vars */
  127.  
  128.     struct _variable *info;
  129.  
  130.     if(!str) {
  131.         bbserr=SYNTAX;
  132.         return;
  133.     }
  134.     info=find_var(name);
  135.     if(!info) {             /* New variable */
  136.         info=(struct _variable *)mymalloc(sizeof(struct _variable));
  137.         if(!info) {
  138.             bbserr=NOMEM;
  139.             return;
  140.         }
  141.         info->name=mystrdup(name);
  142.         if(!info->name) {
  143.             bbserr=NOMEM;
  144.             myfree(info);
  145.             return;
  146.         }
  147.         info->str=mystrdup(str);
  148.         if(!info->str) {
  149.             bbserr=NOMEM;
  150.             myfree(info->name);
  151.             myfree(info);
  152.             return;
  153.         }
  154.         if(tail==NULL) {                    /* First and only element */
  155.             info->next=info->prior=NULL;    /* No links */
  156.             head=tail=info;                 /* All roads lead to Amber */
  157.         }
  158.         else {                  /* Add to end of list */
  159.             tail->next=info;    /* Last points ahead to this */
  160.             info->next=NULL;    /* This points ahead to nothing */
  161.             info->prior=tail;   /* This points back to last */
  162.             tail=info;          /* This is now last */
  163.         }
  164.     }
  165.     else {
  166.         if(info->str)myfree(info->str);
  167.         info->str=mystrdup(str);
  168.         if(!info->str) {
  169.             bbserr=NOMEM;
  170.             myfree(info->name);
  171.             myfree(info);
  172.             return;
  173.         }
  174.     }
  175. }
  176.  
  177.  
  178. void pascal free_vars (void) {      /* Free the entire linked list */
  179.  
  180.     struct _variable *info,*temp;
  181.  
  182.     info=head;
  183.     while(info) {
  184.         if(info->str)myfree(info->str);
  185.         if(info->name)myfree(info->name);
  186.         temp=info->next;
  187.         myfree(info);
  188.         info=temp;
  189.     }
  190. }
  191.