home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / GAPCDR.ZIP / CDOOR.ZIP / CDOOR.C next >
Encoding:
C/C++ Source or Header  |  1989-03-17  |  18.7 KB  |  430 lines

  1. /*
  2.     Copyright (C) 1989 The GAP Development Company
  3.  
  4.     All Rights Reserved
  5.  
  6.  
  7.     CDOOR.C
  8.  
  9.     Demonstration program for GAPCDR
  10.  
  11.     To compile : cl /c cdoor
  12.     To link    : link cdoor,,NUL.MAP,+gapcdrs
  13.  
  14.     Program will need access to CDOOR.CNF, DOOR.SYS, GAPBBS.CNF, GAPDOS.DAT
  15.  
  16. /*
  17.  
  18.  
  19. #define LINT_ARGS
  20.  
  21. #pragma check_stack(off)           // we dont need stack checking
  22.  
  23.  
  24.    /***********************************************************************/
  25.    /*  Before doing ANYTHING else, include the following files.           */
  26.    /***********************************************************************/
  27.  
  28. #include <stdio.h>                        // C standard header file
  29.  
  30. #include "gapcdr.h"                       // header file for GAPCDR
  31.  
  32.  
  33.    /***********************************************************************/
  34.    /*  Declare any global variables                                       */
  35.    /***********************************************************************/
  36.  
  37.    char anystring1 [250];                 // global garbage collector
  38.    char prompt      [80];                 // for the prompt line
  39.  
  40.    char *menug [] =
  41.       {
  42.       "C╔══════════════════════════════════════╗",
  43.       "   ║CMain MenuC║",
  44.       "   ╟──────────────────────────────────────╢",
  45.       "   ║C║",
  46.       "   ║  [T]op PlayersC[P]age Sysop  ║",
  47.       "   ║  [H]elpC[U]ser Stats  ║",
  48.       "   ║  [Q]uitC[G]ambleC║",
  49.       "   ╚══════════════════════════════════════╝",
  50.       "\0"
  51.       };
  52.  
  53.  
  54.    char *menua [] =
  55.       {
  56.       "   ╔══════════════════════════════════════╗",
  57.       "   ║              Main Menu               ║",
  58.       "   ╟──────────────────────────────────────╢",
  59.       "   ║                                      ║",
  60.       "   ║  [T]op Players         [P]age Sysop  ║",
  61.       "   ║  [H]elp                [U]ser Stats  ║",
  62.       "   ║  [Q]uit                [G]amble      ║",
  63.       "   ╚══════════════════════════════════════╝",
  64.       "\0"
  65.       };
  66.  
  67.  
  68.  
  69.    /***********************************************************************/
  70.    /*  Begin main line code here                                          */
  71.    /***********************************************************************/
  72.  
  73. main(argc,argv)
  74. int argc;
  75. char *argv[];
  76. {
  77.    int bobo;                              // to keep track of errors
  78.    int oldbell;                           // status of page bell flag
  79.    char **menu;                           // pointer to menu arrays
  80.    char response [80];                    // for getting responses
  81.  
  82.    FILE *doorcnf;                         // file pointer for cnf file
  83.  
  84.  
  85.    /***********************************************************************/
  86.    /*  Before doing ANYTHING else, initialize the door with the following */
  87.    /*  two function calls.                                                */
  88.    /*  Then, if you have any configuration options, read them in and then */
  89.    /*  close the file.                                                    */
  90.    /*  Note that we are hardcoding the name of the configuration file.    */
  91.    /*  Normally, this is passed as a command line parameter so you would  */
  92.    /*  call read_cnf like this :  doorcnf = read_cnf(argv[1]);            */
  93.    /***********************************************************************/
  94.  
  95.    doorcnf = read_cnf("CDOOR.CNF");       // read door configuration file
  96.    init_door();                           // initialize the door
  97.  
  98.    fclose(doorcnf);                       // we dont have any configuration
  99.                                           // options so we will just close
  100.  
  101.  
  102.    /***********************************************************************/
  103.    /*  Now, we are going to keep track of the time credit variable that   */
  104.    /*  is stored in the GAPDOS.DAT file.  We are going to do this because */
  105.    /*  we are such nice people and we want to credit the caller for the   */
  106.    /*  time spent during our chat demonstration.  Remember, at the        */
  107.    /*  beginning of the program, after initializing the door, is the      */
  108.    /*  place to do this because the variable 'timecredit' is initialized  */
  109.    /*  at doors beginning.                                                */
  110.    /*                                                                     */
  111.    /*  NOTE!  This function will fail (the reason for bobo) if GAPDOS.DAT */
  112.    /*  does not exist (which it will not unless this program is being     */
  113.    /*  tested via the BBS door routines.  If you need to test this        */
  114.    /*  feature, you will need to have a backup copy of GAPDOS.DAT that    */
  115.    /*  can be placed in your BBS default directory prior to running this  */
  116.    /*  demo!                                                              */
  117.    /***********************************************************************/
  118.  
  119.    bobo = read_gapdos();                  // read the system file
  120.  
  121.    /***********************************************************************/
  122.    /*  show_mess is the major string output function.  You have the       */
  123.    /*  option of ringing the bell (second parameter) and sending a CR/LF  */
  124.    /*  (third option) after displaying the string.                        */
  125.    /***********************************************************************/
  126.  
  127.    if (!bobo)
  128.       {
  129.       sprintf(anystring1,"GAPDOS.DAT says you have %d time credits (in minutes).",
  130.               gapdos.timecredit);
  131.       show_mess(anystring1,NO,YES);       // display the string
  132.       }
  133.    else
  134.       {
  135.       nl(2);                              // send a couple of blank lines
  136.       ansi(BRED);                         // change to bright red
  137.       show_mess("Could Not Open GAPDOS.DAT!",YES,YES);  // error message
  138.       }
  139.  
  140.    nl(2);                                 // send a couple of blank lines
  141.  
  142.  
  143.    /***********************************************************************/
  144.    /*  Change the color prior to displaying a prompt.  Send a prompt to   */
  145.    /*  the caller and await an answer.                                    */
  146.    /***********************************************************************/
  147.  
  148.    ansi(BGREEN);                          // set default color to bright Green
  149.  
  150.    show_mess("Please Enter Your Name : ",YES,NO);   // main output routine
  151.  
  152.    /***********************************************************************/
  153.    /*  get_string is the main input routine.  To utilize it, you pass the */
  154.    /*  string to "fill" with the user response.  The length of the input  */
  155.    /*  is determined by the length of the string passed.  The function    */
  156.    /*  empty is provided so that you may "empty" a string by filling it   */
  157.    /*  with spaces.                                                       */
  158.    /***********************************************************************/
  159.  
  160.    empty(response,30);                    // must initialize all variables
  161.                                           // passed to get_string
  162.    get_string(response);                  // now get the caller's name
  163.  
  164.    nl(1);                                 // display a blank line
  165.  
  166.  
  167.    /***********************************************************************/
  168.    /*  An alternative way to change colors on the fly is to do it         */
  169.    /*  inline.  It is also a bit faster since fewer function calls are    */
  170.    /*  made.  But note that to change colors like this, you have to test  */
  171.    /*  if the caller is in color mode.                                    */
  172.    /***********************************************************************/
  173.  
  174.    if (color)
  175.       sprintf(anystring1,"%sYour name is : %s%s%s.",BCYAN,BRED,response,BCYAN);
  176.    else
  177.       sprintf(anystring1,"%sYour name is : %s.",response);
  178.  
  179.    /***********************************************************************/
  180.    /*  Note that you could also do this but be forwarned that this type   */
  181.    /*  of coding produces a LOT of assembler code!                        */
  182.    /*                                                                     */
  183.    /*  sprintf(anystring,"%sYour name is : %s%s%s.",                      */
  184.    /*          color ? BCYAN : "",color ? BRED : "", response,            */
  185.    /*          color ? BCYAN : "");                                       */
  186.    /***********************************************************************/
  187.  
  188.    show_mess(anystring1, NO, YES);        // tell caller what was entered
  189.  
  190.    nl(1);                                 // display a blank line
  191.  
  192.    ansi(BGREEN);                          // change colors
  193.  
  194.    show_mess("But on the BBS, your First Name is : ",NO,NO);
  195.    show_mess(fname,NO,YES);
  196.  
  197.    nl(1);                           // display a blank line
  198.  
  199.    pause();                         // wait for a key press before continuing
  200.  
  201.    nl(2);                           // display a couple of blank lines
  202.  
  203.  
  204.    /***********************************************************************/
  205.    /*  The next example is the WRONG way to display a color string since  */
  206.    /*  it does not bother checking if the user is in color mode.          */
  207.    /*  The correct way is to check the color variable.  If it is a 1      */
  208.    /*  then it is safe to send color.  This is exactly what ansi() does.  */
  209.    /***********************************************************************/
  210.  
  211.    sprintf(anystring1,"%s%s\r\n%s\r\n%s%s",
  212.            BRED,"I'm going to mess up your black & white screen.",
  213.            "Because I'm not checking to see if you have ",
  214.            BWHITE,"Color turned on!!!");
  215.  
  216.    show_mess(anystring1, NO, YES);        // show the ugly mess
  217.  
  218.    nl(2);                                 // couple of blank lines
  219.  
  220.  
  221.  
  222.    /***********************************************************************/
  223.    /*  It is now time to page the sysop.  Just in case the sysop has his  */
  224.    /*  page bell turned off, we are going to cheat a bit and turn it on.  */
  225.    /*  For demonstration purposes only, you see!                          */
  226.    /*  We will also demonstrate how to obtain and display the time left.  */
  227.    /*                                                                     */
  228.    /*  Note that the 'timeleft' variable is automatically updated by the  */
  229.    /*  input routines.  If you need to make sure that it is current, you  */
  230.    /*  can always call time_left() prior to using it.                     */
  231.    /***********************************************************************/
  232.  
  233.    do
  234.       {
  235.       nl(1);
  236.  
  237.       if (color)
  238.          sprintf(anystring1,"%s[%s%d mins%s] To Page the Sysop, type a 'P' : ",
  239.                  YELLOW,BRED,timeleft,YELLOW);
  240.       else
  241.          sprintf(anystring1,"[%d mins] To Page the Sysop, type a 'P' : ",
  242.                  timeleft);
  243.  
  244.       show_mess(anystring1, NO, NO); // show the prompt
  245.  
  246.       empty(response,1);             // must initialize get_string variable
  247.       get_string(response);          // get the caller's response
  248.       }
  249.    while (response[0] != 'P');       // loop till valid response
  250.  
  251.  
  252.    /***********************************************************************/
  253.    /*  We are going to override the sysop's page bell flag so we can      */
  254.    /*  hear the bell.  This is not a good thing to do as it will tend     */
  255.    /*  to anger the sysop if a door program does not honor his BBS        */
  256.    /*  settings.  Sorry sysop.  We'll put the bell flag back the way it   */
  257.    /*  was when we are finished.                                          */
  258.    /***********************************************************************/
  259.  
  260.    oldbell = bell;                        // keep track of old bell setting
  261.    bell = 1;                              // turn sysop's page bell on
  262.  
  263.    pagesysop();                           // now page the sysop
  264.  
  265.    bell = oldbell;                        // restore old bell setting
  266.  
  267.    /***********************************************************************/
  268.    /*  Lets now display a file.  We want to display a color version of    */
  269.    /*  the file if the caller is in color mode and an ascii version of    */
  270.    /*  the file if the caller is in non-color mode.  So, we will ask the  */
  271.    /*  programmer to supply the name of a BBS welcome file.               */
  272.    /*  The programmer should supply the name of the non-color version of  */
  273.    /*  the file (IE, no 'G' at the end of the name).                      */
  274.    /*                                                                     */
  275.    /*  The show_file() routine makes certain assumptions about the file   */
  276.    /*  name being passed to it.  It assumes that you are calling it with  */
  277.    /*  a path and file name for a file that you know is or should be      */
  278.    /*  present.  Show_file() will attempt to find the file, but if it     */
  279.    /*  cannot, it simply returns (no error code).                         */
  280.    /*  If you are gathering input from the user, as this example does,    */
  281.    /*  you may want to call access to first see if the file exists.       */
  282.    /***********************************************************************/
  283.  
  284.    /***********************************************************************/
  285.    /*  A word about the Microsoft C access function.  In short, it is     */
  286.    /*  buggy and not 100% reliable.  In a multi-user system, the          */
  287.    /*  function will fail every time if a user on one node is reading     */
  288.    /*  a file that has been opened with fopen or open, and a user is on   */
  289.    /*  another node trying to use the access function to see if the file  */
  290.    /*  exists.  The access function returns an error that the file is not */
  291.    /*  there, when in fact it IS there.                                   */
  292.    /*  Because of this we have supplied (undocumented) a function called  */
  293.    /*  access1.  It works just like the access function except that it    */
  294.    /*  works on path/filenames only so you dont give it an access mode.   */
  295.    /*  It checks for the existence of a file the proper way and works     */
  296.    /*  irregardless of whether or not the file is already in use by       */
  297.    /*  by another user.                                                   */
  298.    /***********************************************************************/
  299.  
  300.    nl(2);                                 // display a couple of blank lines
  301.  
  302.    while (1)
  303.       {
  304.       ansi(YELLOW);                       // set a default color
  305.  
  306.       show_mess("Enter full path name to your BBS Welcome File : ", NO, NO);
  307.  
  308.       empty(response,65);            // must initialize
  309.       get_string(response);          // get the path and name of file to show
  310.  
  311.       if ( (access1(response)))               // does file exist?
  312.          {
  313.          nl(1);
  314.          ansi(BRED);                          // no, tell them in RED!
  315.          show_mess("File Not Found!",YES,YES);
  316.          nl(1);
  317.          }
  318.       else                                    // yes, go display it
  319.          break;
  320.       }                                       // till valid file name entered
  321.  
  322.  
  323.    ansi(YELLOW);                            // reset default color
  324.  
  325.    show_file(response);                     // now show the file.
  326.  
  327.  
  328.  
  329.    /***********************************************************************/
  330.    /*  Lets see what is going on with the time credit variable.           */
  331.    /*  Depending upon how long the programmer chatted with his/her self,  */
  332.    /*  or if the up or down arrow keys were pressed at any time, it could */
  333.    /*  be quite different from what GAP wrote to the GAPDOS.DAT file!     */
  334.    /***********************************************************************/
  335.  
  336.    
  337.    if (!bobo)                          // only if we could 1st read the file
  338.       {
  339.       nl(2);                           // display a couple of blank lines
  340.       ansi(BGREEN);                    // set a default color
  341.  
  342.       sprintf(anystring1,"GAPDOS timecredit was %d mins.",gapdos.timecredit);
  343.       show_mess(anystring1,NO,YES);
  344.  
  345.       ansi(BWHITE);
  346.  
  347.       show_mess("We are going to explicitly ADD 20 minutes.", NO, YES);
  348.  
  349.       timecredit += 20;                // add 20 minutes
  350.  
  351.       gapdos.timecredit += timecredit;
  352.  
  353.       bobo = write_gapdos();           // update GAPDOS.DAT
  354.       nl(1);                           // show a blank line
  355.       ansi(BGREEN);
  356.  
  357.       sprintf(anystring1,"GAPDOS timecredit now equals %d mins.",
  358.               gapdos.timecredit);
  359.  
  360.       show_mess(anystring1, NO, YES);
  361.  
  362.       ansi(YELLOW);                    // set a default color
  363.  
  364.       show_mess("Any difference came from CHAT or Up/Down Arrow Keys!", YES, NO);
  365.       }
  366.     
  367.    nl(2);                              // display a couple of blank lines
  368.  
  369.    pause();                            // pause before continuing
  370.  
  371.  
  372.    /***********************************************************************/
  373.    /*  Lets now build some menus all at once.                             */
  374.    /*  These menus were created with an ANSI editor.  This is perhaps the */
  375.    /*  fastest and easiest way to create menus.                           */
  376.    /***********************************************************************/
  377.  
  378.  
  379.    if (color)                             // set up the prompts
  380.       sprintf(prompt,"\r\n%s[%s%d mins%s] Main Command : ",
  381.               YELLOW,BRED,timeleft,YELLOW);
  382.    else
  383.       sprintf(prompt,"\r\n[%d mins] Main Command : ",timeleft);
  384.  
  385.    do
  386.       {
  387.       clear_scrn();                       // first clear the screen
  388.  
  389.       nl(2);                              // do a couple of blank lines
  390.  
  391.       if (color)                          // set pointer into menus
  392.          menu = menug;
  393.       else
  394.          menu = menua;
  395.  
  396.       while (**menu)                      // now show the menu
  397.          show_mess(*menu++,NO,YES);
  398.  
  399.       show_mess(prompt,NO,NO);            // show the prompt
  400.  
  401.       empty(response,1);                  // initialize response
  402.       get_string(response);               // get user input
  403.  
  404.       switch (response[0])
  405.          {
  406.          case 'P' :                       // page the sysop
  407.             pagesysop();
  408.             break;
  409.  
  410.          case 'Q' :                       // quit
  411.             break;
  412.  
  413.          case 'T' :                       // show the scoreboard
  414.             read_score("CDOOR.DAT","Top 10 Players Of Our Example Door");
  415.             break;
  416.  
  417.          default :                        // choice no good
  418.             nl(2);
  419.             ansi(BRED);
  420.             show_mess("Only Menu Choices 'Q, P and T' Are Working!",YES,YES);
  421.             nl(1);
  422.             pause();
  423.          }
  424.       }
  425.    while (response[0] != 'Q');
  426.  
  427.    leave(0);                              // must exit properly!
  428. }
  429.  
  430.