home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1999 November
/
PCWorld_1999-11_cd.bin
/
Komunik
/
Sambar
/
_setup.1
/
general.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-03-30
|
6KB
|
281 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
** 12JUL98 Added pragma-no cache to page counter sambar
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <general.h>
/*
** General RPC Commands
*/
typedef struct general__rpcs
{
SA_CHAR *name;
SA_RPCPARAM *params;
SA_INT numparams;
SA_INT auth;
SA_VOID *func;
SA_CHAR *descr;
} GENERAL__RPCS;
static SA_RPCPARAM pagep [] =
{
{ "page", 1, "Page associated with the row counter." },
{ "style", 0, "Page style: standard | plain" }
};
static GENERAL__RPCS general_rpcs [] =
{
{ "pagecount", pagep, sizeof(pagep)/sizeof(SA_RPCPARAM),
SA_AUTHORIZATION_ALL, (SA_VOID *)page_count,
"Retrieve the number of hits for a page." }
};
/*
** 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;
{
int i;
/* Register the User RPCs with the server */
for (i = 0; i < sizeof(general_rpcs) / sizeof(GENERAL__RPCS); i++)
{
if (sa_cmd_init(sactx, general_rpcs[i].name, general_rpcs[i].params,
general_rpcs[i].numparams, general_rpcs[i].auth,
general_rpcs[i].descr, general_rpcs[i].func) != 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 head;
SA_INT datalen;
SA_INT num, i, j;
SA_BOOL plain;
SA_CHAR *data;
SA_INT count[INT_WIDTH + 1];
SA_CHAR pagename[1024];
SA_CHAR dirname[512];
SA_CHAR buffer[256];
plain = 0;
if (sa_param(sactx, saparams, "style", &data, &datalen) == SA_SUCCEED)
{
if ((datalen == 5) && (stricmp(data, "plain") == 0))
plain = 1;
}
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);
}
if (sa_ctx_props(sactx, SA_GET, SA_CTXPROP_APPLICDIR, dirname,
512, &j) != SA_SUCCEED)
{
sa_log(sactx, "page_count(): Failure retrieving SA_CTXPROP_APPLICDIR!");
return (SA_FAIL);
}
sprintf(&pagename[i], "\\%s\\%s", j > 0 ? dirname : "tmp", 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, "%d\n", 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\r\n", SA_NULLTERM)
!= SA_SUCCEED)
{
return (SA_FAIL);
}
if (sa_conn_send(saconn, "Expires: Thu, 01 Jan 1995 01:00:00:00 GMT\r\n",
SA_NULLTERM) != SA_SUCCEED)
{
return (SA_FAIL);
}
if (sa_conn_send(saconn, "Pragma: no-cache\r\n", SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
/* MIME type is x bitmap */
if (sa_conn_send(saconn, "Content-Type: image/x-xbitmap\r\n\r\n",
SA_NULLTERM) != SA_SUCCEED)
{
return (SA_FAIL);
}
/* Strip leading zeros from the 'plain' display. */
head = 0;
if (plain)
{
while ((count[head] == 0) && (head < INT_WIDTH - 1))
head++;
}
/* print the counter definitions */
sprintf(buffer, "#define counter_width %d\r\n",
(INT_WIDTH - head) * COUNT_WIDTH);
if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
sprintf(buffer, "#define counter_height %d\r\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 unsigned char counter_bits[] = {\r\n",
SA_NULLTERM) != SA_SUCCEED)
{
return (SA_FAIL);
}
if (plain)
{
for (i = 0; i < COUNT_HEIGHT; ++i)
{
for (j = head; j < INT_WIDTH; ++j)
{
sprintf(buffer, "0x%02X, ", bitmap2[count[j]][i]);
if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
}
}
}
else
{
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, "\r\n", SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
}
if (sa_conn_send(saconn, "};\r\n", SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
if (sa_conn_flush(saconn) != SA_SUCCEED)
return (SA_FAIL);
return (SA_SUCCEED);
}