home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 2 BBS
/
02-BBS.zip
/
RNDTUNEP.ZIP
/
RANDTUNE.C
next >
Wrap
C/C++ Source or Header
|
1991-12-27
|
3KB
|
129 lines
/*////////////////////////////////////////////////////////////////////////////
// //
// Maximus Random Tune Selector //
// Copyright 1991 by Peter G. Zion. All rights reserved. //
// Source code may be freely distributed as long as copyright message is //
// not removed from this file or the compiled executable. //
// //
////////////////////////////////////////////////////////////////////////////*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define NLEN 80
#define MAXLLEN 512
char tunes_bbs[NLEN] = "Tunes.BBS", events_bbs[NLEN] = "Events00.BBS";
int num_tunes, tune_nums[256];
void HelpMe(void) {
printf( "Usage:\n");
printf("RANDTUNE [<events_bbs> [<tunes_bbs>]]\n\n");
printf("<events_bbs>=The name of the events file for Maximus\n");
printf(" (optional, defaults to EVENTS00.BBS).\n\n");
printf("<tunes_bbs>=The name of the Maximus \"tunes\" file\n");
printf(" (optional, defaults to TUNES.BBS).\n");
exit(0);
}
void GetTunes(void) {
FILE *fp;
char line[MAXLLEN];
if( NULL == (fp = fopen( tunes_bbs, "rt" )) ) {
printf( "Error: tunes file \"%s\" not found\n\n", tunes_bbs );
HelpMe();
}
printf( "Getting tunes from \"%s\"...", tunes_bbs );
num_tunes = 0;
while( !feof( fp ) ) {
fgets( line, MAXLLEN, fp );
if( strnicmp( line, "* Yell", 6 ) == 0 )
tune_nums[num_tunes++] = atoi( &line[6] );
}
fclose( fp );
if( num_tunes == 0 ) {
printf( "\nError: No tunes found in \"%s\"\n\n", tunes_bbs );
HelpMe();
}
printf( "\n" );
}
void WriteNewTune(void) {
FILE *in, *out;
char tmpname[NLEN];
char line[MAXLLEN], *p;
tmpnam( tmpname );
if( NULL == (in = fopen( events_bbs, "rt" )) ) {
printf( "Error: events file \"%s\" not found\n", events_bbs );
HelpMe();
}
if( NULL == (out = fopen( tmpname, "wt" )) ) {
printf( "Error: unable to create temporary file \"%s\"\n", tmpname );
HelpMe();
}
printf( "Writing new random tune..." );
srand( (unsigned)time(NULL) );
for(;;) {
if(fgets( line, MAXLLEN, in )==NULL) break;
if( strnicmp( line, "Event", 5 ) == 0 ) {
if( NULL != (p = strchr( line, 'y' )) || NULL != (p = strchr( line, 'Y' )) ) {
if( NULL != (p = strchr( p, '/' )) ) {
++p;
if( NULL != (p = strchr( p, '/' )) ) {
sprintf( p, "/%d\n", tune_nums[rand()%num_tunes] );
}
}
}
}
fputs( line, out );
}
fclose(in);
fclose(out);
remove( events_bbs );
rename( tmpname, events_bbs );
printf( "\n" );
}
void main( int argc, char *argv[] ) {
printf( "\nRandTune -- Random Tune Selector for Maximus 2.00\nCopyright 1991 by Peter G. Zion. All rights reserved.\n\n" );
if( argc > 3 )
HelpMe();
if( argc > 1 ) {
strncpy( events_bbs, argv[1], NLEN );
if( argc > 2 )
strncpy( tunes_bbs, argv[2], NLEN );
}
GetTunes();
WriteNewTune();
printf( "Done!\n" );
}