home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #3 / amigamamagazinepolishissue1998.iso / bazy / abook_10upd / developer / api_example.c < prev    next >
C/C++ Source or Header  |  1996-03-11  |  8KB  |  250 lines

  1. /*
  2. **  $VER: Api_Example.c 01.00 (18.12.95)
  3. **
  4. **  © 1995 Jörg Krause
  5. **
  6. **  PROGRAMNAME:
  7. **      Api_Example.c
  8. **
  9. **  FUNCTION:
  10. **      Example Programm which explains how to use Adress-Books Api.
  11. **      Remember that the returned Address list is READ-ONLY. Don't
  12. **      try to change it. I'm not responsible for any data lost or
  13. **      system crashes.
  14. **
  15. **  $HISTORY:
  16. **
  17. **   18.12.95 : 01.00 : initial release
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23.  
  24. #include <exec/types.h>
  25. #include <exec/memory.h>
  26.  
  27. #include <utility/tagitem.h>
  28.  
  29. #include <proto/exec.h>
  30.  
  31.  
  32. #include "ABook_Api.h"             /* The API includes */
  33.  
  34. struct MsgPort    *ServerPort;     /* Address-Book's Message Port */
  35. struct MsgPort    *ReplyPort;      /* Our ReplyPort */
  36.  
  37. struct List       *Addresslist;    /* The Addresslist returned by the Server */
  38.  
  39.  
  40.  
  41. /* This function may be used to send an API message to the server safely
  42. **
  43. ** Input: ULONG type
  44. **
  45. **  - AB_Get_Addresses     Obtain a copy of the actual database. You'll get a
  46. **                         pointer to the Addresslist in the replymessage.
  47. **
  48. **  - AB_Dispose           If you are ready with using the address list. Send the
  49. **                         server this command to dipose the copy of your database
  50. **
  51. */
  52.  
  53. struct ApiMessage * SendMessage(ULONG Type)
  54. {
  55.     struct MsgPort *APIPort;
  56.     struct ApiMessage *ApiMsg=0;
  57.  
  58.     /* Allocate the message */
  59.     if ((ApiMsg=AllocVec(sizeof(struct ApiMessage), MEMF_PUBLIC | MEMF_CLEAR)))
  60.     {
  61.         /* Fill in the seperate fields... */
  62.  
  63.         ApiMsg->Message.mn_Node.ln_Type = NT_MESSAGE;
  64.         ApiMsg->Message.mn_Length       = sizeof(struct ApiMessage);
  65.         ApiMsg->Message.mn_ReplyPort    = ReplyPort;
  66.         ApiMsg->type                    = Type;
  67.         ApiMsg->rc                      = 0;
  68.         ApiMsg->AddressList             = 0;
  69.  
  70.         /* PutMessageSafely... */
  71.  
  72.         Forbid();
  73.  
  74.         APIPort=FindPort("ABSERVER");
  75.  
  76.         if (APIPort)
  77.             PutMsg(APIPort,(struct Message *)ApiMsg);
  78.  
  79.         Permit();
  80.  
  81.         /* Wait for the answer */
  82.  
  83.         WaitPort( ReplyPort );
  84.  
  85.         /* Get the replymessage */
  86.  
  87.         ApiMsg=(struct ApiMessage *)GetMsg(ReplyPort);
  88.  
  89.     }
  90.  
  91.     /* And return the result */
  92.     return (ApiMsg);
  93.  
  94. }
  95.  
  96. /* The Complete API example
  97. **
  98. ** This routine sends an "AB_Get_Addresses" to the server to get a
  99. ** copy of the database and then it writes the contents to stdout.
  100. ** After that it sends an "AB_Dipose" to the server to disconnect
  101. ** this prg and get back all of the used memory....
  102. */
  103.  
  104. int main(int argc, char *argv[])
  105. {
  106.     struct ApiMessage *ReplyMessage;
  107.  
  108.     Forbid();
  109.     if ((ServerPort=FindPort("ABSERVER")))
  110.     {
  111.         Permit();
  112.  
  113.  
  114.         if ((ReplyPort=CreateMsgPort()))
  115.         {
  116.             /* Obtain a copy of the database */
  117.             if((ReplyMessage=SendMessage(AB_Get_Addresses)))
  118.             {
  119.                 if (!ReplyMessage->rc)
  120.                 {
  121.                     struct data *node;
  122.  
  123.                     /* All goes well and we have a copy of the database */
  124.  
  125.                     /* Store the Listpointer */
  126.                     Addresslist=ReplyMessage->AddressList;
  127.  
  128.                     /*Dispose the Replymessage */
  129.                     FreeVec(ReplyMessage);
  130.  
  131.                     /* Print the contents */
  132.                     node=(struct data *)Addresslist->lh_Head;
  133.  
  134.                     while (node->node.ln_Succ)
  135.                     {
  136.  
  137.                         if (node->last)
  138.                             printf("Name: %s ",node->last);
  139.  
  140.                         if (node->first)
  141.                             printf(", %s\n",   node->first);
  142.                         else
  143.                             printf("\n");
  144.  
  145.                         if (node->address.lh_Head!=NULL)
  146.                         {
  147.                             struct Address *addrnode;
  148.  
  149.                             addrnode=(struct Address *)node->address.lh_Head;
  150.                             while (addrnode->addrnode.ln_Succ)
  151.                             {
  152.                                 if (addrnode->description)
  153.                                     printf("Address: %s\n",addrnode->description);
  154.  
  155.                                 if (addrnode->Phone.lh_Head)
  156.                                 {
  157.                                     struct MList *phonenode;
  158.  
  159.                                     phonenode=(struct MList *) addrnode->Phone.lh_Head;
  160.                                     while ( phonenode->listnode.ln_Succ)
  161.                                     {
  162.                                         if (phonenode->number)
  163.                                             printf("Number: %s ",phonenode->number);
  164.                                         if (phonenode->description)
  165.                                         {
  166.                                             printf("(%s)\n",phonenode->description);
  167.                                         }
  168.                                         else
  169.                                         {
  170.                                             printf("\n");
  171.                                         }
  172.                                         phonenode=(struct MList *)phonenode->listnode.ln_Succ;
  173.                                     }
  174.                                 }
  175.                                 addrnode=(struct Address *)addrnode->addrnode.ln_Succ;
  176.                             }
  177.                         }
  178.  
  179.                         if (node->Email.lh_Head!=NULL)
  180.                         {
  181.                             struct MList *emailnode;
  182.  
  183.                             emailnode=(struct MList *)node->Email.lh_Head;
  184.                             while (emailnode->listnode.ln_Succ)
  185.                             {
  186.                                 if (emailnode->number)
  187.                                     printf("E-mail: %s ",emailnode->number);
  188.                                 if (emailnode->description)
  189.                                 {
  190.                                     printf("(%s)\n",emailnode->description);
  191.                                 }
  192.                                 else
  193.                                 {
  194.                                     printf("\n");
  195.                                 }
  196.                                 emailnode=(struct MList *)emailnode->listnode.ln_Succ;
  197.                             }
  198.                         }
  199.  
  200.                         node=(struct data *)node->node.ln_Succ;
  201.                     }
  202.  
  203.                     /* Dipose the copy */
  204.                     if ((ReplyMessage=SendMessage(AB_Dispose)))
  205.                     {
  206.                         if (ReplyMessage->rc)
  207.                         {
  208.                             printf("Oops, something goes wrong :(\n");
  209.                         }
  210.                         FreeVec(ReplyMessage);
  211.                     }
  212.                     else
  213.                     {
  214.                         printf("Couldn't dispose the database...\n");
  215.                     }
  216.                 }
  217.                 else
  218.                 {
  219.                     printf("Hmpf, something goes wrong\n");
  220.                 }
  221.             }
  222.             else
  223.             {
  224.                 printf("Couldn't send message to the Server!\n");
  225.             }
  226.  
  227.             while (ReplyMessage=(struct ApiMessage *)GetMsg(ReplyPort))
  228.             {
  229.                 ReplyMsg((struct Message *)ReplyMessage);
  230.             }
  231.  
  232.             DeleteMsgPort(ReplyPort);
  233.         }
  234.         else
  235.         {
  236.             printf("Failed to create the Replyport\n");
  237.             return(10);
  238.         }
  239.     }
  240.     else
  241.     {
  242.         Permit();
  243.         printf("The API-Port is not available. Start the ABook-Server to use it!\n");
  244.         return(10);
  245.     }
  246.  
  247. }
  248.  
  249.  
  250.