Annotation of mstools/samples/rpc/dict/play.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.