home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / mactool / thinkcgu.sit / app ƒ / menapp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-20  |  2.5 KB  |  101 lines

  1. /*
  2. *    FILE:        menapp.c
  3. *    AUTHOR:        R. Gonzalez
  4. *    CREATED:    August 25, 1990
  5. *
  6. *    Defines menu-driven application methods.
  7. */
  8.  
  9. # include    <stdio.h>
  10. # include    "menapp.h"
  11. # include    "morestr.h"
  12.  
  13. # define    MAX_LENGTH    80
  14.  
  15. /************************************************************************
  16. *    initialize application - programmer should override: first call
  17. *    inherited method, then add new menu items
  18. ************************************************************************/
  19. boolean    Menu_App::init(void)
  20. {
  21.     return Generic_App::init();
  22. }
  23.  
  24. /************************************************************************
  25. *    execute application.  No need to override.
  26. ************************************************************************/
  27. void    Menu_App::run(void)
  28. {
  29.     char    line[MAX_LENGTH],
  30.             number[MAX_LENGTH];
  31.     int        menu_num,
  32.             item,
  33.             i;
  34.     comfunc    command_ptr;
  35.     
  36.     while (!done)
  37.     {
  38.         respond("");
  39.         for (i=1 ; i<= menu->num_menus ; i++)
  40.         {
  41.             sprintf(line,"%d\t%s",i,menu->command[i][0]);
  42.             respond(line);
  43.         }
  44.         respond("");
  45.         query("Enter main command number: ",number);
  46.         menu_num = strtoint(number);
  47.         
  48.         if (menu_num > 0 && menu_num <= menu->num_menus)
  49.         {
  50.             respond("");
  51.             for (i=1 ; i<menu->num_items[menu_num] ; i++)
  52.             {
  53.                 sprintf(line,"%d\t%s",i,menu->command[menu_num][i]);
  54.                 respond(line);
  55.             }
  56.             respond("");
  57.             query("Enter command number: ",number);
  58.             item = strtoint(number);
  59.             respond("");
  60.             
  61.             if (item > 0 && item < menu->num_items[menu_num])
  62.             {
  63.                 command_ptr = menu->get_command_ptr(menu_num,item);
  64.                 if (!(*command_ptr)(this))
  65.                     respond("Illegal usage.");
  66.             }
  67.             else
  68.                 respond("Illegal command.");
  69.         }
  70.         else
  71.             respond("Illegal command.");
  72.     }
  73. }
  74.  
  75. /************************************************************************
  76. *    get string from user.  No need to override.
  77. ************************************************************************/
  78. void    Menu_App::query(char *question,char answer[])
  79. {
  80.     printf("%s",question);
  81.     get_line(stdin,answer,MAX_LENGTH);
  82. }
  83.  
  84. /************************************************************************
  85. *    reply to user.  No need to override.
  86. ************************************************************************/
  87. void    Menu_App::respond(char *response)
  88. {
  89.     puts(response);
  90. }
  91.  
  92. /************************************************************************
  93. *    terminate application.  Programmer should call inherited method
  94. *    if the derived application overrides it.
  95. ************************************************************************/
  96. boolean    Menu_App::destroy(void)
  97. {
  98.     return Generic_App::destroy();
  99. }
  100.  
  101.