home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume30 / oraperl-v2 / part03 / getcursor.c next >
Encoding:
C/C++ Source or Header  |  1992-07-06  |  6.5 KB  |  345 lines

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