PSI update
r1464 | ffr | 2007-02-12 12:20:21 +1100 (Mon, 12 Feb 2007) | 2 lines
This commit is contained in:
committed by
Douglas Clowes
parent
634f2023b1
commit
3168325921
160
sicsdata.c
160
sicsdata.c
@@ -4,6 +4,8 @@
|
||||
An attempt to a generic interface to SICS data for all sorts of SICS
|
||||
clients.
|
||||
|
||||
WARNING: this code only works when ints and floats are of the same size!
|
||||
|
||||
copyright: see file COPYRIGHT
|
||||
|
||||
Mark Koennecke, June 2003
|
||||
@@ -17,7 +19,7 @@
|
||||
#include "scan.h"
|
||||
#include "HistMem.h"
|
||||
#include "sicsdata.h"
|
||||
|
||||
#define ABS(x) (x < 0 ? -(x) : (x))
|
||||
/*--------------------------------------------------------------------*/
|
||||
static void KillSICSData(void *pData){
|
||||
pSICSData self = NULL;
|
||||
@@ -60,6 +62,47 @@ pSICSData createSICSData(void){
|
||||
pNew->dataUsed = 0;
|
||||
return pNew;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int getSICSDataInt(pSICSData self, int pos, int *value){
|
||||
|
||||
if(pos >= self->dataUsed || self->dataType[pos] != INTTYPE){
|
||||
return 0;
|
||||
}
|
||||
*value = self->data[pos];
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int getSICSDataFloat(pSICSData self, int pos, float *value){
|
||||
if(pos >= self->dataUsed || self->dataType[pos] != FLOATTYPE){
|
||||
return 0;
|
||||
}
|
||||
memcpy(value,&self->data[pos],sizeof(float));
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int setSICSDataInt(pSICSData self, int pos, int value){
|
||||
int *idata = NULL;
|
||||
|
||||
idata = getSICSDataPointer(self,0,pos+1);
|
||||
if(idata == NULL){
|
||||
return 0;
|
||||
}
|
||||
idata[pos] = value;
|
||||
self->dataType[pos] = INTTYPE;
|
||||
return 1;
|
||||
}
|
||||
/*----------------------------------------------------------------------------*/
|
||||
int setSICSDataFloat(pSICSData self, int pos, float value){
|
||||
int *idata = NULL;
|
||||
|
||||
idata = getSICSDataPointer(self,0,pos+1);
|
||||
if(idata == NULL){
|
||||
return 0;
|
||||
}
|
||||
memcpy(&idata[pos],&value,sizeof(float));
|
||||
self->dataType[pos] = FLOATTYPE;
|
||||
return 1;
|
||||
}
|
||||
/*-------------------------------------------------------------------*/
|
||||
int *getSICSDataPointer(pSICSData self, int start, int end){
|
||||
int newSize;
|
||||
@@ -134,7 +177,7 @@ static void netEncode(pSICSData self){
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------*/
|
||||
static void clearSICSData(pSICSData self){
|
||||
void clearSICSData(pSICSData self){
|
||||
assert(self);
|
||||
|
||||
self->dataUsed = 0;
|
||||
@@ -236,6 +279,74 @@ static int putFloat(pSICSData self, int argc, char *argv[],
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
/*------------------------------------------------------------------*/
|
||||
static int getPos(pSICSData self, char *name,
|
||||
SConnection *pCon, int pos){
|
||||
char pBueffel[512];
|
||||
float value;
|
||||
|
||||
if(pos >= self->dataUsed){
|
||||
SCWrite(pCon,"ERROR: requested position out of range",eError);
|
||||
return 0;
|
||||
}
|
||||
if(self->dataType[pos] == FLOATTYPE){
|
||||
memcpy(&value,&self->data[pos],sizeof(float));
|
||||
snprintf(pBueffel,511,"%s = %f", name, value);
|
||||
SCWrite(pCon,pBueffel,eValue);
|
||||
return 1;
|
||||
}
|
||||
if(self->dataType[pos] == INTTYPE){
|
||||
snprintf(pBueffel,511,"%s = %d", name, self->data[pos]);
|
||||
SCWrite(pCon,pBueffel,eValue);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*------------------------------------------------------------------*/
|
||||
static float getDataPos(pSICSData self, int pos){
|
||||
float value;
|
||||
|
||||
assert(pos < self->dataUsed);
|
||||
if(self->dataType[pos] == FLOATTYPE){
|
||||
memcpy(&value,&self->data[pos],sizeof(float));
|
||||
} else {
|
||||
value = (float)self->data[pos];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
/*------------------------------------------------------------------*/
|
||||
static int divideSicsData(pSICSData self, SicsInterp *pSics,
|
||||
SConnection *pCon, char *name){
|
||||
int i;
|
||||
pSICSData other = NULL;
|
||||
float val, div;
|
||||
|
||||
other = (pSICSData)FindCommandData(pSics,name,"SICSData");
|
||||
if(other == NULL){
|
||||
SCWrite(pCon,"ERROR: requested SICSData object to divide not found",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
if(other->dataUsed < self->dataUsed){
|
||||
SCWrite(pCon,"ERROR: not enough data in SICSData for division",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
for(i = 0; i < self->dataUsed; i++){
|
||||
div = getDataPos(other,i);
|
||||
if(ABS(div) > .00001){
|
||||
val = getDataPos(self,i)/div;
|
||||
} else {
|
||||
val = .0;
|
||||
}
|
||||
if(self->dataType[i] == INTTYPE){
|
||||
self->data[i] = (int)val;
|
||||
} else {
|
||||
memcpy(&self->data[i],&val,sizeof(float));
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*-------------------------------------------------------------------*/
|
||||
static int copyScanCounts(pSICSData self, int argc, char *argv[],
|
||||
SConnection *pCon, SicsInterp *pSics){
|
||||
@@ -475,6 +586,35 @@ static int copyHM(pSICSData self, int argc, char *argv[],
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
/*----------------------------------------------------------------------*/
|
||||
static int copyData(pSICSData self,SicsInterp *pSics,
|
||||
SConnection *pCon,int argc, char *argv[]){
|
||||
pSICSData other = NULL;
|
||||
int pos, start, end, i;
|
||||
|
||||
if(argc < 6){
|
||||
SCWrite(pCon,"ERROR: Insufficient number of arguments to copydata",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
pos = atoi(argv[2]);
|
||||
start = atoi(argv[4]);
|
||||
end = atoi(argv[5]);
|
||||
if((other = FindCommandData(pSics,argv[3],"SICSData")) == NULL){
|
||||
SCWrite(pCon,"ERROR: invalid SICSData requested",eError);
|
||||
return 0;
|
||||
}
|
||||
if(start > end || end > other->dataUsed){
|
||||
SCWrite(pCon,"ERROR: invalid copy range specified",eError);
|
||||
return 0;
|
||||
}
|
||||
getSICSDataPointer(self,pos, pos + (end -start));
|
||||
memcpy(&self->data[pos],&other->data[start],(end-start)*sizeof(int));
|
||||
memcpy(&self->dataType[pos],&other->dataType[start],
|
||||
(end-start)*sizeof(char));
|
||||
|
||||
return 1;
|
||||
}
|
||||
/*----------------------------------------------------------------------
|
||||
Look here in order to find out about commands understood
|
||||
----------------------------------------------------------------------*/
|
||||
@@ -482,6 +622,7 @@ int SICSDataAction(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[]){
|
||||
pSICSData self = NULL;
|
||||
char pBueffel[132];
|
||||
int pos;
|
||||
|
||||
self = (pSICSData)pData;
|
||||
assert(self);
|
||||
@@ -509,6 +650,21 @@ int SICSDataAction(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
return 0;
|
||||
}
|
||||
return dumpSICSData(self,argv[2],pCon);
|
||||
} else if(strcmp(argv[1],"get") == 0){
|
||||
if(argc < 3){
|
||||
SCWrite(pCon,"ERROR: need a position to read",eError);
|
||||
return 0;
|
||||
}
|
||||
pos = atoi(argv[2]);
|
||||
return getPos(self,argv[0],pCon,pos);
|
||||
} else if(strcmp(argv[1],"divideby") == 0){
|
||||
if(argc < 3){
|
||||
SCWrite(pCon,"ERROR: need a SICSdata to divide by",eError);
|
||||
return 0;
|
||||
}
|
||||
return divideSicsData(self,pSics,pCon,argv[2]);
|
||||
} else if(strcmp(argv[1],"copydata") == 0){
|
||||
return copyData(self,pSics,pCon,argc, argv);
|
||||
} else if(strcmp(argv[1],"putint") == 0){
|
||||
/*---------- putint */
|
||||
return putInt(self,argc-2,&argv[2],pCon, pSics);
|
||||
|
||||
Reference in New Issue
Block a user