home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / RNDTUNEP.ZIP / RANDTUNE.C next >
C/C++ Source or Header  |  1991-12-27  |  3KB  |  129 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. ////////////////////////////////////////////////////////////////////////////*/
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16.  
  17. #define NLEN    80
  18. #define MAXLLEN 512
  19.  
  20. char tunes_bbs[NLEN] = "Tunes.BBS", events_bbs[NLEN] = "Events00.BBS";
  21. int num_tunes, tune_nums[256];
  22.  
  23. void HelpMe(void) {
  24.  
  25. printf( "Usage:\n");
  26. printf("RANDTUNE [<events_bbs> [<tunes_bbs>]]\n\n");
  27. printf("<events_bbs>=The name of the events file for Maximus\n");
  28. printf("             (optional, defaults to EVENTS00.BBS).\n\n");
  29. printf("<tunes_bbs>=The name of the Maximus \"tunes\" file\n");
  30. printf("            (optional, defaults to TUNES.BBS).\n");
  31. exit(0);
  32. }
  33.  
  34. void GetTunes(void) {
  35.  
  36.   FILE *fp;
  37.   char line[MAXLLEN];
  38.  
  39.   if( NULL == (fp = fopen( tunes_bbs, "rt" )) ) {
  40.     printf( "Error: tunes file \"%s\" not found\n\n", tunes_bbs );
  41.     HelpMe();
  42.   }
  43.  
  44.   printf( "Getting tunes from \"%s\"...", tunes_bbs );
  45.   num_tunes = 0;
  46.  
  47.   while( !feof( fp ) ) {
  48.     fgets( line, MAXLLEN, fp );
  49.     if( strnicmp( line, "* Yell", 6 ) == 0 )
  50.       tune_nums[num_tunes++] = atoi( &line[6] );
  51.   }
  52.  
  53.   fclose( fp );
  54.  
  55.   if( num_tunes == 0 ) {
  56.     printf( "\nError: No tunes found in \"%s\"\n\n", tunes_bbs );
  57.     HelpMe();
  58.   }
  59.  
  60.   printf( "\n" );
  61.  
  62. }
  63.  
  64. void WriteNewTune(void) {
  65.  
  66.   FILE *in, *out;
  67.   char tmpname[NLEN];
  68.   char line[MAXLLEN], *p;
  69.  
  70.   tmpnam( tmpname );
  71.  
  72.   if( NULL == (in = fopen( events_bbs, "rt" )) ) {
  73.     printf( "Error: events file \"%s\" not found\n", events_bbs );
  74.     HelpMe();
  75.   }
  76.  
  77.   if( NULL == (out = fopen( tmpname, "wt" )) ) {
  78.     printf( "Error: unable to create temporary file \"%s\"\n", tmpname );
  79.     HelpMe();
  80.   }
  81.  
  82.   printf( "Writing new random tune..." );
  83.   srand( (unsigned)time(NULL) );
  84.     for(;;) {
  85.           if(fgets( line, MAXLLEN, in )==NULL) break;
  86.     if( strnicmp( line, "Event", 5 ) == 0 ) {
  87.       if( NULL != (p = strchr( line, 'y' )) || NULL != (p = strchr( line, 'Y' )) ) {
  88.         if( NULL != (p = strchr( p, '/' )) ) {
  89.           ++p;
  90.           if( NULL != (p = strchr( p, '/' )) ) {
  91.             sprintf( p, "/%d\n", tune_nums[rand()%num_tunes] );
  92.           }
  93.         }
  94.       }
  95.     }
  96.     fputs( line, out );
  97.   }
  98.  
  99.   fclose(in);
  100.   fclose(out);
  101.  
  102.   remove( events_bbs );
  103.   rename( tmpname, events_bbs );
  104.  
  105.   printf( "\n" );
  106.  
  107. }
  108.  
  109. void main( int argc, char *argv[] ) {
  110.  
  111.   printf( "\nRandTune -- Random Tune Selector for Maximus 2.00\nCopyright 1991 by Peter G. Zion.  All rights reserved.\n\n" );
  112.  
  113.   if( argc > 3 )
  114.     HelpMe();
  115.  
  116.   if( argc > 1 ) {
  117.     strncpy( events_bbs, argv[1], NLEN );
  118.     if( argc > 2 )
  119.       strncpy( tunes_bbs, argv[2], NLEN );
  120.   }
  121.  
  122.   GetTunes();
  123.  
  124.   WriteNewTune();
  125.  
  126.   printf( "Done!\n" );
  127.  
  128. }
  129.