home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / rpc / dict / play.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  9.0 KB  |  281 lines

  1. /*************************************************************/
  2. /**                                                         **/
  3. /**                 Microsoft RPC Examples                  **/
  4. /**                 Dictionary Application                  **/
  5. /**          Copyright(c) Microsoft Corp. 1992-1996         **/
  6. /**                                                         **/
  7. /*************************************************************/
  8.  
  9. /*
  10.  *************************************************************************
  11.  *                                                                       *
  12.  * Local dictionary :play" example                                       *
  13.  *                                                                       *
  14.  * Description:                                                          *
  15.  * This file contains a simple interactive loop of calls to the          *
  16.  * dictionary.  The interface is identical to the remote dictionary      *
  17.  * program described in the readme file.                                 *
  18.  *                                                                       *
  19.  *************************************************************************
  20. */
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <ctype.h>
  25.  
  26. #include "dict0.h"
  27. #include "play.h"
  28. #include "util0.h"
  29.  
  30. #define TAB_STOPS 3
  31.  
  32.  
  33. void Usage()
  34. {
  35.   printf("Usage : play \n\n");
  36.   exit(1);
  37. }
  38.  
  39. /*************************************************************************/
  40. /***                     Remote Dictionary Test Loop                   ***/
  41. /*************************************************************************/
  42.  
  43. void
  44. Usage_Msg()
  45. {
  46.     printf("\nUsage: \nType a single character, followed by an optional key as follows:\n\n");
  47.     printf("    i <key> :: Insert <key> into dictionary\n");
  48.     printf("    d <key> :: Delete <key> from dictionary\n");
  49.     printf("    f <key> :: Find <key> in dictionary\n");
  50.     printf("    n       :: Next of local current item in dictionary\n");
  51.     printf("    p       :: Previous of local current item in dictionary\n");
  52.     printf("    h       :: Head (first item) of dictionary\n");
  53.     printf("    t       :: Tail (last item) of dictionary\n");
  54.     printf("    ?       :: Print this message\n");
  55.     printf("    q       :: Quit\n\n");
  56.     printf("where <key> is <integer> <string>\n");
  57. }
  58.  
  59. /*************************************************************************/
  60. /***    Minimal Dictionary Operations:                                 ***/
  61. /***                                                                   ***/
  62. /***    Dictionary *Dict_New(Cmp_rec*, Splay*, print_rec*)             ***/
  63. /***                                                                   ***/
  64. /***    Dict_Status Dict_Find(Dictionary*, Item*)                      ***/
  65. /***    Dict_Status Dict_Next(Dictionary*, Item*)                      ***/
  66. /***    Dict_Status Dict_Prev(Dictionary*, Item*)                      ***/
  67. /***    Dict_Status Dict_Insert(Dictionary*, Item*)                    ***/
  68. /***    Dict_Status Dict_Delete(Dictionary*, Item**)                   ***/
  69. /***                                                                   ***/
  70. /***    Item* DICT_CURR_ITEM(Dict*)                                    ***/
  71. /*************************************************************************/
  72.  
  73. void
  74. TestLoop( Dictionary * pdict )
  75. {
  76.     char currName[80];
  77.     char name[80];
  78.     char op = 0;
  79.     char buffer[80];
  80.  
  81.     Record r, currRecord;
  82.     Record *pcurrRecord = &currRecord;
  83.     Record *pr = &r;
  84.  
  85.     Dict_Status status;
  86.     short i;
  87.  
  88.     pcurrRecord->name = currName;
  89.     pr->name = name;
  90.  
  91.     Usage_Msg();
  92.  
  93.     while ( op != 'q' ) {
  94.  
  95.         printf("\nnext op (i d x f n p h t ? q): ");
  96.         gets(buffer);
  97.         op = buffer[0];
  98.  
  99.         if (op == 'i' || op == 'd' || op == 'f' ||
  100.             op == '+' || op == '-' || op == 'I')
  101.               sscanf(buffer+1, "%d %s", &pr->key, pr->name);
  102.  
  103.         switch (op) {
  104.             case 'h':
  105.                 // get Head of list (first record);
  106.  
  107.                 status = Dict_Next(pdict, NULL);
  108.                 ItemCopy(DICT_CURR_ITEM(pdict), pcurrRecord);
  109.                 ItemCopy(DICT_CURR_ITEM(pdict), pr);
  110.                 break;
  111.  
  112.             case 't':
  113.                 // get Tail of list (last record)
  114.  
  115.                 status = Dict_Prev(pdict, NULL);
  116.                 ItemCopy(DICT_CURR_ITEM(pdict), pcurrRecord);
  117.                 ItemCopy(DICT_CURR_ITEM(pdict), pr);
  118.                 break;
  119.  
  120.             case 'n':
  121.                 // get Next record
  122.  
  123.                 status = Dict_Next(pdict, pcurrRecord);
  124.                 ItemCopy(DICT_CURR_ITEM(pdict), pcurrRecord);
  125.                 break;
  126.  
  127.             case 'p':
  128.                 // get Previous record
  129.  
  130.                 status = Dict_Prev(pdict, pcurrRecord);
  131.                 ItemCopy(DICT_CURR_ITEM(pdict), pcurrRecord);
  132.                 break;
  133.  
  134.             case 'N':
  135.                 // get Next record
  136.  
  137.                 status = Dict_Next(pdict, pr);
  138.                 ItemCopy(DICT_CURR_ITEM(pdict), pr);
  139.                 break;
  140.  
  141.             case 'P':
  142.                 // get Previous record
  143.  
  144.                 status = Dict_Prev(pdict, pr);
  145.                 ItemCopy(DICT_CURR_ITEM(pdict), pr);
  146.                 break;
  147.  
  148.             case 'r':
  149.                 ItemCopy(DICT_CURR_ITEM(pdict), pcurrRecord);
  150.                 break;
  151.  
  152.  
  153.             case '+':
  154.                 // get Next record
  155.  
  156.                 status = Dict_Next(pdict, pr);
  157.                 break;
  158.  
  159.             case '-':
  160.                 // get Previous record
  161.  
  162.                 status = Dict_Prev(pdict, pr);
  163.                 break;
  164.  
  165.             case 'f':
  166.                 // Find <key>
  167.  
  168.                 status = Dict_Find(pdict, pr);
  169.                 break;
  170.  
  171.             case 'i':
  172.                 // Insert <key>
  173.  
  174.                 status = Dict_Insert(pdict,
  175.                                      makeRecord(pr->key, pr->name) );
  176.                 break;
  177.  
  178.             case 'I':
  179.                 // Insert (<num'>,<name>) for all num': 3 < num' < num
  180.  
  181.                 for (i = 3; i < pr->key; i++) {
  182.                     status = Dict_Insert(pdict,
  183.                                          makeRecord(i, pr->name) );
  184.                 }
  185.                 break;
  186.  
  187.             case 'd':
  188.                 // Delete <key>
  189.  
  190.                 if (pdict != NULL) {
  191.                     status = Dict_Delete(pdict, (void **)&pr);
  192.                     freeRecord(pr);
  193.                     pr = &r;
  194.                 }
  195.                 break;
  196.  
  197.             case 'x':
  198.                 // Delete DICT_CURR_ITEM
  199.  
  200.                 if ((pdict != NULL) && (pdict->root != NULL)) {
  201.                     pr = DICT_CURR_ITEM(pdict);
  202.                     status = Dict_Delete(pdict, (void **) &pr);
  203.                     freeRecord(pr);
  204.                     pr = &r;
  205.                 }
  206.                 break;
  207.  
  208.             case 'X':
  209.                 // Empty the whole dictionary
  210.  
  211.                 RecordTreeNodeFree((RecordTreeNode*)pdict->root);
  212.                 pdict->root = NULL;
  213.                 pr = &r;
  214.                 break;
  215.  
  216.             case '?':
  217.                 Usage_Msg();
  218.                 break;
  219.         }
  220.  
  221.         if (op != '?' && op != 'q')
  222.             Dict_Print(pdict, TAB_STOPS);
  223.     }
  224. }
  225.  
  226. Dict_Status
  227. Dict_New_Dict(OUT Dictionary ** ppdict)
  228. {
  229.     static Dictionary * pdict;
  230.  
  231.     pdict = Dict_New(comp, tdSplay, printRecord);
  232.     Init_dict(pdict);
  233.  
  234.     *ppdict = pdict;
  235.     return(DICT_SUCCESS);
  236. }
  237.  
  238. void
  239. Init_dict(Dictionary * dp)
  240. {
  241.     Record* rp;
  242.  
  243.     rp = makeRecord((short)0, "jack_smith"); Dict_Insert(dp, rp);
  244.     rp = makeRecord((short)0, "john_doe"); Dict_Insert(dp, rp);
  245.     rp = makeRecord((short)1, "jean_doe"); Dict_Insert(dp, rp);
  246.     rp = makeRecord((short)0, "joana_smith"); Dict_Insert(dp, rp);
  247.     rp = makeRecord((short)1, "michael_jones"); Dict_Insert(dp, rp);
  248.     rp = makeRecord((short)0, "mike_jacobs"); Dict_Insert(dp, rp);
  249.     rp = makeRecord((short)2, "bill_jackson"); Dict_Insert(dp, rp);
  250.     rp = makeRecord((short)0, "jane_doe"); Dict_Insert(dp, rp);
  251.     rp = makeRecord((short)0, "dianne_jackson"); Dict_Insert(dp, rp);
  252.     rp = makeRecord((short)1, "james_doe"); Dict_Insert(dp, rp);
  253.     rp = makeRecord((short)1, "steve_johnson"); Dict_Insert(dp, rp);
  254.     rp = makeRecord((short)2, "debbie_jones"); Dict_Insert(dp, rp);
  255.     rp = makeRecord((short)0, "jacob_jacobson"); Dict_Insert(dp, rp);
  256.  
  257.     Dict_Print(dp, TAB_STOPS);
  258. }
  259.  
  260. /*************************************************************************/
  261. /***                             Main Loop                             ***/
  262. /*************************************************************************/
  263.  
  264. void
  265. main_dict ()
  266. {
  267.     Dictionary * pdict;
  268.     Dictionary ** ppdict = &pdict;
  269.  
  270.     printf("getting a new dict\n");
  271.     Dict_New_Dict(ppdict);
  272.     printf("gotten a new dict in main_dict\n");
  273.     TestLoop(pdict);
  274. }
  275.  
  276. void _CRTAPI1
  277. main(int argc, char *argv[])
  278. {
  279.     main_dict();
  280. }
  281.