PLAY.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:9k
源码类别:

Windows编程

开发平台:

Visual C++

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