home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / devel5 / config.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-28  |  10.8 KB  |  498 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <dos.h>
  4. #include <conio.h>
  5. #include <string.h>
  6.  
  7. FILE *devfile, *outfile, *batfile, *cfgfile, *lastfile, *datfile;
  8. long configpos;
  9.  
  10. char videoname[40] = "vdx256.rvd";
  11. char videonum [40] = "";
  12. char deffiles [180] = "";
  13.  
  14. char batname[40] = "test.bat";
  15. char cfgname[40] = "test.cfg";
  16.  
  17. int vcount = 0;
  18. int scount = 0;
  19. char stag[36];
  20. char *vcompat[36];
  21. char *vdefname[36];
  22. char vtag[36];
  23. char *vdescr[36];
  24. char *sdescr[36];
  25.  
  26. char buff[256];
  27. char *vsslines[50];
  28. int nvss = 0;
  29. int vidline = -1;
  30.  
  31. int lastvid = 0;
  32. int lastst  = 0;
  33. int vidloaded = 0;
  34. int changed = 0;
  35.  
  36.  
  37. #define TEXT   0
  38. #define BLANK -1
  39. #define BEGIN  1
  40. #define END    2
  41. #define VTYPE  3
  42. #define STYPE  4
  43. #define CONFIG 5
  44. #define TYPE   6
  45.  
  46. int getbuff(FILE *f)    /* get line from data file, parse sections */
  47. {
  48.  int i;
  49.  char *p;
  50.  
  51.  fgets(buff, 256, f);
  52.  if(buff[0] == 0 || buff[0]=='\n') return BLANK;
  53.  if(buff[0] != '@') return TEXT;
  54.  p = strtok(buff, "@ \t\n");
  55.  if(!strnicmp(p, "BEG", 3)) return BEGIN;
  56.  if(!strnicmp(p, "end", 3)) return END;
  57.  if(!strnicmp(p, "VID", 3)) return VTYPE;
  58.  if(!strnicmp(p, "STE", 3)) return STYPE;
  59.  if(!strnicmp(p, "CON", 3)) return CONFIG;
  60.  if(!strnicmp(p, "TYP", 3)) return TYPE;
  61.  return TEXT;
  62. }
  63.  
  64.  
  65. void add_default()
  66. {
  67.  printf("\n\nAdd default files and options to REND386 command line:\n");
  68.  printf("Add any number of files {.WLD .FIG .PLG} seperated by spaces\n");
  69.  printf("and/or any of these options (more documented elsewhere):\n");
  70.  printf("-m Microsoft mouse disabled    -g Enable PowerGlove driver\n");
  71.  printf("-r Reverse eyes on Sega driver -x Enable Sega driver\n");
  72.  printf("-1 Force Sega to use COM1      -2 Force Sega to use COM2\n");
  73.  printf("-b Forces monochrome colors\n");
  74.  printf("\nEnter new command line: ");
  75.  while(kbhit()) getch();
  76.  fflush(stdin);
  77.  fgets(deffiles,120,stdin);
  78.  deffiles[strlen(deffiles)-1] = 0;
  79. }
  80.  
  81.  
  82. int get_vconfig()       /* read in all text groups that match, return 1 if error */
  83. {
  84.  int i,j;
  85.  char *tag;
  86.  
  87.  if(!strchr(vcompat[lastvid], stag[lastst]))
  88.   {
  89.    printf("\n Incompatible video driver and stereo configuration selected!");
  90.    delay(1500);
  91.    return(1);
  92.   }
  93.  for(i=0;i<50;i++)
  94.    if (vsslines[i])
  95.     {
  96.      free(vsslines[i]);
  97.      vsslines[i] = NULL;
  98.     }
  99.  vidline = -1;
  100.  nvss = 0;
  101.  i = 0;
  102.  
  103.  fseek(datfile, configpos, SEEK_SET);
  104. readnext:
  105.  while(!feof(datfile))
  106.   {
  107.    while(!feof(datfile) && getbuff(datfile)!=TYPE);
  108.    if(!feof(datfile))
  109.     {
  110.      tag = strtok(NULL, " \n\t");
  111.      if(!tag) continue;
  112.      if(!strchr(tag, vtag[lastvid])) continue;  /* check both exist in tag */
  113.      if(!strchr(tag, stag[lastst ])) continue;
  114.      else goto got_it;
  115.     }
  116.   }
  117.  if(feof(datfile)) return (i==0);
  118.  
  119. got_it:
  120.  while(getbuff(datfile)!=BEGIN && !feof(datfile));
  121.  if(feof(datfile))
  122.   {
  123.    printf("\nError reading data file CFGDATA.DAT\n");
  124.    exit(0);
  125.   }
  126.  
  127.  while(1)
  128.   {
  129.    if(feof(datfile)) break;
  130.    j = getbuff(datfile);
  131.    if(j==BLANK) vsslines[i] = strdup("");
  132.    else if (j!=TEXT) break;
  133.    else
  134.     {
  135.      vsslines[i] = strdup(buff);
  136.      if(!strnicmp(buff,"videodev", 8)) vidline = i;
  137.     }
  138.    i++;
  139.    if(i>49) return 0;
  140.   }
  141.  goto readnext;
  142. }
  143.  
  144.  
  145. void modify_video()
  146. {
  147.  int i;
  148.  char a[40], b[80];
  149.  char x[100];
  150.  
  151.  if(get_vconfig()) return;
  152.  if(vidline<0) return;
  153.  sscanf(vsslines[vidline], "%s %[^\n]", a, b);
  154.  printf("\n Video driver and mode set line: %s", b);
  155.  printf("\n Old: %s", b);
  156.  printf("\n New: ");
  157.  scanf("%[^\n]", b);
  158.  sprintf(x, "%s %s\n", a, b);
  159.  free(vsslines[vidline]);
  160.  vsslines[vidline] = strdup(x);
  161.  vidloaded = 1;
  162. }
  163.  
  164.  
  165. int getmenu(char *str[], int n, int video)
  166. {
  167.  int i,j,k;
  168.  char c[] = "A) ";
  169.  
  170.  for(i=0;i<n;i++)
  171.   {
  172.    if(video) k = (strchr(vcompat[i], stag[lastst]))  ? LIGHTGRAY : BLUE ;
  173.    else      k = (strchr(vcompat[lastvid], stag[i])) ? LIGHTGRAY : BLUE ;
  174.    textcolor(k);
  175.    gotoxy( (i&1)?40:1, (i/2)+6 );
  176.    c[0] = (i<9) ? i+'1' : i+'A'-9;
  177.    if(video) c[2] = (i==lastvid) ? '*' : ' ' ;
  178.    else      c[2] = (i==lastst)  ? '*' : ' ' ;
  179.    if(strlen(str[i])>37) str[i][37] = 0;
  180.    cprintf("%s%s", c, str[i]);
  181.   }
  182.  textcolor(LIGHTGRAY);
  183. retry:
  184.  i = toupper(getch());
  185.  if(i==27) return -1;
  186.  i = (i<'A') ? i-'1' : i -'A'+9;
  187.  if(i<0 || i>n) goto retry;
  188.  return i;
  189. }
  190.  
  191.  
  192. void select_video()
  193. {
  194.  int i;
  195.  
  196.  clrscr();
  197.  printf("\n                     VIDEO DRIVERS");
  198.  printf("\n                 ----------------------");
  199.  printf("\n  Colored entries are incompatible with currently selected stereo mode\n");
  200.  i = getmenu(vdescr, vcount, 1);
  201.  if(i>=0)
  202.   {
  203.    lastvid = i;
  204.    vidloaded = 0;
  205.    changed = 0;
  206.   }
  207. }
  208.  
  209. void select_stereo()
  210. {
  211.  int i;
  212.  
  213.  clrscr();
  214.  printf("\n                     STEREO MODES");
  215.  printf("\n                 ----------------------");
  216.  printf("\n  Colored entries are incompatible with currently selected video driver\n");
  217.  i = getmenu(sdescr, scount, 0);
  218.  if(i>=0)
  219.   {
  220.    lastst = i;
  221.    vidloaded = 0;
  222.   }
  223. }
  224.  
  225.  
  226. void add_ext(char *name, char *ext)
  227. {
  228.  char *c;
  229.  if(strchr(name, '.')) return;
  230.  c = strchr(name, 0);
  231.  *c++ = '.';
  232.  *c++ = *ext++;
  233.  *c++ = *ext++;
  234.  *c++ = *ext++;
  235.  *c = 0;
  236. }
  237.  
  238.  
  239. int set_filenames()  /* returns 1 if to write */
  240. {
  241.  int i,j;
  242.  
  243.  if(!changed)
  244.   {
  245.    if(vdefname[lastvid][0])
  246.     {
  247.      strcpy(batname, vdefname[lastvid]);
  248.      strcpy(cfgname, vdefname[lastvid]);
  249.      add_ext(batname, "bat");
  250.      add_ext(cfgname, "cfg");
  251.      strupr(batname);
  252.      strupr(cfgname);
  253.     }
  254.    else
  255.     {
  256.      strcpy(batname, "REND.BAT");
  257.      strcpy(cfgname, "REND.CFG");
  258.     }
  259.   }
  260.  
  261. main_menu:
  262.  clrscr();
  263.  gotoxy(1,5);
  264.  printf("                 SAVE, SET FILENAMES:\n");
  265.  printf("                    ---------------------------\n");
  266.  printf("                   1) Save REND386.CFG only\n");
  267.  printf("                   2) Save files %s, %s\n", batname, cfgname);
  268.  printf("                   3) Change .BAT Name\n");
  269.  printf("                   4) Change .CFG Name \n");
  270.  printf("                   5) Change both .BAT and .CFG\n");
  271.  printf("                   6) Return to Main Menu\n");
  272.  
  273.  i = getch();
  274.  if(i==27) exit(0);
  275.  if(i<'1' || i>'6') goto main_menu;
  276.  switch(i)
  277.   {
  278.    case '1':
  279.      strcpy(cfgname, "REND386.CFG");
  280.      batname[0] = 0;
  281.    case '2':
  282.      return 1;
  283.    case '3':
  284.      printf("\nEnter new .BAT filename: ");
  285.      scanf("%s", batname);
  286.      add_ext(batname, "bat");
  287.      strupr(batname);
  288.      changed = 1;
  289.      break;
  290.    case '4':
  291.      printf("\nEnter new .CFG filename: ");
  292.      scanf("%s", cfgname);
  293.      add_ext (cfgname, "cfg");
  294.      strupr(cfgname);
  295.      changed = 1;
  296.      break;
  297.    case '5':
  298.      printf("\nEnter new filename for both .BAT and .CFG: ");
  299.      scanf("%s", batname);
  300.      strcpy(cfgname, batname);
  301.      add_ext(batname, "bat");
  302.      strupr(batname);
  303.      add_ext(cfgname, "cfg");
  304.      strupr(cfgname);
  305.      changed = 1;
  306.      break;
  307.    case '6': return 0;
  308.    default: break;
  309.   }
  310.  goto main_menu;
  311. }
  312.  
  313.  
  314. void write_files()
  315. {
  316.  int i,j;
  317.  char line[256];
  318.  
  319.  if(vidloaded==0)
  320.   if(get_vconfig()) return;
  321.  
  322.  if(set_filenames()==0) return;
  323.  
  324.  if(batname[0]!=0 && batname[0] != '.')
  325.   {
  326.    batfile = fopen(batname, "w");
  327.    if(!batfile)
  328.     {
  329.      printf("\nCannot create batch file %s\n", batname);
  330.      delay(1500);
  331.      return;
  332.     }
  333.    fprintf(batfile, "rend386 /c %s %s %1 %2 %3 %4 %5 %6 \n", cfgname, deffiles);
  334.    fclose(batfile);
  335.   }
  336.  printf("\nBatch file %s sucessfully created\n", batname);
  337.  
  338.  cfgfile = fopen(cfgname, "w");
  339.  if(!cfgfile)
  340.   {
  341.    printf("\nCannot create configuration file %s\n", cfgname);
  342.    delay(1500);
  343.    return;
  344.   }
  345.  
  346.  fprintf(cfgfile, "\n# Video and Stereo mode set by Config:");
  347.  fprintf(cfgfile, "\n# Video Driver = %s", vdescr[lastvid]);
  348.  fprintf(cfgfile, "\n# Stereo Mode =  %s\n", sdescr[lastst]);
  349.  
  350.  fprintf(cfgfile, "\n\ninclude devices.cfg   # devices control\n\n");
  351.  
  352.  for(i=0;i<50;i++)
  353.    if(vsslines[i]) fprintf(cfgfile, "%s", vsslines[i]);
  354.  
  355.  fclose(cfgfile);
  356.  printf("\nConfiguration file %s sucessfully created\n", cfgname);
  357.  delay(1000);
  358.  
  359.  lastfile = fopen("lastcfg.dat", "w");
  360.  if(lastfile)
  361.   {
  362.    fprintf(lastfile,"%d %d %s", lastvid, lastst, deffiles);
  363.    fclose(lastfile);
  364.   }
  365. }
  366.  
  367.  
  368. main()
  369. {
  370.  int i,j,k;
  371.  char *c;
  372.  
  373.  for(i=0;i<50;i++) vsslines[i] = NULL;
  374.  
  375.  datfile = fopen("cfgdata.dat", "r");
  376.  if(!datfile)
  377.   {
  378.    printf("\nCannot open data file CFGDATA.DAT\n");
  379.    exit(0);
  380.   }
  381.  
  382.  lastfile = fopen("lastcfg.dat", "r");
  383.  if(lastfile)
  384.   {
  385.    fscanf(lastfile,"%d %d %[^\n]",
  386.       &lastvid, &lastst, deffiles);
  387.    changed = 0;
  388.    fclose(lastfile);
  389.   }
  390.  else
  391.   {
  392.    deffiles[0] = 0;
  393.    lastvid = lastst = 0;
  394.   }
  395.  
  396.  while(getbuff(datfile)!=STYPE && !feof(datfile));
  397.  if(feof(datfile))
  398.   {
  399.    printf("\nError reading data file CFGDATA.DAT\n");
  400.    exit(0);
  401.   }
  402.  
  403.  while(!feof(datfile))
  404.   {
  405.    k = getbuff(datfile);
  406.    if (k==BLANK) continue;
  407.    if (k==END) break;
  408.    if (k!=TEXT)
  409.     {
  410.      printf("\nError reading data file CFGDATA.DAT\n");
  411.      exit(0);
  412.     }
  413.    stag[scount] = *(strtok(buff," \t,\n"));
  414.    sdescr[scount++] = strdup(strtok(NULL, "\n"));
  415.    if (scount>=20) break;
  416.   }
  417.  
  418.  if(feof(datfile))
  419.   {
  420.    printf("\nError reading data file CFGDATA.DAT\n");
  421.    exit(0);
  422.   }
  423.  while(getbuff(datfile)!=VTYPE && !feof(datfile));
  424.  if(feof(datfile))
  425.   {
  426.    printf("\nError reading data file CFGDATA.DAT\n");
  427.    exit(0);
  428.   }
  429.  
  430.  while(!feof(datfile))
  431.   {
  432.    k = getbuff(datfile);
  433.    if (k==BLANK) continue;
  434.    if (k==END) break;
  435.    if (k!=TEXT)
  436.     {
  437.      printf("\nError reading data file CFGDATA.DAT\n");
  438.      exit(0);
  439.     }
  440.    vtag[vcount] = *(strtok(buff," \t,\n"));
  441.    vcompat[vcount] = strdup(strtok(NULL," \t,\n"));
  442.    vdefname[vcount] = strdup(strtok(NULL," \t,\n"));
  443.    vdescr[vcount++] = strdup(strtok(NULL, "\n"));
  444.    if (vcount>=36) break;
  445.   }
  446.  
  447.  if(feof(datfile))
  448.   {
  449.    printf("\nError reading data file CFGDATA.DAT\n");
  450.    exit(0);
  451.   }
  452.  while(getbuff(datfile)!=CONFIG && !feof(datfile));
  453.  if(feof(datfile))
  454.   {
  455.    printf("\nError reading data file CFGDATA.DAT\n");
  456.    exit(0);
  457.   }
  458.  
  459.  configpos = ftell(datfile);
  460.  
  461.  if(lastvid >= vcount) lastvid = 0;
  462.  if(lastst  >= scount) lastst  = 0;
  463.  
  464. main_menu:
  465.  clrscr();
  466.  gotoxy(1,5);
  467.  printf("                 MAIN MENU\n");
  468.  printf("                    ---------------------------\n");
  469.  printf("                   1) Select Video Driver\n");
  470.  printf("                   2) Select Stereo Mode\n");
  471.  printf("                   3) Modify Video Driver\n");
  472.  printf("                   4) Default Files to Load\n");
  473.  printf("                   5) Write .CFG, .BAT Files\n");
  474.  printf("                   6) Quit\n");
  475.  
  476.  i = getch();
  477.  if(i==27) exit(0);
  478.  if(i<'1' || i>'6') goto main_menu;
  479.  switch(i)
  480.   {
  481.    case '1': select_video();  break;
  482.    case '2': select_stereo(); break;
  483.    case '3': modify_video();  break;
  484.    case '4': add_default();   break;
  485.    case '5': write_files();   break;
  486.    case '6': exit(0);
  487.    default: break;
  488.   }
  489.  goto main_menu;
  490. }
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.