home *** CD-ROM | disk | FTP | other *** search
/ IBM CD Showcase / OS2_CD_ROM.iso / smce0001 / faxpm / demo / api0 / SAMPLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-10  |  14.0 KB  |  404 lines

  1. /*****************************************************************************
  2. /*
  3. /* File         SAMPLE.C
  4. /*
  5. /* Program      Sample for Fax/PM API use.
  6. /*
  7. /* Last modif   February 5, 1993
  8. /*
  9. /* Copyright    (c) Microformatic S.A. 1993
  10. /*
  11. /*****************************************************************************/
  12.  
  13. /*****************************************************************************
  14. /* Includes
  15. /*****************************************************************************/
  16.  
  17. #define INCL_DOS
  18. #define INCL_ERROR
  19. #define INCL_ERRORS
  20. #define INCL_PM
  21. #include <os2.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <memory.h>
  26.  
  27. #include <recip.h>
  28. #include <group.h>
  29.  
  30. /*****************************************************************************
  31. /* Includes
  32. /*****************************************************************************/
  33.  
  34. #define MAX    10
  35. #define GROUP_NAME    "All names"
  36. #define NEW_GROUP_NAME    "New All names"
  37.  
  38.  
  39. /*****************************************************************************
  40. /* Global variables
  41. /*****************************************************************************/
  42.  
  43. FaxRecipient *MyPhoneBook;
  44. FaxGroup * MyGroup;
  45.  
  46. CHAR szArg[512];
  47. CHAR szArg1[80];
  48. CHAR szArg2[80];
  49.  
  50.  
  51. /*****************************************************************************
  52. /*
  53. /* Function     LISTALL
  54. /*
  55. /* Description  Lists the names and groups defined in the Fax/PM phone book.
  56. /*
  57. /* Parameters   none
  58. /*
  59. /* Return code  none
  60. /*
  61. /****************************************************************************/
  62.  
  63. VOID APIENTRY  LISTALL()
  64. {
  65.    APIRET rc;                         /* return code for function calls      */
  66.    ULONG ulLen;                       /* length of required memory           */
  67.    PSZ pszGroups;                     /* pointer to group list               */
  68.    PSZ pszNames;                      /* pointer to name list                */
  69.    ULONG i ;                          /* index in a list                     */
  70.  
  71.   /***************************************************************************
  72.   /* List all groups : issue a first call to get the length of the required
  73.   /* memory, then issue a second call to actually retrieve the information.
  74.   /**************************************************************************/
  75.    rc = FaxGroup_GetAllNames(MyGroup, (PSZ) 0, &ulLen);
  76.    if (rc != FAX_PHONEBOOK_OK) {
  77.       ulLen=0 ;
  78.    };
  79.    printf("-----------------------> Groups in Fax/PM phone book :\n");
  80.    if (ulLen != 0) {
  81.       pszGroups = (PSZ) malloc(ulLen);
  82.       rc = FaxGroup_GetAllNames(MyGroup, (PSZ) pszGroups, &ulLen);
  83.       if (rc != FAX_PHONEBOOK_OK) {
  84.          printf("LISTALL: GetAllNames rc: %d\n", rc);
  85.          free(pszGroups);
  86.          return;
  87.       };
  88.       for (i=0; pszGroups[i] != 0; ) {
  89.          printf("\t>>> %s\n", &pszGroups[i]);
  90.          i = i + strlen(&pszGroups[i]) + 1;
  91.       } /* endfor */
  92.    free(pszGroups);
  93.    } else {
  94.       printf("No group defined !\n");
  95.    }; /* endif */
  96.  
  97.  
  98.   /***************************************************************************
  99.   /* List all names : issue a first call to get the length of the required
  100.   /* memory, then issue a second call to actually retrieve the information.
  101.   /**************************************************************************/
  102.    rc = FaxRecipient_GetAllNames(MyPhoneBook, (PSZ) 0, &ulLen);
  103.    if (rc != FAX_PHONEBOOK_OK) {
  104.       ulLen=0 ;
  105.    };
  106.    printf("-----------------------> Names in Fax/PM phone book :\n");
  107.    if (ulLen != 0) {
  108.       pszNames = (PSZ) malloc(ulLen);
  109.       rc = FaxRecipient_GetAllNames(MyPhoneBook, (PSZ) pszNames, &ulLen);
  110.       if (rc != FAX_PHONEBOOK_OK) {
  111.          printf("LISTALL: GetAllRecipientNames rc: %d\n", rc);
  112.          free(pszNames);
  113.          return;
  114.       };
  115.       for (i=0; pszNames[i] != 0; ) {
  116.          printf("\t>>> %s\n", &pszNames[i]);
  117.          i = i + strlen(&pszNames[i]) + 1;
  118.       } /* endfor */
  119.    } else {
  120.       printf("No name defined !\n");
  121.       return;
  122.    }; /* endif */
  123.    printf("-----------------------> End of list\n");
  124.    free(pszNames);
  125.    return;
  126. }
  127.  
  128.  
  129. /*****************************************************************************
  130. /*
  131. /* Function     GO1
  132. /*
  133. /* Description  Process 1 parameter command line
  134. /*
  135. /* Parameters   none
  136. /*
  137. /* Return code  none
  138. /*
  139. /****************************************************************************/
  140.  
  141. VOID APIENTRY GO1()
  142. {
  143.    APIRET rc;                         /* return code for function calls      */
  144.    ULONG ulLen;                       /* length of required memory           */
  145.    ULONG i ;                          /* index in a list                     */
  146.    PSZ p1;                            /* parameter 1                         */
  147.    PSZ p2;                            /* parameter 2                         */
  148.  
  149.    p1 = (PSZ) 0;
  150.    p2 = (PSZ) 0;
  151.  
  152.   /***************************************************************************
  153.   /* Delete a group
  154.   /**************************************************************************/
  155.    p1 = strstr(szArg1, "-DN:");
  156.    if (p1 != (PSZ) 0) {
  157.       printf("Delete group name: %s\n", &p1[4]);
  158.       rc = FaxGroup_Delete(MyGroup, &p1[4]);
  159.       printf("Delete group name: %s rc:%ld\n", &p1[4], rc);
  160.       return;
  161.    };
  162.  
  163.   /***************************************************************************
  164.   /* Get a recipient from the phonebook
  165.   /**************************************************************************/
  166.    p1 = strstr(szArg1, "-LN:");
  167.    if (p1 != (PSZ) 0) {
  168.       rc = FaxRecipient_Get(MyPhoneBook, &p1[4]);
  169.       if (rc != FAX_PHONEBOOK_OK) {
  170.          printf("Get recipient: %s rc:%ld\n", &p1[4], rc);
  171.          return;
  172.       };
  173.       rc = FaxRecipient_GetTel(MyPhoneBook, (PSZ) 0, &ulLen);
  174.       if (rc != FAX_PHONEBOOK_OK) {
  175.          printf("Get fax phone number for: %s rc:%ld\n", &p1[4], rc);
  176.       };
  177.       p2 = (PSZ) malloc(ulLen);
  178.       rc = FaxRecipient_GetTel(MyPhoneBook, (PSZ) p2, &ulLen);
  179.       printf("Name: %s\nFax phone number: %s\n", &p1[4], p2);
  180.       free(p2);
  181.       return;
  182.    };
  183.  
  184.   /***************************************************************************
  185.   /* Get the list of recipients is a group
  186.   /**************************************************************************/
  187.    p1 = strstr(szArg1, "-LG:");
  188.    if (p1 != (PSZ) 0) {
  189.       rc = FaxGroup_GetGroupContents(MyGroup,
  190.                                          (PSZ) 0,
  191.                                          &ulLen,
  192.                                          &p1[4]);
  193.       if (rc == FAX_PHONEBOOK_GROUP_NOTFOUND) {
  194.          printf("Group: %s not found rc: %ld\n", &p1[4], rc);
  195.          return;
  196.       };
  197.       if (rc != FAX_PHONEBOOK_OK) {
  198.          printf("Group: %s not found error rc: %ld\n", &p1[4], rc);
  199.          return;
  200.       };
  201.       p2 = (PSZ) malloc(ulLen);
  202.       rc = FaxGroup_GetGroupContents(MyGroup,
  203.                                          (PSZ) p2,
  204.                                          &ulLen,
  205.                                          &p1[4]);
  206.       if (rc != FAX_PHONEBOOK_OK) {
  207.          printf("Group: %s not found error rc: %ld\n", &p1[4], rc);
  208.          return;
  209.       };
  210.       printf("Group: %s contents:\n", &p1[4]);
  211.       for (i=0; p2[i] != 0; ) {
  212.          printf("\t>>> %s\n", &p2[i]);
  213.          i = i + strlen(&p2[i]) + 1;
  214.       } /* endfor */
  215.       printf("end Group: %s contents:\n", &p1[4]);
  216.       free(p2);
  217.       return;
  218.    };
  219.  
  220.   /***************************************************************************
  221.   /* Delete a group
  222.   /**************************************************************************/
  223.    p1 = strstr(szArg1, "-DG:");
  224.    if (p1 != (PSZ) 0) {
  225.       rc = FaxGroup_Delete(MyGroup, &p1[4]);
  226.       if (rc == FAX_PHONEBOOK_GROUP_NOTFOUND) {
  227.          printf("Delete group: %s rc:%ld (Group unknown !)\n", &p1[4], rc);
  228.          return;
  229.       };
  230.       if (rc == FAX_PHONEBOOK_OK) {
  231.          printf("Delete group: %s error rc:%ld\n", &p1[4], rc);
  232.          return;
  233.       };
  234.       printf("Delete group: %s rc:%ld\n", &p1[4], rc);
  235.       return;
  236.    };
  237.  
  238.    return;
  239. }
  240.  
  241.  
  242. /*****************************************************************************
  243. /*
  244. /* Function     GO2
  245. /*
  246. /* Description  Process 2 parameters command line
  247. /*
  248. /* Parameters   none
  249. /*
  250. /* Return code  none
  251. /*
  252. /****************************************************************************/
  253.  
  254. VOID APIENTRY GO2()
  255. {
  256.    APIRET rc;                         /* return code for function calls      */
  257.    ULONG ulLen;                       /* length of required memory           */
  258.    PSZ p1;                            /* parameter 1                         */
  259.    PSZ p2;                            /* parameter 2                         */
  260.  
  261.   /***************************************************************************
  262.   /* Add recipient in the phonebook
  263.   /**************************************************************************/
  264.    p1 = strstr(szArg1, "-AN:");
  265.    if (p1 != (PSZ) 0) {
  266.       rc = FaxRecipient_SetName(MyPhoneBook, &p1[4]);
  267.       rc = FaxRecipient_SetTel(MyPhoneBook,  szArg2);
  268.       rc = FaxRecipient_Create(MyPhoneBook);
  269.       if (rc != FAX_PHONEBOOK_OK) {
  270.          printf("Add recipient name: %s error rc: %ld\n", &p1[4], rc);
  271.          return;
  272.       };
  273.       printf("Add recipient name: %s Ok\n", &p1[4]);
  274.       return;
  275.    };
  276.  
  277.   /***************************************************************************
  278.   /* Add recipient in a group
  279.   /**************************************************************************/
  280.    p1 = strstr(szArg1, "-NG:");
  281.    if (p1 != (PSZ) 0) {
  282.       rc = FaxGroup_AddName(MyGroup, szArg2, &p1[4]);
  283.       if (rc != FAX_PHONEBOOK_OK) {
  284.          printf("Add recipient %s to group: %s error rc: %ld", &p1[4], szArg2, rc);
  285.          return;
  286.       };
  287.       printf("Add recipient %s to group: %s Ok\n", &p1[4], szArg2);
  288.       return;
  289.    };
  290.  
  291.   /***************************************************************************
  292.   /* Update recipient
  293.   /**************************************************************************/
  294.    p1 = strstr(szArg1, "-UN:");
  295.    if (p1 != (PSZ) 0) {
  296.       rc = FaxRecipient_SetName(MyPhoneBook, &p1[4]);
  297.       rc = FaxRecipient_SetTel(MyPhoneBook,  szArg2);
  298.       rc = FaxRecipient_Update(MyPhoneBook);
  299.       if (rc != FAX_PHONEBOOK_OK) {
  300.          printf("Update recipient name: %s error rc: %ld\n", &p1[4], rc);
  301.          return;
  302.       };
  303.       printf("Update recipient name: %s Ok\n", &p1[4]);
  304.       return;
  305.    };
  306.  
  307.   /***************************************************************************
  308.   /* Delete name from a group
  309.   /**************************************************************************/
  310.    p1 = strstr(szArg1, "-SG:");
  311.    if (p1 != (PSZ) 0) {
  312.       rc = FaxGroup_DeleteName(MyGroup, szArg2, &p1[4]);
  313.       if (rc == FAX_PHONEBOOK_GROUP_NOTFOUND) {
  314.          printf("Delete recipient: %s in group: %s, group not found\n", &p1[4], szArg2);
  315.          return;
  316.       };
  317.       if (rc != FAX_PHONEBOOK_OK) {
  318.          printf("Delete recipient: %s in group: %s error rc:%ld\n", &p1[4], szArg2, rc);
  319.          return;
  320.       };
  321.       printf("Delete recipient: %s in group: %s Ok\n", &p1[4], szArg2);
  322.       return;
  323.    };
  324.  
  325.  
  326.    return;
  327. }
  328.  
  329. /*****************************************************************************
  330. /*
  331. /* Function     main
  332. /*
  333. /* Description  main function : check parameters and take appropriate action.
  334. /*
  335. /* Parameters   int argc : arguments count
  336. /*              char * argv[] : array of string pointers
  337. /*
  338. /* Return code  0
  339. /*
  340. /****************************************************************************/
  341.  
  342. int _Optlink main ( int argc, char* argv [] ) {
  343.  
  344.   /***************************************************************************
  345.   /* Initialize all the objects
  346.   /**************************************************************************/
  347.    FaxRecipientNewClass(0,0);
  348.    FaxGroupNewClass(0,0);
  349.    MyPhoneBook = _somNew(_FaxRecipient);
  350.    if (MyPhoneBook == 0) {
  351.       printf("ERROR : Recipient Creation failed");
  352.       return 0  ;
  353.    };
  354.    MyGroup = _somNew(_FaxGroup);
  355.    if (MyGroup == 0) {
  356.       printf("ERROR : Group Creation failed");
  357.       return 0  ;
  358.    };
  359.    FaxRecipient_Setup(MyPhoneBook, "");
  360.    FaxGroup_Setup(MyGroup, "");
  361.  
  362.   /***************************************************************************
  363.   /* No argument : print command line syntax
  364.   /**************************************************************************/
  365.    if (argc == 1) {
  366.       printf("Fax/PM API Sample Program - (c) Microformatic 1993\n\n") ;
  367.       printf("Syntax :\n");
  368.       printf("      commands used on Recipient names\n");
  369.       printf("         -DN:name                      : delete a name\n");
  370.       printf("         -AN:name phone                : add name & phone\n");
  371.       printf("         -LN:name                      : list a name\n");
  372.       printf("         -NG:name group                : add a name to group\n");
  373.       printf("         -UN:name phone                : update a name & phone\n");
  374.       printf("\n");
  375.       printf("      commands used on Group names\n");
  376.       printf("         -LG:group                     : list a group contents\n");
  377.       printf("         -DG:group                     : delete a group\n");
  378.       printf("         -SG:name group                : supress a name in a group\n");
  379.       printf("\n");
  380.       printf("      no parameters                    : list all groups & names\n");
  381.       printf("\n");
  382.       printf("\n");
  383.    };
  384.  
  385.   /***************************************************************************
  386.   /* Take the action depending on the number of parameters
  387.   /**************************************************************************/
  388.    szArg[0] = 0;
  389.    if (argc == 1) {
  390.       LISTALL();
  391.    } else {
  392.       if (argc == 2) {
  393.          strcpy(szArg1, argv[1]);
  394.          GO1();
  395.       } else {
  396.          strcpy(szArg1, argv[1]);
  397.          strcpy(szArg2, argv[2]);
  398.          GO2();
  399.       }; /* endif */
  400.    }; /* endif */
  401.  
  402.    return 0 ;
  403. }
  404.