home *** CD-ROM | disk | FTP | other *** search
- /* DiscIndex 1.00
- * Record searching code
- * By Neil A Carson 1994 A.D.
- */
-
- #include <ctype.h>
- #include <string.h>
-
- #include "dbox.h"
- #include "discindex.h"
- #include "visdelay.h"
- #include "werr.h"
-
- extern dbox find_dbox, matches_dbox;
- extern BOOL find_open, matches_open;
- extern match_win_type match_win_info;
- extern search_win_type search_win_info;
-
- extern void fill_in_matches(dbox, match_win_type *);
-
- void matches_event_handler(dbox, void *);
- void do_search(search_win_type *s);
- void searchnstore(int, int, match_type, search_win_type *);
- void clean_up(void);
- BOOL mem_find(BOOL case_sens, char *start, char *to_match);
-
- void search(void)
- {
- BOOL filling = TRUE, all_go = FALSE;
-
- if (!find_open)
- {
- find_dbox = dbox_new("Find");
- dbox_show(find_dbox);
- }
-
- if (search_win_info.fields & disc_name) dbox_setnumeric(find_dbox, 2, 1);
- else dbox_setnumeric(find_dbox, 2, 0);
- if (search_win_info.fields & disc_desc) dbox_setnumeric(find_dbox, 3, 1);
- else dbox_setnumeric(find_dbox, 3, 0);
- if (search_win_info.fields & file_name) dbox_setnumeric(find_dbox, 4, 1);
- else dbox_setnumeric(find_dbox, 4, 0);
- if (search_win_info.fields & file_desc) dbox_setnumeric(find_dbox, 5, 1);
- else dbox_setnumeric(find_dbox, 5, 0);
- if (search_win_info.case_sensitive) dbox_setnumeric(find_dbox,8,1);
- else dbox_setnumeric(find_dbox, 8, 0);
- dbox_setfield(find_dbox, 11, search_win_info.string);
-
- if (find_open) return;
- find_open = TRUE;
-
- while (filling)
- {
- switch(dbox_fillin(find_dbox))
- {
- case 12:
- if (!dbox_persist())
- {
- all_go = TRUE;
- filling = FALSE;
- }
- break;
-
- case dbox_CLOSE:
- filling = FALSE;
- break;
-
- default: break;
- }
- }
-
- search_win_info.fields = 0;
- if (dbox_getnumeric(find_dbox, 2)) search_win_info.fields |= disc_name;
- if (dbox_getnumeric(find_dbox, 3)) search_win_info.fields |= disc_desc;
- if (dbox_getnumeric(find_dbox, 4)) search_win_info.fields |= file_name;
- if (dbox_getnumeric(find_dbox, 5)) search_win_info.fields |= file_desc;
- search_win_info.case_sensitive = dbox_getnumeric(find_dbox, 8);
- dbox_getfield(find_dbox, 11, search_win_info.string, 32);
- dbox_dispose(&find_dbox);
- find_open = FALSE;
-
- if (!all_go) return;
-
- if (search_win_info.fields == 0)
- {
- werr(0, "With search criteria like that, nothing will happen!");
- return;
- }
- match_win_info.no = 0;
- visdelay_begin();
- memset(&match_win_info, 0, sizeof(match_win_type));
- do_search(&search_win_info);
- visdelay_end();
- if (match_win_info.matches == 0)
- {
- werr(0, "None found, sorry!");
- return;
- }
- clean_up();
- }
-
- void do_search(search_win_type *s)
- {
- disc_type *disc;
- int disc_cnt, file_cnt;
-
- for (disc_cnt = 0; disc_cnt < records.no; disc_cnt ++)
- {
- disc = disc_locate(disc_cnt);
- if (disc == 0) werr(1, "Flawed disc numbering in search!");
- if (s->fields & disc_name)
- {
- searchnstore(disc_cnt, -1, match_dname, s);
- if (match_win_info.no == 16) return;
- }
- if (s->fields & disc_desc)
- {
- searchnstore(disc_cnt, -1, match_ddesc, s);
- if (match_win_info.no == 16) return;
- }
-
- if (disc->files != 0)
- {
- if ((s->fields & file_name) || (s->fields & file_desc))
- {
- for (file_cnt = 0; file_cnt < disc->no; file_cnt ++)
- {
- if (s->fields & file_name)
- {
- searchnstore(disc_cnt, file_cnt, match_fname, s);
- if (match_win_info.no == 16) return;
- }
- if (s->fields & file_desc)
- {
- searchnstore(disc_cnt, file_cnt, match_fdesc, s);
- if (match_win_info.no == 16) return;
- }
- }
- }
- }
- }
- }
-
- void searchnstore(int disc, int file, match_type to_match, search_win_type *s)
- {
- char string[32];
- disc_type *d;
-
- d = disc_locate(disc);
- if (d == 0)
- werr(1, "Attempt to get non-existent disc!");
-
- switch(to_match)
- {
- case match_dname:
- strncpy(string, d->disc_name, 31);
- break;
-
- case match_ddesc:
- strncpy(string, d->disc_desc, 31);
- break;
-
- case match_fname:
- strncpy(string, d->files[file].name, 31);
- break;
-
- case match_fdesc:
- strncpy(string, d->files[file].desc, 31);
- break;
-
- default: werr(1, "Fatal call to searchnstore!");
- }
-
- string[31] = 0;
- if (!mem_find(s->case_sensitive, string, s->string)) return;
-
- /* Now we have found something, store it! */
-
- match_win_info.disc_nos[match_win_info.no] = disc;
- if (file != -1) match_win_info.file_nos[match_win_info.no] = file;
- else match_win_info.file_nos[match_win_info.no] = 0;
- match_win_info.matches[match_win_info.no] = to_match;
- if (file != -1) match_win_info.matches[match_win_info.no] |= show_fname;
- match_win_info.no ++;
- }
-
- void clean_up(void)
- {
- if (!matches_open)
- {
- matches_dbox = dbox_new("Matches");
- dbox_eventhandler(matches_dbox, matches_event_handler, 0);
- dbox_showstatic(matches_dbox);
- matches_open = TRUE;
- }
- fill_in_matches(matches_dbox, &match_win_info);
- }
-
- /* Fast(ish) function for finding a string in a given area of memory.
- * Returns NULL is string not found
- */
- BOOL mem_find(BOOL case_sens, char *start, char *to_match)
- {
- char lhs[32], rhs[32];
- int len, cnt, lcnt;
- BOOL matched;
-
- len = strlen(to_match);
- strncpy(lhs, start, 32);
- lhs[31] = 0;
- strncpy(rhs, to_match, 32);
- rhs[31] = 0;
- if (!case_sens)
- for (cnt = 0; cnt < 32; cnt ++)
- {
- lhs[cnt] = (char) toupper(lhs[cnt]);
- rhs[cnt] = (char) toupper(rhs[cnt]);
- }
-
- for (cnt = 0; cnt < 32; cnt ++)
- {
- if (lhs[cnt] == rhs[0])
- {
- matched = TRUE;
- for (lcnt = 0; lcnt < len; lcnt ++)
- {
- if (lhs[cnt+lcnt] != rhs[lcnt]) matched = FALSE;
- if ((matched == FALSE) || ((lcnt + cnt) >= 32)) break;
- }
- if (matched) return TRUE;
- }
- }
- return FALSE;
- }
-
- void matches_event_handler(dbox d, void *handle)
- {
- dbox_field f;
- int disc;
-
- handle = handle;
-
- f = dbox_get(d);
-
- if (f == dbox_CLOSE)
- {
- dbox_eventhandler(d, 0, 0);
- dbox_dispose(&d);
- matches_open = FALSE;
- return;
- }
- /* Otherwise it must be a disc icon */
-
- disc = (f - 3) / 3;
- if (disc < match_win_info.no)
- set_current_disc(match_win_info.disc_nos[disc]);
- }
-