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

  1. /*
  2. *    FILE:        comapp.c
  3. *    AUTHOR:        R. Gonzalez
  4. *    CREATED:    August 25, 1990
  5. *
  6. *    Defines command-line application methods.
  7. */
  8.  
  9. # ifdef        THINK_C
  10. # include    <oops.h>
  11. # endif
  12.  
  13. # include    <string.h>
  14. # include    <stdio.h>
  15. # include    "comapp.h"
  16.  
  17. # define    MAX_LENGTH    80
  18.  
  19. /************************************************************************
  20. *    initialize application - programmer should override: first call
  21. *    inherited method, then add new menu items
  22. ************************************************************************/
  23. boolean    Command_App::init(void)
  24. {
  25.     Generic_App::init();
  26.     comline = new(Comline);
  27.     comline->init();
  28.     
  29.     return TRUE;
  30. }
  31.  
  32. /************************************************************************
  33. *    execute application - no need to override
  34. ************************************************************************/
  35. void    Command_App::run(void)
  36. {
  37.     char    command[MAX_LENGTH];
  38.     int        menu_num,
  39.             item;
  40.     comfunc    command_ptr;
  41.     
  42.     respond("Type '?' for help.");
  43.     respond("");
  44.     
  45.     while (!done && comline->read())
  46.     {
  47.         comline->get_command(command);
  48.         if (strcmp(command,"") != 0)
  49.         {
  50.             menu_num = menu->get_menu(command);
  51.             if (menu_num != ILLEGAL)
  52.             {
  53.                 item = menu->get_item(command);
  54.                 command_ptr = menu->get_command_ptr(menu_num,item);
  55.                 if (!(*command_ptr)(this))
  56.                     respond("Illegal usage.");
  57.             }
  58.             else
  59.                 list_commands();
  60.         }
  61.     }
  62. }
  63.  
  64. /************************************************************************
  65. *    get string from user.  For command-line application the question
  66. *    is not used.  No need to override.
  67. ************************************************************************/
  68. void    Command_App::query(char *question,char answer[])
  69. {
  70.     comline->get_next_argument(answer);
  71. }
  72.  
  73. /************************************************************************
  74. *    reply to user.  No need to override.
  75. ************************************************************************/
  76. void    Command_App::respond(char *response)
  77. {
  78.     puts(response);
  79. }
  80.  
  81. /************************************************************************
  82. *    list commands logged into menu.
  83. ************************************************************************/
  84. void    Command_App::list_commands(void)
  85. {
  86.     int        menu_num,
  87.             item,
  88.             added,
  89.             length;
  90.     char    row[MAX_LENGTH];
  91.     
  92.     respond("");
  93.     respond("Type first few letters of command;");
  94.     respond("separate arguments using semicolons (;).");
  95.     respond("");
  96.     
  97.     strcpy(row,"");
  98.     length = 0;
  99.     
  100.     for (menu_num=1 ; menu_num <= menu->num_menus ; menu_num++)
  101.     {
  102.         for (item=1 ; item < menu->num_items[menu_num] ; item++)
  103.         {
  104.             added = strlen(menu->command[menu_num][item]) + 2;
  105.             if (length+added >= MAX_LENGTH)
  106.             {
  107.                 respond(row);
  108.                 strcpy(row,"");
  109.                 length = 0;
  110.             }
  111.             
  112.             strcat(row,menu->command[menu_num][item]);
  113.             strcat(row,"  ");
  114.             length += added;
  115.         }
  116.     
  117.         if (length > 0)
  118.         {
  119.             respond(row);
  120.             strcpy(row,"");
  121.             length = 0;
  122.         }
  123.     }
  124.         
  125.     respond("");
  126. }
  127.  
  128. /************************************************************************
  129. *    terminate application.  Programmer should call inherited method
  130. *    if the derived application overrides it.
  131. ************************************************************************/
  132. boolean    Command_App::destroy(void)
  133. {
  134.     Generic_App::destroy();
  135.     comline->destroy();
  136.     delete(comline);
  137.     
  138.     return TRUE;
  139. }
  140.  
  141.