home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 May / PCO_5_97.ISO / FilesBBS / OS2 / OS2WWW42.ARJ / OS2WWW42 / OS2WWW42.ZIP / SYS402.R4 / TOUR / SOURCE / TOUR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-21  |  8.1 KB  |  304 lines

  1. /*******************************************************************************
  2. *
  3. * This module presents four example PowerWeb Server++ API Hook Functions:
  4. * (all are accessible via the Guided Tour)
  5. *
  6. *    TourCalculation    - used in the Pizza example
  7. *    TourPresentCSV        - used in the Database Viewer example
  8. *    TourDirect            - used in the Direct-Call example
  9. *    TourTree                - used as a more sophisticated Direct-Call example
  10. *
  11. * COPYRIGHT:
  12. *   CompuSource (Pty) Ltd
  13. *   Licensed Materials - Property of CompuSource (Pty) Ltd
  14. *   (C) Copyright CompuSource (Pty) Ltd 1994-1996.
  15. *   All Rights Reserved
  16. *   Use, duplication, or disclosure restricted by international
  17. *   copyright law.
  18. *
  19. *******************************************************************************/
  20.  
  21. #pragma    strings(readonly)
  22.  
  23. #include    <stdio.h>
  24. #include    <stdlib.h>
  25. #include    <string.h>
  26. #include    <math.h>
  27.  
  28. #include    "PowerAPI.hpp"
  29.  
  30. #if    defined(__IBMCPP__)
  31.     #define    EXPORT    _Export
  32. #else
  33.     #define    EXPORT
  34. #endif
  35.  
  36. // -------------------------------------------------------------------------
  37.  
  38. static const char*    pszHeader    = "<html><body background=/icons/textures/paper.jpg>";
  39. static const char*    pszTrailer    = "</body></html>";
  40.  
  41. // -------------------------------------------------------------------------
  42. //    This function is called from the order entry form to perform calculations
  43. //    based on data entered. The results are then fed back to the form.
  44. //    Only very simple error checking is performed.
  45.  
  46. long        APIENTRY    EXPORT TourCalculation    (void*    parcel)
  47. {
  48.     long        cPepperoni;
  49.     long        cHawaiian;
  50.     double    dPrice    = 0.0;
  51.     double    dTax;
  52.     double    dTotal;
  53.     void*        hFormFields;
  54.  
  55.     // Get a handle to the list of all form fields for quick reference.
  56.  
  57.     ServerFind(parcel, "Request:/Argument", &hFormFields);
  58.  
  59.     // Read the two data entry fields - note there is no decoding of
  60.     //    POST arguments - PowerWeb Server++ does this all for us.
  61.  
  62.     ServerReadInteger(hFormFields, "Pepperoni",    &cPepperoni);
  63.     ServerReadInteger(hFormFields, "Hawaiian",    &cHawaiian);
  64.  
  65.     //    Perform our computations.
  66.  
  67.     dPrice += cPepperoni    * 10.95;
  68.     dPrice += cHawaiian    * 12.95;
  69.  
  70.     dTax         = floor(dPrice * 10.0) / 100.0;
  71.     dTotal    = dPrice + dTax;
  72.  
  73.     //    Create three new form fields derived from the existing fields.
  74.  
  75.     ServerNewFloat(hFormFields, "Price",    dPrice);
  76.     ServerNewFloat(hFormFields, "Tax",        dTax);
  77.     ServerNewFloat(hFormFields, "Total",    dTotal);
  78.  
  79.     //    Tell the server we had no errors.
  80.  
  81.     return HOOK_OK;
  82. }
  83.  
  84. // -------------------------------------------------------------------------
  85. //    This function is called whenever a ".CSV" file is loaded from the
  86. //    Tour database directory. It presents comma-separated-variable files
  87. //    in an HTML table.
  88. //    Only very simple error checking is performed.
  89.  
  90. long        APIENTRY    EXPORT TourPresentCSV        (void*    parcel)
  91. {
  92.     char        aszFile[_MAX_PATH];
  93.     FILE*        csv;
  94.     char        buffer[1024];
  95.     void*        hResult;
  96.     char*        pszBegin;
  97.     char*        pszComma;
  98.  
  99.     //    Query the current physical resource being accessed.
  100.  
  101.     ServerReadText(parcel, "Request:/Resource", aszFile, sizeof(aszFile));
  102.  
  103.     //    Open the file so that we can translate it into HTML.
  104.  
  105.     csv = fopen(aszFile, "r");
  106.  
  107.     if (csv == 0) {
  108.         ServerWriteInteger(parcel, "Request:/StatusCode", 400);
  109.         return HOOK_ERROR;
  110.     }
  111.  
  112.     //    Get a handle to the HTML result output variable.
  113.  
  114.     ServerFind(parcel, "Request:/Result", &hResult);
  115.  
  116.     //    Output the HTML preamble.
  117.  
  118.     ServerAppendText(hResult, 0, pszHeader);
  119.     ServerAppendText(hResult, 0, "<h2>Tabular View of ");
  120.     ServerAppendText(hResult, 0, aszFile);
  121.     ServerAppendText(hResult, 0, "</h2><table border=1>");
  122.  
  123.     //    Process each line (record) within the database file.
  124.  
  125.     while (fgets(buffer, sizeof(buffer), csv)) {
  126.         ServerAppendText(hResult, 0, "<tr>");
  127.  
  128.         // Scan for each field and output it with a <td> prefix.
  129.  
  130.         pszBegin = buffer;
  131.  
  132.         while ((pszComma = strchr(pszBegin, ',')) != 0) {
  133.             *pszComma = 0;
  134.  
  135.             //    Remove surrounding quotes, if any.
  136.  
  137.             if (*pszBegin == '"') {
  138.                 pszBegin++;
  139.  
  140.                 if (pszComma[-1] == '"') {
  141.                     pszComma[-1] = 0;
  142.                 }
  143.                 else {
  144.                     *pszComma = ',';
  145.                     pszComma = strchr(pszComma, '"');
  146.  
  147.                     if (pszComma == 0)
  148.                         break;
  149.  
  150.                     *pszComma = 0;
  151.                     pszComma++;
  152.                 }
  153.             }
  154.  
  155.             ServerAppendText(hResult, 0, "<td>");
  156.             ServerAppendText(hResult, 0, (*pszBegin) ? pszBegin : ".");
  157.  
  158.             pszBegin = pszComma + 1;
  159.         }
  160.  
  161.         //    Output the last field in the record.
  162.  
  163.         pszComma = pszBegin + strlen(pszBegin);
  164.         if (pszComma[-1] == '\n') pszComma--;
  165.  
  166.         if (*pszBegin == '"' && pszComma[-1] == '"') {
  167.             pszBegin++;
  168.             pszComma[-1] = 0;
  169.         }
  170.  
  171.         ServerAppendText(hResult, 0, "<td>");
  172.         ServerAppendText(hResult, 0, (*pszBegin) ? pszBegin : ".");
  173.         ServerAppendText(hResult, 0, "\n");
  174.     }
  175.  
  176.     fclose(csv);
  177.  
  178.     //    Output the closing HTML code.
  179.  
  180.     ServerAppendText(hResult, 0, "</table>");
  181.     ServerAppendText(hResult, 0, pszTrailer);
  182.  
  183.     return HOOK_OK;
  184. }
  185.  
  186. // -------------------------------------------------------------------------
  187. //    This function is called directly from a URL without needing an
  188. //    associated resource to manage it.
  189. //    It is very simple - outputs "Hello World". Our apologies for
  190. //    this over-used example, but script writing doesn't get more complex!
  191.  
  192. long        APIENTRY    EXPORT TourDirect    (void*    parcel)
  193. {
  194.     void*        hResult;
  195.  
  196.     //    Get a handle to the HTML result output variable.
  197.     ServerFind(parcel, "Request:/Result", &hResult);
  198.  
  199.     // Output our message, surrounded by standard HTML
  200.     ServerAppendText(hResult, 0, pszHeader);
  201.     ServerAppendText(hResult, 0, "Hello World, from a Direct C Interface.");
  202.     ServerAppendText(hResult, 0, pszTrailer);
  203.  
  204.     return HOOK_OK;
  205. }
  206.  
  207. // -------------------------------------------------------------------------
  208. //    Internal function called recursively by TourTree().
  209.  
  210. void        TourTreeRecursion(void* hRoot, void* hResult, int iLevel)
  211. {
  212.     unsigned long        iKind;
  213.     char                    buffer[256];
  214.  
  215.     ServerKind(hRoot, &iKind);
  216.  
  217.     // If not at the root, display the hRoot Variable's name and value.
  218.  
  219.     if (iLevel > 0) {
  220.         ServerName(hRoot, buffer, sizeof(buffer));
  221.         ServerAppendText(hResult, 0, "<li>");
  222.  
  223.         if (iKind == TYPE_LIST) {
  224.             ServerAppendText(hResult, 0, "<b>");
  225.         }
  226.  
  227.         ServerAppendText(hResult, 0, buffer);
  228.  
  229.         if (iKind == TYPE_LIST) {
  230.             ServerAppendText(hResult, 0, "</b>");
  231.         }
  232.         else {
  233.             ServerAppendText(hResult, 0, " = ");
  234.  
  235.             ServerReadText(hRoot, 0, buffer, sizeof(buffer));
  236.             ServerAppendText(hResult, 0, (strlen(buffer) == 0) ? "(None)" : buffer);
  237.         }
  238.     }
  239.  
  240.     // If the hRoot Variable is a list, display it recursively.
  241.  
  242.     if (iKind == TYPE_LIST) {
  243.         void*        hNext;
  244.  
  245.         if (ServerChild(hRoot, &hNext) == ERR_NONE) {
  246.             ServerAppendText(hResult, 0, "<ul>");
  247.  
  248.             do {
  249.                 TourTreeRecursion(hNext, hResult, iLevel+1);
  250.             } while (ServerSibling(hNext, &hNext) == ERR_NONE);
  251.  
  252.             ServerAppendText(hResult, 0, "</ul>\n");
  253.         }
  254.     }
  255. }
  256.  
  257. // -------------------------------------------------------------------------
  258. //    Recursively dump a multi-level list representation of the entire
  259. //    tree of Variables, or of a sub-tree if there is an argument to the URL.
  260. //    Example: "http://127.0.0.1/example/bin/tour!TourTree?Request:/"
  261.  
  262. long        APIENTRY    EXPORT TourTree    (void*    parcel)
  263. {
  264.     void*        hResult;
  265.     void*        hRoot;
  266.     char        buffer[256];
  267.  
  268.     //    Get a handle to the HTML result output variable.
  269.  
  270.     ServerFind(parcel, "Request:/Result", &hResult);
  271.  
  272.     // Read the argument to the URL (if any)
  273.  
  274.     ServerReadText(parcel, "Request:/ArgumentText", buffer, sizeof(buffer));
  275.  
  276.     // If no argument given, use a default of the Configuration database.
  277.  
  278.     if (strlen(buffer) == 0)
  279.         strcpy(buffer, "Config:/");
  280.  
  281.     // Output an HTML background
  282.  
  283.     ServerAppendText(hResult, 0, pszHeader);
  284.  
  285.     // Test whether the requested root of the Variable tree actually exists.
  286.  
  287.     if (ServerFind(parcel, buffer, &hRoot) != ERR_NONE) {
  288.         ServerAppendText(hResult, 0, "Failed to Find the Requested Variable Directory: ");
  289.         ServerAppendText(hResult, 0, buffer);
  290.     }
  291.     else {
  292.         ServerAppendText(hResult, 0, "<h2>Tree of Variables Under: ");
  293.         ServerAppendText(hResult, 0, buffer);
  294.         ServerAppendText(hResult, 0, "</h2>\n");
  295.  
  296.         TourTreeRecursion(hRoot, hResult, 0);
  297.     }
  298.  
  299.     ServerAppendText(hResult, 0, pszTrailer);
  300.  
  301.     return HOOK_OK;
  302. }
  303.  
  304.