home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 May
/
Pcwk5b98.iso
/
WEBSERVE
/
SAMBAR
/
DATA.1
/
general.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-06-11
|
4KB
|
192 lines
/*
** GENERAL
**
** HTTP Wrapper for General utility applications.
**
** Confidential Property of Tod Sambar
** (c) Copyright Tod Sambar 1997
** All rights reserved.
**
**
** Public Functions:
**
** page_count
**
**
** History:
** Chg# Date Description Resp
** ---- ------- ------------------------------------------------------- ----
** 10JUN97 Created sambar
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <general.h>
/*
** GENERAL_INIT
**
** Initialize the General Utility RPCS use by the Sambar Server plugins.
**
**
** Parameters:
** sactx Sambar Server context
**
** Returns:
** SA_SUCCEED | SA_FAIL
*/
SA_RETCODE SA_PUBLIC
general_init(sactx)
SA_CTX *sactx;
{
/* Register the User RPCs with the server */
if (sa_cmd_init(sactx, "pagecount", SA_AUTHORIZATION_ALL,
"Retrieve the number of hits for a page.", page_count) != SA_SUCCEED)
{
sa_log(sactx, "Unable to initialize General Utility RPCs");
return (SA_FAIL);
}
sa_log(sactx, "General Utilities Initialized");
return (SA_SUCCEED);
}
/*
** PAGE_COUNT
**
** Retrieve the count associated with a page.
**
** Parameters:
** sactx Sambar Server context
** saconn Sambar Server connection
** saparams RPC Parameters
** infop Error parameters
**
** Returns:
** SA_SUCCEED | SA_FAIL
*/
SA_RETCODE SA_PUBLIC
page_count(sactx, saconn, saparams, infop)
SA_CTX *sactx;
SA_CONN *saconn;
SA_PARAMS *saparams;
SA_INT *infop;
{
int fh;
SA_INT datalen;
SA_INT num, i, j;
SA_CHAR *data;
SA_INT count[INT_WIDTH + 1];
SA_CHAR pagename[1024];
SA_CHAR buffer[256];
if (sa_param(sactx, saparams, "page", &data, &datalen) != SA_SUCCEED)
{
*infop = SA_E_INVALIDDATA;
sa_log(sactx, "page_count(): Expected parameter 'page'!");
return (SA_FAIL);
}
if ((datalen == 0) || (datalen > 40))
{
*infop = SA_E_INVALIDDATA;
sa_log(sactx, "page_count(): 'page' field is NULL!");
return (SA_FAIL);
}
/*
** Open the file with the current count.
** The file must exist or we return 0.
*/
if (sa_ctx_props(sactx, SA_GET, SA_CTXPROP_HOMEDIR, pagename,
900, &i) != SA_SUCCEED)
{
sa_log(sactx, "page_count(): Failure retrieving SA_CTXPROP_HOMEDIR!");
return (SA_FAIL);
}
sprintf(&pagename[i], "\\tmp\\%s", data);
num = 0;
if ((fh = _sopen(pagename, _O_RDWR, _SH_DENYNO, _S_IREAD|_S_IWRITE)) != -1)
{
if (_locking(fh, _LK_LOCK, 1) != -1)
{
buffer[0] = '\0';
_read(fh, buffer, INT_WIDTH);
num = atol(buffer) + 1;
lseek(fh, 0L, SEEK_SET);
sprintf(buffer, "%ld", num);
_write(fh, buffer, strlen(buffer));
_locking(fh, _LK_UNLCK, 1);
}
_close(fh);
}
/* Convert the current count to an array of numbers. */
count[INT_WIDTH] = '\0';
for (i = 0; i < INT_WIDTH; ++i)
{
j = num % 10;
count[INT_WIDTH - 1 - i] = j;
num /= 10;
}
/* Send HTTP header */
if (sa_send_header(saconn, "HTTP/1.0 200 OK\n", SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
/* MIME type is x bitmap */
if (sa_conn_send(saconn, "Content-type:image/x-xbitmap\n\n", SA_NULLTERM)
!= SA_SUCCEED)
{
return (SA_FAIL);
}
/* print the counter definitions */
sprintf(buffer, "#define count_width %d\n", INT_WIDTH * COUNT_WIDTH);
if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
sprintf(buffer, "#define count_height %d\n\n", COUNT_HEIGHT);
if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
/* print out the bitmap itself */
if (sa_conn_send(saconn, "static char page_count[] = {\n", SA_NULLTERM)
!= SA_SUCCEED)
{
return (SA_FAIL);
}
for (i = 0; i < COUNT_HEIGHT; ++i)
{
for (j = 0; j < INT_WIDTH; ++j)
{
sprintf(buffer, "%s", bitmap[(count[j] * COUNT_HEIGHT) + i]);
if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
if (( i < COUNT_HEIGHT - 1) || (j < INT_WIDTH - 1))
{
if (sa_conn_send(saconn, ", ", SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
}
}
if (sa_conn_send(saconn, "\n", SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
}
if (sa_conn_send(saconn, "}\n", SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
return (SA_SUCCEED);
}