home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / ST_USER / 1990 / USERDC90.MSA / ACCSEL.ARC / ACCSEL.C
C/C++ Source or Header  |  1990-09-29  |  16KB  |  391 lines

  1. /*--------------------------------------------------------------------------
  2.   ACCSEL - a simple accessory selector
  3.  
  4.   Copy the program into a folder called AUTO on the boot disk. It must be 
  5.   called ACCSEL.PRG. When the computer is switched on the names of the 
  6.   accessories on the disk will be listed on the screen. Choose the ones 
  7.   you want and then hit return.
  8.  
  9.   The source code is Laser C, by Les Kneeling, 1990
  10.  
  11. --------------------------------------------------------------------------*/
  12. #include <stdio.h>
  13. #include <osbind.h>
  14. #include <strings.h>
  15.  
  16. /*--------------------------------------------------------------------------
  17.   Structure definitions
  18. --------------------------------------------------------------------------*/
  19. typedef struct dta  {
  20.                     char    reserved[21];
  21.                     char    fattr;
  22.                     int     ftime;
  23.                     int     fdate;
  24.                     long    fsize;
  25.                     char    fname[14];
  26.                     };
  27.  
  28. typedef struct fentry {
  29.                     char    fname[20];      /* extra wide to give four
  30.                                                names per line           */
  31.                     struct fentry *next;    /* pointer to next struct in
  32.                                                the linked list - used when
  33.                                                going forwards           */
  34.                     struct fentry *last;    /* pointer to the last struct
  35.                                                in the list - used when
  36.                                                going forwards           */
  37.                     };
  38.  
  39. /*--------------------------------------------------------------------------
  40.   Globals
  41. --------------------------------------------------------------------------*/
  42. struct fentry start;
  43. char file_count;
  44. char from[21], to[21];
  45. char resolution;                /* Scaling factor for display           */
  46. /*-------------------------------------------------------------------------
  47.   Start
  48. --------------------------------------------------------------------------*/
  49.  
  50. main()
  51. {
  52. start.last = 0l;            /* Initialise struct at beginning of list   */
  53. start.next = 0l;
  54. if( Getrez() == 2 ) resolution = 4;     /* Set up scaling factor for    */
  55. else resolution = 2;                    /* colour or mono display       */
  56. clrscr();
  57. gotoxy( 0, 0 );                         /* Go to top left corner        */
  58. inverted_text();                        /* White text on black          */
  59. if( resolution == 4 ) Cconws("                                        ");
  60. Cconws("               ACCSEL - by Les Kneeling " );
  61.                                         /* That's me                    */
  62. gotoxy( 23, 0 );                        /* Bottom two lines             */
  63. inverted_text();                        /* White on black               */
  64. if( resolution == 4 )
  65. Cconws("             <+> switch accessory on,     <-> switch accessory off              ");
  66. else Cconws("  <+> accessory on, <-> accessory off   ");
  67. auto_overflow_off();                    /* Stop the screen scrolling when
  68.                                            the bottom left corner has a
  69.                                            character printed in it      */
  70. gotoxy( 24, 0 );
  71. if( resolution == 4 )
  72.     {
  73.     Cconws("      " );
  74.     Bconout( 5, 4 );
  75.     Cconws( " and " );
  76.     Bconout( 5, 3 ); 
  77.     Cconws( " select file,   ESC clears all accessories    <return> finish      ");
  78.     }
  79. else 
  80.     {
  81.     Cconws( "  " );
  82.     Bconout( 5, 4 );
  83.     Bconout( 2, 32 );
  84.     Bconout( 5, 3 );
  85.     Cconws( " select, + on, - off, Return end   " );
  86.     }
  87. auto_overflow_on();                     /* Back to normal               */
  88. normal_text();                          /* and black on white           */
  89.  
  90. get_filenames();
  91. show_filenames();
  92. interact();
  93. free_memory();
  94. }
  95.  
  96. /*--------------------------------------------------------------------------
  97.   Read the disk directory
  98. --------------------------------------------------------------------------*/
  99. get_filenames()
  100. {
  101. int error; 
  102. struct dta newdta;
  103. struct fentry *ptr, *holder;
  104.  
  105. ptr = &start;
  106.  
  107. Fsetdta( &newdta );                     /* User defined disk transfer
  108.                                            address  */
  109.  
  110. file_count = 0;
  111. error = Fsfirst( "\\*.ACC", 0x3f );     /* First search for the ACCs    */
  112. while((!error)&&(file_count < 20 * resolution ))
  113.     {
  114.     file_count++;                       /* Make sure files don't over-
  115.                                            write the stuff on the screen*/
  116.     strcpy( ptr->fname, newdta.fname ); /* Copy filename into struct    */
  117.     expand( ptr->fname );               /* Make the string 20 characters
  118.                                            wide                         */
  119.     holder = ptr;                       /* Keep address for the ->last
  120.                                            field in the next struct in
  121.                                            the list                     */
  122.     ptr->next = (struct fentry *)malloc( sizeof( struct fentry ) );
  123.                                         /* Add a new struct to the list 
  124.                                            and link list forwards       */
  125.     ptr = ptr->next;                    /* Move to new struct           */
  126.     ptr->last = holder;                 /* Link the list backwards      */
  127.     ptr->next = 0l;                     /* Put stop value at end of list*/
  128.     *ptr->fname = 0;                    /* Zap the filename just in case*/
  129.     error = Fsnext();                   /* Any more entries in directory*/
  130.     }
  131.  
  132. error = Fsfirst( "\\*.ACX", 0x3f );     /* Now for the ACXs             */
  133. while((!error)&&(file_count < 20 * resolution ))        
  134.     {                                   /* Exactly the same again       */
  135.     file_count++;
  136.     strcpy( ptr->fname, newdta.fname );
  137.     expand( ptr->fname );
  138.     holder = ptr;
  139.     ptr->next = (struct fentry *)malloc( sizeof( struct fentry ) );
  140.     ptr = ptr->next;
  141.     ptr->last = holder;
  142.     ptr->next = 0l;
  143.     *ptr->fname = 0;
  144.     error = Fsnext();
  145.     }
  146. }
  147.  
  148. /*--------------------------------------------------------------------------
  149.   Display the filenames on the screen
  150. --------------------------------------------------------------------------*/
  151. show_filenames()
  152. {
  153. struct fentry *ptr;                     /* Local pointer to list        */
  154. int count = 0;
  155.  
  156. ptr = &start;                           /* Initialise pointer to start  */
  157. gotoxy( 1, 0 );                         /* Next line                    */
  158. normal_text();                          /* Black text on white          */
  159. Cconws( ptr->fname );                   /* Print the filename           */
  160. while( ptr->next != 0l )                /* while not at end of list     */
  161.     {
  162.     ptr = ptr->next;                    /* advance                      */
  163.     count++;                            /* keep count                   */
  164.     gotoxy( count/resolution + 1, (count % resolution) * 20 );
  165.                                         /* position cursor              */
  166.     Cconws( ptr->fname );               /* print filename               */
  167.     }
  168. }
  169.  
  170. /*--------------------------------------------------------------------------
  171.   Pad the filename with spaces
  172. --------------------------------------------------------------------------*/
  173. expand( filename )
  174. char *filename;
  175. {
  176. char count;
  177.  
  178. count = strlen( filename );             /* Add spaces to the end of the */
  179. while( count < 20 )                     /* filename until it is 20      */
  180.     *(filename + count++) = 32;         /* characters long to keep the  */
  181. *(filename + count) = 0;                /* display pretty when it is    */
  182. }                                       /* printed white on black       */
  183.  
  184. /*--------------------------------------------------------------------------
  185.   Take care of the keystrokes
  186. --------------------------------------------------------------------------*/
  187. interact()
  188. {
  189. struct fentry *ptr;                     /* Local pointer for the list   */
  190. char temp[20];
  191. char count = 0;
  192. union   {                               /* Union used to split up the   */
  193.         long whole;                     /* high and low words of the    */
  194.         struct  {                       /* long returned by Bconin()    */
  195.                 int highword;
  196.                 int lowword;
  197.                 }split;
  198.         } key;
  199.  
  200. ptr = &start;                           /* Initialise pointer to start  */
  201.  
  202. gotoxy( 1, 0 );                         /* Beginning of display         */
  203. inverted_text();
  204. Cconws( ptr->fname );                   /* Put cursor over first file   */
  205. normal_text();
  206.  
  207. /*--------------------------------------------------------------------------
  208.    The Bconin() call returns a long, but the switch statement can only 
  209.    handle an int so it is necessary to go through a few gyrations to
  210.    make it work
  211. --------------------------------------------------------------------------*/
  212. while( key.split.lowword != 13 )        /* Return finishes it           */
  213.     {
  214.     key.whole = Bconin( 2 );            /* Get the keytroke             */
  215.     if( key.split.lowword != 0 )
  216.         switch( key.split.lowword )
  217.             {
  218.             case '+':                   /* Switch accessory on          */
  219.                 gotoxy( count/resolution + 1, (count % resolution) * 20 );
  220.                 inverted_text();
  221.                 strcpy( from, "\\" );
  222.                 strcat( from, ptr->fname );
  223.                 strcpy( to, "\\" );
  224.                 strcat( to, ptr->fname );   /* Build a new filename */
  225.                 *index( to, '.' ) = 0;      /* with ACC extension   */
  226.                 strcat( to, ".ACC" );
  227.                 if( !Frename( 0, from, to ))
  228.                     strcpy( ptr->fname, to + 1 );
  229.                 /* If the rename doesn't work don't
  230.                 change the name on the screen*/
  231.                 expand( ptr->fname );
  232.                 Cconws( ptr->fname );
  233.                 normal_text();
  234.                 break;
  235.             case '-':               /* Switch accessory off         */
  236.                 gotoxy( count/resolution + 1, (count % resolution) * 20 );
  237.                 inverted_text();
  238.                 strcpy( from, "\\" );
  239.                 strcat( from, ptr->fname );
  240.                 strcpy( to, "\\" );
  241.                 strcat( to, ptr->fname );
  242.                 *index( to, '.' ) = 0;
  243.                 strcat( to, ".ACX" );
  244.                 if( !Frename( 0, from, to ))
  245.                     strcpy( ptr->fname, to + 1 );
  246.                 expand( ptr->fname );
  247.                 Cconws( ptr->fname );
  248.                 normal_text();
  249.                 break;
  250.             case 27:    
  251.                 ptr = &start;       /* Clear all accessories        */
  252.                 count = 0;
  253.                 while( ptr->next != 0l )    /* For each filename    */
  254.                     {                       /* in turn              */
  255.                     gotoxy( count/resolution + 1, (count % resolution) * 20 );
  256.                     inverted_text();
  257.                     strcpy( from, "\\" );
  258.                     strcat( from, ptr->fname );
  259.                     strcpy( to, "\\" );
  260.                     strcat( to, ptr->fname );
  261.                     *index( to, '.' ) = 0;
  262.                     strcat( to, ".ACX" );   /* Try to rename it ACX */
  263.                     if( !Frename( 0, from, to ))
  264.                         strcpy( ptr->fname, to + 1);
  265.                     expand( ptr->fname );
  266.                     Cconws( ptr->fname );
  267.                     normal_text();
  268.                     gotoxy( count/resolution + 1, (count % resolution) * 20 );
  269.                     Cconws( ptr->fname );
  270.                     ptr = ptr->next;
  271.                     count++;
  272.                     }
  273.                 count = 0;          /* Put cursor at beginning again*/
  274.                 ptr = &start;
  275.                 inverted_text();
  276.                 gotoxy( count/resolution + 1, (count % resolution) * 20 );
  277.                 Cconws( ptr->fname );
  278.                 normal_text();
  279.                 break;
  280.             }
  281.     else
  282.         switch( key.split.highword )
  283.             {
  284.             case 0x4d:      /* Right arrow */
  285.                 gotoxy( count/resolution + 1, (count % resolution) * 20 );
  286.                                     /* Position cursor              */
  287.                 normal_text();
  288.                 Cconws( ptr->fname );
  289.                                     /* Erase old cursor             */
  290.                 inverted_text();
  291.                 if( count < file_count - 1 )    /* Keep the cursor  */
  292.                     {                           /* inside the list  */
  293.                     count++;
  294.                     ptr = ptr->next;        /* Advance through list */
  295.                     }
  296.                 else
  297.                     {
  298.                     count = 0;              /* End of list - go back*/
  299.                     ptr = &start;           /* to the beginning     */
  300.                     }
  301.                 gotoxy( count/resolution + 1, (count % resolution) * 20 );
  302.                                     /* Position cursor              */
  303.                 Cconws( ptr->fname );
  304.                                     /* Highlight new filename       */
  305.                 normal_text();
  306.                 break;
  307.             case 0x4b:              /* Last filename                */
  308.                 gotoxy( count/resolution + 1, (count % resolution) * 20 );
  309.                 normal_text();
  310.                 Cconws( ptr->fname );
  311.                 inverted_text();
  312.                 if( count > 0 )
  313.                     {
  314.                     count--;
  315.                     ptr = ptr->last;    /* Going backwards this time*/
  316.                     }
  317.                 gotoxy( count/resolution + 1, (count % resolution) * 20 );
  318.                 Cconws( ptr->fname );
  319.                 normal_text();
  320.                 break;
  321.             }
  322.     }
  323. }
  324. /*--------------------------------------------------------------------------
  325.   Release memory reserved in program
  326. --------------------------------------------------------------------------*/
  327. free_memory()
  328. {
  329. struct fentry *ptr;
  330.  
  331. ptr = &start;
  332.  
  333. while( ptr->next != 0l )            /* Proceed to the end of the list   */
  334.     ptr = ptr->next;
  335.  
  336. while( ptr->last != 0l )            /* Then go back to the beginning    */
  337.     {                               /* releasing the blocks as we go    */
  338.     ptr = ptr->last;
  339.     free( ptr->next );
  340.     }
  341. }
  342.  
  343. /*--------------------------------------------------------------------------
  344.                 Screen handling 
  345. --------------------------------------------------------------------------*/
  346. gotoxy( xpos, ypos )                /* Position VT52 cursor         */
  347. char xpos, ypos;
  348. {
  349. Bconout( 2, 27 );
  350. Bconout( 2, 'Y' );
  351. Bconout( 2, 32 + xpos );
  352. Bconout( 2, 32 + ypos );
  353. }
  354.  
  355. clrscr()                            /* Clear the screen             */
  356. {
  357. Bconout( 2, 27 );
  358. Bconout( 2, 'E' );
  359. }
  360.  
  361. clreol()                            /* Clear to end of line         */
  362. {
  363. Bconout( 2, 27 );
  364. Bconout( 2, 'l' );
  365. }
  366.  
  367. inverted_text()                     /* White print on black         */
  368. {
  369. Bconout( 2, 27 );
  370. Bconout( 2, 'p' );
  371. }
  372.  
  373. normal_text()                       /* Black text on white          */
  374. {
  375. Bconout( 2, 27 );
  376. Bconout( 2, 'q' );
  377. }
  378.  
  379. auto_overflow_off()                 /* Switch off automatic overflow*/
  380. {                                   /* which normally occurs when   */
  381. Bconout( 2, 27 );                   /* the end of the screen is     */
  382. Bconout( 2, 'w' );                  /* reached - this stops the     */
  383. }                                   /* screen scrolling             */
  384.  
  385. auto_overflow_on()                  /* Restore setting to normal    */
  386. {
  387. Bconout( 2, 27 );
  388. Bconout( 2, 'v' );
  389. }
  390.  
  391.