home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / WEBSERVE / SAMBAR / DATA.1 / general.c < prev    next >
C/C++ Source or Header  |  1997-06-11  |  4KB  |  192 lines

  1. /*
  2. ** GENERAL
  3. **
  4. **      HTTP Wrapper for General utility applications.
  5. **
  6. **        Confidential Property of Tod Sambar
  7. **        (c) Copyright Tod Sambar 1997
  8. **        All rights reserved.
  9. **
  10. **
  11. ** Public Functions:
  12. **
  13. **        page_count
  14. **
  15. **
  16. ** History:
  17. ** Chg#    Date    Description                                                Resp
  18. ** ----    -------    -------------------------------------------------------    ----
  19. **         10JUN97    Created                                                    sambar
  20. */
  21.  
  22. #include    <stdio.h>
  23. #include    <stdlib.h>
  24. #include    <memory.h>
  25. #include    <string.h>
  26. #include    <general.h>
  27.  
  28. /*
  29. **  GENERAL_INIT
  30. **
  31. **    Initialize the General Utility RPCS use by the Sambar Server plugins.
  32. **
  33. **
  34. **  Parameters:
  35. **    sactx        Sambar Server context
  36. **
  37. **  Returns:
  38. **    SA_SUCCEED | SA_FAIL
  39. */
  40. SA_RETCODE SA_PUBLIC
  41. general_init(sactx)
  42. SA_CTX        *sactx;
  43. {
  44.     /* Register the User RPCs with the server                            */
  45.     if (sa_cmd_init(sactx, "pagecount", SA_AUTHORIZATION_ALL,
  46.         "Retrieve the number of hits for a page.", page_count) != SA_SUCCEED)
  47.     {
  48.         sa_log(sactx, "Unable to initialize General Utility RPCs");
  49.         return (SA_FAIL);
  50.     } 
  51.  
  52.     sa_log(sactx, "General Utilities Initialized");
  53.  
  54.     return (SA_SUCCEED);
  55. }
  56.  
  57. /*
  58. **  PAGE_COUNT
  59. **
  60. **    Retrieve the count associated with a page.
  61. **
  62. **  Parameters:
  63. **    sactx        Sambar Server context
  64. **    saconn        Sambar Server connection
  65. **    saparams    RPC Parameters
  66. **    infop        Error parameters
  67. **
  68. **  Returns:
  69. **    SA_SUCCEED | SA_FAIL
  70. */
  71. SA_RETCODE SA_PUBLIC
  72. page_count(sactx, saconn, saparams, infop)
  73. SA_CTX        *sactx;
  74. SA_CONN        *saconn;
  75. SA_PARAMS    *saparams;
  76. SA_INT        *infop;
  77. {
  78.     int            fh;
  79.     SA_INT        datalen;
  80.       SA_INT         num, i, j;
  81.     SA_CHAR        *data;
  82.       SA_INT         count[INT_WIDTH + 1];
  83.     SA_CHAR     pagename[1024];
  84.     SA_CHAR        buffer[256];
  85.  
  86.  
  87.     if (sa_param(sactx, saparams, "page", &data, &datalen) != SA_SUCCEED)
  88.     {
  89.         *infop = SA_E_INVALIDDATA;
  90.         sa_log(sactx, "page_count(): Expected parameter 'page'!");
  91.         return (SA_FAIL);
  92.     }
  93.     
  94.     if ((datalen == 0) || (datalen > 40))
  95.     {
  96.         *infop = SA_E_INVALIDDATA;
  97.         sa_log(sactx, "page_count(): 'page' field is NULL!");
  98.         return (SA_FAIL);
  99.     }
  100.  
  101.  
  102.     /*
  103.     ** Open the file with the current count.
  104.     ** The file must exist or we return 0.
  105.     */
  106.     if (sa_ctx_props(sactx, SA_GET, SA_CTXPROP_HOMEDIR, pagename,
  107.         900, &i) != SA_SUCCEED)
  108.     {
  109.         sa_log(sactx, "page_count(): Failure retrieving SA_CTXPROP_HOMEDIR!");
  110.         return (SA_FAIL);
  111.     }
  112.  
  113.     sprintf(&pagename[i], "\\tmp\\%s", data);
  114.  
  115.     num = 0;
  116.     if ((fh = _sopen(pagename, _O_RDWR, _SH_DENYNO, _S_IREAD|_S_IWRITE)) != -1)
  117.     {
  118.         if (_locking(fh, _LK_LOCK, 1) != -1)
  119.         {
  120.             buffer[0] = '\0';
  121.             _read(fh, buffer, INT_WIDTH);
  122.             num = atol(buffer) + 1;
  123.             lseek(fh, 0L, SEEK_SET);
  124.             sprintf(buffer, "%ld", num);
  125.             _write(fh, buffer, strlen(buffer));
  126.             _locking(fh, _LK_UNLCK, 1);
  127.         }
  128.  
  129.         _close(fh);
  130.     }
  131.  
  132.     /* Convert the current count to an array of numbers. */
  133.     count[INT_WIDTH] = '\0';
  134.     for (i = 0; i < INT_WIDTH; ++i) 
  135.     {
  136.         j = num % 10;
  137.         count[INT_WIDTH - 1 - i] = j;
  138.         num /= 10;
  139.     }
  140.  
  141.     /* Send HTTP header */
  142.     if (sa_send_header(saconn, "HTTP/1.0 200 OK\n", SA_NULLTERM) != SA_SUCCEED)
  143.         return (SA_FAIL);
  144.  
  145.     /* MIME type is x bitmap */
  146.     if (sa_conn_send(saconn, "Content-type:image/x-xbitmap\n\n", SA_NULLTERM)
  147.         != SA_SUCCEED)
  148.     {
  149.         return (SA_FAIL);
  150.     }
  151.  
  152.     /* print the counter definitions */
  153.     sprintf(buffer, "#define count_width %d\n", INT_WIDTH * COUNT_WIDTH);
  154.     if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
  155.         return (SA_FAIL);
  156.  
  157.     sprintf(buffer, "#define count_height %d\n\n", COUNT_HEIGHT);
  158.     if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
  159.         return (SA_FAIL);
  160.  
  161.     /* print out the bitmap itself */
  162.     if (sa_conn_send(saconn, "static char page_count[] = {\n", SA_NULLTERM) 
  163.         != SA_SUCCEED)
  164.     {
  165.         return (SA_FAIL);
  166.     }
  167.  
  168.     for (i = 0; i < COUNT_HEIGHT; ++i) 
  169.     {
  170.         for (j = 0; j < INT_WIDTH; ++j) 
  171.         {
  172.             sprintf(buffer, "%s", bitmap[(count[j] * COUNT_HEIGHT) + i]);
  173.             if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
  174.                 return (SA_FAIL);
  175.  
  176.              if (( i < COUNT_HEIGHT - 1) || (j < INT_WIDTH - 1)) 
  177.             {
  178.                 if (sa_conn_send(saconn, ", ", SA_NULLTERM) != SA_SUCCEED)
  179.                     return (SA_FAIL);
  180.             }
  181.         }
  182.  
  183.         if (sa_conn_send(saconn, "\n", SA_NULLTERM) != SA_SUCCEED)
  184.             return (SA_FAIL);
  185.     }
  186.  
  187.     if (sa_conn_send(saconn, "}\n", SA_NULLTERM) != SA_SUCCEED)
  188.         return (SA_FAIL);
  189.  
  190.     return (SA_SUCCEED);
  191. }
  192.