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