home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume18 / oraperl / part01 / getcursor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-11  |  5.6 KB  |  282 lines

  1. /* getcursor.c
  2.  *
  3.  * Functions to deal with allocating and freeing cursors for Oracle
  4.  */
  5. /* Copyright 1991 Kevin Stock.
  6.  *
  7.  * You may copy this under the terms of the GNU General Public License,
  8.  * a copy of which should have accompanied your Perl kit.
  9.  */
  10.  
  11. #include    "EXTERN.h"
  12. #include    <stdio.h>
  13. #include    <ctype.h>
  14. #include    "orafns.h"
  15.  
  16.  
  17. /* head of the cursor list */
  18. struct cursor csr_list = { NULL, NULL, NULL, 0, NULL };
  19.  
  20.  
  21. /* ora_free_data(csr)
  22.  *
  23.  * Frees memory attached to csr->data
  24.  */
  25.  
  26. void ora_free_data(csr)
  27. struct cursor *csr;
  28. {
  29.     int i;
  30.  
  31.     DEBUG(8, (fprintf(stderr, "ora_free_data(%#lx)\n", (long) csr)));
  32.  
  33.     if (csr->data == NULL)
  34.     {
  35.         DEBUG(8, (fputs("ora_free_data: returning\n", stderr)));
  36.         return;
  37.     }
  38.  
  39.     for (i = 0 ; i < csr->nfields ; i++)
  40.     {
  41.         if (csr->data[i] != NULL)
  42.         {
  43.             DEBUG(128, (fprintf(stderr, "freeing (%d) == %#lx\n",
  44.                 i, (long) csr->data[i])));
  45.             free(csr->data[i]);
  46.         }
  47.     }
  48.  
  49.     DEBUG(128, (fprintf(stderr, "freeing %#lx\n", (long) csr->data)));
  50.     free(csr->data);
  51.     csr->data = NULL;
  52.     csr->nfields = 0;
  53.     DEBUG(8, (fputs("ora_free_data: returning\n", stderr)));
  54. }
  55.  
  56.  
  57. /* ora_getcursor()
  58.  *
  59.  * Allocates memory for a new cursor and returns its address.
  60.  * Inserts the cursor at the front of the list.
  61.  * Returns NULL if it can't get enough memory.
  62.  */
  63.  
  64. struct cursor *ora_getcursor()
  65. {
  66.     struct cursor *tmp;
  67.  
  68.     DEBUG(8, (fputs("ora_getcursor()\n", stderr)));
  69.  
  70.     if ((tmp = (struct cursor *) malloc(sizeof(struct cursor))) == NULL)
  71.     {
  72.         DEBUG(128, (fputs("ora_getcursor: out of memory\n", stderr)));
  73.         DEBUG(8, (fputs("ora_getcursor: returning NULL\n", stderr)));
  74.         ora_errno = ORAP_NOMEM;
  75.         return(NULL);
  76.     }
  77.     DEBUG(128, (fprintf(stderr,
  78.         "ora_getcursor: got cursor at %#lx\n", (long) tmp)));
  79.  
  80.     if ((tmp->csr = (struct csrdef *) malloc(sizeof(struct csrdef))) == NULL)
  81.     {
  82.         free(tmp);
  83.         DEBUG(128, (fputs("ora_getcursor: out of memory\n", stderr)));
  84.         DEBUG(8, (fputs("ora_getcursor: returning NULL\n", stderr)));
  85.         ora_errno = ORAP_NOMEM;
  86.         return(NULL);
  87.     }
  88.     DEBUG(128, (fprintf(stderr,
  89.         "ora_getcursor: got csr at %#lx\n", tmp->csr)));
  90.  
  91.     tmp->hda = NULL;
  92.     tmp->data = NULL;
  93.     tmp->nfields = 0;
  94.     tmp->next = csr_list.next;
  95.     csr_list.next = tmp;
  96.  
  97.     ora_errno = 0;
  98.     DEBUG(8, (fprintf(stderr,"ora_getcursor: returning %#lx\n",(long)tmp)));
  99.     return(tmp);
  100. }
  101.  
  102.  
  103. /* ora_getlda()
  104.  *
  105.  * Gets a new login data area.
  106.  * Uses ora_getcursor and then allocates the host data area.
  107.  */
  108.  
  109. struct cursor *ora_getlda()
  110. {
  111.     struct cursor *tmp;
  112.  
  113.     DEBUG(8, (fputs("ora_getlda()\n", stderr)));
  114.  
  115.     if ((tmp = ora_getcursor()) == NULL)
  116.     {
  117.         DEBUG(8, (fputs("ora_getlda: returning NULL\n", stderr)));
  118.         return(NULL);
  119.     }
  120.  
  121.     if ((tmp->hda = malloc(256)) == NULL)
  122.     {
  123.         DEBUG(128, (fputs("ora_getlda: out of memory\n", stderr)));
  124.         ora_dropcursor(tmp);
  125.         DEBUG(8, (fputs("ora_getlda: returning NULL\n", stderr)));
  126.         ora_errno = ORAP_NOMEM;
  127.         return(NULL);
  128.     }
  129.     DEBUG(128, (fprintf(stderr,
  130.         "ora_getlda: got hda at %#lx\n", tmp->hda)));
  131.  
  132.     DEBUG(8, (fprintf(stderr, "ora_getlda: returning %#lx\n", tmp)));
  133.     return(tmp);
  134. }
  135.  
  136.  
  137. /* ora_dropcursor(csr)
  138.  *
  139.  * Frees the space occupied by a given cursor, removing it from the list.
  140.  */
  141.  
  142. int ora_dropcursor(csr)
  143. struct cursor *csr;
  144. {
  145.     struct cursor *tmp, *t;
  146.  
  147.     tmp = &csr_list;
  148.  
  149.     DEBUG(8, (fprintf(stderr, "ora_dropcursor(%#lx)\n", (long) csr)));
  150.  
  151.     while ((tmp->next != NULL) && (tmp->next != csr))
  152.     {
  153.         tmp = tmp->next;
  154.     }
  155.  
  156.     if (tmp->next == NULL)
  157.     {
  158.         DEBUG(8, (fputs("ora_dropcursor: invalid\n", stderr)));
  159.         ora_errno = ORAP_INVCSR;
  160.         return(0);
  161.     }
  162.  
  163.     t = tmp->next;
  164.  
  165.     if (t->hda != NULL)
  166.     {
  167.         DEBUG(128, (fprintf(stderr,
  168.             "ora_dropcursor: freeing hda at %#lx\n", (long) t->hda)));
  169.         free(t->hda);
  170.     }
  171.     if (t->data != NULL)
  172.     {
  173.         DEBUG(128, (fputs("ora_dropcursor: freeing data\n", stderr)));
  174.         ora_free_data(t);
  175.     }
  176.  
  177.     DEBUG(128, (fprintf(stderr,
  178.         "ora_dropcursor: freeing csr at %#lx\n", (long) t->csr)));
  179.     free(t->csr);
  180.  
  181.     t = t->next;
  182.     DEBUG(128, (fprintf(stderr,
  183.         "ora_dropcursor: freeing cursor at %#lx\n", (long) tmp->next)));
  184.     free(tmp->next);
  185.     tmp->next = t;
  186.  
  187.     DEBUG(8, (fputs("ora_dropcursor: returning\n", stderr)));
  188.     return(1);
  189. }
  190.  
  191.  
  192. /* ora_droplda()
  193.  *
  194.  * This is just here for completeness' sake.
  195.  * (I suppose we could check the value of hda in dropcursor and droplda
  196.  * but I don't think it's worth it
  197.  */
  198.  
  199. int ora_droplda(lda)
  200. struct cursor *lda;
  201. {
  202.     DEBUG(8, (fprintf(stderr,
  203.         "ora_droplda(%#lx): calling ora_dropcursor\n", lda)));
  204.     return(ora_dropcursor(lda));
  205. }
  206.  
  207.  
  208. /* ora_findcursor()
  209.  *
  210.  * Checks whether the specified csr is present in the list
  211.  */
  212.  
  213. int ora_findcursor(csr)
  214. struct cursor *csr;
  215. {
  216.     struct cursor *tmp;
  217.  
  218.     tmp = &csr_list;
  219.  
  220.     DEBUG(8, (fprintf(stderr, "ora_findcursor(%#lx)\n", (long) csr)));
  221.  
  222.     while ((tmp->next != NULL) && (tmp->next != csr))
  223.     {
  224.         tmp = tmp->next;
  225.     }
  226.  
  227.     if (tmp->next == NULL)
  228.     {
  229.         DEBUG(8, (fputs("ora_findcursor: not valid\n", stderr)));
  230.         return(0);
  231.     }
  232.  
  233.     DEBUG(8, (fputs("ora_findcursor: valid\n", stderr)));
  234.     return(1);
  235. }
  236.  
  237.  
  238. /* check_lda()
  239.  *
  240.  * Checks whether the given address corresponds to a valid lda
  241.  */
  242.  
  243.  int check_lda(lda)
  244.  struct cursor *lda;
  245.  {
  246.     DEBUG(8, (fprintf(stderr, "check_lda(%#lx)\n", (long) lda)));
  247.  
  248.     if (ora_findcursor(lda) && (lda->hda != NULL) && (lda->data == NULL))
  249.     {
  250.         DEBUG(8, (fputs("check_lda: valid\n", stderr)));
  251.         return (1);
  252.     }
  253.     else
  254.     {
  255.         DEBUG(8, (fputs("check_lda: invalid\n", stderr)));
  256.         return (0);
  257.     }
  258. };
  259.  
  260.  
  261. /* check_csr()
  262.  *
  263.  * Checks whether the given address corresponds to a valid csr
  264.  */
  265.  
  266.  int check_csr(csr)
  267.  struct cursor *csr;
  268.  {
  269.     DEBUG(8, (fprintf(stderr, "check_csr(%#lx)\n", (long) csr)));
  270.  
  271.     if (ora_findcursor(csr) && (csr->hda == NULL) && (csr->data != NULL))
  272.     {
  273.         DEBUG(8, (fputs("check_csr: valid\n", stderr)));
  274.         return (1);
  275.     }
  276.     else
  277.     {
  278.         DEBUG(8, (fputs("check_csr: invalid\n", stderr)));
  279.         return (0);
  280.     }
  281. };
  282.