home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1994 Brad Eacker,
- * (Music, Intuition, Software, and Computers)
- * All Rights Reserved
- */
-
- #include <stdio.h>
- #include <fcntl.h>
- #include "sql.h"
- #include "dbf.h"
-
- struct fld_lst {
- struct fld_lst *fld_next;
- dbfield_t *fld_db_f;
- };
- typedef struct fld_lst fld_lst_t;
-
- extern fld_lst_t *bld_fld_list();
-
- sql_sel(a_or_d, s_list, t_ref, w_def, o_def)
- int a_or_d;
- list_elem_t *s_list, *t_ref, *w_def, *o_def;
- {
- int retval = 0;
- dbhead_t *dbh;
- char *cp;
- fld_lst_t *flp, *tflp;
-
- if (debug_output) {
- printf("selection\nColumn list:\n");
- scan_elems(s_list);
- printf("Table list:\n");
- scan_elems(t_ref);
- printf("Where Defs:\n");
- scan_elems(w_def);
- printf("Order by Defs:\n");
- scan_elems(o_def);
- printf("\n");
- }
- cp = get_list_name(t_ref);
- if (dbh = dbf_open(cp, O_RDONLY)) {
- if (verbose)
- dbf_head_info(dbh);
- if (flp = bld_fld_list(dbh, s_list)) {
- while (cp = dbf_get_next(dbh)) {
- if ( !is_valid_rec(cp) )
- continue;
- tflp = flp;
- while (tflp) {
- dbfield_t *cur_f = tflp->fld_db_f;
- char *tcp = get_field_val(cp, cur_f, NULL);
-
- printf(cur_f->db_format, tcp);
- free(tcp);
- if (tflp = tflp->fld_next)
- printf(" ");
- }
- printf("\n");
- free(cp);
- }
- free_fld_list(flp);
- }
- } else {
- printf("unable to open %s\n", cp);
- }
-
- return retval;
- }
-
- fld_lst_t *
- add_fld_list(fld_head, db_f)
- fld_lst_t *fld_head;
- dbfield_t *db_f;
- {
- fld_lst_t *fld_elem, *tfld;
-
- if (fld_elem = (fld_lst_t *)malloc(sizeof(fld_lst_t))) {
- fld_elem->fld_next = NULL;
- fld_elem->fld_db_f = db_f;
- if (tfld = fld_head) {
- while (tfld->fld_next)
- tfld = tfld->fld_next;
- tfld->fld_next = fld_elem;
- }
- }
- return fld_elem;
- }
-
- fld_lst_t *
- bld_fld_list(dbh, s_list)
- dbhead_t *dbh;
- list_elem_t *s_list;
- {
- fld_lst_t *fld_head = NULL, *fld_elem;
- int nfields = dbh->db_nfields;
- dbfield_t *db_f = dbh->db_fields;
-
- if (strcmp(get_list_name(s_list), "*") == 0) {
- while (nfields-- > 0) {
- if (fld_elem = add_fld_list(fld_head, db_f++)) {
- if (fld_head == NULL)
- fld_head = fld_elem;
- } else {
- free_fld_list(fld_head);
- fld_head = NULL;
- break;
- }
- }
- } else {
- char *cp;
- int i;
-
- while (s_list) {
- cp = get_list_name(s_list);
- db_f = dbh->db_fields;
- for (i = nfields; i > 0; i--) {
- if (strcmp(db_f->db_fname, cp) == 0) {
- if (fld_elem = add_fld_list(fld_head, db_f)) {
- if (fld_head == NULL)
- fld_head = fld_elem;
- } else {
- free_fld_list(fld_head);
- fld_head = NULL;
- break;
- }
- break;
- }
- db_f++;
- }
- s_list = s_list->l_next;
- }
- }
- return fld_head;
- }
-
- free_fld_list(fld_head)
- fld_lst_t *fld_head;
- {
- fld_lst_t *fld_elem;
- while (fld_elem = fld_head) {
- fld_head = fld_elem->fld_next;
- free(fld_elem);
- }
- }
-