- Dokumentation updates
- Fixed bad file generated through nuweb - fixed problems in synchronize.c - New file writing scheme implemented - Changes to hkl
This commit is contained in:
242
danu.c
242
danu.c
@ -4,7 +4,12 @@
|
||||
Implementation file for the data number module.
|
||||
|
||||
Mark Koennecke, Juli 1997
|
||||
|
||||
|
||||
Added callbacks, stepping to next thousand
|
||||
|
||||
Mark Koennecke, December 2003
|
||||
|
||||
|
||||
Copyright:
|
||||
|
||||
Labor fuer Neutronenstreuung
|
||||
@ -38,18 +43,87 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
#include "fortify.h"
|
||||
#include "conman.h"
|
||||
#include "obdes.h"
|
||||
#include "sics.h"
|
||||
#include "danu.h"
|
||||
|
||||
/* ------------------ the data structure ----------------------------------*/
|
||||
typedef struct __DataNumber {
|
||||
pObjectDescriptor pDes;
|
||||
pICallBack pCall;
|
||||
char *pFileName;
|
||||
} DataNumber;
|
||||
/*----------------------------------------------------------------------*/
|
||||
static int readDataNumber(pDataNumber self)
|
||||
{
|
||||
FILE *fd = NULL;
|
||||
int iNum = 0;
|
||||
|
||||
/* open file */
|
||||
fd = fopen(self->pFileName,"r");
|
||||
if(!fd)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* get and increment number */
|
||||
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)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* write file and leave */
|
||||
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");
|
||||
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;
|
||||
|
||||
if(iEvent != VALUECHANGE)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
assert(pEvent);
|
||||
assert(pUser);
|
||||
|
||||
pCon = (SConnection *)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)
|
||||
{
|
||||
@ -64,7 +138,8 @@
|
||||
memset(pNew,0,sizeof(DataNumber));
|
||||
|
||||
pNew->pDes = CreateDescriptor("DataNumber");
|
||||
if(!pNew->pDes)
|
||||
pNew->pCall = CreateCallBackInterface();
|
||||
if(!pNew->pDes || !pNew->pCall)
|
||||
{
|
||||
free(pNew);
|
||||
return NULL;
|
||||
@ -95,6 +170,11 @@
|
||||
{
|
||||
DeleteDescriptor(self->pDes);
|
||||
}
|
||||
if(self->pCall)
|
||||
{
|
||||
DeleteCallBackInterface(self->pCall);
|
||||
self->pCall = NULL;
|
||||
}
|
||||
if(self->pFileName)
|
||||
{
|
||||
free(self->pFileName);
|
||||
@ -109,36 +189,26 @@
|
||||
time_t iTime;
|
||||
struct tm *psTime;
|
||||
|
||||
/* open file */
|
||||
fd = fopen(self->pFileName,"r");
|
||||
if(!fd)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* get and increment number */
|
||||
fscanf(fd,"%d",&iNum);
|
||||
iNum++;
|
||||
fclose(fd);
|
||||
|
||||
/* reopen for rewriting */
|
||||
fd = fopen(self->pFileName,"w");
|
||||
if(fd == NULL)
|
||||
iNum = readDataNumber(self);
|
||||
if(iNum < 0)
|
||||
{
|
||||
return -1;
|
||||
return iNum;
|
||||
}
|
||||
|
||||
/* write file and leave */
|
||||
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");
|
||||
fclose(fd);
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
@ -146,34 +216,55 @@
|
||||
int DecrementDataNumber(pDataNumber self)
|
||||
{
|
||||
FILE *fd = NULL;
|
||||
int iNum;
|
||||
|
||||
/* open file */
|
||||
fd = fopen(self->pFileName,"r");
|
||||
if(!fd)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* get and decrement number */
|
||||
fscanf(fd,"%d",&iNum);
|
||||
iNum--;
|
||||
if(iNum < 0)
|
||||
iNum = 0;
|
||||
fclose(fd);
|
||||
int iNum, currentThousand;
|
||||
|
||||
/* reopen for rewriting */
|
||||
fd = fopen(self->pFileName,"w");
|
||||
|
||||
/* write file and leave */
|
||||
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");
|
||||
fclose(fd);
|
||||
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)
|
||||
{
|
||||
int iNum, currentThousand;
|
||||
|
||||
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 DNWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
@ -182,6 +273,7 @@
|
||||
FILE *fd = NULL;
|
||||
int iNum, iYear;
|
||||
char pBueffel[512];
|
||||
long lID;
|
||||
|
||||
self = (pDataNumber)pData;
|
||||
assert(self);
|
||||
@ -190,18 +282,16 @@
|
||||
argtolower(argc,argv);
|
||||
if(argc < 2) /* value request */
|
||||
{
|
||||
fd = fopen(self->pFileName,"r");
|
||||
if(!fd)
|
||||
{
|
||||
iNum = readDataNumber(self);
|
||||
if(iNum < 0)
|
||||
{
|
||||
sprintf(pBueffel,"ERROR: cannot open file %s",self->pFileName);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
}
|
||||
fscanf(fd,"%d",&iNum);
|
||||
fclose(fd);
|
||||
sprintf(pBueffel,"%s = %d",argv[0],iNum);
|
||||
SCWrite(pCon,pBueffel,eValue);
|
||||
return 1;
|
||||
}
|
||||
sprintf(pBueffel,"%s = %d",argv[0],iNum);
|
||||
SCWrite(pCon,pBueffel,eValue);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(strcmp(argv[1],"incr") == 0)
|
||||
@ -219,6 +309,38 @@
|
||||
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,
|
||||
pCon, NULL);
|
||||
SCRegister(pCon,pSics, self->pCall,lID);
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
if(strcmp(argv[1],"uninterest") == 0)
|
||||
{
|
||||
RemoveCallback2(self->pCall,pCon);
|
||||
SCSendOK(pCon);
|
||||
}
|
||||
|
||||
sprintf(pBueffel,"ERROR: unknown command %s supplied to %s",
|
||||
argv[1], argv[0]);
|
||||
|
Reference in New Issue
Block a user