home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 192_01 / tds.c < prev    next >
Text File  |  1979-12-31  |  12KB  |  354 lines

  1. /*   (C) Copyright 1985, 1986 Neil V. Deasy                             
  2.         Program Name: Terminal Digit Sorter
  3.           Where Used: In hospital's Medical Records Department.  It speeds
  4.             the task of pulling and re-filing records into
  5.             terminal digit (non-numeric) order.
  6.             Features: Program is driven by the key words Sort, Exit, Help.
  7.             Anything else is checked upon input to meet the
  8.             expected criteria of numbers of six digits length.
  9.             Improper entry is rejected and an error message
  10.             explaines why.  Record numbers are not sorted until
  11.             specified, and if not specified, are saved in the
  12.             file 'DATA.DAT'.  When the operator requests sorting
  13.             all numbers are sorted, that includes ones previously
  14.             stored as well as those which may have just been
  15.             entered at the keyboard.  The Record numbers are output
  16.             on the printer, with page headers and numbering, in
  17.             their peculiar but proper order.  Beside each Record
  18.             number is printed a line, so that patient names or
  19.             other marginal notes can be made.
  20.  
  21.    ====================================================================  ;
  22. ;   This program sorts numbers into terminal digit order. The numbers    ;
  23. ;   are actually strings, since it is necessary to partition them for    ;
  24. ;   sorting.                                                             ;
  25. ;   Present limit on entries is 600, a considerable number.  Version 1.0 ;
  26. ;   lacked a 'help' screen. Version 1.3 fixed that and added video       ;
  27. ;   attributes.  Version 1.5 fixed an unnecessary 'beep' and provided    ;
  28. ;   nicely paginated output.                                             ;
  29. ;   Version 1.6 adds color and 'boxes' the help text.                    ;
  30. ;                                                                        ;
  31. ;    Eco-C88 is a copyright of Ecosoft, Inc., 1983 - 1986.             ;
  32. ; ===================================================================== */
  33.  
  34. /*  Uses functions available with the Ecosoft Eco-C88 Compiler  Ver 3.00  */
  35. /*  Runs on the IBM and most generics, uses color or monochrome which  */
  36. /*  includes the Hercules clones.  Printer is assumed to be Epson-code  */
  37. /*  compatible, like the Gemini and other printers.  The routines which */
  38. /*  bold, underline or reverse video the text assume ANSI.SYS is installed. */
  39.  
  40.  
  41. #include "stdio.h"
  42. #include "color.h"
  43. #include "string.h"
  44. #define BELL 7
  45. #define ESC 27
  46. int _colorflag = TRUE;   /* says you have color, but works if you don't */
  47. char bigbuff[4200]; /* the size of bigbuff determines how many records  */
  48. int strcmp();       /* can be brought into memory and sorted, in this   */
  49. int cnt, flag, page; /* case the size is 4200/7 = 600 Record numbers,   */
  50. unsigned row, col;   /* see msg4();, elsewhere in this program          */
  51. FILE *fd;
  52.  
  53. main()
  54. {
  55. char *gets();
  56. char s[7];       /* holds 6 digits plus the \0 string terminator */
  57. char *strcpy();
  58. int c, p;
  59. flag = cnt = 0;
  60. row = 4;
  61. col = 3;
  62.     fd = fopen("data.dat","a");     /* opening and/or creating this file */
  63.     if((c = ferror(fd)) != NULL)    /* should produce no problem, however*/
  64.     {                               /* this message is a warning   */
  65.         puts("A file error has occured...");
  66.         exit();
  67.     }
  68.     clrscr();
  69.     cursor(0,29);
  70.     bld();            
  71.     mascr(2,FRED,BOLD);
  72.     puts("TERMINAL DIGIT SORTER");
  73.     unbld();
  74.     cursor(22,27);
  75.     revon();
  76.     mascr(1,FGREEN);
  77.     puts("enter Exit or Sort or Help");
  78.     revoff();
  79.     cursor(23,21);
  80.     bld();
  81.     mascr(2,FBLUE,BOLD);
  82.     puts("(C) Copyright 1985, 1986 Neil V. Deasy");
  83.     unbld();
  84.     mascr(1,FWHITE);
  85. loop:    cursor(row,col);
  86. while(TRUE)    {
  87.     printf("Enter number or selection  ");
  88.     gets(s);
  89.     if(flag != 0)    {   /*  flag is set by error routines */
  90.         eraeol(cursor(24,0));  /* so at this point in the program */
  91.         flag = 0;   /* we erase the error message and reset the */
  92.         }                          /*    flag   */
  93.     cursor(++row,col);
  94.     eraeol(row,col);
  95.     if(tolower(s[0]) == 'e' || tolower(s[0]) == 's' || tolower(s[0])=='h')
  96.     {
  97.         switch(tolower(s[0]))
  98.         {                           /*  here are your choices  */
  99.             case 'e':
  100.                 clrscr();
  101.                 fclose(fd);
  102.                 exit();
  103.             case 's':
  104.                 fclose(fd);
  105.                 append1();
  106.                 exit();
  107.             case 'h':
  108.                 help();
  109.                 printf("\nEnter <CR> to continue. . . .");
  110.                 gets(s);
  111.                 eraeol(cursor(21,0)); /* erase the help msg */
  112.                 for(row = 20; row > 4; row--) /* by backing */
  113.                     eraeol(cursor(row,0));/* up 15 lines*/
  114.                 row = 4;
  115.                 col = 0;
  116.                 eraeol(cursor(row,col));
  117.                 goto loop;
  118.         }
  119.     }      /*  input MUST be 6 characters  */
  120.     if((p = strlen(s)) < 6)  /* this input is less than 6  */
  121.         msg1();
  122.     if((p = strlen(s)) > 6)  /* this input is more than 6  */
  123.         msg2();
  124.     if((p = strlen(s)) ==6)  /* this is exactly 6, BUT  */
  125.     {
  126.         int w;
  127.         for(w = 0; w < 6; w++)   /* let's check each to make */
  128.         if((c = isdigit(s[w])) == 0) /* sure there are NO letters */
  129.         {
  130.             msg3();  /*  it isn't a digit!  */
  131.         }
  132.     }
  133.     if(flag == 0)
  134.         {                           /* since we got here with no */
  135.         fprintf(fd,"%6s\n",&s[0]);  /* errors - write it to the file */
  136.             cnt += 1;
  137.         }
  138.     if(cnt > 14)   /* screen display area is now at max of 15 lines  */
  139.     {
  140.         for(row = 4; row < 20; row++)  /* so clear screen and  */
  141.             eraeol(cursor(row,0));
  142.         cnt = 0; row = 4; col = 3;     /* start over again  */
  143.         cursor(row,col);
  144.         }
  145.     }
  146. }
  147. msg1()   /* messages follow a similar format, see below  */
  148. {
  149.     flag = 1;                    /*  set error flag */
  150.     cursor(--row,col);           /* adjust row count location */
  151.     cursor(24,0);                /* bottom of screen for error message */
  152.     bld();                       /*  attribute on  */
  153.     puts("Error: too FEW digits (6 needed) "); /* what U did wrong */
  154.     unbld();                     /* reset attribute  */
  155.     eraeol(cursor(row,30));      /*  erase prompting line  */
  156.     cursor(row,col);             /* put cursor at start of prompt line  */
  157. putchar(BELL);                       /* audible indication of error  */
  158.     return(flag);                /* says it all */
  159. }
  160. msg2()
  161. {
  162.     flag = 1;
  163.     cursor(--row,col);
  164.     cursor(24,0);
  165.     bld();
  166.     puts("Error: too MANY digits (6 needed)");
  167.     unbld();
  168.     eraeol(cursor(row,30));
  169.     cursor(row,col);
  170. putchar(BELL);
  171.     return(flag);
  172. }
  173. msg3()
  174. {
  175.     flag = 1;
  176.     cursor(--row,col);
  177.     cursor(24,0);
  178.     bld();
  179.     puts("Error: input must be NUMBERS     ");
  180.     unbld();
  181.     eraeol(cursor(row,30));
  182.     cursor(row,col);
  183. putchar(BELL);
  184.     return(flag);
  185. }
  186. msg4()
  187. {
  188.     clrscr();
  189.     puts("TDS will not sort more than 600 numbers.");
  190.     puts("Delete some entries from file 'DATA.DAT' and re-run TDS.");
  191.     exit();
  192. }
  193. append1()
  194. {
  195.     char temp[2];   /* a 2 character swap buffer */
  196.     int x, y;
  197.     x = y = 0;
  198.  
  199.     fd = fopen("data.dat","r");
  200.     clrscr();
  201.     bld();
  202.     puts("READING DATA FILE");
  203.     unbld();
  204.     if((y = fgetc(fd)) == EOF)   /* see if file is empty to start */
  205.     {
  206.         puts("You made no entries to sort.        Program ends.");
  207.         exit();              /* it was, so exit  */
  208.     }
  209.     ungetc(y,fd);                /* it wasn't, so put back a character */
  210.     while((y = feof(fd))==FALSE)    {  /* read file till EOF detected */
  211.         fscanf(fd,"%6s\n",&bigbuff[x]);
  212.         strncpy(&temp[0],&bigbuff[x+4],2);    /* put last 2 char's */
  213.         strncpy(&bigbuff[x+4],&bigbuff[x+2],2); /* up front and */
  214.         strncpy(&bigbuff[x+2],&bigbuff[x],2); /* slide the others  */
  215.         strncpy(&bigbuff[x],&temp[0],2);      /* over to align */
  216.         x += 7;   /* bump count by 7 to include the '\0'  */
  217.     }
  218.     fclose(fd);
  219.     if((x/7) > 600)
  220.         msg4();
  221.     printf("sorting %d entries\t\t",x/7);  /* a visual marker of progress*/
  222. qsort(&bigbuff,(x/7),7,strcmp);     /* a library sort, using string compare  */
  223. bld();
  224. puts("Sorted!");
  225. unbld();
  226. header(&bigbuff,x);     /*  print page header */
  227. fflush(stdlst);
  228. unlink("data.dat");     /* discard the file now that it's printed */
  229. }
  230. header(buf,x)
  231. int x;
  232. {
  233.     int  y, loop, page;
  234.     char temp[2];
  235.     y = loop = 0;
  236.     page = 1;
  237.     dump(page); /* this function prints page header info only */
  238.     for(y = 0; y < x; y++)
  239.     {  /* print sorted Record Numbers  */
  240.         strncpy(&temp[0],&bigbuff[y],2);        /* now that they */
  241.         strncpy(&bigbuff[y],&bigbuff[y+2],2);   /* are sorted put */
  242.         strncpy(&bigbuff[y+2],&bigbuff[y+4],2); /* them back in the */
  243.         strncpy(&bigbuff[y+4],&temp[0],2);      /* proper format and */
  244. fprintf(stdlst,"\t%.2s-",&bigbuff[y]);                  /* send to printer */
  245. fprintf(stdlst,"%.2s-",&bigbuff[y+2]);
  246. fprintf(stdlst,"%.2s",&bigbuff[y+4]);
  247. fprintf(stdlst," ___________\n\n");
  248.         y += 6;
  249.         ++loop;
  250.     if(loop > 25)    /* 26 Records per page, double spaced  */
  251.     {
  252.         loop = 0;
  253.         page += 1;
  254.         putc(12,stdlst);  /*  a form-feed  */
  255.         putc(27,stdlst); putc(87,stdlst); putc(0,stdlst);
  256.         fflush(stdlst);
  257.         dump(page);
  258.         }
  259.     }
  260. fprintf(stdlst,"Total Records: %d",x/7);
  261. putc(12,stdlst);
  262. putc(27,stdlst); putc(87,stdlst); putc(0,stdlst); /* set printer to     */
  263. }                                                 /* normal becaues the */
  264. dump(d)                                           /* program ends here. */
  265. int d;
  266. {
  267. putc(27,stdlst);  putc(69,stdlst);   /* set to print dark  */
  268. fprintf(stdlst,"------------------------------------------------------");
  269. fprintf(stdlst,"---------------------------\n");
  270. clk(); /* current time */
  271. fprintf(stdlst,"\t\t\tTERMINAL DIGIT SORTER\t\t  ");
  272. dte(); /* current date */
  273. fprintf(stdlst,"\nVersion 1.60\t\t\t\t\t\t   Page %d",d);
  274. fprintf(stdlst,"\n------------------------------------------------------");
  275. fprintf(stdlst,"---------------------------\n\n");
  276. putc(27,stdlst);  putc(70,stdlst); /* reset from darkend printing  */
  277. fprintf(stdlst,"\n\t\t  Record Number\t     Last Name\n\n");
  278. putc(27,stdlst);
  279. putc(87,stdlst); /* set printer for double-width characters  */
  280. putc(1,stdlst);
  281. return;
  282. }
  283. help()
  284. {
  285. int wide, deep, r, column;
  286. wide = 71;
  287. deep = 15;
  288. r = 5;
  289. column = 1;
  290. cursor(6,2);
  291. mascr(1,BMAGEN);
  292. puts("You are prompted to enter either a number or a selection which will  ");
  293. puts(" then determine what happens next. Your selections are: Sort, Exit and");
  294. puts(" Help.                                                                ");
  295. puts("----------------------------------------------------------------------");
  296. puts(" HELP - will display this screen.                                     ");
  297. puts(" SORT - will sort the numbers just entered as well as those previously");
  298. puts("        saved in the data file.                                       ");
  299. puts(" EXIT - is the normal ending place after you have entered numbers but ");
  300. puts("        do not want to sort yet. Once you select sort, all numbers are");
  301. puts("        selected and printed.  The number file is then deleted.       ");
  302. puts(" Numbers is the expected (or default) entry if none of the above 3 are");
  303. puts(" selected.  The program will accept no more or no less than 6 digits  ");
  304. puts(" and if you accidently enter letters (other than H,E or S) the program");
  305. puts(" will prompt you for the right kind of input.                         ");
  306. mascr(1,BBLACK);
  307. bld();
  308. mascr(2,FCYAN,BOLD);
  309. box(wide,deep,r,column); /* draw pretty box around help text */
  310. unbld();
  311. mascr(1,FWHITE);
  312. return;
  313. }
  314. bld()     /*  turn bold video on  */
  315. {
  316.     fprintf(stderr,"%c[1m",ESC);
  317. }
  318. unbld()   /*  turn bold video off  */
  319. {
  320.     fprintf(stderr,"%c[0m",ESC);
  321. }
  322. clk()
  323. {
  324. struct time    {
  325.     unsigned int hour;
  326.     unsigned int min;
  327.     unsigned int sec;
  328. };
  329.     struct time *clk;
  330.     clk = 0;
  331. gettime(clk);
  332.     fprintf(stdlst,"%.2u:%.2u:%.2u",clk->hour,clk->min,clk->sec);
  333. }
  334. dte()
  335. {
  336. struct date    {
  337.     unsigned int month;
  338.     unsigned int day;
  339.     unsigned int year;
  340. };
  341.     struct date *dte;
  342.     dte = 0;
  343. getdate(dte);
  344.     fprintf(stdlst,"%.2u/%.2u/%.4u",dte->month,dte->day,dte->year);
  345. }
  346. revon()   /*  turn on reverse video */
  347. {
  348.     fprintf(stderr,"%c[7m",ESC);
  349. }
  350. revoff()  /*  turn off reverse video  */
  351. {
  352.     fprintf(stderr,"%c[0m",ESC);
  353. }
  354.