home *** CD-ROM | disk | FTP | other *** search
/ Mega A/V / mega_av.zip / mega_av / SOUNDUTL / MUSIQUE.ZIP / SOURCE.ZIP / MENU.C next >
C/C++ Source or Header  |  1991-09-06  |  18KB  |  558 lines

  1. /* Warning! This C source contains extended characters! */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <dos.h>
  7. #include <conio.h>
  8. #include <bios.h>
  9. #include <dir.h>
  10. #include <musique.h>
  11. #include <menu.e>
  12.  
  13. int getmode(void)
  14. /* get the current video mode since Turbo-C may be out of sync */
  15. {
  16.   union REGS regs;
  17.  
  18.   regs.h.ah=(unsigned)'\xF';
  19.   int86(0x10,®s,®s);
  20.   return((int)regs.h.al);
  21. }
  22.  
  23. int getcolumn(void)
  24. /* get the current number of column since Turbo-C may be out of sync */
  25. {
  26.   union REGS regs;
  27.  
  28.   regs.h.ah=(unsigned)'\xF';
  29.   int86(0x10,®s,®s);
  30.   return((int)regs.h.ah);
  31. }
  32.  
  33. void findports(PARAMETERTYPE *parameter, int verbose)
  34. /* plays STEREO iff two parallel ports found */
  35. /* plays SINGLE iff one parallel port found */
  36. /* plays SPEAKER iff no parallel port found */
  37. /* shuts off initially any parallel port to be used */
  38. {
  39.   if (parameter->A)
  40.     if (parameter->B) {
  41.       if (verbose) message(8);
  42.       strcpy(parameter->ports,STEREO);
  43.       strcat(parameter->ports,PORT1S);
  44.       shutup(PORT1);
  45.       strcat(parameter->ports,PORT2S);
  46.       shutup(PORT2);
  47.       parameter->C=FALSE;
  48.     }
  49.     else
  50.       if (parameter->C) {
  51.         if (verbose) message(8);
  52.         strcpy(parameter->ports,STEREO);
  53.         strcat(parameter->ports,PORT1S);
  54.         shutup(PORT1);
  55.         strcat(parameter->ports,PORT3S);
  56.         shutup(PORT3);
  57.       } else {
  58.         if (verbose) message(26);
  59.         strcpy(parameter->ports,SINGLE);
  60.         strcat(parameter->ports,PORT1S);
  61.         shutup(PORT1);
  62.       }
  63.   else
  64.     if (parameter->B)
  65.       if (parameter->C) {
  66.         if (verbose) message(8);
  67.         strcpy(parameter->ports,STEREO);
  68.         strcat(parameter->ports,PORT2S);
  69.         shutup(PORT2);
  70.         strcat(parameter->ports,PORT3S);
  71.         shutup(PORT3);
  72.       } else {
  73.         if (verbose) message(26);
  74.         strcpy(parameter->ports,SINGLE);
  75.         strcat(parameter->ports,PORT2S);
  76.         shutup(PORT2);
  77.       }
  78.     else
  79.       if (parameter->C) {
  80.         if (verbose) message(26);
  81.         strcpy(parameter->ports,SINGLE);
  82.         strcat(parameter->ports,PORT3S);
  83.         shutup(PORT3);
  84.       } else {
  85.         if (verbose) message(9);
  86.         strcpy(parameter->ports,SPEAKER);
  87.       }
  88. }
  89.  
  90. int scannumber(char source[])
  91. /* scans a strings for a positive decimal integer */
  92. /* the source must contain the number only */
  93. {
  94.   int number,ptr;
  95.   char c;
  96.   
  97.   if (source[0]=='\0') error(23);
  98.   number=ptr=0;
  99.   while ((c=source[ptr++])!='\0')
  100.     if ((c>='0') && (c<='9')) number=10*number+(int)(c-'0');
  101.     else error(23);
  102.   return(number);
  103. }
  104.  
  105. void scanarguments(int argc, char *argv[], char menuname[], 
  106.                    PARAMETERTYPE *parameter)
  107. /* scans command line parameters */
  108. {
  109.   int argptr;
  110.  
  111.   for (argptr=1 ; argptr<argc; argptr++) {
  112.     if ((argv[argptr][0]==FLAG1) || (argv[argptr][0]==FLAG2))
  113.       /* parameter */
  114.       if (argv[argptr][2]!='\0') {
  115.         /* long parameter */
  116.         if ((char)toupper((int)argv[argptr][1])==TEMPFLAG) {
  117.           parameter->tempdrive=(int)((char)toupper((int)argv[argptr][2])-'A');
  118.           if (argv[argptr][3]!='\0') error(23);
  119.         }
  120.         else
  121.         if ((char)toupper((int)argv[argptr][1])==MODEFLAG) 
  122.           parameter->videomode=scannumber(&(argv[argptr][2]));
  123.         else
  124.         if ((char)toupper((int)argv[argptr][1])==COLORFLAG) 
  125.           switch (argv[argptr][2]) {
  126.             case '1':coloroutline=scannumber(&(argv[argptr][3]));
  127.                      break;
  128.             case '2':colortitle=scannumber(&(argv[argptr][3]));
  129.                      break;
  130.             case '3':coloritem=scannumber(&(argv[argptr][3]));
  131.                      break;
  132.             case '4':colorpicked=scannumber(&(argv[argptr][3]));
  133.                      break;
  134.             case '5':colorcurrent=scannumber(&(argv[argptr][3]));
  135.                      break;
  136.             case '6':colorcursor=scannumber(&(argv[argptr][3]));
  137.                      break;
  138.             case '7':colorborder=scannumber(&(argv[argptr][3]));
  139.                      break;
  140.             default:error(23);
  141.           }
  142.         else error(23);
  143.       }
  144.       else
  145.       /* short parameter */
  146.       if ((char)toupper((int)argv[argptr][1])==SPEED1FLAG) 
  147.         parameter->speed[1]='a';
  148.       else
  149.       if ((char)toupper((int)argv[argptr][1])==SPEED2FLAG) 
  150.         parameter->speed[1]='b';
  151.       else
  152.       if ((char)toupper((int)argv[argptr][1])==SPEED3FLAG) 
  153.         parameter->speed[1]='c';
  154.       else
  155.       if ((char)toupper((int)argv[argptr][1])==FRENCHFLAG) language=FRENCH;
  156.       else
  157.       if ((char)toupper((int)argv[argptr][1])==ENGLISHFLAG) language=ENGLISH;
  158.       else
  159.       if ((char)toupper((int)argv[argptr][1])==GERMANFLAG) language=GERMAN;
  160.       else
  161.       if ((char)toupper((int)argv[argptr][1])==HIGHFLAG) {
  162.         coloroutline|=HIGHINT;
  163.         colortitle|=HIGHINT;
  164.         coloritem|=HIGHINT;
  165.         colorpicked|=HIGHINT;
  166.         colorcurrent|=HIGHINT;
  167.         colorcursor|=HIGHINT;
  168.         colorborder|=HIGHINT;
  169.       }
  170.       else
  171.       if ((char)toupper((int)argv[argptr][1])==MOUSEFLAG) 
  172.         parameter->mouse=TRUE;
  173.       else
  174.       if ((char)toupper((int)argv[argptr][1])==SONGFLAG) {
  175.         parameter->verbose=FALSE;
  176.         parameter->DOS=FALSE;
  177.       }
  178.       else
  179.       if ((char)toupper((int)argv[argptr][1])==VERBOSEFLAG) {
  180.         parameter->verbose=TRUE;
  181.         parameter->DOS=FALSE;
  182.       }
  183.       else
  184.       if ((char)toupper((int)argv[argptr][1])==DOSFLAG) {
  185.         parameter->verbose=FALSE;
  186.         parameter->DOS=TRUE;
  187.       }
  188.       else
  189.       if ((char)toupper((int)argv[argptr][1])==PAUSEFLAG) 
  190.         parameter->pause=TRUE;
  191.       else
  192.       if ((char)toupper((int)argv[argptr][1])==BIOSFLAG) 
  193.         parameter->rawvideo=FALSE;
  194.       else
  195.       if (argv[argptr][1]==NOPORTFLAG) {
  196.         parameter->A=parameter->B=parameter->C=FALSE;
  197.         strcpy(parameter->ports,SPEAKER);
  198.         }
  199.       else
  200.       if (argv[argptr][1]==PORT1FLAG) {
  201.         parameter->A=TRUE;
  202.         parameter->ports[0]=ODDPORTS;
  203.       }
  204.       else
  205.       if (argv[argptr][1]==PORT2FLAG) {
  206.         parameter->B=TRUE;
  207.         parameter->ports[0]=ODDPORTS;
  208.       }
  209.       else
  210.       if (argv[argptr][1]==PORT3FLAG) {
  211.         parameter->C=TRUE;
  212.         parameter->ports[0]=ODDPORTS;
  213.       }
  214.       else
  215.       if ((char)toupper((int)argv[argptr][1])==QUESTIONFLAG) error(1);
  216.       else error(23);
  217.     else 
  218.       /* must be a menu name */
  219.       if (strlen(argv[argptr])>=(LINELENGTH-strlen(MENUENDING))) error(22);
  220.       else strcpy(menuname,argv[argptr]);
  221.   }
  222.   colorcurrent<<=4;
  223.   colorcursor<<=4;
  224.   colorborder<<=4;
  225.   /* no harm done if the user specifies three parallel ports */
  226.   if (parameter->A || parameter->B || parameter->C) findports(parameter,FALSE);
  227. }
  228.  
  229. void extractstring(char **scannerptr, char target[], int targetlength, 
  230.                    int spacesin)
  231. /* copies "intelligently" a token from a string into another string */
  232. /* target has a fixed width */
  233. {
  234.   int scanner;
  235.  
  236.   /* skip leading spaces */
  237.   while ((**scannerptr==' ') && (**scannerptr!='\0') && (**scannerptr!='\n'))
  238.     (*scannerptr)++;
  239.   /* there should be something left to read */
  240.   if ((**scannerptr=='\0') || (**scannerptr=='\n')) error(10);
  241.   if (spacesin) {
  242.     /* read until EOL or nothing left */
  243.     for (scanner=0 ; (**scannerptr!='\0') && (**scannerptr!='\n') ;
  244.          (*scannerptr)++) target[scanner++]=**scannerptr;
  245.     /* discard pending spaces for now */
  246.     while (target[scanner-1]==' ') scanner--;
  247.   }
  248.   else
  249.     /* read until space, EOL, or nothing left */
  250.     for (scanner=0 ; (**scannerptr!=' ') && (**scannerptr!='\0') && 
  251.          (**scannerptr!='\n') ; (*scannerptr)++) target[scanner++]=**scannerptr;
  252.   /* check if not too long */
  253.   if (scanner>targetlength) error(11);
  254.   /* fill with space until full length */
  255.   while (scanner<targetlength) target[scanner++]=' ';
  256.   /* put EOS character */
  257.   target[targetlength]='\0';
  258. }
  259.  
  260. void flushright(char target[], int targetlength)
  261. /* inserts spaces until the content is flush right */
  262. {
  263.   int ptr,endptr;
  264.  
  265.   ptr=endptr=targetlength-1;
  266.   while (target[ptr]==' ') ptr--;
  267.   while (ptr>=0) target[endptr--]=target[ptr--];
  268.   while (endptr>=0) target[endptr--]=' ';
  269. }
  270.  
  271. void centerstring(char target[], int targetlength)
  272. /* inserts spaces until the content is centered */
  273. {
  274.   int ptr,endptr;
  275.  
  276.   ptr=targetlength-1;
  277.   while (target[ptr]==' ') ptr--;
  278.   endptr=(ptr+targetlength)/2;
  279.   while (ptr>=0) target[endptr--]=target[ptr--];
  280.   while (endptr>=0) target[endptr--]=' ';
  281. }
  282.  
  283. void pathcopy(SONGSTYPE *songs, char newDOSpath[])
  284. /* copies a paths to the pool and updates the last song */
  285. {
  286.   char *poolptr;
  287.   int scanner;
  288.  
  289.   scanner=LINELENGTH-1; /* length of newDOSpath */
  290.   while ((newDOSpath[--scanner]==' ') && (scanner>=0)) newDOSpath[scanner]='\0';
  291.   poolptr=songs->songpath+songs->songpathoffset;
  292.   songs->song[songs->songnumber].DOSpath=poolptr;
  293.   songs->songpathoffset+=strlen(newDOSpath);
  294.   if (songs->songpathoffset++>PATHLENGTH) error(24);
  295.   strcpy(poolptr,newDOSpath);
  296. }
  297.  
  298. void readmenu(FILE *menufile, SONGSTYPE *songs)
  299. /* each line contains diskette name, path name, DOS file name, and song name */
  300. /* these subitems are surrounded by spaces */
  301. /* the song name may contain spaces */
  302. {
  303.   char line[LINELENGTH];
  304.   char newdiskname[LINELENGTH];
  305.   char newscore[LINELENGTH];
  306.   char newDOSpath[LINELENGTH];
  307.   char newDOSname[LINELENGTH];
  308.   char newsongname[LINELENGTH];
  309.   char *errorchar;
  310.   char *scannerptr;
  311.  
  312.   errorchar=fgets(line,LINELENGTH,menufile); /* let fgets handle EOL */
  313.   while (errorchar!=NULL) {
  314.     if (strlen(line)==(LINELENGTH-1)) error(4);
  315.     if (line[0]!=COMMENTFLAG) {
  316.       scannerptr=line;
  317.       extractstring(&scannerptr,newdiskname,DISKNAMELENGTH,FALSE);
  318.       flushright(newdiskname,DISKNAMELENGTH);
  319.       extractstring(&scannerptr,newscore,SCORELENGTH,FALSE);
  320.       flushright(newscore,SCORELENGTH);
  321.       extractstring(&scannerptr,newDOSpath,(LINELENGTH-1),FALSE);
  322.       extractstring(&scannerptr,newDOSname,DOSNAMELENGTH,FALSE);
  323.       extractstring(&scannerptr,newsongname,SONGNAMELENGTH,TRUE);
  324.       centerstring(newsongname,SONGNAMELENGTH);
  325.       songs->songnumber++;
  326.       if (songs->songnumber>MAXSONGS) error(5);
  327.       strcpy(songs->song[songs->songnumber].diskname,newdiskname);
  328.       strcpy(songs->song[songs->songnumber].score,newscore);
  329.       pathcopy(songs,newDOSpath);
  330.       strcpy(songs->song[songs->songnumber].DOSname,newDOSname);
  331.       strcpy(songs->song[songs->songnumber].songname,newsongname);
  332.       songs->song[songs->songnumber].picked=0;
  333.     }
  334.     errorchar=fgets(line,LINELENGTH,menufile);
  335.   }
  336.   if (songs->songnumber==0) error(6);
  337.   printf(" %d",songs->songnumber);
  338.   message(7);
  339.   printf(" %d",(int)(((long)100*(long)songs->songpathoffset)/(long)PATHLENGTH));
  340.   message(0);
  341. }
  342.  
  343. int findport(int port)
  344. /* port deemed valid iff data can be written and read back */
  345. /* returns TRUE iff parallel port found */
  346. /* bus remanence (?!) calls for the delay used between write and read */
  347. /* this delay also makes for a not so loud speaker noise */
  348. /* warning: may not work on some parallel ports */
  349. {
  350.   int data;
  351.  
  352.   shutup(port);
  353.   data=0x0;
  354.   do {
  355.     data++;
  356.     outportb(port,(unsigned char)data);
  357.     delay(SHUTUPDELAY);
  358.     if ((unsigned char)data!=inportb(port)) return(FALSE);
  359.   } while (data!=0xFF);
  360.   shutup(port);
  361.   return(TRUE);
  362. }
  363.  
  364. int findprogram(char program[], int tempdrive, int currentdrive)
  365. /* searches temporary drive(RAM?), PATH(HD?), and current drive */
  366. /* returns drive number or -1 */
  367. {
  368.   struct ffblk fff;
  369.  
  370.   setdisk(tempdrive);
  371.   if (searchpath(program)==NULL) {
  372.     setdisk(currentdrive);
  373.     /* normal, read-only, hidden, system, archive */
  374.     if (findfirst(program,&fff,39)) return(-1);
  375.     else return(currentdrive);
  376.   }
  377.   else {
  378.     setdisk(currentdrive);
  379.     return(tempdrive);
  380.   }
  381. }
  382.  
  383. int moveprogram(PARAMETERTYPE *parameter, char program[], int *programcopied) 
  384. /* the program is on the temporary drive (or path), or on the current drive */
  385. /* if necessary, the program is moved temporarily onto the temporary drive */
  386. /* this will most likely optimize the speed of operation of system() calls */
  387. /* returns TRUE iff the program has been found */
  388. {
  389.   char buffer[BUFFERSIZE];
  390.   char filename[LINELENGTH];
  391.   FILE *source,*target;
  392.   int programdrive,errorint,tomove,moved;
  393.   char temporarytext[3];
  394.   char programtext[3];
  395.  
  396.   *programcopied=FALSE;
  397.   programdrive=findprogram(program,parameter->tempdrive,
  398.                            parameter->currentdrive);
  399.   if (programdrive==-1) return(FALSE);
  400.   if (programdrive!=parameter->tempdrive) {
  401.     strcpy(temporarytext,"A:");
  402.     strcpy(programtext,"A:");
  403.     temporarytext[0]+=(char)parameter->tempdrive;
  404.     programtext[0]+=(char)programdrive;
  405.     message(10);
  406.     strcpy(filename,programtext);
  407.     strcat(filename,program);
  408.     source=fopen(filename,"rb");
  409.     if (source==NULL) error(13);
  410.     strcpy(filename,temporarytext);
  411.     strcat(filename,program);
  412.     target=fopen(filename,"wb");
  413.     if (target==NULL) error(14);
  414.     tomove=(int)fread(buffer,(unsigned)1,(unsigned)BUFFERSIZE,source);
  415.     while (tomove!=0) {
  416.       moved=(int)fwrite(buffer,(unsigned)1,(unsigned)tomove,target);
  417.       if (moved!=tomove) error(15);
  418.       tomove=(int)fread(buffer,(unsigned)1,(unsigned)BUFFERSIZE,source);
  419.     }
  420.     errorint=fclose(source);
  421.     if (errorint==EOF) error(16);
  422.     errorint=fclose(target);
  423.     if (errorint==EOF) error(17);
  424.     *programcopied=TRUE;
  425.     message(11);
  426.   }                                                            
  427.   else message(12);
  428.   return(TRUE);
  429. }
  430.  
  431. void readparameter(PARAMETERTYPE *parameter, MOUSETYPE *mousedata,
  432.                    int songnumber)
  433. /* collects information about drives, program location, ports and screen */
  434. {
  435.   int height;
  436.   int found;
  437.   int lastdrive;
  438.   int bigtotal;
  439.  
  440.   message(28);
  441.   printf("%c\n",(char)toupper((int)parameter->speed[1]));
  442.   if (parameter->verbose) {
  443.     message(41);
  444.     parameter->itemlength=1+DISKNAMELENGTH+1+SCORELENGTH+1+DOSNAMELENGTH+1+
  445.                             SONGNAMELENGTH+1;
  446.   }
  447.   else 
  448.     if (parameter->DOS) {
  449.       message(46);
  450.       parameter->itemlength=1+DOSNAMELENGTH+1;
  451.     }
  452.     else {
  453.       message(42);
  454.       parameter->itemlength=1+SONGNAMELENGTH+1;
  455.     }
  456.   message(13);
  457.   parameter->currentdrive=getdisk();
  458.   printf("%c\n",(char)((int)'A'+parameter->currentdrive));
  459.   /* works iff LASTDRIVE statement found in CONFIG */
  460.   lastdrive=setdisk(parameter->currentdrive)-1;
  461.   if (parameter->tempdrive==ODDDRIVE) {
  462.     if (parameter->currentdrive<=1)
  463.       /* from floppy */
  464.       parameter->tempdrive=lastdrive;
  465.     else
  466.       /* from hard-disk or RAM-disk */
  467.       parameter->tempdrive=parameter->currentdrive;
  468.   }
  469.   message(14);
  470.   printf("%c\n",(char)((int)'A'+parameter->tempdrive));
  471.   if ((parameter->tempdrive<0) || (parameter->tempdrive>lastdrive)) error(30);
  472.   message(15);
  473.   printf("%s:\n",MODPLAY1);
  474.   found=moveprogram(parameter,MODPLAY1,&(parameter->modplaycopied));
  475.   if (!found) {
  476.     message(15);
  477.     printf("%s:\n",MODPLAY2);
  478.     found=moveprogram(parameter,MODPLAY2,&(parameter->modplaycopied));
  479.     if (!found) {
  480.       message(15);
  481.       printf("%s:\n",MODPLAY3);
  482.       found=moveprogram(parameter,MODPLAY3,&(parameter->modplaycopied));
  483.       if (!found) error(12);
  484.     }
  485.   }
  486.   message(15);
  487.   printf("%s:\n",PKUNZIP1);
  488.   found=moveprogram(parameter,PKUNZIP1,&(parameter->pkunzipcopied));
  489.   if (!found) message(29);
  490.   parameter->pkunzipfound=found;
  491.   if (parameter->ports[0]==ODDPORTS) {
  492.     message(16);
  493.     parameter->A=findport(PORT1);
  494.     parameter->B=findport(PORT2);
  495.     if (!parameter->A || !parameter->B) parameter->C=findport(PORT3);
  496.     findports(parameter,TRUE);
  497.   }
  498.   else message(27);
  499.   message(17);
  500.   if (parameter->rawvideo) message(48);
  501.   else message(49);
  502.   if (parameter->videomode==ODDMODE) {
  503.     message(18);
  504.     parameter->videomode=getmode();
  505.     printf("%d\n",parameter->videomode);
  506.   }
  507.   else {
  508.     message(47);
  509.     printf("%d\n",parameter->videomode);
  510.   }
  511.   parameter->maxcolumn=getcolumn();
  512.   printf("  %d ",parameter->maxcolumn);
  513.   message(19);
  514.   parameter->maxline=25;
  515.   printf("  %d ",parameter->maxline);
  516.   message(20);
  517.   height=(int)peekb((unsigned)0x40,(unsigned)0x84)+1;
  518.   /* guess if this information makes sense */
  519.   if ((height>25) && (height<=60)) {
  520.     parameter->maxline=height;
  521.     printf("  %d ",parameter->maxline);
  522.     message(21);
  523.   }
  524.   /* number of item lines in menu */
  525.   parameter->bigline=parameter->maxline-4;
  526.   /* number of item columns in menu */
  527.   parameter->bigcolumn=(parameter->maxcolumn-4)/parameter->itemlength;
  528.   /* total number of items on menu */
  529.   bigtotal=parameter->bigcolumn*parameter->bigline;
  530.   /* adjust if screen not fully used */
  531.   if (bigtotal>songnumber) {
  532.     /* number of item columns in menu */
  533.     parameter->bigcolumn=(songnumber-1)/parameter->bigline+1;
  534.     /* number of item lines in menu */
  535.     parameter->bigline=(songnumber-1)/parameter->bigcolumn+1;
  536.   }
  537.   if (parameter->bigcolumn<1) error(7);
  538.   if (parameter->bigline<1) error(8);
  539.   /* total number of item lines, displayed and not displayed */
  540.   parameter->hugeline=(songnumber-1)/parameter->bigcolumn+1;
  541.   /* left out column space */
  542.   parameter->leftcolumn=parameter->maxcolumn-4-
  543.                         parameter->bigcolumn*parameter->itemlength;
  544.   if (parameter->mouse) {
  545.     message(44);
  546.     if (setupmouse(parameter,mousedata)) message(45);
  547.     else error(31);
  548.   }
  549.   if (parameter->pause) {
  550.     bellit();
  551.     message(22);
  552.     switch (bioskey(0)) {
  553.       case KEYESC:error(9);
  554.       default:break;
  555.     }
  556.   }
  557. }
  558.