home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume43 / dbf / part02 / sql_sel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-27  |  2.7 KB  |  145 lines

  1. /*
  2.  * Copyright (c) 1994 Brad Eacker,
  3.  *              (Music, Intuition, Software, and Computers)
  4.  * All Rights Reserved
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include "sql.h"
  10. #include "dbf.h"
  11.  
  12. struct fld_lst {
  13.     struct fld_lst    *fld_next;
  14.     dbfield_t    *fld_db_f;
  15. };
  16. typedef struct fld_lst fld_lst_t;
  17.  
  18. extern fld_lst_t *bld_fld_list();
  19.  
  20. sql_sel(a_or_d, s_list, t_ref, w_def, o_def)
  21. int    a_or_d;
  22. list_elem_t *s_list, *t_ref, *w_def, *o_def;
  23. {
  24.     int    retval = 0;
  25.     dbhead_t *dbh;
  26.     char    *cp;
  27.     fld_lst_t *flp, *tflp;
  28.  
  29.     if (debug_output) {
  30.         printf("selection\nColumn list:\n");
  31.         scan_elems(s_list);
  32.         printf("Table list:\n");
  33.         scan_elems(t_ref);
  34.         printf("Where Defs:\n");
  35.         scan_elems(w_def);
  36.         printf("Order by Defs:\n");
  37.         scan_elems(o_def);
  38.         printf("\n");
  39.     }
  40.     cp = get_list_name(t_ref);
  41.     if (dbh = dbf_open(cp, O_RDONLY)) {
  42.         if (verbose)
  43.             dbf_head_info(dbh);
  44.         if (flp = bld_fld_list(dbh, s_list)) {
  45.             while (cp = dbf_get_next(dbh)) {
  46.             if ( !is_valid_rec(cp) )
  47.                 continue;
  48.             tflp = flp;
  49.             while (tflp) {
  50.                 dbfield_t *cur_f = tflp->fld_db_f;
  51.                 char *tcp = get_field_val(cp, cur_f, NULL);
  52.  
  53.                 printf(cur_f->db_format, tcp);
  54.                 free(tcp);
  55.                 if (tflp = tflp->fld_next)
  56.                 printf(" ");
  57.             }
  58.             printf("\n");
  59.             free(cp);
  60.             }
  61.             free_fld_list(flp);
  62.         }
  63.     } else {
  64.         printf("unable to open %s\n", cp);
  65.     }
  66.  
  67.     return retval;
  68. }
  69.  
  70. fld_lst_t *
  71. add_fld_list(fld_head, db_f)
  72. fld_lst_t *fld_head;
  73. dbfield_t *db_f;
  74. {
  75.     fld_lst_t *fld_elem, *tfld;
  76.  
  77.     if (fld_elem = (fld_lst_t *)malloc(sizeof(fld_lst_t))) {
  78.         fld_elem->fld_next = NULL;
  79.         fld_elem->fld_db_f = db_f;
  80.         if (tfld = fld_head) {
  81.             while (tfld->fld_next)
  82.             tfld = tfld->fld_next;
  83.             tfld->fld_next = fld_elem;
  84.         }
  85.     }
  86.     return fld_elem;
  87. }
  88.  
  89. fld_lst_t *
  90. bld_fld_list(dbh, s_list)
  91. dbhead_t    *dbh;
  92. list_elem_t    *s_list;
  93. {
  94.     fld_lst_t    *fld_head = NULL, *fld_elem;
  95.     int        nfields = dbh->db_nfields;
  96.     dbfield_t    *db_f = dbh->db_fields;
  97.  
  98.     if (strcmp(get_list_name(s_list), "*") == 0) {
  99.         while (nfields-- > 0) {
  100.         if (fld_elem = add_fld_list(fld_head, db_f++)) {
  101.             if (fld_head == NULL)
  102.                 fld_head = fld_elem;
  103.         } else {
  104.             free_fld_list(fld_head);
  105.             fld_head = NULL;
  106.             break;
  107.         }
  108.         }
  109.     } else {
  110.         char *cp;
  111.         int  i;
  112.  
  113.         while (s_list) {
  114.         cp = get_list_name(s_list);
  115.         db_f = dbh->db_fields;
  116.         for (i = nfields; i > 0; i--) {
  117.             if (strcmp(db_f->db_fname, cp) == 0) {
  118.             if (fld_elem = add_fld_list(fld_head, db_f)) {
  119.                 if (fld_head == NULL)
  120.                 fld_head = fld_elem;
  121.             } else {
  122.                 free_fld_list(fld_head);
  123.                 fld_head = NULL;
  124.                 break;
  125.             }
  126.             break;
  127.             }
  128.             db_f++;
  129.         }
  130.         s_list = s_list->l_next;
  131.         }
  132.     }
  133.     return fld_head;
  134. }
  135.  
  136. free_fld_list(fld_head)
  137. fld_lst_t *fld_head;
  138. {
  139.     fld_lst_t *fld_elem;
  140.     while (fld_elem = fld_head) {
  141.         fld_head = fld_elem->fld_next;
  142.         free(fld_elem);
  143.     }
  144. }
  145.