home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / RTUNEP.LZH / RANDTUNE.C < prev    next >
C/C++ Source or Header  |  1993-01-16  |  4KB  |  143 lines

  1.  
  2. /*////////////////////////////////////////////////////////////////////////////
  3. //                                                                          //
  4. //  Maximus Random Tune Selector                                            //
  5. //  Copyright 1991 by Peter G. Zion.  All rights reserved.                  //
  6. //  Source code may be freely distributed as long as copyright message is   //
  7. //  not removed from this file or the compiled executable.                  //
  8. //                                                                          //
  9. //  Supplementary code by Richard Butler of 1:396/61                        //
  10. //                                                                          //
  11. ////////////////////////////////////////////////////////////////////////////*/
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <time.h>
  18.  
  19. #define NLEN    80
  20. #define MAXLLEN 512
  21.  
  22. char tunes_bbs[NLEN] = "Tunes.BBS", events_bbs[NLEN] = "Events00.BBS";
  23. int num_tunes, tune_nums[256];
  24. char *prog;
  25. char *whitespace = " \t\n\r";
  26.  
  27. void HelpMe(void) {
  28.  
  29.   int i;
  30.  
  31.   strupr(prog);
  32.   printf( "Usage:\n        %s [<events_bbs> [<tunes_bbs>]]\n\n<events_bbs> is the name of the events file for Maximus\n(optional, defaults to EVENTS00.BBS).\n\n<tunes_bbs> is the name of the Maximus \"tunes\" file\n(optional, defaults to TUNES.BBS).\n",prog);
  33.   exit(0);
  34.  
  35. }
  36.  
  37. void GetTunes(void) {
  38.  
  39.   FILE *fp;
  40.   char line[MAXLLEN];
  41.   char *ptr;
  42.  
  43.   if( NULL == (fp = fopen( tunes_bbs, "r" )) ) {
  44.     printf( "Error: tunes file \"%s\" not found\n\n", tunes_bbs );
  45.     HelpMe();
  46.   }
  47.  
  48.   printf( "Getting tunes from \"%s\"...", tunes_bbs );
  49.   fflush(stdout);
  50.   num_tunes = 0;
  51.  
  52.   while(fgets(line,MAXLLEN,fp) != NULL) {
  53.     ptr = line + strspn(line,whitespace);
  54.     if( strnicmp( ptr, "* YELL", 6 ) == 0 )
  55.       tune_nums[num_tunes++] = atoi( ptr + 6 );
  56.   }
  57.  
  58.   fclose( fp );
  59.  
  60.   if( num_tunes == 0 ) {
  61.     printf( "\nError: No tunes found in \"%s\"\n\n", tunes_bbs );
  62.     HelpMe();
  63.   }
  64.  
  65.   printf( "\n" );
  66.  
  67. }
  68.  
  69. void WriteNewTune(void) {
  70.  
  71.   FILE *in, *out;
  72.   char tmpname[NLEN];
  73.   char line[MAXLLEN], *p;
  74.   char *ptr;
  75.  
  76.   tmpnam( tmpname );
  77.  
  78.   if( NULL == (in = fopen( events_bbs, "r" )) ) {
  79.     printf( "Error: events file \"%s\" not found\n", events_bbs );
  80.     HelpMe();
  81.   }
  82.  
  83.   if( NULL == (out = fopen( tmpname, "w" )) ) {
  84.     printf( "Error: unable to create temporary file \"%s\"\n", tmpname );
  85.     HelpMe();
  86.   }
  87.  
  88.   printf( "Writing new random tune..." );
  89.   fflush(stdout);
  90.   srand( (unsigned)time(NULL) );
  91.  
  92.   while(fgets(line,MAXLLEN,in) != NULL) {
  93.     ptr = line + strspn(line,whitespace);
  94.     if( strnicmp( ptr, "EVENT", 5 ) == 0 ) {
  95.       if( NULL != (p = strchr( line, 'y' )) || NULL != (p = strchr( line, 'Y' )) ) {
  96.         if( NULL != (p = strchr( p, '/' )) ) {
  97.           ++p;
  98.           if( NULL != (p = strchr( p, '/' )) ) {
  99.             sprintf( p, "/%d\n", tune_nums[rand()%num_tunes] );
  100.           }
  101.         }
  102.       }
  103.     }
  104.     fputs( line, out );
  105.   }
  106.  
  107.   fclose(in);
  108.   fclose(out);
  109.  
  110.   remove( events_bbs );
  111.   rename( tmpname, events_bbs );
  112.  
  113.   printf( "\n" );
  114.  
  115. }
  116.  
  117. void main( int argc, char *argv[] ) {
  118.  
  119.   printf( "\nRandTune/2 -- Random Tune Selector for Maximus 2.00\nCopyright 1991 by Peter G. Zion.  All rights reserved.\n" );
  120.   printf( "Supplementary code by Richard Butler of 1:396/61\n\n");
  121.   fflush(stdout);
  122.  
  123.   prog = argv[0];
  124.  
  125.   if( argc > 3 )
  126.     HelpMe();
  127.  
  128.   if( argc > 1 ) {
  129.     strncpy( events_bbs, argv[1], NLEN );
  130.     if( argc > 2 )
  131.       strncpy( tunes_bbs, argv[2], NLEN );
  132.   }
  133.  
  134.   GetTunes();
  135.  
  136.   WriteNewTune();
  137.  
  138.   printf( "Done!\n" );
  139.  
  140. }
  141.  
  142.  
  143.