home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / ST_USER / 1989 / USER1289.MSA / LISTINGS_CRC.C < prev    next >
C/C++ Source or Header  |  1989-09-28  |  3KB  |  146 lines

  1. /*       CRC claculator      */
  2. /*    By R.A.Waddilove    */
  3. /* Last modified 08/09/89 */
  4.  
  5. #include <strings.h>
  6. #include <obdefs.h>
  7. #include <stdio.h>
  8. #include <osbind.h>
  9.  
  10. #define DIALOGUE    0
  11. #define CRC            5
  12. #define QUIT        6
  13.  
  14. #define POINTER 0
  15. #define BEE        2
  16. #define HIDE    256
  17. #define SHOW    257
  18.  
  19. int    contrl[12],
  20.     intin[128], intout[128],
  21.     ptsin[128], ptsout[128],
  22.     work_in[12], work_out[57];
  23.  
  24. int    handle,            /* vdi handle */
  25.     hchar,            /* character height */
  26.     wchar,            /* character width */
  27.     dx,dy,dw,dh;    /* dialog coords */
  28.   
  29. OBJECT *dialog;
  30.  
  31. gem_on()
  32. {
  33.     int i;
  34.     appl_init();
  35.     handle = graf_handle(&wchar,&hchar,&i,&i);   /* i is dummy variable */
  36.     for (i = 0; i < 10; work_in[i++] = 1);
  37.     work_in[10] = 2;
  38.     v_opnvwk(work_in,&handle,work_out);
  39. }
  40.  
  41. gem_off()
  42. {
  43.     rsrc_free();
  44.     v_clsvwk(handle);
  45.     appl_exit();
  46. }
  47.  
  48. char path[80],name[20];
  49.  
  50. main()
  51. {
  52.     gem_on();
  53.     if ( !rsrc_load("crc.rsc") ) {
  54.         v_clsvwk(handle);
  55.         appl_exit();    /* can't find resource */
  56.     }
  57.     rsrc_gaddr(0,DIALOGUE,&dialog);            /* Get dialog address */
  58.     form_center(dialog,&dx,&dy,&dw,&dh);    /* centre dialog */
  59.     do_dialog();
  60.     gem_off();
  61. }
  62.  
  63. do_dialog()
  64. {
  65.     long crc,get_crc();
  66.     int result;
  67.     char number[5];
  68.     graf_mouse(POINTER,0);
  69.     graf_mouse(HIDE,0);
  70.     form_dial(0,0,0,0,0,dx,dy,dw,dh);        /* save screen */
  71.     objc_draw(dialog,0,32767,dx,dy,dw,dh);    /* draw dialog */
  72.     graf_mouse(SHOW,0);
  73.     do {
  74.         do {
  75.             result = form_do(dialog,0);
  76.         } while ( result<1 || result>7 );
  77.         dialog[result].ob_state ^= SELECTED;
  78.         if ( result==CRC )
  79.             if ( get_file() ) {
  80.                 graf_mouse(HIDE,0);
  81.                 v_gtext(handle,dx+wchar*10,dy+hchar*14,name);
  82.                 crc = get_crc();
  83.                 num_to_asc(crc,number);
  84.                 v_gtext(handle,dx+wchar*10,dy+hchar*16,number);
  85.                 graf_mouse(SHOW,0);
  86.             }
  87.     } while ( result!=QUIT );
  88.     graf_mouse(HIDE,0);
  89.     form_dial(3,0,0,0,0,dx,dy,dw,dh);    /* restore screen */
  90.     graf_mouse(SHOW,0);
  91. }
  92.  
  93. get_file()
  94. {
  95.     int button,len;
  96.     strcpy(path,"A:\\*.*");    /* default path */
  97.     path[0] += Dgetdrv();    /* set to current drive */
  98.     strcpy(name,"");        /* null filename */
  99.     fsel_input(path,name,&button);    /* do file selector */
  100.     graf_mouse(HIDE,0);
  101.     objc_draw(dialog,0,32767,dx,dy,dw,dh);    /* re-draw dialog */
  102.     graf_mouse(SHOW,0);
  103.     if ( name[0]=='\0' || button==0 )
  104.         return(0);    /* no filename or Cancel pressed */
  105.     len = strlen(path);
  106.     while ( path[len-1]!='\\' )
  107.         --len;
  108.     path[len] = '\0';
  109.     strcat(path,name);
  110.     return(1);
  111. }
  112.  
  113. long get_crc()
  114. {
  115.     register int x;
  116.     register long hl,c;
  117.     FILE *stream = fopen(path,"br");
  118.     if ( stream==0 ) return;
  119.     hl = c = 0;
  120.     while ( !feof(stream ) ) {
  121.         hl ^= (unsigned) (getc(stream) << 8);
  122.         for (x = 8; x > 0; --x){
  123.             c = hl>>15;
  124.             if (c)
  125.                 hl ^= 0x810;
  126.             hl = hl<<1; hl |= c; c = hl>>16; hl &= 0xFFFF;
  127.        }
  128.     }
  129.     fclose(stream);
  130.     return(hl);
  131. }
  132.  
  133. num_to_asc(n,s)
  134. register long n;
  135. char s[];
  136. {
  137.     register int i,r;
  138.     s[0]=' '; s[1]=' '; s[2]=' '; s[3]=' '; s[4] = '\0';
  139.     for ( i=3; i>-1; --i ) {
  140.         r = n % 16;
  141.         if ( r>9 )    r += 7;
  142.         s[i] = r+48;
  143.         n /= 16;
  144.     }
  145. }
  146.