home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / apps / educatin / morse / sender.c < prev    next >
C/C++ Source or Header  |  1985-11-19  |  5KB  |  216 lines

  1. #include <stdio.h>
  2. #include <gemlib.h>
  3. #include <sender.h>
  4. #include <morse.c>
  5. #include <sendgem.c>
  6.  
  7. #define RSRC_FILENAME    "SENDER.RSC"
  8. #define MAX_DEPTH    10
  9. #define BLANKLINE    -1
  10. #define MORSE_CHARS    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.,?-/"
  11.  
  12. char    buf[132];
  13. short    skipblank = TRUE, filter = TRUE, cspeed, sspeed, csspeed, tbox[4];
  14.  
  15. main()
  16. {
  17.     short done, item;
  18.  
  19.     gem_init();
  20.     clear_window( ywork );
  21.     dirspec[0] = '\0';
  22.     item = (wwork - gl_wchar*40)/2;
  23.     if( item < 0 ) terminate( "[3][Insufficient screen resolution!][Abort]" );
  24.     tbox[0] = xwork+item;
  25.     tbox[2] = xwork+wwork-item;
  26.     item = (hwork - gl_hchar*7)/2;
  27.     tbox[1] = ywork+item;
  28.     tbox[3] = ywork+hwork-item;
  29.  
  30.     if( !rsrc_load( RSRC_FILENAME ) )
  31.         terminate( no_rsc_file );
  32.     if( rsrc_gaddr( 0, 0, &resrcs ) == 0 )
  33.         terminate( bad_rsc_file );
  34.     rsrc_gaddr( R_TREE, TREE1, &resrcs );
  35.  
  36.     done = FALSE;
  37.     while( !done )
  38.     {
  39.         form_center( resrcs, &xobj, &yobj, &wobj, &hobj );
  40.         form_dial( 0, xobj, yobj, wobj, hobj );
  41.         form_dial( 1, 1, 1, 1, 1, xobj, yobj, wobj, hobj );
  42.  
  43.         graf_mouse( ARROW, 0x0L );
  44.         objc_draw( resrcs, 0, MAX_DEPTH, 0, 0, wdesk, hdesk );
  45.  
  46.         if( filter ) select( T1FILTER );
  47.         item = form_do( resrcs, TREE1 );
  48.         if( item == T1EXIT ) done = TRUE;
  49.         else
  50.         {
  51.             form_dial( 2, 1, 1, 1, 1, xobj, yobj, wobj, hobj );
  52.             form_dial( 3, xobj, yobj, wobj, hobj );
  53.             deselect( T1SEND );
  54.             clear_window( ywork );
  55.  
  56.             analyze_form( resrcs );
  57.             send_file();
  58.         }
  59.     }
  60.     rsrc_free();
  61.     terminate( NULL );
  62. }    /* main */
  63.  
  64. adjust_speed()
  65. {
  66.     short element;
  67.  
  68.     if( csspeed < 1 ) return( FALSE );
  69.     sspeed += csspeed;
  70.     if( sspeed > cspeed ) sspeed = cspeed;
  71.     element = 1200/sspeed;
  72.     l_char = element<<1;
  73.     l_word = element<<2;
  74. }    /* adjust_speed */
  75.  
  76. analyze_form( tree )
  77. long tree;
  78. {
  79.     short element;
  80.  
  81.     /* Check speed settings */
  82.     cspeed = atoi( (char *) (*TE_PTEXT( (*OB_SPEC( T1CSPEED )) )) );
  83.     if( cspeed < 5 ) cspeed = 5;
  84.     l_dot = l_el = 1200/cspeed;
  85.     l_dash = 3*l_dot;
  86.     sspeed = atoi( (char *) (*TE_PTEXT( (*OB_SPEC( T1SSPEED )) )) );
  87.     if( sspeed < 1 ) sspeed = 1;
  88.     element = 1200/sspeed;
  89.     l_char = element<<1;
  90.     l_word = element<<2;
  91.     csspeed = atoi( (char *) (*TE_PTEXT( (*OB_SPEC( T1CSSPD )) )) );
  92.  
  93.     /* Check file filter option */
  94.     if( (*OB_STATE( T1FILTER )) & SELECTED ) filter = TRUE;
  95.     else filter = FALSE;
  96. }    /* analyze_form */
  97.  
  98. fetch( str, in )
  99. char *str;
  100. FILE *in;
  101. {
  102.     short i, j, skipspace;
  103.  
  104.     while( fgets( buf, 132, in ) != NULL )
  105.     {
  106.         skipspace = TRUE;
  107.         for( i=j=0; i<strlen( buf ); i++ )
  108.             if( !filter ) str[j++] = buf[i];
  109.             else if( isspace( buf[i] ) && !skipspace )
  110.             {
  111.                 str[j++] = ' ';
  112.                 skipspace = TRUE;
  113.             }
  114.             else if( strchr( MORSE_CHARS, toupper(buf[i]) ) != NULL )
  115.             {
  116.                 str[j++] = buf[i];
  117.                 skipspace = FALSE;
  118.             }
  119.         str[j] = '\0';
  120.         while( --j >= 0 )
  121.             if( str[j] == ' ' ) str[j] = '\0';
  122.             else break;
  123.         if( j < 0 )
  124.             if( skipblank ) continue;
  125.             else
  126.             {
  127.                 skipblank = TRUE;
  128.                 return( BLANKLINE );
  129.             }
  130.         skipblank = FALSE;
  131.         strcat( str, " " );    /* Treat \n as space */
  132.         return( TRUE );
  133.     }
  134.     return( FALSE );
  135. }    /* fetch */
  136.  
  137. print_info()
  138. {
  139.     char text[128];
  140.  
  141.     textbox( tbox );
  142.     graf_mouse( M_OFF, 0x0L );
  143.     wputs( "Morse Code Sender" );
  144.     ypos += gl_hchar;
  145.     sprintf( text, "Sending: %s", infile );
  146.     wputs( text );
  147.     sprintf( text, "Speed:   %2d WPM", cspeed );
  148.     wputs( text );
  149.     if( sspeed != cspeed )
  150.     {
  151.         sprintf( text, "Spacing: %2d WPM", sspeed );
  152.         wputs( text );
  153.     }
  154.     wputs( "Press any key to interrupt sending." );
  155.     graf_mouse( M_ON, 0x0L );
  156. }    /* print_info */
  157.  
  158. send_file()
  159. {
  160.     char text[128];
  161.     FILE *in;
  162.     short ans, stat, i, send_morse();
  163.  
  164.     if( !getfspec( dirspec, infile, "\\*.*" ) ) return( FALSE );
  165.  
  166.     if( (in = fopen( infile, "r" )) == NULL )
  167.     {
  168.         sprintf( text, "[2][Unable to open input file %s][Try again]",
  169.           infile );
  170.         form_alert( 1, text );
  171.         return( FALSE );
  172.     }
  173.  
  174.     graf_mouse( BUSYBEE, 0x0L );
  175.     print_info();
  176.  
  177.     stat = TRUE;
  178.     while( stat != FALSE )
  179.     {
  180.         stat = fetch( text, in );
  181.         if( stat == FALSE )
  182.             if( filter ) strcpy( text, "[SK]" );
  183.             else continue;
  184.         if( stat == BLANKLINE )
  185.         {
  186.             if( filter ) strcpy( text, "[AR]  " );
  187.             else strcpy( text, "  " );
  188.             ans = sspeed;
  189.             if( sspeed < cspeed ) adjust_speed();
  190.             if( ans != sspeed ) print_info();
  191.         }
  192.         i = send_morse( text );
  193.         if( ev_mkreturn != 0 )
  194.         {
  195.             graf_mouse( ARROW, 0x0L );
  196.             ans = form_alert( 1, "[1][Code practice interrupted!][Continue|Stop]" );
  197.             if( ans == 2 )
  198.             {
  199.                 fclose( in );
  200.                 return( FALSE );
  201.             }
  202.             else
  203.             {
  204.                 graf_mouse( BUSYBEE, 0x0L );
  205.                 if( i > 5 ) i -= 5;
  206.                 else i = 0;
  207.                 send_morse( &text[i] );
  208.             }
  209.             ev_mkreturn = 0;
  210.         }
  211.     }
  212.     fclose( in );
  213.     graf_mouse( ARROW, 0x0L );
  214.     return( TRUE );
  215. }    /* send_file */
  216.