home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************
- * This code is provided as an example of how to interface your C programs *
- * with DATAMAGE files. The program makes use of the two libraries provided *
- * but does not call all the functions they contain. *
- * *
- * Only REGISTERED USERS of DATAMAGE are allowed to insert this code into *
- * their programs. It is provided here for reference and testing only. *
- * *
- * The implementation of this program required the writing of only one func- *
- * tion: browse_MAGE_files. Of course, I made it out of a function already *
- * present in the library: vs_MAGE_record. *
- * *
- * The code compiles with Quick C 2.5. If you are a turbo fan you should be *
- * able to get it to go, but you may need to write your own functions to *
- * convert the "mbf" numerics to ieee, and vice-versa. *
- * *
- * I thougt long and hard before giving this code away. It's not that great *
- * but it took me a few minutes to write it, I'll guarantee you! I hope you *
- * can find it in your heart to register your DATAMAGE. *
- * *
- * If you find you like the code I'm looking for a good, steady job. *
- *****************************************************************************/
-
- #include <direct.h>
- #include <math.h>
- #include <malloc.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- #include "hcwp_lib.h"
- #include "mageisam.h"
-
- void draw_border ()
- {
- BYTE register d;
-
- locate (0,0); crt_out (201);
- locate (0,1); line_out (78,205);
- locate (0,79); crt_out (187);
-
- locate (20,0); crt_out (204);
- locate (20,1); line_out (78,205);
- locate (20,79); crt_out (185);
-
- for (d = 1; d < 24; d++)
- {
- if (d == 20) continue;
- locate (d,0); crt_out (186);
- locate (d,79); crt_out (186);
- }
-
- locate (24,0); crt_out (200);
- locate (24,1); line_out (78,205);
- locate (24,79); crt_out (188);
-
- locate (0,29);
- clr_prnt ("[ D A T A M A G E ]", 25);
-
- locate (24,30);
- clr_prnt ("[C LANGUAGE DEMO]", 25);
- }
-
- BYTE b_line (BYTE file, BYTE field, SWORD line,
- SWORD subscr, BYTE revsw, SWORD far * base)
- {
- /* print line for browse file */
-
- BYTE b, c, d, row, z;
-
- WORD x;
-
- SWORD rno;
-
- DWORD offset;
-
- double temp, msbin;
-
- row = (line * 2) + 2;
-
- /* get offset of next record from array */
-
- x = * (base + subscr);
-
- /* offset in control file */
-
- offset = x * 2;
-
- /* read in record number */
-
- c = fseek (fi [file].ctrl, offset, SEEK_SET);
- fread (& rno, 2, 1, fi [file].ctrl);
-
- strcpy (gp_str [12], "RCD# ");
- sprintf (gp_str [11], "%hi", rno);
- strcat (gp_str [12], gp_str [11]);
-
- locate (row, 7); prnt (gp_str [12]);
-
- strcpy (gp_str [12], "# ");
- sprintf (gp_str [11], "%u", field + 1);
- strcat (gp_str [12], gp_str [11]);
-
- locate (row, 20); prnt (gp_str [12]);
-
- /* offset in main file */
-
- offset = (fi [file].rcd_len * x) + fi [file].key [field] -> offset;
-
- /* read in field */
-
- c = fseek (fi [file].main, offset, SEEK_SET);
- fread (f_buff, 1, fi [file].key [field] -> length, fi [file].main);
-
- f_buff [fi [file].key [field] -> length] = 0;
-
- if (fi [file].key [field] -> type < 2) rtrim (f_buff);
-
- else
- {
- c = dmsbintoieee ( (double *) f_buff, & temp);
-
- strcpy (gp_str [4], "%. lf");
- gp_str [4] [2] = fi [file].key [field] -> spare + 48;
-
- sprintf (f_buff, gp_str [4], temp);
- }
-
- locate (row, 30);
-
- prnt (f_buff);
-
- return (0);
- }
-
- BYTE fill_b (BYTE file, BYTE field, SWORD * curr_line,
- SWORD * top_line, WORD last, SWORD far * base)
- {
- /* fill screen for browse file */
-
- BYTE c;
-
- SWORD d, m;
-
- clw (2);
-
- /* do lines */
-
- for (d = 0; d < 9; d++)
- {
- m = d + * top_line;
-
- if (m >= last) continue;
-
- c = b_line (file, field, d, m, 0, base);
- if (c) return (1);
- }
-
- /* check current line */
-
- if (* top_line + * curr_line >= last)
- * curr_line = last - * top_line - 1;
-
- return (0);
- }
-
- void b_menu (BYTE file)
- {
- /* menu for browse file */
-
- clw (3);
- locate (21,6);
- prnt ("FILE: ");
-
- prnt (fi [file].dir);
-
- locate (21, 58);
- prnt ("LOCATION: ");
-
- locate (22,6);
- prnt ("CONTROL KEYS: \x018 \x019 Pg-U Pg-D Home End Quit Eneter Delete View Search");
- }
-
- BYTE far browse_MAGE_files (BYTE file, BYTE field, BYTE * error_str)
- {
- /* browses file passed. this is a NEW one, and not well tested */
-
- BYTE c, close_sw [3], d, ft_sw, wfg = 0, wfg2 = 0, ret_val, row;
-
- WORD i = 0, max_rcd, n = 0, x;
-
- SWORD rno, top_line = 0, curr_line = 5, far * base;
-
- DWORD offset = 0;
-
- BYTE b_line (BYTE, BYTE, SWORD, SWORD, BYTE, SWORD far *);
-
- BYTE fill_b (BYTE, BYTE, SWORD *, SWORD *, WORD, SWORD far *);
-
- void b_menu (BYTE);
-
- for (c = 0; c < 3; c++) close_sw [c] = 0;
-
- /* open ctrl file */
-
- c = condit_open (file, 3, "r+b");
-
- switch (c)
- {
- case 0: break;
-
- case 1: {close_sw [2] = 1; break;}
-
- case 2: return (1);
- }
-
- c = fseek (fi [file].ctrl, offset, SEEK_SET);
-
- /* open main file */
-
- c = condit_open (file, 1, "r+b");
-
- switch (c)
- {
- case 0: break;
-
- case 1: {close_sw [0] = 1; break;}
-
- case 2: return (1);
- }
-
- check_open:
-
- if (wfg)
- {
- strcpy (error_str, "UNABLE TO ACCESS BROWSE FILES");
-
- ret_val = 2;
- goto bf_end;
- }
-
- while (i < fi [file].num_rcds)
- {
-
- fread (& rno, 2, 1, fi [file].ctrl);
-
- if (rno) n++;
-
- i ++;
- }
-
- if (! n)
- {
- strcpy (error_str, "BROWSE FILE HAS NO RECORDS");
-
- ret_val = 2;
- goto bf_end;
- }
-
- max_rcd = n + 100;
-
- base = (SWORD far *) _fmalloc ( (max_rcd * 2) + 10);
-
- if (base == NULL)
- {
- strcpy (error_str, "INSUFFICIANT MEMORY FOR FILE BROWSE");
-
- ret_val = 2;
- goto bf_end;
- }
-
- i = x = 0;
-
- c = fseek (fi [file].ctrl, (DWORD) 0, SEEK_SET);
-
- while (i < fi [file].num_rcds)
- {
- fread (& rno, 2, 1, fi [file].ctrl);
-
- if (rno) * (base + x++) = i;
-
- i ++;
- }
-
- ft_sw = 0; /* check_heap (); */
-
- c = fill_b (file, field, & curr_line, & top_line, n, base);
-
- if (c) {ret_val = 1; goto bf_end_2;}
-
- while (! wfg)
- {
- if (! ft_sw) {b_menu (file); ft_sw = 1;}
-
- row = (curr_line * 2) + 2;
-
- gp_str [6] [0] = 0;
- sprintf (gp_str [6],"%hi", top_line + curr_line + 1);
-
- locate (21, 68);
- prnt (" ");
-
- locate (21, 68);
- prnt (gp_str [6]);
-
- /* get offset of next record from array */
-
- x = * (base + top_line + curr_line);
-
- /* offset in control file */
-
- offset = x * 2;
-
- /* read in record number */
-
- c = fseek (fi [file].ctrl, offset, SEEK_SET);
- fread (& rno, 2, 1, fi [file].ctrl);
-
- sprintf (gp_str [11], "%hi", rno);
-
- strcpy (gp_str [12], "#"); strcat (gp_str [12], gp_str [11]);
-
- locate (row, 7);
- clr_prnt ("RCD# ", 155);
- clr_prnt (gp_str [11], 155);
-
- get_one (& ctrl_val, & nrml_val);
-
- locate (row, 7);
- clr_prnt ("RCD# ", attr);
- clr_prnt (gp_str [11], attr);
-
- if (nrml_val)
- {
- switch (nrml_val)
- {
- case 'Q' :
- case 'q' :
- {
- clw (3); locate (22,32); ft_sw = 0;
- prnt ("1=QUIT 2=NO");
-
- if (get_choice (2) - 1) break;
-
- wfg = 1; ret_val = 1;
- nrml_val = 0; break;
- }
-
- case 13 : /* record selected */
- {
- ft_sw = 0;
-
- /* get offset of record from array */
-
- x = * (base + top_line + curr_line);
-
- fi [file].sequence = x;
-
- /* offset in control file */
-
- offset = x * 2;
-
- /* read in record number */
-
- c = fseek (fi [file].ctrl, offset, SEEK_SET);
- fread (& rno, 2, 1, fi [file].ctrl);
-
- fi [file].r_number = rno;
-
- /* read record in */
-
- c = read_MAGErcd (file, x);
-
- /* display/edit record */
-
- c = access_MAGE_record (file, 0);
-
- c = fill_b (file, field, & curr_line,
- & top_line, n, base);
-
- break;
- }
-
- case 'e':
- case 'E':
- {
- clw (3); locate (22,28); ft_sw = 0;
- prnt ("1=ENTER A RECORD 2=NO");
-
- if (get_choice (2) - 1) break;
-
- /* get next record number and sequence */
-
- c = get_next_rno (file);
- if (c) break;
-
- /* balk if addition and 100 have been added */
-
- if (fi [file].sequence == fi [file].num_rcds &&
- n + 1 > max_rcd)
- {
- clw (3); beep(); locate (22, 25);
- prnt ("100 RECORDS HAVE BEEN ENTERED");
- wait (4); break;
- }
-
- /* display record for entry */
-
- c = access_MAGE_record (file, 0);
-
- /* if write add new record to display,
- increment total */
-
- if (! c) * (base + n++) = fi [file].sequence;
-
- /* new screen */
-
- c = fill_b (file, field, & curr_line,
- & top_line, n, base);
-
- break;
- }
-
- case 'd':
- case 'D':
- {
- ft_sw = 0;
-
- strcpy (c_s [0], "1=DELETE RECORD: ");
-
- /* get offset of record from array */
-
- x = * (base + top_line + curr_line);
-
- fi [file].sequence = x;
-
- /* offset in control file */
-
- offset = x * 2;
-
- /* read in record number */
-
- c = fseek (fi [file].ctrl, offset, SEEK_SET);
- fread (& rno, 2, 1, fi [file].ctrl);
-
- sprintf (gp_str [14], "%hi", rno);
- strcat (c_s [0], gp_str [14]);
-
- strcpy (c_s [1], "2=NO");
-
- c = cntrstrp ();
-
- clw (3); locate (22,c);
- prnt (c_s [2]);
-
- if (get_choice (2) - 1) break;
-
- /* set index values */
-
- for (d = 0; d < 5; d++)
- fi [file].index_results [d] = 0;
-
- /* blank buffer */
-
- ready_files (file);
-
- /* zero record number */
-
- fi [file].r_number = 0;
-
- /* write blank */
-
- c = write_MAGErcd (file);
-
- /* remove record from display array */
-
- for (i = top_line + curr_line; i < n; i++)
- {
- * (base + i) = * (base + i + 1);
- }
-
- /* decrement number of records */
-
- n--;
-
- /* protect from showing blank screen */
-
- if (top_line >= n) top_line = n - 9;
- if (top_line < 0) top_line = 0;
-
- c = fill_b (file, field, & curr_line,
- & top_line, n, base);
-
- break;
- }
-
- case 'v':
- case 'V':
- {
- clw (3); locate (22,31); ft_sw = 0;
- prnt ("SELECT VIEW FIELD");
-
- save_window (9); clw (9);
- c = show_fields (file, 9);
- repl_window (9);
-
- if (! c) field = nrml_val;
-
- c = fill_b (file, field, & curr_line,
- & top_line, n, base);
-
- break;
- }
-
- case 's':
- case 'S':
- {
- ft_sw = 0;
- c = search_MAGE_files (file);
-
- if (! c)
- {
- /* find offset in array */
-
- for (wfg2 = 0, i = 0; ! wfg2 && i < n; i++)
- if (* (base + i) ==
- fi [file].sequence) wfg2 = 1;
-
- /* locate display */
-
- if (wfg2)
- {
- c = 0;
- top_line = i - 6;
-
- if (top_line < 0)
- {
- c = 0 - top_line;
- top_line = 0;
- }
-
- curr_line = 5 - c;
- }
-
- }
-
- c = fill_b (file, field, & curr_line,
- & top_line, n, base);
-
- break;
- }
-
-
- default : beep (); break;
- }
-
- continue;
- }
-
- switch (ctrl_val)
- {
- case 71: /* home */
- {
- top_line = 0;
-
- c = fill_b (file, field, & curr_line,
- & top_line, n, base);
-
- if (c) return (1);
-
- continue;
- }
-
- case 72: /* line up */
- {
- if ( (curr_line) - 1 >= 0) {curr_line -= 1; continue;}
-
- if ( (top_line) - 1 < 0) {beep(); continue;}
- top_line -= 1;
- scroll_window (2, 2, 2);
-
- c = b_line (file, field, 0, top_line, 0, base);
- if (c) return (1);
-
- continue;
- }
-
- case 73: /* page up */
- {
- top_line -= 9;
- if (top_line < 0) top_line = 0;
-
- c = fill_b (file, field,
- & curr_line, & top_line, n, base);
-
- if (c) return (1);
-
- continue;
- }
-
- case 79: /* end */
- {
- top_line = n - 9;
- if (top_line < 0) top_line = 0;
-
- c = fill_b (file, field, & curr_line,
- & top_line, n, base);
-
- if (c) return (1);
-
- continue;
- }
-
- case 80: /* line down */
- {
- x = curr_line + 1;
-
- if (x <= 8)
- {
- if (x < n) curr_line += 1; else beep();
- continue;
- }
-
- if (top_line + 10 > n)
- {beep (); continue;}
-
- top_line += 1;
- scroll_window (2, 1, 2);
-
- c = b_line (file, field, 8, top_line + 8, 0, base);
-
- if (c) {wfg = 1; ret_val = 1; continue;}
-
- continue;
- }
-
- case 81: /* page down */
- {
- top_line += 9;
-
- if (top_line > n - 9)
- top_line = n - 9;
-
- if (top_line < 0) top_line = 0;
-
- c = fill_b (file, field,
- & curr_line, & top_line, n, base);
-
- if (c) return (1);
-
- continue;
- }
-
- default: beep(); continue;
- }
-
- }
-
- if (wfg == 2) {_ffree (base); goto check_open;}
-
- bf_end_2:
-
- _ffree (base);
-
- bf_end:
-
- if (close_sw [3]) {fclose (fi [file].ctrl); fi [file].ctrl = NULL;}
- if (close_sw [1]) {fclose (fi [file].main); fi [file].main = NULL;}
-
- return (ret_val);
- }
-
- main (BYTE argc, BYTE * argv [])
- {
- BYTE c, choice, color = 0, d, i, running = 1;
-
- BYTE * cwd;
-
- SBYTE swfg;
-
- FILE * target;
-
- /* find current path */
-
- cwd = getcwd (pth_str, 55);
-
- if (cwd == NULL)
- {
- prnt ("CURRENT PATH TOO LONG - PROCESSING TERMINATED");
- return (0);
- }
-
- c = strlen (pth_str) - 1;
-
- if (pth_str [c] == 92) pth_str [c] = 0;
-
- /* get video */
-
- get_vs (& attr);
- if (attr != 7) color = 1;
-
- /* define windows */
-
- c = 0; d = attr;
-
- if (color) d = 22;
- c = set_window (0, 0, 0, 24, 79, d); /* whole screen */
- if (c) return (0);
-
- c = set_window (2, 1, 1, 19, 78, attr); /* data display area */
- if (c) return (0);
-
- if (color) d = 31;
- c = set_window (3, 21, 1, 23, 78, d); /* dialog box */
- if (c) return (0);
-
- if (color) d = 14; else d = 120;
- c = set_window (9, 3, 8, 19, 71, d); /* 1 taller than files window */
- if (c) return (0);
-
- if (color) d = 75; else d = attr;
- c = set_window (4, 4, 8, 19, 71, d); /* files border */
- if (c) return (0);
-
- c = set_window (5, 5, 9, 18, 70, d); /* files/fields window */
- if (c) return (0);
-
- if (color) d = 14; else d = 120;
- c = set_window (8, 9, 8, 14, 71, d); /* 1 taller than datafiles */
- if (c) return (0);
-
- if (color) d = 44; else d = attr;
- c = set_window (6, 10, 8, 14, 71, d); /* datafiles border */
- if (c) return (0);
-
- if (color) d = 46;
- c = set_window (7, 11, 9, 13, 70, d); /* datafiles window */
- if (c) return (0);
-
- /* re-vector int 24h - establish hardware error handler */
-
- _harderr (hard_err);
-
- /* ready buffers */
-
- for (d = 0; d < 8; d++) fi [d].buff = NULL;
-
- /* ready field definition pointers */
-
- for (d = 0; d < 8; d++)
- for (i = 0; i < 200; i++) fi [d].key [i] = NULL;
-
- /* begin program */
-
- no_cursor (); clw (0);
-
- draw_border (); clw (2); clw (3);
-
- /* strcpy (pth_str, "b:"); */
-
- strcpy (gp_str [10], pth_str);
-
- c = select_datafile();
- if (c) {cls (); return (0);}
-
- strcpy (gp_str [1], gp_str [10]);
- strcat (gp_str [1], "\\");
- get_farst (gp_str [11], files [0] [nrml_val].name);
- strcat (gp_str [1], gp_str [11]);
- rtrim (gp_str [1]);
-
- strcpy (c_s [0], "LOADING DATAFILE IN: ");
- strcpy (c_s [1], gp_str [1]);
- c = cntrstrp ();
- clw (3); locate (22, c); prnt (c_s [2]);
-
- c = open_file (0); clw (2);
-
- if (!c)
- {
- c = browse_MAGE_files (0, 0, gp_str [10]);
-
- close_file (0);
- }
-
- for (d = 1; d < 10; d++) free (w_a [d].save);
-
- attr = 7; cls (); nrml_cursor (); locate (12, 0);
- }
-