- Adapted indenation to new agreed upon system
- Added support for second generation scriptcontext based counter
This commit is contained in:
625
danu.c
625
danu.c
@ -51,11 +51,11 @@
|
||||
#include "danu.h"
|
||||
|
||||
/* ------------------ the data structure ----------------------------------*/
|
||||
typedef struct __DataNumber {
|
||||
pObjectDescriptor pDes;
|
||||
pICallBack pCall;
|
||||
char *pFileName;
|
||||
} DataNumber;
|
||||
typedef struct __DataNumber {
|
||||
pObjectDescriptor pDes;
|
||||
pICallBack pCall;
|
||||
char *pFileName;
|
||||
} DataNumber;
|
||||
/*----------------------------------------------------------------------*/
|
||||
static int readDataNumber(pDataNumber self)
|
||||
{
|
||||
@ -63,360 +63,329 @@ static int readDataNumber(pDataNumber self)
|
||||
int iNum = 0;
|
||||
|
||||
/* open file */
|
||||
fd = fopen(self->pFileName,"r");
|
||||
if(!fd)
|
||||
{
|
||||
fd = fopen(self->pFileName, "r");
|
||||
if (!fd) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* get and increment number */
|
||||
fscanf(fd,"%d",&iNum);
|
||||
fscanf(fd, "%d", &iNum);
|
||||
fclose(fd);
|
||||
return iNum;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
static int writeDataNumber(pDataNumber self, int iNum)
|
||||
{
|
||||
FILE *fd = NULL;
|
||||
|
||||
/* reopen for rewriting */
|
||||
fd = fopen(self->pFileName,"w");
|
||||
if(fd == NULL)
|
||||
{
|
||||
fd = fopen(self->pFileName, "w");
|
||||
if (fd == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* write file and leave */
|
||||
fprintf(fd," %d \n",iNum);
|
||||
fprintf(fd,"NEVER, EVER modify or delete this file\n");
|
||||
fprintf(fd, " %d \n", iNum);
|
||||
fprintf(fd, "NEVER, EVER modify or delete this file\n");
|
||||
fprintf(fd,
|
||||
"You'll risk eternal damnation and a reincarnation as a cockroach!|n");
|
||||
"You'll risk eternal damnation and a reincarnation as a cockroach!|n");
|
||||
fclose(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*------------------- The CallBack function for interest ------------------*/
|
||||
static int InterestCallback(int iEvent, void *pEvent, void *pUser)
|
||||
{
|
||||
pDataNumber self = NULL;
|
||||
SConnection *pCon = NULL;
|
||||
char pBueffel[132];
|
||||
int iNum;
|
||||
|
||||
pCon = (SConnection *)pUser;
|
||||
if(pCon == NULL || !SCisConnected(pCon))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(iEvent != VALUECHANGE)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
assert(pEvent);
|
||||
assert(pUser);
|
||||
|
||||
self = (pDataNumber)pEvent;
|
||||
|
||||
/*
|
||||
read number
|
||||
*/
|
||||
iNum = readDataNumber(self);
|
||||
if(iNum > 0)
|
||||
{
|
||||
snprintf(pBueffel,131,"sicsdatanumber = %d", iNum);
|
||||
SCWrite(pCon,pBueffel,eValue);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
pDataNumber CreateDataNumber(char *pFileName)
|
||||
{
|
||||
pDataNumber pNew = NULL;
|
||||
FILE *fd = NULL;
|
||||
|
||||
pNew = (pDataNumber)malloc(sizeof(DataNumber));
|
||||
if(!pNew)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
memset(pNew,0,sizeof(DataNumber));
|
||||
|
||||
pNew->pDes = CreateDescriptor("DataNumber");
|
||||
pNew->pCall = CreateCallBackInterface();
|
||||
if(!pNew->pDes || !pNew->pCall)
|
||||
{
|
||||
free(pNew);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* check filename */
|
||||
fd = fopen(pFileName,"r");
|
||||
if(!fd)
|
||||
{
|
||||
printf("Serious error: cannot open file for Data Number!!!!\n");
|
||||
printf("I continue, but you should not write data files!\n");
|
||||
pNew->pFileName = strdup("default.num");
|
||||
return pNew;
|
||||
}
|
||||
fclose(fd);
|
||||
pNew->pFileName = strdup(pFileName);
|
||||
return pNew;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
void DeleteDataNumber(void *pData)
|
||||
{
|
||||
pDataNumber self = NULL;
|
||||
|
||||
self = (pDataNumber)pData;
|
||||
assert(self);
|
||||
|
||||
if(self->pDes)
|
||||
{
|
||||
DeleteDescriptor(self->pDes);
|
||||
}
|
||||
if(self->pCall)
|
||||
{
|
||||
DeleteCallBackInterface(self->pCall);
|
||||
self->pCall = NULL;
|
||||
}
|
||||
if(self->pFileName)
|
||||
{
|
||||
free(self->pFileName);
|
||||
}
|
||||
free(self);
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
int IncrementDataNumber(pDataNumber self, int *iYear)
|
||||
{
|
||||
FILE *fd = NULL;
|
||||
int iNum;
|
||||
time_t iTime;
|
||||
struct tm *psTime;
|
||||
|
||||
|
||||
iNum = readDataNumber(self);
|
||||
if(iNum < 0)
|
||||
{
|
||||
return iNum;
|
||||
}
|
||||
|
||||
iNum++;
|
||||
|
||||
/* get year */
|
||||
iTime = time(NULL);
|
||||
psTime = localtime(&iTime);
|
||||
*iYear = psTime->tm_year + 1900;
|
||||
|
||||
if(writeDataNumber(self,iNum) < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
InvokeCallBack(self->pCall, VALUECHANGE, self);
|
||||
|
||||
return iNum;
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
int DecrementDataNumber(pDataNumber self)
|
||||
{
|
||||
FILE *fd = NULL;
|
||||
int iNum, currentThousand;
|
||||
|
||||
iNum = readDataNumber(self);
|
||||
if(iNum < 0)
|
||||
{
|
||||
return iNum;
|
||||
}
|
||||
|
||||
/*
|
||||
decrement DataNumber with restrictions:
|
||||
- not at all lower 0
|
||||
- do not understep a thousand boundary
|
||||
*/
|
||||
currentThousand = (int)floor(iNum/1000.);
|
||||
iNum--;
|
||||
if((int)floor(iNum/1000.) < currentThousand)
|
||||
{
|
||||
iNum++;
|
||||
}
|
||||
|
||||
if(writeDataNumber(self,iNum) < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return iNum;
|
||||
}
|
||||
/*------------------------------------------------------------------------*/
|
||||
int NewThousand(pDataNumber self)
|
||||
static int InterestCallback(int iEvent, void *pEvent, void *pUser)
|
||||
{
|
||||
int iNum, currentThousand;
|
||||
|
||||
pDataNumber self = NULL;
|
||||
SConnection *pCon = NULL;
|
||||
char pBueffel[132];
|
||||
int iNum;
|
||||
|
||||
pCon = (SConnection *) pUser;
|
||||
if (pCon == NULL || !SCisConnected(pCon)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (iEvent != VALUECHANGE) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
assert(pEvent);
|
||||
assert(pUser);
|
||||
|
||||
self = (pDataNumber) pEvent;
|
||||
|
||||
/*
|
||||
read number
|
||||
*/
|
||||
iNum = readDataNumber(self);
|
||||
if(iNum < 0)
|
||||
{
|
||||
if (iNum > 0) {
|
||||
snprintf(pBueffel, 131, "sicsdatanumber = %d", iNum);
|
||||
SCWrite(pCon, pBueffel, eValue);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
pDataNumber CreateDataNumber(char *pFileName)
|
||||
{
|
||||
pDataNumber pNew = NULL;
|
||||
FILE *fd = NULL;
|
||||
|
||||
pNew = (pDataNumber) malloc(sizeof(DataNumber));
|
||||
if (!pNew) {
|
||||
return NULL;
|
||||
}
|
||||
memset(pNew, 0, sizeof(DataNumber));
|
||||
|
||||
pNew->pDes = CreateDescriptor("DataNumber");
|
||||
pNew->pCall = CreateCallBackInterface();
|
||||
if (!pNew->pDes || !pNew->pCall) {
|
||||
free(pNew);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* check filename */
|
||||
fd = fopen(pFileName, "r");
|
||||
if (!fd) {
|
||||
printf("Serious error: cannot open file for Data Number!!!!\n");
|
||||
printf("I continue, but you should not write data files!\n");
|
||||
pNew->pFileName = strdup("default.num");
|
||||
return pNew;
|
||||
}
|
||||
fclose(fd);
|
||||
pNew->pFileName = strdup(pFileName);
|
||||
return pNew;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
void DeleteDataNumber(void *pData)
|
||||
{
|
||||
pDataNumber self = NULL;
|
||||
|
||||
self = (pDataNumber) pData;
|
||||
assert(self);
|
||||
|
||||
if (self->pDes) {
|
||||
DeleteDescriptor(self->pDes);
|
||||
}
|
||||
if (self->pCall) {
|
||||
DeleteCallBackInterface(self->pCall);
|
||||
self->pCall = NULL;
|
||||
}
|
||||
if (self->pFileName) {
|
||||
free(self->pFileName);
|
||||
}
|
||||
free(self);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
int IncrementDataNumber(pDataNumber self, int *iYear)
|
||||
{
|
||||
FILE *fd = NULL;
|
||||
int iNum;
|
||||
time_t iTime;
|
||||
struct tm *psTime;
|
||||
|
||||
|
||||
iNum = readDataNumber(self);
|
||||
if (iNum < 0) {
|
||||
return iNum;
|
||||
}
|
||||
|
||||
/* set to next thousand number */
|
||||
currentThousand = (int)floor(iNum/1000.);
|
||||
iNum = (currentThousand + 1)*1000;
|
||||
|
||||
if(writeDataNumber(self,iNum) < 0)
|
||||
{
|
||||
iNum++;
|
||||
|
||||
/* get year */
|
||||
iTime = time(NULL);
|
||||
psTime = localtime(&iTime);
|
||||
*iYear = psTime->tm_year + 1900;
|
||||
|
||||
if (writeDataNumber(self, iNum) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
InvokeCallBack(self->pCall, VALUECHANGE, self);
|
||||
|
||||
return iNum;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
int DecrementDataNumber(pDataNumber self)
|
||||
{
|
||||
FILE *fd = NULL;
|
||||
int iNum, currentThousand;
|
||||
|
||||
iNum = readDataNumber(self);
|
||||
if (iNum < 0) {
|
||||
return iNum;
|
||||
}
|
||||
|
||||
/*
|
||||
decrement DataNumber with restrictions:
|
||||
- not at all lower 0
|
||||
- do not understep a thousand boundary
|
||||
*/
|
||||
currentThousand = (int) floor(iNum / 1000.);
|
||||
iNum--;
|
||||
if ((int) floor(iNum / 1000.) < currentThousand) {
|
||||
iNum++;
|
||||
}
|
||||
|
||||
if (writeDataNumber(self, iNum) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return iNum;
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
int DNWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
pDataNumber self = NULL;
|
||||
FILE *fd = NULL;
|
||||
int iNum, iYear;
|
||||
char pBueffel[512];
|
||||
long lID;
|
||||
|
||||
self = (pDataNumber)pData;
|
||||
assert(self);
|
||||
assert(pCon);
|
||||
|
||||
argtolower(argc,argv);
|
||||
if(argc < 2) /* value request */
|
||||
{
|
||||
iNum = readDataNumber(self);
|
||||
if(iNum < 0)
|
||||
{
|
||||
sprintf(pBueffel,"ERROR: cannot open file %s",self->pFileName);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
}
|
||||
sprintf(pBueffel,"%s = %d",argv[0],iNum);
|
||||
SCWrite(pCon,pBueffel,eValue);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(strcmp(argv[1],"incr") == 0)
|
||||
{
|
||||
iNum = IncrementDataNumber(self,&iYear);
|
||||
if(iNum > 0)
|
||||
{
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(pBueffel,"ERROR: cannot increment %s",argv[0]);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if(strcmp(argv[1],"nextthousand") == 0)
|
||||
{
|
||||
if(!SCMatchRights(pCon,usMugger))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
iNum = NewThousand(self);
|
||||
if(iNum > 0)
|
||||
{
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(pBueffel,"ERROR: cannot increment %s",argv[0]);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if(strcmp(argv[1],"interest") == 0)
|
||||
{
|
||||
lID = RegisterCallback(self->pCall,
|
||||
VALUECHANGE, InterestCallback,
|
||||
SCCopyConnection(pCon),
|
||||
SCDeleteConnection);
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
if(strcmp(argv[1],"uninterest") == 0)
|
||||
{
|
||||
RemoveCallbackCon(self->pCall,pCon);
|
||||
SCSendOK(pCon);
|
||||
}
|
||||
|
||||
sprintf(pBueffel,"ERROR: unknown command %s supplied to %s",
|
||||
argv[1], argv[0]);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
int DEWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
pDataNumber self = NULL;
|
||||
int iNum;
|
||||
char pBueffel[512];
|
||||
|
||||
self = (pDataNumber)pData;
|
||||
assert(self);
|
||||
assert(pCon);
|
||||
int NewThousand(pDataNumber self)
|
||||
{
|
||||
int iNum, currentThousand;
|
||||
|
||||
if(SCMatchRights(pCon,usMugger))
|
||||
{
|
||||
iNum = DecrementDataNumber(self);
|
||||
snprintf(pBueffel,511,"Data file %d killed", iNum+1);
|
||||
SCWrite(pCon,pBueffel,eWarning);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
SCWrite(pCon,"ERROR: you are not authorized to kill data files",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
iNum = readDataNumber(self);
|
||||
if (iNum < 0) {
|
||||
return iNum;
|
||||
}
|
||||
|
||||
/* set to next thousand number */
|
||||
currentThousand = (int) floor(iNum / 1000.);
|
||||
iNum = (currentThousand + 1) * 1000;
|
||||
|
||||
if (writeDataNumber(self, iNum) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return iNum;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
int DNFactory(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
pDataNumber self = NULL;
|
||||
char pBueffel[512];
|
||||
int iRet;
|
||||
|
||||
if(argc < 3)
|
||||
{
|
||||
SCWrite(pCon,
|
||||
"ERROR: not enough arguments provided to make DataNumber",eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
self = CreateDataNumber(argv[2]);
|
||||
if(!self)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: no memory to create data number",eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
iRet = AddCommand(pSics, argv[1],DNWrapper, DeleteDataNumber, self);
|
||||
if(!iRet)
|
||||
{
|
||||
sprintf(pBueffel,"ERROR: duplicate command %s not created",argv[1]);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
}
|
||||
iRet = AddCommand(pSics,"killfile",DEWrapper,NULL, self);
|
||||
if(!iRet)
|
||||
{
|
||||
sprintf(pBueffel,"ERROR: duplicate command %s not created",argv[1]);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int DNWrapper(SConnection * pCon, SicsInterp * pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
pDataNumber self = NULL;
|
||||
FILE *fd = NULL;
|
||||
int iNum, iYear;
|
||||
char pBueffel[512];
|
||||
long lID;
|
||||
|
||||
|
||||
self = (pDataNumber) pData;
|
||||
assert(self);
|
||||
assert(pCon);
|
||||
|
||||
argtolower(argc, argv);
|
||||
if (argc < 2) { /* value request */
|
||||
iNum = readDataNumber(self);
|
||||
if (iNum < 0) {
|
||||
sprintf(pBueffel, "ERROR: cannot open file %s", self->pFileName);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
sprintf(pBueffel, "%s = %d", argv[0], iNum);
|
||||
SCWrite(pCon, pBueffel, eValue);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "incr") == 0) {
|
||||
iNum = IncrementDataNumber(self, &iYear);
|
||||
if (iNum > 0) {
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
} else {
|
||||
sprintf(pBueffel, "ERROR: cannot increment %s", argv[0]);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (strcmp(argv[1], "nextthousand") == 0) {
|
||||
if (!SCMatchRights(pCon, usMugger)) {
|
||||
return 0;
|
||||
}
|
||||
iNum = NewThousand(self);
|
||||
if (iNum > 0) {
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
} else {
|
||||
sprintf(pBueffel, "ERROR: cannot increment %s", argv[0]);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (strcmp(argv[1], "interest") == 0) {
|
||||
lID = RegisterCallback(self->pCall,
|
||||
VALUECHANGE, InterestCallback,
|
||||
SCCopyConnection(pCon), SCDeleteConnection);
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(argv[1], "uninterest") == 0) {
|
||||
RemoveCallbackCon(self->pCall, pCon);
|
||||
SCSendOK(pCon);
|
||||
}
|
||||
|
||||
sprintf(pBueffel, "ERROR: unknown command %s supplied to %s",
|
||||
argv[1], argv[0]);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
int DEWrapper(SConnection * pCon, SicsInterp * pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
pDataNumber self = NULL;
|
||||
int iNum;
|
||||
char pBueffel[512];
|
||||
|
||||
self = (pDataNumber) pData;
|
||||
assert(self);
|
||||
assert(pCon);
|
||||
|
||||
if (SCMatchRights(pCon, usMugger)) {
|
||||
iNum = DecrementDataNumber(self);
|
||||
snprintf(pBueffel, 511, "Data file %d killed", iNum + 1);
|
||||
SCWrite(pCon, pBueffel, eWarning);
|
||||
return 1;
|
||||
} else {
|
||||
SCWrite(pCon, "ERROR: you are not authorized to kill data files",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
int DNFactory(SConnection * pCon, SicsInterp * pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
pDataNumber self = NULL;
|
||||
char pBueffel[512];
|
||||
int iRet;
|
||||
|
||||
if (argc < 3) {
|
||||
SCWrite(pCon,
|
||||
"ERROR: not enough arguments provided to make DataNumber",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
self = CreateDataNumber(argv[2]);
|
||||
if (!self) {
|
||||
SCWrite(pCon, "ERROR: no memory to create data number", eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
iRet = AddCommand(pSics, argv[1], DNWrapper, DeleteDataNumber, self);
|
||||
if (!iRet) {
|
||||
sprintf(pBueffel, "ERROR: duplicate command %s not created", argv[1]);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
iRet = AddCommand(pSics, "killfile", DEWrapper, NULL, self);
|
||||
if (!iRet) {
|
||||
sprintf(pBueffel, "ERROR: duplicate command %s not created", argv[1]);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user